A - Handmaid

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

ある人物の名前 S が与えられます。S の先頭の文字は英大文字であり、先頭以外の文字は英小文字です。

この人物の侍女の名前は、S の頭文字を英小文字に直し、先頭に Of をつけて得られる文字列です。この侍女の名前を答えてください。

制約

  • S は長さ 1 以上 10 以下の文字列
  • S の先頭の文字は英大文字
  • S の先頭以外の文字は英小文字

入力

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

S

出力

答えを 1 行に出力せよ。


入力例 1

Glen

出力例 1

Ofglen

Glen の頭文字を英小文字に直すと glen となり、さらに先頭に Of をつけると Ofglen となります。


入力例 2

I

出力例 2

Ofi

入力例 3

Fred

出力例 3

Offred

Score : 100 points

Problem Statement

You are given the name S of a certain person. The first character of S is an uppercase English letter, and the other characters are lowercase English letters.

The name of this person's handmaid is the string obtained by converting the first letter of S to lowercase and adding Of to the beginning. Find the name of this handmaid.

Constraints

  • S is a string of length between 1 and 10, inclusive.
  • The first character of S is an uppercase English letter.
  • The characters of S other than the first are lowercase English letters.

Input

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

S

Output

Output the answer on one line.


Sample Input 1

Glen

Sample Output 1

Ofglen

Converting the first letter of Glen to lowercase gives glen, and adding Of to the beginning gives Ofglen.


Sample Input 2

I

Sample Output 2

Ofi

Sample Input 3

Fred

Sample Output 3

Offred
B - Adjacent Squares

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100

問題文

H 行、横 W 列のマス目があり、このうち上から i 個目、左から j 個目のマスを (i,j) と呼びます。
このとき、マス (R,C) に辺で隣接するマスの個数を求めてください。

ただし、ある 2 つのマス (a,b),(c,d) が辺で隣接するとは、 |a-c|+|b-d|=1 (|x|x の絶対値とする) であることを言います。

制約

  • 入力は全て整数
  • 1 \le R \le H \le 10
  • 1 \le C \le W \le 10

入力

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

H W
R C

出力

答えを整数として出力せよ。


入力例 1

3 4
2 2

出力例 1

4

入出力例 1,2,3 に対する説明は、出力例 3 の下にまとめて示します。


入力例 2

3 4
1 3

出力例 2

3

入力例 3

3 4
3 4

出力例 3

2

H=3,W=4 のとき、マス目は以下のようになります。

  • 入力例 1 について、マス (2,2) に隣接するマスは 4 つです。
  • 入力例 2 について、マス (1,3) に隣接するマスは 3 つです。
  • 入力例 3 について、マス (3,4) に隣接するマスは 2 つです。


入力例 4

1 10
1 5

出力例 4

2

入力例 5

8 1
8 1

出力例 5

1

入力例 6

1 1
1 1

出力例 6

0

Score : 100 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.
Find the number of squares that share a side with Square (R, C).

Here, two squares (a,b) and (c,d) are said to share a side if and only if |a-c|+|b-d|=1 (where |x| denotes the absolute value of x).

Constraints

  • All values in input are integers.
  • 1 \le R \le H \le 10
  • 1 \le C \le W \le 10

Input

Input is given from Standard Input in the following format:

H W
R C

Output

Print the answer as an integer.


Sample Input 1

3 4
2 2

Sample Output 1

4

We will describe Sample Inputs/Outputs 1,2, and 3 at once below Sample Output 3.


Sample Input 2

3 4
1 3

Sample Output 2

3

Sample Input 3

3 4
3 4

Sample Output 3

2

When H=3 and W=4, the grid looks as follows.

  • For Sample Input 1, there are 4 squares adjacent to Square (2,2).
  • For Sample Input 2, there are 3 squares adjacent to Square (1,3).
  • For Sample Input 3, there are 2 squares adjacent to Square (3,4).


