A - Online Shopping

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

AtCoder 社は、オンラインショップでグッズを販売しています。

高橋君はそこで N 種類の商品を購入することにしました。
1 以上 N 以下の整数 i について、i 種類目の商品は 1P_i 円で、高橋君は Q_i 個購入します。

また、高橋君は送料を支払う必要があります。
送料は購入する商品の合計金額が S 円以上なら 0 円、そうでないならば K 円です。

高橋君が支払う金額は購入する商品の合計金額と送料の和です。
高橋君が支払う金額を求めてください。

制約

  • 1\leq N\leq 100
  • 1\leq S\leq 10000
  • 1\leq K\leq 10000
  • 1\leq P_i\leq 10000
  • 1\leq Q_i\leq 100
  • 入力はすべて整数

入力

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

N S K
P_1 Q_1
P_2 Q_2
\vdots
P_N Q_N

出力

高橋君がオンラインショッピングで支払う金額を出力せよ。


入力例 1

2 2000 500
1000 1
100 6

出力例 1

2100

高橋君は 11000 円の商品を 1 個と、 1100 円の商品を 6 つ購入します。
よって、購入する商品の合計金額は 1000\times 1+100\times 6=1600 円となります。
このとき購入する商品の合計金額は 2000 円未満であるため、送料は 500 円となります。
よって、高橋君の支払う金額は 1600+500=2100 円となります。


入力例 2

3 2000 500
1000 1
100 6
5000 1

出力例 2

6600

購入する商品の合計金額は 1000\times 1+100\times 6+5000\times 1=6600 円となります。
このとき購入する商品の合計金額は 2000 円以上であるため、送料は 0 円となります。
よって、高橋君の支払う金額は 6600+0=6600 円となります。


入力例 3

2 2000 500
1000 1
1000 1

出力例 3

2000

1 個あたりの価格が同じ商品が複数存在することもあります。

Score : 100 points

Problem Statement

AtCoder Inc. sells merchandise through its online shop.

Takahashi has decided to purchase N types of products from there.
For each integer i from 1 to N, the i-th type of product has a price of P_i yen each, and he will buy Q_i of this.

Additionally, he must pay a shipping fee.
The shipping fee is 0 yen if the total price of the products purchased is S yen or above, and K yen otherwise.

He will pay the total price of the products purchased plus the shipping fee.
Calculate the amount he will pay.

Constraints

  • 1\leq N\leq 100
  • 1\leq S\leq 10000
  • 1\leq K\leq 10000
  • 1\leq P_i\leq 10000
  • 1\leq Q_i\leq 100
  • All input values are integers.

Input

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

N S K
P_1 Q_1
P_2 Q_2
\vdots
P_N Q_N

Output

Print the amount Takahashi will pay for online shopping.


Sample Input 1

2 2000 500
1000 1
100 6

Sample Output 1

2100

Takahashi buys one product for 1000 yen and six products for 100 yen each.
Thus, the total price of the products is 1000\times 1+100\times 6=1600 yen.
Since the total amount for the products is less than 2000 yen, the shipping fee will be 500 yen.
Therefore, the amount Takahashi will pay is 1600+500=2100 yen.


Sample Input 2

3 2000 500
1000 1
100 6
5000 1

Sample Output 2

6600

The total price of the products is 1000\times 1+100\times 6+5000\times 1=6600 yen.
Since the total amount for the products is not less than 2000 yen, the shipping fee will be 0 yen.
Therefore, the amount Takahashi will pay is 6600+0=6600 yen.


Sample Input 3

2 2000 500
1000 1
1000 1

Sample Output 3

2000

There may be multiple products with the same price per item.

B - Glass and Mug

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

AtCoder 社はグラスマグカップを販売しています。

高橋君は容量が G ml のグラスと、容量が M ml のマグカップを 1 つずつ持っています。
ここで、G,MG<M をみたします。

最初、グラスとマグカップはいずれも空です。
以下の操作を K 回繰り返した後で、グラスとマグカップに水がそれぞれ何 ml ずつ入っているか求めてください。

  • グラスが水で満たされているとき、すなわちグラスにちょうど G ml 入っているとき、グラスの水をすべて捨てる。
  • そうでなく、マグカップが空であるとき、マグカップを水で満たす。
  • 上のいずれでもないとき、マグカップが空になるかグラスが水で満たされるまで、マグカップからグラスに水を移す。

