A - Alchemy Materials

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

錬金術師の高橋君は N 個の材料を持っています。材料は長さ N の文字列 S で表され、 S の i 文字目は i 番目の材料の種類です。 G は金、 S は銀、 C は銅を表します。

材料の価値は、金が 1 個 3 点、銀が 1 個 2 点、銅が 1 個 1 点です。高橋君が持っている材料の価値の合計を求めてください。

制約

  • 1 \leq N \leq 100
  • S は G 、 S 、 C のみからなる長さ N の文字列

入力

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

N
S
  • 1 行目には、材料の個数 N が与えられる。
  • 2 行目には、材料の種類を表す文字列 S が与えられる。

出力

材料の価値の合計を 1 行で出力せよ。


入力例 1

3
GSC

出力例 1

6

入力例 2

3
GGG

出力例 2

9

入力例 3

5
SSCCG

出力例 3

9

入力例 4

9
SSSGCCCCS

出力例 4

15

入力例 5

21
GSSGGGSGSGCGGCGCGCCSG

出力例 5

48

Score : 200 pts

Problem Statement

Takahashi, an alchemist, has N materials. The materials are represented by a string S of length N, where the i-th character of S is the type of the i-th material. G represents gold, S represents silver, and C represents copper.

The values of the materials are: 3 points for each gold, 2 points for each silver, and 1 point for each copper. Find the total value of the materials Takahashi has.

Constraints

  • 1 \leq N \leq 100
  • S is a string of length N consisting only of G, S, and C.

Input

The input is given from Standard Input in the following format:

N
S
  • The first line contains the number of materials N.
  • The second line contains the string S representing the types of the materials.

Output

Print the total value of the materials in one line.


Sample Input 1

3
GSC

Sample Output 1

6

Sample Input 2

3
GGG

Sample Output 2

9

Sample Input 3

5
SSCCG

Sample Output 3

9

Sample Input 4

9
SSSGCCCCS

Sample Output 4

15

Sample Input 5

21
GSSGGGSGSGCGGCGCGCCSG

Sample Output 5

48
B - Building Heights

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 233 点

問題文

高橋君は市内のビル群を真横から見た図を持っています。図は縦 H 行、横 W 列のマス目で、 H 個の文字列 S_1, S_2, \ldots, S_H で表されます。 S_i の j 文字目は上から i 行目、左から j 列目のマスの様子で、 # はビルの一部を、 . は空を表します。

左から j 列目には j 番目のビルが建っており、その高さは j 列目に含まれる # の個数です。

W 棟のビルの高さを、左から順に出力してください。

制約

  • 1 \leq H \leq 10
  • 1 \leq W \leq 10
  • S_i は # と . のみからなる長さ W の文字列

入力

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

H W
S_1
S_2
\vdots
S_H
  • 1 行目には、図の行数 H と列数 W がスペース区切りで与えられる。
  • 2 行目から H 行にわたり、 1 + i 行目には上から i 行目の様子を表す文字列 S_i が与えられる。

出力

W 棟のビルの高さを、左から順にスペース区切りで 1 行に出力せよ。


入力例 1

3 3
#.#
...
#.#

出力例 1

2 0 2

入力例 2

1 1
#

出力例 2

1

入力例 3

2 2
##
##

出力例 3

2 2

入力例 4

2 2
#.
.#

出力例 4

1 1

入力例 5

3 3
#.#
.#.
#.#

出力例 5

2 1 2

Score : 233 pts

Problem Statement

Takahashi has a side-view picture of buildings in the city. The picture is a grid of H rows and W columns, represented by H strings S_1, S_2, \ldots, S_H. The j-th character of S_i describes the cell in the i-th row from the top and the j-th column from the left: # means part of a building and . means sky.

In the j-th column from the left stands the j-th building, whose height is the number of # contained in the j-th column.

Print the heights of the W buildings, from left to right.

Constraints

  • 1 \leq H \leq 10
  • 1 \leq W \leq 10
  • S_i is a string of length W consisting only of # and .

Input

The input is given from Standard Input in the following format:

H W
S_1
S_2
\vdots
S_H
  • The first line contains the number of rows H and the number of columns W of the picture, separated by a space.
  • For each of the next H lines, the (1 + i)-th line contains the string S_i describing the i-th row from the top.

Output

Print the heights of the W buildings from left to right, separated by spaces, on a single line.


Sample Input 1

3 3
#.#
...
#.#

Sample Output 1

2 0 2

Sample Input 2

1 1
#

Sample Output 2

1

Sample Input 3

2 2
##
##

Sample Output 3

2 2

Sample Input 4

2 2
#.
.#

Sample Output 4

1 1

Sample Input 5

3 3
#.#
.#.
#.#

Sample Output 5