Sample Input 4

1 10
1 5

Sample Output 4

2

Sample Input 5

8 1
8 1

Sample Output 5

1

Sample Input 6

1 1
1 1

Sample Output 6

0
C - Buy One Carton of Milk

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

スーパーマーケットで卵のパックが売られています。

6 個入りのパックは S 円、卵 8 個入りのパックは M 円、卵 12 個入りのパックは L 円です。

どのパックも何パックでも購入できるとき、N 個以上の卵を買うために必要な最小の金額を求めてください。

制約

  • 1 \leq N \leq 100
  • 1 \leq S,M,L \leq 10^4
  • 入力は全て整数である

入力

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

N S M L

出力

答えを出力せよ。


入力例 1

16 120 150 200

出力例 1

300

8 個入りのパックを 2 個買うのが最適です。


入力例 2

10 100 50 10

出力例 2

10

12 個入りのパックを 1 個買うのが最適です。


入力例 3

99 600 800 1200

出力例 3

10000

8 個入りのパックと 12 個入りのパックを 5 個ずつ買うのが最適です。

Score : 200 points

Problem Statement

A supermarket sells egg packs.

A pack of 6 eggs costs S yen, a pack of 8 eggs costs M yen, and a pack of 12 eggs costs L yen.

When you can buy any number of each pack, find the minimum amount of money required to purchase at least N eggs.

Constraints

  • 1 \leq N \leq 100
  • 1 \leq S,M,L \leq 10^4
  • All input values are integers.

Input

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

N S M L

Output

Print the answer.


Sample Input 1

16 120 150 200

Sample Output 1

300

It is optimal to buy two 8-egg packs.


Sample Input 2

10 100 50 10

Sample Output 2

10

It is optimal to buy one 12-egg pack.


Sample Input 3

99 600 800 1200

Sample Output 3

10000

It is optimal to buy five 8-egg packs and five 12-egg packs.

D - Integer Division Returns

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200

問題文

-10^{18} 以上 10^{18} 以下の整数 X が与えられるので、\left\lceil \dfrac{X}{10} \right\rceil を出力してください。
ここで、\left\lceil a \right\rceila 以上の整数のうち最小のものを意味します。

制約

  • -10^{18} \leq X \leq 10^{18}
  • X は整数

入力

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

X

出力

\left\lceil \dfrac{X}{10} \right\rceil を整数として出力せよ。


入力例 1

27

出力例 1

3

\frac{27}{10} = 2.7 以上の整数は 3, 4, 5, \dots です。この中で一番小さい整数は 3 なので、\left \lceil \frac{27}{10} \right \rceil = 3 となります。


入力例 2

-13

出力例 2

-1

\frac{-13}{10} = -1.3 以上の整数は、全ての正整数および 0, -1 です。この中で一番小さい整数は -1 なので、\left \lceil \frac{-13}{10} \right \rceil = -1 となります。


入力例 3

40

出力例 3

4

\frac{40}{10} = 4 以上の整数で一番小さい整数は 4 自身です。


入力例 4

-20

出力例 4

-2

入力例 5

123456789123456789

出力例 5

12345678912345679

Score: 200 points

Problem Statement

Given an integer X between -10^{18} and 10^{18}, inclusive, print \left\lceil \dfrac{X}{10} \right\rceil.
Here, \left\lceil a \right\rceil denotes the smallest integer not less than a.

Constraints

  • -10^{18} \leq X \leq 10^{18}
  • X is an integer.

Input

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

X

Output

Print \left\lceil \dfrac{X}{10} \right\rceil as an integer.


Sample Input 1

27

Sample Output 1

3

The integers not less than \frac{27}{10} = 2.7 are 3, 4, 5, \dots. Among these, the smallest is 3, so \left \lceil \frac{27}{10} \right \rceil = 3.


Sample Input 2

-13

Sample Output 2

-1