制約

  • 1\leq K\leq 100
  • 1\leq G<M\leq 1000
  • G,M,K は整数

入力

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

K G M

出力

操作を K 回行った後で、グラスとマグカップに水がそれぞれ何 ml ずつ入っているか、この順に空白区切りで出力せよ。


入力例 1

5 300 500

出力例 1

200 500

操作は次の順で行われます。最初、グラスとマグカップはいずれも空です。

  • マグカップを水で満たす。グラスには 0 ml, マグカップには 500 ml 入った状態になる。
  • グラスが満たされるまでマグカップからグラスに水を移す。グラスには 300 ml, マグカップには 200 ml 入った状態になる。
  • グラスの水をすべて捨てる。グラスには 0 ml, マグカップには 200 ml 入った状態になる。
  • マグカップが空になるまでマグカップからグラスに水を移す。グラスには 200 ml, マグカップには 0 ml 入った状態になる。
  • マグカップを水で満たす。グラスには 200 ml, マグカップには 500 ml 入った状態になる。

よって、5 回の操作の後でグラスには 200 ml, マグカップには 500 ml 入った状態になります。 ゆえに、200, 500 を空白区切りでこの順に出力します。


入力例 2

5 100 200

出力例 2

0 0

Score : 200 points

Problem Statement

AtCoder Inc. sells glasses and mugs.

Takahashi has a glass with a capacity of G milliliters and a mug with a capacity of M milliliters.
Here, G<M.

Initially, both the glass and the mug are empty.
After performing the following operation K times, determine how many milliliters of water are in the glass and the mug, respectively.

  • When the glass is filled with water, that is, the glass contains exactly G milliliters of water, discard all the water from the glass.
  • Otherwise, if the mug is empty, fill the mug with water.
  • Otherwise, transfer water from the mug to the glass until the mug is empty or the glass is filled with water.

Constraints

  • 1\leq K\leq 100
  • 1\leq G<M\leq 1000
  • G, M, and K are integers.

Input

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

K G M

Output

Print the amounts, in milliliters, of water in the glass and the mug, in this order, separated by a space, after performing the operation K times.


Sample Input 1

5 300 500

Sample Output 1

200 500

The operation will be performed as follows. Initially, both the glass and the mug are empty.

  • Fill the mug with water. The glass has 0 milliliters, and the mug has 500 milliliters of water.
  • Transfer water from the mug to the glass until the glass is filled. The glass has 300 milliliters, and the mug has 200 milliliters of water.
  • Discard all the water from the glass. The glass has 0 milliliters, and the mug has 200 milliliters of water.
  • Transfer water from the mug to the glass until the mug is empty. The glass has 200 milliliters, and the mug has 0 milliliters of water.
  • Fill the mug with water. The glass has 200 milliliters, and the mug has 500 milliliters of water.

Thus, after five operations, the glass has 200 milliliters, and the mug has 500 milliliters of water. Hence, print 200 and 500 in this order, separated by a space.


Sample Input 2

5 100 200

Sample Output 2

0 0
C - T-shirts

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

AtCoder 社はロゴ入りの T シャツを販売しています。

高橋君の N 日間の予定が 0, 1, 2 のみからなる長さ N の文字列 S で与えられます。
具体的には、1\leq i\leq N をみたす整数 i について、

  • Si 文字目が 0 のとき、i 日目に何の予定も入っていません。
  • Si 文字目が 1 のとき、i 日目に高橋君は食事に行く予定があります。
  • Si 文字目が 2 のとき、i 日目に高橋君は競技プログラミングのイベントに行く予定が入っています。

高橋君は無地の T シャツを M 枚持っており、1 日目の直前の時点ですべて洗濯済みの状態となっています。
これに加えて、次の条件をみたすように行動できるように、高橋君は AtCoder のロゴ入りの T シャツを何枚か購入する事にしました。

  • 食事に行く日には、無地の T シャツ 1 枚またはロゴ入りの T シャツ 1 枚を着用する。
  • 競技プログラミングのイベントに行く日にはロゴ入りの T シャツ 1 枚を着用する。
  • 何の予定もない日には T シャツを着用しない。加えて、その時点で着用済みの T シャツを全て洗濯する。 洗濯した T シャツは翌日から着用できる。
  • 一度着用した T シャツは次に洗濯するまで着用できない。

