A - Traveling Budget

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 100

問題文

あなたは、電車とバスを乗り継いで旅行をする計画を立てました。 電車は旅程に沿って通常のきっぷを買うと A 円かかり、乗り放題きっぷを買うと B 円かかります。 バスは旅程に沿って通常のきっぷを買うと C 円かかり、乗り放題きっぷを買うと D 円かかります。

電車およびバスについて通常の切符を買うか乗り放題きっぷを買うかをうまく選んだときの、運賃の合計の最小値を求めてください。

制約

  • 1 \leq A \leq 1,000
  • 1 \leq B \leq 1,000
  • 1 \leq C \leq 1,000
  • 1 \leq D \leq 1,000
  • 入力値はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

A
B
C
D

出力

運賃の合計の最小値を出力せよ。


入力例 1

600
300
220
420

出力例 1

520

電車の通常のきっぷを買うと 600 円かかり、乗り放題きっぷを買うと 300 円かかります。 よって、電車については乗り放題きっぷを 300 円で買うほうが運賃が少なくなります。 一方、バスについては通常の切符を 220 円で買うほうが運賃が少なくなります。

したがって、運賃の合計の最小値は 300 + 220 = 520 円となります。


入力例 2

555
555
400
200

出力例 2

755

入力例 3

549
817
715
603

出力例 3

1152

Score : 100 points

Problem Statement

You planned a trip using trains and buses. The train fare will be A yen (the currency of Japan) if you buy ordinary tickets along the way, and B yen if you buy an unlimited ticket. Similarly, the bus fare will be C yen if you buy ordinary tickets along the way, and D yen if you buy an unlimited ticket.

Find the minimum total fare when the optimal choices are made for trains and buses.

Constraints

  • 1 \leq A \leq 1 000
  • 1 \leq B \leq 1 000
  • 1 \leq C \leq 1 000
  • 1 \leq D \leq 1 000
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

A
B
C
D

Output

Print the minimum total fare.


Sample Input 1

600
300
220
420

Sample Output 1

520

The train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you buy an unlimited ticket. Thus, the optimal choice for trains is to buy an unlimited ticket for 300 yen. On the other hand, the optimal choice for buses is to buy ordinary tickets for 220 yen.

Therefore, the minimum total fare is 300 + 220 = 520 yen.


Sample Input 2

555
555
400
200

Sample Output 2

755

Sample Input 3

549
817
715
603

Sample Output 3

1152
B - Chocolate

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 200

問題文

ある合宿におやつとしてチョコレートが何個か準備されました。 合宿は N 人が参加して D 日間行われました。 i 人目の参加者 (1 \leq i \leq N) は合宿の 1, A_i + 1, 2A_i + 1, ... 日目にチョコレートを 1 個ずつ食べました。 その結果、合宿終了後に残っていたチョコレートは X 個となりました。また、合宿の参加者以外がチョコレートを食べることはありませんでした。

合宿開始前に準備されていたチョコレートの個数を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq D \leq 100
  • 1 \leq X \leq 100
  • 1 \leq A_i \leq 100 (1 \leq i \leq N)

入力

入力は以下の形式で標準入力から与えられる。

N
D X
A_1
A_2
:
A_N

出力

合宿開始前に準備されていたチョコレートの個数を出力せよ。


入力例 1

3
7 1
2
5
10

出力例 1

8

3 人が 7 日間の合宿に参加します。 それぞれの参加者は以下のようにチョコレートを食べます。

  • 1 人目の参加者は、1, 3, 5, 7 日目にチョコレートを 1 個ずつ、合計 4 個食べます。
  • 2 人目の参加者は、1 日目および 6 日目にチョコレートを 1 個ずつ、合計 2 個食べます。
  • 3 人目の参加者は、1 日目だけにチョコレートを食べます。食べるチョコレートは合計 1 個です。

合宿終了後に残っていたチョコレートは 1 個であるので、合宿開始前に準備されていたチョコレートは 1 + 4 + 2 + 1 = 8 個あったことになります。


入力例 2

2
8 20
1
10

出力例 2

29

入力例 3

5
30 44
26
18
81
18
6

出力例 3

56

Score : 200 points

Problem Statement

Some number of chocolate pieces were prepared for a training camp. The camp had N participants and lasted for D days. The i-th participant (1 \leq i \leq N) ate one chocolate piece on each of the following days in the camp: the 1-st day, the (A_i + 1)-th day, the (2A_i + 1)-th day, and so on. As a result, there were X chocolate pieces remaining at the end of the camp. During the camp, nobody except the participants ate chocolate pieces.

Find the number of chocolate pieces prepared at the beginning of the camp.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq D \leq 100
  • 1 \leq X \leq 100
  • 1 \leq A_i \leq 100 (1 \leq i \leq N)
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
D X
A_1
A_2
:
A_N

Output

Find the number of chocolate pieces prepared at the beginning of the camp.


Sample Input 1

3
7 1
2
5
10

Sample Output 1

8