The integers not less than \frac{-13}{10} = -1.3 are all positive integers, 0, and -1. Among these, the smallest is -1, so \left \lceil \frac{-13}{10} \right \rceil = -1.


Sample Input 3

40

Sample Output 3

4

The smallest integer not less than \frac{40}{10} = 4 is 4 itself.


Sample Input 4

-20

Sample Output 4

-2

Sample Input 5

123456789123456789

Sample Output 5

12345678912345679
E - Max - Min Query

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

整数の多重集合 S があります。はじめ S は空です。

Q 個のクエリが与えられるので順に処理してください。 クエリは次の 3 種類のいずれかです。

  • 1 x : Sx1 個追加する。

  • 2 x c : S から x\mathrm{min}(c, (S に含まれる x の個数 )) 個削除する。

  • 3 : (S の最大値 )- (S の最小値 ) を出力する。このクエリを処理するとき、 S が空でないことが保証される。

制約

  • 1 \leq Q \leq 2\times 10^5
  • 0 \leq x \leq 10^9
  • 1 \leq c \leq Q
  • 3 のクエリを処理するとき、S は空でない。
  • 入力は全て整数

入力

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

Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

i 番目のクエリを表す \mathrm{query}_i は以下の 3 種類のいずれかである。

1 x
2 x c
3 

出力

3 のクエリに対する答えを順に改行区切りで出力せよ。


入力例 1

8
1 3
1 2
3
1 2
1 7
3
2 2 3
3

出力例 1

1
5
4

多重集合 S は以下のように変化します。

  • 1 番目のクエリ : S3 を追加する。S\lbrace3 \rbrace となる。
  • 2 番目のクエリ : S2 を追加する。S\lbrace2, 3\rbrace となる。
  • 3 番目のクエリ : S = \lbrace 2, 3\rbrace の最大値は 3 、最小値は 2 なので、 3-2=1 を出力する。
  • 4 番目のクエリ : S2 を追加する。S\lbrace2,2,3 \rbrace となる。
  • 5 番目のクエリ : S7 を追加する。S\lbrace2, 2,3, 7\rbrace となる。
  • 6 番目のクエリ : S = \lbrace2,2,3, 7\rbrace の最大値は 7 、最小値は 2 なので、 7-2=5 を出力する。
  • 7 番目のクエリ : S に含まれる 2 の個数は 2 個なので、 \mathrm{min(2,3)} = 2 個の 2S から削除する。S\lbrace3, 7\rbrace となる。
  • 8 番目のクエリ : S = \lbrace3, 7\rbrace の最大値は 7 、最小値は 3 なので、 7-3=4 を出力する。

入力例 2

4
1 10000
1 1000
2 100 3
1 10

出力例 2


クエリ 3 が含まれない場合、何も出力してはいけません。

Score : 300 points

Problem Statement

We have a multiset of integers S, which is initially empty.