N 日間のうち予定が入っている日すべてについて、条件をみたす T シャツを着用できるようにするために、高橋君は最低何枚のTシャツを購入する必要があるか求めてください。 新しく T シャツを購入する必要がないならば 0 を出力してください。
ただし、購入した T シャツも 1 日目の直前の時点ですべて洗濯済みの状態で存在するものとします。

制約

  • 1\leq M\leq N\leq 1000
  • S0, 1, 2 のみからなる長さ N の文字列
  • N,M は整数

入力

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

N M
S

出力

問題文の条件をみたすように行動するために 高橋君が購入する必要のある T シャツの枚数の最小値を出力せよ。
新しく購入する必要がないならば 0 を出力せよ。


入力例 1

6 1
112022

出力例 1

2

高橋君がロゴ入りの T シャツを 2 枚購入したとき、次のようにして高橋君は T シャツを着用することができます。

  • 1 日目、高橋君はロゴ入りの T シャツを着用して食事に行きます。
  • 2 日目、高橋君は無地の T シャツを着用して食事に行きます。
  • 3 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
  • 4 日目、高橋君は予定がないため、着用した T シャツをすべて洗濯します。これにより、1,2,3 日目に着用した T シャツを再び着用することが可能になります。
  • 5 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。
  • 6 日目、高橋君はロゴ入りの T シャツを着用して競技プログラミングのイベントに行きます。

高橋君がロゴ入りの T シャツを 1 枚以下しか購入しなかった場合には、 どのようにしても条件をみたすように T シャツを着用することができません。
よって、2 を出力します。


入力例 2

3 1
222

出力例 2

3

入力例 3

2 1
01

出力例 3

0

高橋君は新しく T シャツを購入する必要はありません。

Score : 300 points

Problem Statement

AtCoder Inc. sells T-shirts with its logo.

You are given Takahashi's schedule for N days as a string S of length N consisting of 0, 1, and 2.
Specifically, for an integer i satisfying 1\leq i\leq N,

  • if the i-th character of S is 0, he has no plan scheduled for the i-th day;
  • if the i-th character of S is 1, he plans to go out for a meal on the i-th day;
  • if the i-th character of S is 2, he plans to attend a competitive programming event on the i-th day.

Takahashi has M plain T-shirts, all washed and ready to wear just before the first day.
In addition, to be able to satisfy the following conditions, he will buy several AtCoder logo T-shirts.

  • On days he goes out for a meal, he will wear a plain or logo T-shirt.
  • On days he attends a competitive programming event, he will wear a logo T-shirt.
  • On days with no plans, he will not wear any T-shirts. Also, he will wash all T-shirts worn at that point. He can wear them again from the next day onwards.
  • Once he wears a T-shirt, he cannot wear it again until he washes it.

Determine the minimum number of T-shirts he needs to buy to be able to wear appropriate T-shirts on all scheduled days during the N days. If he does not need to buy new T-shirts, print 0.
Assume that the purchased T-shirts are also washed and ready to use just before the first day.

Constraints

  • 1\leq M\leq N\leq 1000
  • S is a string of length N consisting of 0, 1, and 2.
  • N and M are integers.

Input

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

N M
S

Output

Print the minimum number of T-shirts Takahashi needs to buy to be able to satisfy the conditions in the problem statement.
If he does not need to buy new T-shirts, print 0.


Sample Input 1

6 1
112022

Sample Output 1

2

If Takahashi buys two logo T-shirts, he can wear T-shirts as follows:

  • On the first day, he wears a logo T-shirt to go out for a meal.
  • On the second day, he wears a plain T-shirt to go out for a meal.
  • On the third day, he wears a logo T-shirt to attend a competitive programming event.
  • On the fourth day, he has no plans, so he washes all the worn T-shirts. This allows him to reuse the T-shirts worn on the first, second, and third days.
  • On the fifth day, he wears a logo T-shirt to attend a competitive programming event.
  • On the sixth day, he wears a logo T-shirt to attend a competitive programming event.