The camp has 3 participants and lasts for 7 days. Each participant eats chocolate pieces as follows:

  • The first participant eats one chocolate piece on Day 1, 3, 5 and 7, for a total of four.
  • The second participant eats one chocolate piece on Day 1 and 6, for a total of two.
  • The third participant eats one chocolate piece only on Day 1, for a total of one.

Since the number of pieces remaining at the end of the camp is one, the number of pieces prepared at the beginning of the camp is 1 + 4 + 2 + 1 = 8.


Sample Input 2

2
8 20
1
10

Sample Output 2

29

Sample Input 3

5
30 44
26
18
81
18
6

Sample Output 3

56
C - Traveling Plan

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 300

問題文

x 軸上に N 個の観光スポットがあり、1, 2, ..., N の番号がついています。 観光スポット i は座標 A_i の点にあります。 また、x 軸上を座標 a の点から座標 b の点まで移動するには |a - b| 円かかります。

あなたは x 軸上を旅行する計画を立てました。 計画では、最初に座標 0 の点を出発し、N 個の観光スポットを番号順に訪れ、最後に座標 0 の点に戻ってくることになっています。

ところが、旅行の直前に急用が入り、N 個すべての観光スポットを訪れる時間的余裕がなくなってしまいました。 そこで、ある i を選び、観光スポット i を訪れるのを取りやめることにしました。 それ以外の観光スポットは予定通り番号順に訪れます。 また、最初に座標 0 の点を出発し、最後に座標 0 の点に戻ってくることについても、予定に変更はありません。

i = 1, 2, ..., N それぞれについて、観光スポット i を訪れるのを取りやめたときの、旅行中の移動にかかる金額の総和を求めてください。

制約

  • 2 \leq N \leq 10^5
  • -5000 \leq A_i \leq 5000 (1 \leq i \leq N)
  • 入力値はすべて整数である。

入力

入力は以下の形式で標準入力から与えられる。

N
A_1 A_2 ... A_N

出力

N 行出力せよ。 このうち i 行目には、観光スポット i を訪れるのを取りやめたときの、旅行中の移動にかかる金額の総和を出力せよ。


入力例 1

3
3 5 -1

出力例 1

12
8
10

観光スポット 1, 2, 3 はそれぞれ座標 3, 5, -1 の点にあります。 各 i について、観光スポット i を訪れるのを取りやめた場合の移動経路および移動にかかる金額は以下のようになります。

  • i = 1 のとき、移動経路は 0 \rightarrow 5 \rightarrow -1 \rightarrow 0 となり、移動にかかる金額は 5 + 6 + 1 = 12 円となります。
  • i = 2 のとき、移動経路は 0 \rightarrow 3 \rightarrow -1 \rightarrow 0 となり、移動にかかる金額は 3 + 4 + 1 = 8 円となります。
  • i = 3 のとき、移動経路は 0 \rightarrow 3 \rightarrow 5 \rightarrow 0 となり、移動にかかる金額は 3 + 2 + 5 = 10 円となります。

入力例 2

5
1 1 1 2 0

出力例 2

4
4
4
2
4

入力例 3

6
-679 -2409 -3258 3095 -3291 -4462

出力例 3

21630
21630
19932
8924
21630
19288

Score : 300 points

Problem Statement

There are N sightseeing spots on the x-axis, numbered 1, 2, ..., N. Spot i is at the point with coordinate A_i. It costs |a - b| yen (the currency of Japan) to travel from a point with coordinate a to another point with coordinate b along the axis.

You planned a trip along the axis. In this plan, you first depart from the point with coordinate 0, then visit the N spots in the order they are numbered, and finally return to the point with coordinate 0.

However, something came up just before the trip, and you no longer have enough time to visit all the N spots, so you decided to choose some i and cancel the visit to Spot i. You will visit the remaining spots as planned in the order they are numbered. You will also depart from and return to the point with coordinate 0 at the beginning and the end, as planned.

For each i = 1, 2, ..., N, find the total cost of travel during the trip when the visit to Spot i is canceled.

Constraints

  • 2 \leq N \leq 10^5
  • -5000 \leq A_i \leq 5000 (1 \leq i \leq N)
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N lines. In the i-th line, print the total cost of travel during the trip when the visit to Spot i is canceled.


Sample Input 1

3
3 5 -1

Sample Output 1

12
8
10

Spot 1, 2 and 3 are at the points with coordinates 3, 5 and -1, respectively. For each i, the course of the trip and the total cost of travel when the visit to Spot i is canceled, are as follows:

  • For i = 1, the course of the trip is 0 \rightarrow 5 \rightarrow -1 \rightarrow 0 and the total cost of travel is 5 + 6 + 1 = 12 yen.
  • For i = 2, the course of the trip is 0 \rightarrow 3 \rightarrow -1 \rightarrow 0 and the total cost of travel is 3 + 4 + 1 = 8 yen.
  • For i = 3, the course of the trip is 0 \rightarrow 3 \rightarrow 5 \rightarrow 0 and the total cost of travel is 3 + 2 + 5 = 10 yen.