Given Q queries, process them in order. Each query is of one of the following types.

  • 1 x: Insert an x into S.

  • 2 x c: Remove an x from S m times, where m = \mathrm{min}(c,( the number of x's contained in S)).

  • 3 : Print ( maximum value of S)-( minimum value of S). It is guaranteed that S is not empty when this query is given.

Constraints

  • 1 \leq Q \leq 2\times 10^5
  • 0 \leq x \leq 10^9
  • 1 \leq c \leq Q
  • When a query of type 3 is given, S is not empty.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
\mathrm{query}_1
\vdots
\mathrm{query}_Q

\mathrm{query}_i, which denotes the i-th query, is in one of the following formats:

1 x
2 x c
3 

Output

Print the answers for the queries of type 3 in the given order, separated by newlines.


Sample Input 1

8
1 3
1 2
3
1 2
1 7
3
2 2 3
3

Sample Output 1

1
5
4

The multiset S transitions as follows.

  • 1-st query: insert 3 into S. S is now \lbrace 3 \rbrace.
  • 2-nd query: insert 2 into S. S is now \lbrace 2, 3 \rbrace.
  • 3-rd query: the maximum value of S = \lbrace 2, 3\rbrace is 3 and its minimum value is 2, so print 3-2=1.
  • 4-th query: insert 2 into S. S is now \lbrace 2,2,3 \rbrace.
  • 5-th query: insert 7 into S. S is now \lbrace 2, 2,3, 7\rbrace.
  • 6-th query: the maximum value of S = \lbrace 2,2,3, 7\rbrace is 7 and its minimum value is 2, so print 7-2=5.
  • 7-th query: since there are two 2's in S and \mathrm{min(2,3)} = 2, remove 2 from S twice. S is now \lbrace 3, 7\rbrace.
  • 8-th query: the maximum value of S = \lbrace 3, 7\rbrace is 7 and its minimum value is 3, so print 7-3=4.

Sample Input 2

4
1 10000
1 1000
2 100 3
1 10

Sample Output 2


If the given queries do not contain that of type 3, nothing should be printed.

F - Yamanote Line Game

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 300

問題文

高橋君と青木君は 2 人で次の対戦ゲームをします。

高橋君が先手でゲームを始め、ゲームが終了するまでの間、 2 人は交互に 1 以上 2N+1 以下の整数を 1 つずつ宣言します。 どちらかが一度でも宣言した整数は、それ以降どちらも二度と宣言することが出来ません。 先に整数を宣言することが出来なくなった方のプレイヤーの負けとなり、負けなかった方のプレイヤーの勝ちとなります。

このゲームでは必ず高橋君が勝ちます。 高橋君の立場で実際にゲームを行い、ゲームに勝ってください。

制約

  • 1 \leq N \leq 1000
  • N は整数

入出力

この問題はインタラクティブな問題(あなたの作成したプログラムとジャッジプログラムが入出力を介して対話を行う形式の問題)です。
あなたのプログラムが高橋君の立場で、ジャッジプログラムが青木君の立場でゲームを行います。

まず、あなたのプログラムに標準入力から正の整数 N が与えられます。 その後、ゲームが終了するまで下記の手順を繰り返します。

  1. あなたのプログラムが、高橋君が宣言する整数として、1 以上 2N+1 以下の整数を標準出力に出力します。(どちらかのプレイヤーによってすでに宣言されている整数を出力することは出来ません。)
  2. ジャッジプログラムによって、青木君が宣言する整数があなたのプログラムに標準入力から与えられます。(どちらかのプレイヤーによってすでに宣言されている整数が入力されることはありません。) ただし、青木君が宣言できる整数が残っていない場合は、代わりに 0 が与えられ高橋君の勝ちでゲームが終了します。

注意点

  • 出力を行うたびに標準出力をflushしてください。そうしなかった場合、ジャッジ結果が TLE となる可能性があります。
  • 高橋君の勝ちでゲームが終了したあと、あなたのプログラムは直ちに終了しなければなりません。そうしなかった場合、ジャッジ結果が AC とならない可能性があります。
  • ゲームの途中で不正な出力を行った場合(例えば、すでにどちらかのプレイヤーによって宣言されている整数を出力した場合)は不正解となりますが、そのときのジャッジ結果は不定です。WA になるとは限りません。

入出力例

入力 出力 説明
2 まず整数 N が与えられます。
1 高橋君が 1 を宣言します。
3 青木君が 3 を宣言します。
2 高橋君が 2 を宣言します。
4 青木君が 4 を宣言します。
5 高橋君が 5 を宣言します。
0 青木君が宣言できる整数が残っていないため、高橋君の勝ちでゲームが終了します。

Score : 300 points

Problem Statement

Takahashi and Aoki will play the following game against each other.

Starting from Takahashi, the two alternatingly declare an integer between 1 and 2N+1 (inclusive) until the game ends. Any integer declared by either player cannot be declared by either player again. The player who is no longer able to declare an integer loses; the player who didn't lose wins.

In this game, Takahashi will always win. Your task is to actually play the game on behalf of Takahashi and win the game.

Constraints

  • 1 \leq N \leq 1000
  • N is an integer.

Input and Output

This task is an interactive task (in which your program and the judge program interact with each other via inputs and outputs).
Your program plays the game on behalf of Takahashi, and the judge program plays the game on behalf of Aoki.

First, your program is given a positive integer N from Standard Input. Then, the following procedures are repeated until the game ends.

  1. Your program outputs an integer between 1 and 2N+1 (inclusive) to Standard Output, which defines the integer that Takahashi declares. (You cannot output an integer that is already declared by either player.)
  2. The integer that Aoki declares is given by the judge program to your program from Standard Input. (No integer that is already declared by either player will be given.) If Aoki has no more integer to declare, 0 is given instead, which means that the game ended and Takahashi won.

Notes

  • After each output, you must flush Standard Output. Otherwise, you may get TLE.
  • After the game ended and Takahashi won, the program must be terminated immediately. Otherwise, the judge does not necessarily give AC.
  • If your program outputs something that violates the rules of the game (such as an integer that has already been declared by either player), your answer is considered incorrect. In such case, the verdict is indeterminate. It does not necessarily give WA.

Sample Input and Output

Input Output Description
2 First, an integer N is given.
1 Takahashi declares an integer 1.
3 Aoki declares an integer 3.
2 Takahashi declares an integer 2.
4 Aoki declares an integer 4.
5 Takahashi declares an integer 5.
0 Aoki has no more integer to declare, so Takahashi wins, and the game ends.
G - Santa Claus 2

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 425

問題文

2次元平面上の N(X_1,Y_1),\ldots,(X_N,Y_N) に家が建っています。

最初、点 (S_x,S_y) にサンタクロースがいます。サンタクロースは列 (D_1,C_1),\ldots,(D_M,C_M) に従って以下の行動を行います。

  • i=1,2,\ldots,M の順に以下のように移動する。
    • 現在サンタクロースがいる点を (x,y) とする。
      • D_iU なら、(x,y) から (x,y+C_i) に直線で移動する。
      • D_iD なら、(x,y) から (x,y-C_i) に直線で移動する。
      • D_iL なら、(x,y) から (x-C_i,y) に直線で移動する。
      • D_iR なら、(x,y) から (x+C_i,y) に直線で移動する。

行動を終えたあとにサンタクロースがいる点と、行動により通過または到達した家の数を求めてください。ただし、同じ家を複数回通過または到達してもそれらは重複して数えません。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • -10^9 \leq X_i,Y_i \leq 10^9
  • (X_i,Y_i) は相異なる
  • -10^9 \leq S_x,S_y \leq 10^9
  • (S_x,S_y) に家は建っていない
  • D_iU, D, L, R のいずれかである
  • 1 \leq C_i \leq 10^9
  • 与えられる数値は全て整数である

入力

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

N M S_x S_y
X_1 Y_1
\vdots
X_N Y_N
D_1 C_1
\vdots
D_M C_M

出力

行動を終えたあとサンタクロースがいる点を (X,Y)、行動により通過または到達した家の数を C とするとき、X,Y,C をこの順に空白区切りで出力せよ。


入力例 1

3 4 3 2
2 2
3 3
2 1
L 2
D 1
R 1
U 2

出力例 1

2 3 2

サンタクロースは以下のように行動します。

図

  • D_1= L なので (3,2) から (3-2,2) に直線で移動する。このとき (2,2) に建っている家を通過する。
  • D_2= D なので (1,2) から (1,2-1) に直線で移動する。
  • D_3= R なので (1,1) から (1+1,1) に直線で移動する。このとき (2,1) に建っている家を通過する。
  • D_4= U なので (2,1) から (2,1+2) に直線で移動する。このとき (2,2) に建っている家を通過するが、この家はすでに通過したことがある家である。

行動により通過または到達した家の数は 2 です。


入力例 2

1 3 0 0
1 1
R 1000000000
R 1000000000
R 1000000000

出力例 2

3000000000 0 0

オーバーフローに注意してください。

Score : 425 points

Problem Statement

There are N houses at points (X_1,Y_1),\ldots,(X_N,Y_N) on a two-dimensional plane.

Initially, Santa Claus is at point (S_x,S_y). He will act according to the sequence (D_1,C_1),\ldots,(D_M,C_M) as follows:

  • For i=1,2,\ldots,M in order, he moves as follows:
    • Let (x,y) be the point where he currently is.
      • If D_i is U, move in a straight line from (x,y) to (x,y+C_i).
      • If D_i is D, move in a straight line from (x,y) to (x,y-C_i).
      • If D_i is L, move in a straight line from (x,y) to (x-C_i,y).
      • If D_i is R, move in a straight line from (x,y) to (x+C_i,y).

Find the point where he is after completing all actions, and the number of distinct houses he passed through or arrived at during his actions. If the same house is passed multiple times, it is only counted once.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • -10^9 \leq X_i,Y_i \leq 10^9
  • The pairs (X_i,Y_i) are distinct.
  • -10^9 \leq S_x,S_y \leq 10^9
  • There is no house at (S_x,S_y).
  • Each D_i is one of U, D, L, R.
  • 1 \leq C_i \leq 10^9
  • All input numbers are integers.

Input

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

N M S_x S_y
X_1 Y_1
\vdots
X_N Y_N
D_1 C_1
\vdots
D_M C_M

Output

Let (X,Y) be the point where he is after completing all actions, and C be the number of distinct houses passed through or arrived at. Print X,Y,C in this order separated by spaces.


Sample Input 1

3 4 3 2
2 2
3 3
2 1
L 2
D 1
R 1
U 2

Sample Output 1

2 3 2

Santa Claus behaves as follows:

Figure

  • D_1= L, so he moves from (3,2) to (3-2,2) in a straight line. During this, he passes through the house at (2,2).
  • D_2= D, so he moves from (1,2) to (1,2-1) in a straight line.
  • D_3= R, so he moves from (1,1) to (1+1,1) in a straight line. During this, he passes through the house at (2,1).
  • D_4= U, so he moves from (2,1) to (2,1+2) in a straight line. During this, he passes through the house at (2,2), but it has already been passed.

The number of houses he passed or arrived during his actions is 2.


Sample Input 2

1 3 0 0
1 1
R 1000000000
R 1000000000
R 1000000000

Sample Output 2

3000000000 0 0

Be careful with overflow.

H - Wish List

実行時間制限: 3 sec / メモリ制限: 1024 MiB

配点 : 500

問題文

お店に N 個の商品が並んでおり、それらは商品 1 、商品 2\ldots 、商品 N と番号づけられています。
i = 1, 2, \ldots, N について、商品 i定価A_i 円です。また、各商品の在庫は 1 つです。

高橋君は、商品 X_1 、商品 X_2\ldots 、商品 X_MM 個の商品が欲しいです。

高橋君は、欲しい商品をすべて手に入れるまで、下記の行動を繰り返します。

現在売れ残っている商品の個数を r とする。 1 \leq j \leq r を満たす整数 j を選び、現在売れ残っている商品のうち番号が j 番目に小さい商品を、その定価C_j 円だけ加えた金額で購入する。

高橋君が欲しい商品をすべて手に入れるまでにかかる合計費用としてあり得る最小値を出力してください。

なお、高橋君は欲しい商品ではない商品を購入することもできます。

制約

  • 1 \leq M \leq N \leq 5000
  • 1 \leq A_i \leq 10^9
  • 1 \leq C_i \leq 10^9
  • 1 \leq X_1 \lt X_2 \lt \cdots \lt X_M \leq N
  • 入力はすべて整数

入力

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

N M
A_1 A_2 \ldots A_N
C_1 C_2 \ldots C_N
X_1 X_2 \ldots X_M

出力

答えを出力せよ。


入力例 1

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

出力例 1

17

高橋君は下記の手順で行動することで、欲しい商品をすべて手に入れるまでにかかる合計費用を最小にすることができます。

  • はじめ、商品 1, 2, 3, 4, 55 個の商品が売れ残っています。 高橋君は j = 5 を選び、売れ残っている商品のうち番号が 5 番目に小さい商品 5 を、A_5 + C_5 = 5 + 3 = 8 円で購入します。
  • その後、商品 1, 2, 3, 44 個の商品が売れ残っています。 高橋君は j = 2 を選び、売れ残っている商品のうち番号が 2 番目に小さい商品 2 を、A_2 + C_2 = 1 + 2 = 3 円で購入します。
  • その後、商品 1, 3, 43 個の商品が売れ残っています。 高橋君は j = 2 を選び、売れ残っている商品のうち番号が 2 番目に小さい商品 3 を、A_3 + C_2 = 4 + 2 = 6 円で購入します。

以上の手順によって、高橋君は欲しい商品である商品 3, 5 のすべて(および、欲しい商品ではない商品 2 )を手に入れることができ、 それまでにかかる合計費用は 8 + 3 + 6 = 17 円です。これが合計費用としてあり得る最小値です。


入力例 2

20 8
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62
32 38 84 49 93 53 26 13 25 2 76 32 42 34 18 77 14 67 88 12
1 3 4 5 8 14 16 20

出力例 2

533

Score : 500 points

Problem Statement

There are N items in a shop, numbered as Item 1, Item 2, \ldots, Item N.
For each i = 1, 2, \ldots, N, the regular price of Item i is A_i yen. For each item, there is only one in stock.

Takahashi wants M items: Item X_1, Item X_2, \ldots, Item X_M.

He repeats the following until he gets all items he wants.

Let r be the number of unsold items now. Choose an integer j such that 1 \leq j \leq r, and buy the item with the j-th smallest item number among the unsold items, for its regular price plus C_j yen.

Print the smallest total amount of money needed to get all items Takahashi wants.

Takahashi may also buy items other than the ones he wants.

Constraints

  • 1 \leq M \leq N \leq 5000
  • 1 \leq A_i \leq 10^9
  • 1 \leq C_i \leq 10^9
  • 1 \leq X_1 \lt X_2 \lt \cdots \lt X_M \leq N
  • All values in the input are integers.

Input

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

N M
A_1 A_2 \ldots A_N
C_1 C_2 \ldots C_N
X_1 X_2 \ldots X_M

Output

Print the answer.


Sample Input 1

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

Sample Output 1

17

Here is a way for Takahashi to get all items he wants with the smallest total amount of money.

  • Initially, five items, Items 1, 2, 3, 4, 5, are remaining. Choose j = 5 to buy the item with the fifth smallest item number among the remaining, Item 5, for A_5 + C_5 = 5 + 3 = 8 yen.
  • Then, four items, Items 1, 2, 3, 4, are remaining. Choose j = 2 to buy the item with the second smallest item number among the remaining, Item 2, for A_2 + C_2 = 1 + 2 = 3 yen.
  • Then, three items, Items 1, 3, 4, are remaining. Choose j = 2 to buy the item with the second smallest item number among the remaining, Item 3, for A_3 + C_2 = 4 + 2 = 6 yen.

Now, Takahashi has all items he wants, Items 3 and 5, (along with Item 2, which is not wanted) for a total cost of 8 + 3 + 6 = 17 yen, the minimum possible.


Sample Input 2

20 8
29 27 79 27 30 4 93 89 44 88 70 75 96 3 78 39 97 12 53 62
32 38 84 49 93 53 26 13 25 2 76 32 42 34 18 77 14 67 88 12
1 3 4 5 8 14 16 20

Sample Output 2

533
I - Perfect Matching on a Tree

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 550

問題文

N 頂点の木 T が与えられます。T の頂点には 1 から N の番号がついており、 i\,(1\leq i \leq N-1) 番目の辺は頂点 u_i と頂点 v_i を双方向に結んでいます。

T を用いて、N 頂点の完全グラフ G を次のように定めます。

  • G の頂点 x と頂点 y の間の辺の重み w(x,y) を、T における頂点 x と頂点 y の間の最短距離とする

G最大重み最大マッチングを一つ求めてください。すなわち、\lfloor N/2 \rfloor 個の頂点のペアの集合 M=\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} であって、各頂点 1,2,\dots, NM に現れる回数がたかだか 1 回であるようなもののうち、 \displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i) が最大であるものを一つ求めてください。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq u_i < v_i \leq N
  • 入力されるグラフは木である
  • 入力はすべて整数