2 1 2
C - The Best-Selling Period

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君は商店街でお店を経営しています。

お店では N 日間の売上データを記録しており、i 日目(1 \leq i \leq N)の損益は V_i 円でした。V_i が正のときはその日に V_i 円の利益があったことを、V_i が負のときはその日に |V_i| 円の損失があったことを、V_i が 0 のときは利益も損失もなかったことを意味します。

高橋君は、最も調子が良かった時期を把握するために、連続する 1 日以上の期間を 1 つ選び、その期間の損益の合計を最大化したいと考えています。

具体的には、1 \leq L \leq R \leq N を満たすすべての整数の組 (L, R) について、L 日目から R 日目までの損益の合計

\displaystyle\sum_{i=L}^{R} V_i

を考えたとき、その最大値を求めてください。

ただし、期間は必ず 1 日以上選ばなければなりません。すべての日が損失である場合でも、損失が最も小さい(V_i が最も大きい)1 日間を選んだ場合の値(負の値)を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • -10^9 \leq V_i \leq 10^9
  • 入力はすべて整数

入力

N
V_1 V_2 \cdots V_N
  • 1 行目には、記録した日数を表す整数 N が与えられる。
  • 2 行目には、各日の損益を表す N 個の整数 V_1, V_2, \ldots, V_N がスペース区切りで与えられる。

出力

連続する 1 日以上の期間における損益の合計の最大値を、整数で 1 行に出力してください。


入力例 1

5
3 -2 5 -1 2

出力例 1

7

入力例 2

8
-3 4 -1 2 1 -5 4 3

出力例 2

8

入力例 3

12
-2 -3 4 -1 -2 1 5 -3 2 -1 4 -2

出力例 3

9

Score : 333 pts

Problem Statement

Takahashi runs a shop in a shopping district.

The shop has recorded sales data over N days, and the profit/loss on day i (1 \leq i \leq N) was V_i yen. When V_i is positive, it means there was a profit of V_i yen on that day; when V_i is negative, it means there was a loss of |V_i| yen on that day; when V_i is 0, it means there was neither profit nor loss.

To identify the period when business was at its best, Takahashi wants to select one contiguous period of at least 1 day and maximize the total profit/loss over that period.

Specifically, for all pairs of integers (L, R) satisfying 1 \leq L \leq R \leq N, consider the total profit/loss from day L to day R:

\displaystyle\sum_{i=L}^{R} V_i

Find the maximum value of this sum.

Note that the selected period must contain at least 1 day. Even if every day results in a loss, you must output the value (which will be negative) obtained by selecting the single day with the smallest loss (the largest V_i).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • -10^9 \leq V_i \leq 10^9
  • All input values are integers

Input

N
V_1 V_2 \cdots V_N
  • The first line contains an integer N representing the number of recorded days.
  • The second line contains N integers V_1, V_2, \ldots, V_N separated by spaces, representing the profit/loss for each day.

Output

Print the maximum total profit/loss over a contiguous period of at least 1 day, as an integer on a single line.


Sample Input 1

5
3 -2 5 -1 2

Sample Output 1

7

Sample Input 2

8
-3 4 -1 2 1 -5 4 3

Sample Output 2

8

Sample Input 3

12
-2 -3 4 -1 -2 1 5 -3 2 -1 4 -2

Sample Output 3

9
D - Three Strings and an Even Number of Pieces

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

N 本のひもがあり、ひも i の長さは A_i です。

異なるひもをちょうど 3 本選び、選んだひもをすべて同じ長さの切れ端に切り分けます。余りが出てはいけません。ただし、切れ端の長さは正の整数とし、これらの条件で選べる長さのうち最大のものを使います。切らずにそのまま残るひもも、切れ端 1 個と数えます。

得られる切れ端の総数が偶数となるような、ひも 3 本の選び方は何通りありますか。長さが同じでも、ひもの番号が異なれば区別します。選ぶ順序は区別しません。

制約

  • 3 \leq N \leq 200000
  • 1 \leq A_i \leq 1000000 (1 \leq i \leq N)
  • 入力はすべて整数である。

入力

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

N
A_1 A_2 \ldots A_N
  • 1 行目には、ひもの本数 N が与えられる。
  • 2 行目には、各ひもの長さ A_1,A_2,\ldots,A_N が空白区切りで与えられる。

出力

条件を満たすひも 3 本の選び方の個数を、整数で出力せよ。


入力例 1

3
1 1 2

出力例 1

1

入力例 2

3
2 2 2

出力例 2

0

入力例 3

4
1 2 3 4

出力例 3

2

入力例 4

6
2 2 4 4 8 8

出力例 4

6

入力例 5

3
1 1000000 999999

出力例 5

1

Score : 400 pts

Problem Statement

There are N ropes, and the length of rope i is A_i.