Sample Input 2

5
1 1 1 2 0

Sample Output 2

4
4
4
2
4

Sample Input 3

6
-679 -2409 -3258 3095 -3291 -4462

Sample Output 3

21630
21630
19932
8924
21630
19288
D - Grid Components

Time Limit: 2 sec / Memory Limit: 256 MB

配点 : 500

問題文

2 つの整数 A, B が与えられます。

各マスが白または黒に塗られたグリッドであって以下の条件を満たすものを、出力の項で指定されたフォーマットに従って一つ出力してください。

  • グリッドの大きさを縦 h 行横 w 列としたとき、h および w はともに 100 以下である。
  • 白く塗られたマスの集合はちょうど A 個の連結成分に分かれている (連結成分という単語の定義については後の注釈を参照)。
  • 黒く塗られたマスの集合はちょうど B 個の連結成分に分かれている。

制約の項で指定される条件のもとで解は必ず一つ以上存在することが証明できます。 解が複数ある場合、どれを出力しても構いません。

注釈

2 つの白く塗られたマス c_1, c_2が連結であるとは、マス c_1 からマス c_2 へ、上下左右に隣り合うマスへの移動を繰り返して、 白く塗られたマスだけを通って移動できることを意味します。

白く塗られたマスの集合 S が連結成分であるとは、S が以下の条件を満たすことを意味します。

  • S のどの 2 つのマスも連結である。
  • S に含まれないどの白く塗られたマスと、S に含まれるどのマスも連結ではない。

黒く塗られたマスについても連結成分を同様に定義します。

制約

  • 1 \leq A \leq 500
  • 1 \leq B \leq 500

入力

入力は以下の形式で標準入力から与えられる。

A B

出力

以下の形式で出力せよ。

  • 1 行目には構成したグリッドの大きさを表す整数 h, w を空白を区切りとして出力せよ。
  • 続いて h 行出力せよ。このうちの i 行目 (1 \leq i \leq h) には以下のような長さ w の文字列 s_i を出力せよ。
    • 構成したグリッドの ij (1 \leq j \leq w) 列のマスが白く塗られているならば、s_ij 文字目は . である。
    • 構成したグリッドの ij (1 \leq j \leq w) 列のマスが黒く塗られているならば、s_ij 文字目は # である。

入力例 1

2 3

出力例 1

3 3
##.
..#
#.#

この出力は以下のグリッドに対応します。

2701558bf42f7c088abad927b419472a.png

入力例 2

7 8

出力例 2

3 5
#.#.#
.#.#.
#.#.#

入力例 3

1 1

出力例 3

4 2
..
#.
##
##

入力例 4

3 14

出力例 4

8 18
..................
..................
....##.......####.
....#.#.....#.....
...#...#....#.....
..#.###.#...#.....
.#.......#..#.....
#.........#..####.

Score : 500 points

Problem Statement

You are given two integers A and B.

Print a grid where each square is painted white or black that satisfies the following conditions, in the format specified in Output section:

  • Let the size of the grid be h \times w (h vertical, w horizontal). Both h and w are at most 100.
  • The set of the squares painted white is divided into exactly A connected components.
  • The set of the squares painted black is divided into exactly B connected components.

It can be proved that there always exist one or more solutions under the conditions specified in Constraints section. If there are multiple solutions, any of them may be printed.

Notes

Two squares painted white, c_1 and c_2, are called connected when the square c_2 can be reached from the square c_1 passing only white squares by repeatedly moving up, down, left or right to an adjacent square.

A set of squares painted white, S, forms a connected component when the following conditions are met:

  • Any two squares in S are connected.
  • No pair of a square painted white that is not included in S and a square included in S is connected.

A connected component of squares painted black is defined similarly.

Constraints

  • 1 \leq A \leq 500
  • 1 \leq B \leq 500

Input

Input is given from Standard Input in the following format:

A B

Output

Output should be in the following format:

  • In the first line, print integers h and w representing the size of the grid you constructed, with a space in between.
  • Then, print h more lines. The i-th (1 \leq i \leq h) of these lines should contain a string s_i as follows:
    • If the square at the i-th row and j-th column (1 \leq j \leq w) in the grid is painted white, the j-th character in s_i should be ..
    • If the square at the i-th row and j-th column (1 \leq j \leq w) in the grid is painted black, the j-th character in s_i should be #.

Sample Input 1

2 3

Sample Output 1

3 3
##.
..#
#.#

This output corresponds to the grid below:

2701558bf42f7c088abad927b419472a.png

Sample Input 2

7 8

Sample Output 2

3 5
#.#.#
.#.#.
#.#.#

Sample Input 3

1 1

Sample Output 3

4 2
..
#.
##
##

Sample Input 4

3 14

Sample Output 4

8 18
..................
..................
....##.......####.
....#.#.....#.....
...#...#....#.....
..#.###.#...#.....
.#.......#..#.....
#.........#..####.