入力

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

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

出力

答えを \{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} として、以下の形式で出力せよ。答えが複数あり得る場合、そのうちどれを出力しても良い。

x_1 y_1
x_2 y_2
\vdots
x_{\lfloor N/2 \rfloor} y_{\lfloor N/2 \rfloor}

入力例 1

4
1 2
2 3
3 4

出力例 1

2 4
1 3

T において、頂点 2,4 間の距離は 2、頂点 1,3 間の距離は 2 なので、マッチング \{(2,4),(1,3)\} の重みは 4 です。重みが 4 より大きいマッチングは存在しないので、これが最大重み最大マッチングの一つです。他にも、

2 3
1 4

などを出力しても正解になります。


入力例 2

3
1 2
2 3

出力例 2

1 3

T において、頂点 1,3 間の距離は 2 なので、マッチング \{(1,3)\} の重みは 2 です。重みが 2 より大きいマッチングは存在しないので、これが最大重み最大マッチングの一つです。他にも、

3 1

を出力しても正解になります。

Score : 550 points

Problem Statement

You are given a tree T with N vertices. The vertices are numbered 1 to N, and the i-th edge (1 \leq i \leq N-1) connects vertices u_i and v_i bidirectionally.

Using T, define a complete graph G with N vertices as follows:

  • The weight w(x,y) of the edge between vertices x and y in G is the shortest distance between vertices x and y in T.