If he buys one or fewer logo T-shirts, he cannot use T-shirts to meet the conditions no matter what. Hence, print 2.


Sample Input 2

3 1
222

Sample Output 2

3

Sample Input 3

2 1
01

Sample Output 3

0

He does not need to buy new T-shirts.

D - Swapping Puzzle

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 425

問題文

HW 列の 2 つのグリッド A, B が与えられます。

1 \leq i \leq H1 \leq j \leq W を満たす各整数の組 (i, j) について、 i 行目 j 列目にあるマスをマス (i, j) と呼ぶとき、 グリッド A の マス (i, j) には整数 A_{i, j} が、 グリッド B の マス (i, j) には整数 B_{i, j} がそれぞれ書かれています。

あなたは「下記の 2 つのうちのどちらか 1 つを行う」という操作を好きな回数( 0 回でもよい)だけ繰り返します。

  • 1 \leq i \leq H-1 を満たす整数 i を選び、グリッド A の i 行目と (i+1) 行目を入れ替える。
  • 1 \leq i \leq W-1 を満たす整数 i を選び、グリッド A の i 列目と (i+1) 列目を入れ替える。

上記の操作の繰り返しによって、グリッド A をグリッド B に一致させることが可能かどうかを判定してください。 さらに、一致させることが可能な場合は、そのために行う操作回数の最小値を出力してください。

ここで、グリッド A とグリッド B が一致しているとは、 1 \leq i \leq H1 \leq j \leq W を満たす全ての整数の組 (i, j) について、 グリッド A の マス (i, j) とグリッド B の マス (i, j) に書かれた整数が等しいこととします。

制約

  • 入力される値は全て整数
  • 2 \leq H, W \leq 5
  • 1 \leq A_{i, j}, B_{i, j} \leq 10^9

入力

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

H W
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
B_{1, 1} B_{1, 2} \cdots B_{1, W}
B_{2, 1} B_{2, 2} \cdots B_{2, W}
\vdots
B_{H, 1} B_{H, 2} \cdots B_{H, W}

出力

グリッド A をグリッド B に一致させることが不可能な場合は -1 を、 可能な場合はグリッド A をグリッド B に一致させるために行う操作回数の最小値を出力せよ。


入力例 1

4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

出力例 1

3

初期状態のグリッド A の 4 列目と 5 列目を入れ替えると、グリッド A は下記の通りになります。

1 2 3 5 4
6 7 8 10 9
11 12 13 15 14
16 17 18 20 19

続けて、グリッド A の 2 行目と 3 行目を入れ替えると、グリッド A は下記の通りになります。

1 2 3 5 4
11 12 13 15 14
6 7 8 10 9
16 17 18 20 19

最後に、グリッド A の 2 列目と 3 列目を入れ替えると、グリッド A は下記の通りになり、グリッド B に一致します。

1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

上に述べた 3 回の操作でグリッド A をグリッド B に一致させることができ、 これより少ない回数の操作でグリッド A をグリッド B に一致させることはできないため、 3 を出力します。


入力例 2

2 2
1 1
1 1
1 1
1 1000000000

出力例 2

-1

問題文中の操作をどのように行ってもグリッド A をグリッド B に一致させることは不可能であるため -1 を出力します。


入力例 3

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

出力例 3

0

グリッド A ははじめからグリッド B に一致しています。


入力例 4

5 5
710511029 136397527 763027379 644706927 447672230
979861204 57882493 442931589 951053644 152300688
43971370 126515475 962139996 541282303 834022578
312523039 506696497 664922712 414720753 304621362
325269832 191410838 286751784 732741849 806602693
806602693 732741849 286751784 191410838 325269832
304621362 414720753 664922712 506696497 312523039
834022578 541282303 962139996 126515475 43971370
152300688 951053644 442931589 57882493 979861204
447672230 644706927 763027379 136397527 710511029

出力例 4

20

Score : 425 points

Problem Statement

You are given two grids, A and B, each with H rows and W columns.