You will choose exactly 3 distinct ropes and cut all of the chosen ropes into pieces of the same length, with no leftovers. However, the length of each piece must be a positive integer, and you must use the maximum possible length that can be chosen under these conditions. A rope that remains uncut is also counted as 1 piece.

How many ways are there to choose 3 ropes such that the total number of pieces obtained is even? Two ropes with different indices are distinguished even if they have the same length. The order in which they are chosen does not matter.

Constraints

  • 3 \leq N \leq 200000
  • 1 \leq A_i \leq 1000000 (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 \ldots A_N
  • The 1-st line contains the number of ropes N.
  • The 2-nd line contains the lengths of the ropes A_1,A_2,\ldots,A_N, separated by spaces.

Output

Output the number of ways to choose 3 ropes that satisfy the condition as an integer.


Sample Input 1

3
1 1 2

Sample Output 1

1

Sample Input 2

3
2 2 2

Sample Output 2

0

Sample Input 3

4
1 2 3 4

Sample Output 3

2

Sample Input 4

6
2 2 4 4 8 8

Sample Output 4

6

Sample Input 5

3
1 1000000 999999

Sample Output 5

1
E - Closure Separating Three Lines Ahead

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 433 点

問題文

N 個の施設があり、1 から N までの番号が付いています。施設の間には N-1 本の双方向の通路があり、通路 i は施設 u_i と施設 v_i を結んでいます。どの 2 つの施設の間にも、同じ施設を 2 回以上通らない道筋がちょうど 1 つあります。

いくつかの施設を閉鎖します。施設 i を閉鎖する費用は A_i です。施設を閉鎖すると、その施設と、その施設につながるすべての通路が使えなくなります。

閉鎖後、通路をたどって行き来できる任意の 2 つの施設について、使える通路を高々 2 本通るだけで一方から他方へ移動できるようにしたいです。閉鎖されていない施設が一つもなくてもかまいません。

この条件を満たすために必要な、閉鎖費用の総和の最小値を求めてください。

制約

  • 1\leq N\leq 200000
  • 0\leq A_i\leq 10^9 (1\leq i\leq N)
  • 1\leq u_i,v_i\leq N (1\leq i\leq N-1)
  • 与えられる施設と通路は木をなす。
  • 入力はすべて整数である。

入力

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

N
A_1 A_2 \ldots A_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
  • 1 行目には施設の個数 N が与えられる。
  • 2 行目には各施設の閉鎖費用が番号順に与えられる。
  • 続く N-1 行には各通路が結ぶ施設の番号が与えられる。N=1 の場合、これらの行はない。

出力

閉鎖費用の総和の最小値を整数で出力せよ。


入力例 1

4
8 2 9 7
1 2
2 3
3 4

出力例 1

2

入力例 2

5
5 8 3 9 4
1 2
1 3
1 4
1 5

出力例 2

0

入力例 3

7
5 3 4 8 8 8 8
1 2
1 3
2 4
2 5
3 6
3 7

出力例 3

3

入力例 4

10
9 1 7 8 4 2 6 3 5 9
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

出力例 4

6

入力例 5

1
0

出力例 5

0

Score : 433 pts

Problem Statement

There are N facilities, numbered 1 to N. There are N-1 bidirectional pathways between the facilities, and pathway i connects facility u_i and facility v_i. Between any two facilities, there is exactly one path that does not visit the same facility more than once.

You will close some facilities. The cost to close facility i is A_i. When a facility is closed, that facility and all pathways connected to it become unusable.

After the closures, for any two facilities that can reach each other by following pathways, you want it to be possible to travel between them by traversing at most 2 usable pathways. It is acceptable if no facilities remain unclosed.

Find the minimum total closure cost required to satisfy this condition.

Constraints

  • 1\leq N\leq 200000
  • 0\leq A_i\leq 10^9 (1\leq i\leq N)
  • 1\leq u_i,v_i\leq N (1\leq i\leq N-1)
  • The given facilities and pathways form a tree.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 \ldots A_N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}
  • The first line contains the number of facilities, N.
  • The second line contains the closure cost of each facility in order of their numbers.
  • The following N-1 lines contain the numbers of the facilities connected by each pathway. If N=1, these lines are absent.

Output

Print the minimum total closure cost as an integer.


Sample Input 1

4
8 2 9 7
1 2
2 3
3 4

Sample Output 1

2

Sample Input 2

5
5 8 3 9 4
1 2
1 3
1 4
1 5

Sample Output 2

0

Sample Input 3

7
5 3 4 8 8 8 8
1 2
1 3
2 4
2 5
3 6
3 7

Sample Output 3

3

Sample Input 4

10
9 1 7 8 4 2 6 3 5 9
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10

Sample Output 4

6

Sample Input 5

1
0

Sample Output 5

0