Find one maximum weight maximum matching in G. That is, find a set of \lfloor N/2 \rfloor pairs of vertices M=\{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} such that each vertex 1,2,\dots, N appears in M at most once, and \displaystyle \sum_{i=1}^{\lfloor N/2 \rfloor} w(x_i,y_i) is maximized.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq u_i < v_i \leq N
  • The input graph is a tree.
  • All input values are integers.

Input

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

N
u_1 v_1
u_2 v_2
\vdots
u_{N-1} v_{N-1}

Output

Print a solution as \{(x_1,y_1),(x_2,y_2),\dots,(x_{\lfloor N/2 \rfloor},y_{\lfloor N/2 \rfloor})\} in the following format. If multiple solutions exist, any of them is acceptable.

x_1 y_1
x_2 y_2
\vdots
x_{\lfloor N/2 \rfloor} y_{\lfloor N/2 \rfloor}

Sample Input 1

4
1 2
2 3
3 4

Sample Output 1

2 4
1 3

In T, the distance between vertices 2 and 4 is 2, and the distance between vertices 1 and 3 is 2, so the weight of the matching \{(2,4),(1,3)\} is 4. There is no matching with a weight greater than 4, so this is a maximum weight maximum matching. Other acceptable outputs include:

2 3
1 4

Sample Input 2

3
1 2
2 3

Sample Output 2

1 3

In T, the distance between vertices 1 and 3 is 2, so the weight of the matching \{(1,3)\} is 2. There is no matching with a weight greater than 2, so this is a maximum weight maximum matching. Another acceptable output is:

3 1