For each pair of integers (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W, let (i, j) denote the cell in the i-th row and j-th column. In grid A, cell (i, j) contains the integer A_{i, j}. In grid B, cell (i, j) contains the integer B_{i, j}.

You will repeat the following operation any number of times, possibly zero. In each operation, you perform one of the following:

  • Choose an integer i satisfying 1 \leq i \leq H-1 and swap the i-th and (i+1)-th rows in grid A.
  • Choose an integer i satisfying 1 \leq i \leq W-1 and swap the i-th and (i+1)-th columns in grid A.

Determine whether it is possible to make grid A identical to grid B by repeating the above operation. If it is possible, print the minimum number of operations required to do so.

Here, grid A is identical to grid B if and only if, for all pairs of integers (i, j) satisfying 1 \leq i \leq H and 1 \leq j \leq W, the integer written in cell (i, j) of grid A is equal to the integer written in cell (i, j) of grid B.

Constraints

  • All input values are integers.
  • 2 \leq H, W \leq 5
  • 1 \leq A_{i, j}, B_{i, j} \leq 10^9

Input

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

H W
A_{1, 1} A_{1, 2} \cdots A_{1, W}
A_{2, 1} A_{2, 2} \cdots A_{2, W}
\vdots
A_{H, 1} A_{H, 2} \cdots A_{H, W}
B_{1, 1} B_{1, 2} \cdots B_{1, W}
B_{2, 1} B_{2, 2} \cdots B_{2, W}
\vdots
B_{H, 1} B_{H, 2} \cdots B_{H, W}

Output

If it is impossible to make grid A identical to grid B, output -1. Otherwise, print the minimum number of operations required to make grid A identical to grid B.


Sample Input 1

4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

Sample Output 1

3

Swapping the fourth and fifth columns of the initial grid A yields the following grid:

1 2 3 5 4
6 7 8 10 9
11 12 13 15 14
16 17 18 20 19

Then, swapping the second and third rows yields the following grid:

1 2 3 5 4
11 12 13 15 14
6 7 8 10 9
16 17 18 20 19

Finally, swapping the second and third columns yields the following grid, which is identical to grid B:

1 3 2 5 4
11 13 12 15 14
6 8 7 10 9
16 18 17 20 19

You can make grid A identical to grid B with the three operations above and cannot do so with fewer operations, so print 3.


Sample Input 2

2 2
1 1
1 1
1 1
1 1000000000

Sample Output 2

-1

There is no way to perform the operation to make grid A match grid B, so print -1.


Sample Input 3

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

Sample Output 3

0

Grid A is already identical to grid B at the beginning.


Sample Input 4

5 5
710511029 136397527 763027379 644706927 447672230
979861204 57882493 442931589 951053644 152300688
43971370 126515475 962139996 541282303 834022578
312523039 506696497 664922712 414720753 304621362
325269832 191410838 286751784 732741849 806602693
806602693 732741849 286751784 191410838 325269832
304621362 414720753 664922712 506696497 312523039
834022578 541282303 962139996 126515475 43971370
152300688 951053644 442931589 57882493 979861204
447672230 644706927 763027379 136397527 710511029

Sample Output 4

20
E - Lucky bag

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 525

問題文

AtCoder 社は、オンラインショップでグッズを販売しています。

今、N 個のグッズが社内に残っています。 ここで、i (1\leq i\leq N) 個目のグッズの重さは W_i です。

高橋君は残ったグッズをまとめて D 袋の福袋として販売する事にしました。
高橋君は各福袋に入ったグッズの重さの合計の分散を最小にしたいと考えています。
ここで、各福袋に入ったグッズの重さの合計がそれぞれ x_1,x_2,\ldots,x_D であるとき、
それらの平均を \bar{x}=\frac{1}{D}(x_1+x_2+\cdots+x_D) として、 分散は V=\frac{1}{D}\displaystyle\sum_{i=1}^D (x_i-\bar{x})^2 として定義されます。

各福袋に入ったグッズの重さの合計の分散が最小になるようにグッズを分けた時の分散の値を求めてください。
ただし、空の福袋が存在してもかまいません(この時福袋に入ったグッズの重さの合計は 0 として定義されます)が、
どのグッズも D 袋のうちちょうど 1 つの福袋に入っている ようにするものとします。

制約

  • 2 \leq D\leq N\leq 15
  • 1 \leq W_i\leq 10^8
  • 入力はすべて整数

入力

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

N D
W_1 W_2 \ldots W_N

出力

各福袋に入ったグッズの重さの合計の分散が最小になるようにグッズを分けた時の分散の値を出力せよ。
出力は、真の値との絶対誤差または相対誤差が 10^{-6} 以下のとき正解と判定される。


入力例 1

5 3
3 5 3 6 3

出力例 1

0.888888888888889

1 つめの福袋に 1,3 個目のグッズを、 2 つめの福袋に 2,5 個目のグッズを、 3 つめの福袋に 4 個目のグッズを入れると、
それぞれの福袋に入ったグッズの重さの合計は 6,8,6 となります。

このとき、重さの平均は \frac{1}{3}(6+8+6)=\frac{20}{3} であり、
分散は \frac{1}{3}\left\{\left(6-\frac{20}{3}\right)^2+\left(8-\frac{20}{3}\right)^2+\left(6-\frac{20}{3}\right)^2 \right\}=\frac{8}{9}=0.888888\ldots となり、このときが最小です。

同じ重さのグッズが複数存在し得ること、 各グッズはいずれかの福袋に入っている必要があることに注意してください。

Score : 525 points

Problem Statement

AtCoder Inc. sells merchandise on its online shop.

There are N items remaining in the company. The weight of the i-th item (1\leq i\leq N) is W_i.

Takahashi will sell these items as D lucky bags.
He wants to minimize the variance of the total weights of the items in the lucky bags.
Here, the variance is defined as V=\frac{1}{D}\displaystyle\sum_{i=1}^D (x_i-\bar{x})^2, where x_1,x_2,\ldots,x_D are the total weights of the items in the lucky bags, and \bar{x}=\frac{1}{D}(x_1+x_2+\cdots+x_D) is the average of x_1,x_2,\ldots,x_D.

Find the variance of the total weights of the items in the lucky bags when the items are divided to minimize this value.
It is acceptable to have empty lucky bags (in which case the total weight of the items in that bag is defined as 0),
but each item must be in exactly one of the D lucky bags.

Constraints

  • 2 \leq D\leq N\leq 15
  • 1 \leq W_i\leq 10^8
  • All input values are integers.

Input

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

N D
W_1 W_2 \ldots W_N

Output

Print the variance of the total weights of the items in the lucky bags when the items are divided to minimize this value.
Your output will be considered correct if the absolute or relative error from the true value is at most 10^{-6}.


Sample Input 1

5 3
3 5 3 6 3

Sample Output 1

0.888888888888889

If you put the first and third items in the first lucky bag, the second and fifth items in the second lucky bag, and the fourth item in the third lucky bag, the total weight of the items in the bags are 6, 8, and 6, respectively.

Then, the average weight is \frac{1}{3}(6+8+6)=\frac{20}{3},
and the variance is \frac{1}{3}\left\{\left(6-\frac{20}{3}\right)^2+\left(8-\frac{20}{3}\right)^2+\left(6-\frac{20}{3}\right)^2 \right\}=\frac{8}{9}=0.888888\ldots, which is the minimum.

Note that multiple items may have the same weight, and that each item must be in one of the lucky bags.

F - Random Update Query

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 550

問題文

長さ N の整数列 A = (A_1, A_2, \ldots, A_N) が与えられます。

A に対して、i = 1, 2, \ldots, M の順に下記の操作を行います。

  • まず、L_i 以上 R_i 以下の整数を等確率でランダムに 1 個を選び、それを p とおく。
  • そして、A_p の値を整数 X_i に変更する。

上記の手順を行った後の最終的な A における、A_i の期待値 \text{mod } 998244353 を、 各 i = 1, 2, \ldots, N について出力してください。

期待値 \text{mod } 998244353 の定義

この問題で求める期待値は必ず有理数になることが証明できます。 また、この問題の制約下では、求める期待値を既約分数 \frac{y}{x} で表したときに x998244353 で割り切れないことが保証されます。

このとき xz \equiv y \pmod{998244353} を満たすような 0 以上 998244352 以下の整数 z が一意に定まります。この z を答えてください。

制約

  • 入力される値は全て整数
  • 1 \leq N, M \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 \leq L_i \leq R_i \leq N
  • 0 \leq X_i \leq 10^9

入力

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

N M
A_1 A_2 \ldots A_N
L_1 R_1 X_1
L_2 R_2 X_2
\vdots
L_M R_M X_M

出力

i = 1, 2, \ldots, N に対する最終的な A_i の期待値 E_i を、 下記の形式の通りに空白区切りで出力せよ。

E_1 E_2 \ldots E_N

入力例 1

5 2
3 1 4 1 5
1 2 2
2 4 0

出力例 1

499122179 1 665496238 665496236 5

A = (3, 1, 4, 1, 5) である初期状態からはじめ、下記の通りに 2 個の操作が行われます。

  • まず、1 個目の操作で、A_1A_2 のどちらか一方が等確率でランダムに選ばれ、その値が 2 に変更されます。
  • その後、2 個目の操作で、A_2, A_3, A_4 のうちいずれか 1 個が等確率でランダムに選ばれ、その値が 0 に変更されます。

その結果、最終的な A の各要素の期待値は (E_1, E_2, E_3, E_4, E_5) = (\frac{5}{2}, 1, \frac{8}{3}, \frac{2}{3}, 5) です。


入力例 2

2 4
1 2
1 1 3
2 2 4
1 1 5
2 2 6

出力例 2

5 6

入力例 3

20 20
998769066 273215338 827984962 78974225 994243956 791478211 891861897 680427073 993663022 219733184 570206440 43712322 66791680 164318676 209536492 137458233 289158777 461179891 612373851 330908158
12 18 769877494
9 13 689822685
6 13 180913148
2 16 525285434
2 14 98115570
14 17 622616620
8 12 476462455
13 17 872412050
14 15 564176146
7 13 143650548
2 5 180435257
4 10 82903366
1 2 643996562
8 10 262860196
10 14 624081934
11 13 581257775
9 19 381806138
3 12 427930466
6 19 18249485
14 19 682428942

出力例 3

821382814 987210378 819486592 142238362 447960587 678128197 687469071 405316549 318941070 457450677 426617745 712263899 939619994 228431878 307695685 196179692 241456697 12668393 685902422 330908158

Score : 550 points

Problem Statement

You are given an integer sequence A = (A_1, A_2, \ldots, A_N) of length N.

We will perform the following operation on A for i = 1, 2, \ldots, M in this order.

  • First, choose an integer between L_i and R_i, inclusive, uniformly at random and denote it as p.
  • Then, change the value of A_p to the integer X_i.

For the final sequence A after the above procedure, print the expected value, modulo 998244353, of A_i for each i = 1, 2, \ldots, N.

How to print expected values modulo 998244353

It can be proved that the expected values sought in this problem are always rational. Furthermore, the constraints of this problem guarantee that if each of those expected values is expressed as an irreducible fraction \frac{y}{x}, then x is not divisible by 998244353.

Now, there is a unique integer z between 0 and 998244352, inclusive, such that xz \equiv y \pmod{998244353}. Report this z.

Constraints

  • All input values are integers.
  • 1 \leq N, M \leq 2 \times 10^5
  • 0 \leq A_i \leq 10^9
  • 1 \leq L_i \leq R_i \leq N
  • 0 \leq X_i \leq 10^9

Input

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

N M
A_1 A_2 \ldots A_N
L_1 R_1 X_1
L_2 R_2 X_2
\vdots
L_M R_M X_M

Output

Print the expected values E_i of the final A_i for i = 1, 2, \ldots, N in the format below, separated by spaces.

E_1 E_2 \ldots E_N

Sample Input 1

5 2
3 1 4 1 5
1 2 2
2 4 0

Sample Output 1

499122179 1 665496238 665496236 5

We start from the initial state A = (3, 1, 4, 1, 5) and perform the following two operations.

  • The first operation chooses A_1 or A_2 uniformly at random, and changes its value to 2.
  • Then, the second operation chooses one of A_2, A_3, A_4 uniformly at random, and changes its value to 0.

As a result, the expected values of the elements in the final A are (E_1, E_2, E_3, E_4, E_5) = (\frac{5}{2}, 1, \frac{8}{3}, \frac{2}{3}, 5).


Sample Input 2

2 4
1 2
1 1 3
2 2 4
1 1 5
2 2 6

Sample Output 2

5 6

Sample Input 3

20 20
998769066 273215338 827984962 78974225 994243956 791478211 891861897 680427073 993663022 219733184 570206440 43712322 66791680 164318676 209536492 137458233 289158777 461179891 612373851 330908158
12 18 769877494
9 13 689822685
6 13 180913148
2 16 525285434
2 14 98115570
14 17 622616620
8 12 476462455
13 17 872412050
14 15 564176146
7 13 143650548
2 5 180435257
4 10 82903366
1 2 643996562
8 10 262860196
10 14 624081934
11 13 581257775
9 19 381806138
3 12 427930466
6 19 18249485
14 19 682428942

Sample Output 3

821382814 987210378 819486592 142238362 447960587 678128197 687469071 405316549 318941070 457450677 426617745 712263899 939619994 228431878 307695685 196179692 241456697 12668393 685902422 330908158
G - Not Too Many Balls

Time Limit: 3 sec / Memory Limit: 1024 MB

配点 : 625

問題文

いくつかのボールがあります。 各ボールは色 1 、色 2\ldots 、色 N のうちのいずれかであり、 i = 1, 2, \ldots, N について、色 i のボールは全部で A_i 個あります。

また、M 個の箱があります。 j = 1, 2, \ldots, M について、j 番目の箱には合計で B_j 個までのボールを入れることができます。

ただし、1 \leq i \leq N1 \leq j \leq M を満たすすべての整数の組 (i, j) について、 色 i のボールを j 番目の箱に入れる個数は (i \times j) 個以下でなければなりません。

M 個の箱の中に入れることができるボールの合計個数の最大値を出力してください。

制約

  • 入力される値は全て整数
  • 1 \leq N \leq 500
  • 1 \leq M \leq 5 \times 10^5
  • 0 \leq A_i, B_i \leq 10^{12}

入力

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

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

出力

答えを出力せよ。


入力例 1

2 3
8 10
4 3 8

出力例 1

14

下記の通りにボールを箱に入れることで、問題文中の条件を満たした上で合計 14 個のボールを箱に入れることができます。

  • 1 のボールを、1 番目の箱に 1 個、2 番目の箱に 1 個、3 番目の箱に 3 個入れる。
  • 2 のボールを、1 番目の箱に 2 個、2 番目の箱に 2 個、3 番目の箱に 5 個入れる。

入力例 2

1 1
1000000000000
0

出力例 2

0

入力例 3

10 12
59 168 130 414 187 236 330 422 31 407
495 218 351 105 351 414 198 230 345 297 489 212

出力例 3

2270

Score : 625 points

Problem Statement

There are several balls. Each ball is one of the colors 1, 2, \ldots, N, and for each i = 1, 2, \ldots, N, there are A_i balls of color i.

Additionally, there are M boxes. For each j = 1, 2, \ldots, M, the j-th box can hold up to B_j balls in total.

Here, for all pairs of integers (i, j) satisfying 1 \leq i \leq N and 1 \leq j \leq M, the j-th box may hold at most (i \times j) balls of color i.

Print the maximum total number of balls that the M boxes can hold.

Constraints

  • All input values are integers.
  • 1 \leq N \leq 500
  • 1 \leq M \leq 5 \times 10^5
  • 0 \leq A_i, B_i \leq 10^{12}

Input

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

N M
A_1 A_2 \ldots A_N
B_1 B_2 \ldots B_M

Output

Print the answer.


Sample Input 1

2 3
8 10
4 3 8

Sample Output 1

14

You can do as follows to put a total of 14 balls into the boxes while satisfying the conditions in the problem statement.

  • For balls of color 1, put one into the first box, one into the second box, and three into the third box.
  • For balls of color 2, put two into the first box, two into the second box, and five into the third box.

Sample Input 2

1 1
1000000000000
0

Sample Output 2

0

Sample Input 3

10 12
59 168 130 414 187 236 330 422 31 407
495 218 351 105 351 414 198 230 345 297 489 212

Sample Output 3

2270