C - Attention

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

N 人の人が東西方向に一列に並んでいます。 それぞれの人は、東または西を向いています。 誰がどの方向を向いているかは長さ N の文字列 S によって与えられます。 西から i 番目に並んでいる人は、S_i = E なら東を、S_i = W なら西を向いています。

あなたは、N 人のうち誰か 1 人をリーダーとして任命します。 そして、リーダー以外の全員に、リーダーの方向を向くように命令します。 このとき、リーダーはどちらの方向を向いていても構いません。

並んでいる人は、向く方向を変えるのを嫌っています。 そのためあなたは、向く方向を変える人数が最小になるようにリーダーを選びたいです。 向く方向を変える人数の最小値を求めてください。

制約

  • 2 \leq N \leq 3 \times 10^5
  • |S| = N
  • S_iE または W である

入力

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

N
S

出力

向く方向を変える人数の最小値を出力せよ。


入力例 1

5
WEEWW

出力例 1

1

西から 3 番目に並んでいる人をリーダーに任命するとします。 すると、西から 1 番目に並んでいる人は東を向かなくてはならないので、向く方向を変える必要があります。 ほかの人は向く方向を変える必要がないので、この場合、向く方向を変える人は 1 人になります。 向く方向を変える人を 0 人にすることは出来ないので、答えは 1 になります。


入力例 2

12
WEWEWEEEWWWE

出力例 2

4

入力例 3

8
WWWWWEEE

出力例 3

3

Score : 300 points

Problem Statement

There are N people standing in a row from west to east. Each person is facing east or west. The directions of the people is given as a string S of length N. The i-th person from the west is facing east if S_i = E, and west if S_i = W.

You will appoint one of the N people as the leader, then command the rest of them to face in the direction of the leader. Here, we do not care which direction the leader is facing.

The people in the row hate to change their directions, so you would like to select the leader so that the number of people who have to change their directions is minimized. Find the minimum number of people who have to change their directions.

Constraints

  • 2 \leq N \leq 3 \times 10^5
  • |S| = N
  • S_i is E or W.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of people who have to change their directions.


Sample Input 1

5
WEEWW

Sample Output 1

1

Assume that we appoint the third person from the west as the leader. Then, the first person from the west needs to face east and has to turn around. The other people do not need to change their directions, so the number of people who have to change their directions is 1 in this case. It is not possible to have 0 people who have to change their directions, so the answer is 1.


Sample Input 2

12
WEWEWEEEWWWE

Sample Output 2

4

Sample Input 3

8
WWWWWEEE

Sample Output 3

3
D - Xor Sum 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

長さ N の整数列 A があります。

次の条件を満たす整数 l, r ( 1 \leq l \leq r \leq N ) の組の個数を求めてください。

  • A_l\ xor\ A_{l+1}\ xor\ ...\ xor\ A_r = A_l\ +\ A_{l+1}\ +\ ...\ +\ A_r

xorの説明

整数 c_1, c_2, ..., c_mxor は以下のように定義されます。

  • xor の値を X とおく。X2 進数表記したときの 2^k ( 0 \leq k, k は整数 ) の位の値は、c_1, c_2, ...c_m のうち、2 進数表記したときの 2^k の位の値が 1 となるものが奇数個ならば 1、偶数個ならば 0 となる。

例えば、35xor の値は、32 進数表記が 01152 進数表記が 101 のため、2 進数表記が 1106 となります。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq A_i < 2^{20}
  • 入力はすべて整数である

入力

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

N
A_1 A_2 ... A_N

出力

条件を満たす整数 l, r ( 1 \leq l \leq r \leq N ) の組の個数を出力せよ。


入力例 1

4
2 5 4 6

出力例 1

5

明らかに、(l,r)=(1,1),(2,2),(3,3),(4,4) は条件を満たします。 また、(l,r)=(1,2) の場合、A_1\ xor\ A_2 = A_1\ +\ A_2 = 7 となるので、これも条件を満たします。 ほかに条件を満たす組はないので、答えは 5 になります。


入力例 2

9
0 0 0 0 0 0 0 0 0

出力例 2

45

入力例 3

19
885 8 1 128 83 32 256 206 639 16 4 128 689 32 8 64 885 969 1

出力例 3

37

Score : 500 points

Problem Statement

There is an integer sequence A of length N.

Find the number of the pairs of integers l and r (1 \leq l \leq r \leq N) that satisfy the following condition:

  • A_l\ xor\ A_{l+1}\ xor\ ...\ xor\ A_r = A_l\ +\ A_{l+1}\ +\ ...\ +\ A_r

Here, xor denotes the bitwise exclusive OR.

Definition of XOR

The XOR of integers c_1, c_2, ..., c_m is defined as follows:

  • Let the XOR be X. In the binary representation of X, the digit in the 2^k's place (0 \leq k; k is an integer) is 1 if there are an odd number of integers among c_1, c_2, ...c_m whose binary representation has 1 in the 2^k's place, and 0 if that number is even.

For example, let us compute the XOR of 3 and 5. The binary representation of 3 is 011, and the binary representation of 5 is 101, thus the XOR has the binary representation 110, that is, the XOR is 6.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq A_i < 2^{20}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the number of the pairs of integers l and r (1 \leq l \leq r \leq N) that satisfy the condition.


Sample Input 1

4
2 5 4 6

Sample Output 1

5

(l,r)=(1,1),(2,2),(3,3),(4,4) clearly satisfy the condition. (l,r)=(1,2) also satisfies the condition, since A_1\ xor\ A_2 = A_1\ +\ A_2 = 7. There are no other pairs that satisfy the condition, so the answer is 5.


Sample Input 2

9
0 0 0 0 0 0 0 0 0

Sample Output 2

45

Sample Input 3

19
885 8 1 128 83 32 256 206 639 16 4 128 689 32 8 64 885 969 1

Sample Output 3

37
E - Range Minimum Queries

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長さ N の数列 A と整数 K が与えられます。 この配列に、以下の操作を Q 回行います。

  • 長さ K の連続する部分列を 1 つ選ぶ。 そして、選んだ部分列に含まれる K 個の要素のうち最小のもの(複数ある場合はその中で好きなものを 1 個)を取り除く。

Q 回の操作で取り除いた要素の最大値、最小値をそれぞれ X, Y としたときに、X-Y をできるだけ小さくしたいです。 Q 回の操作を適切に行ったときの X-Y の最小値を求めてください。

制約

  • 1 \leq N \leq 2000
  • 1 \leq K \leq N
  • 1 \leq Q \leq N-K+1
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数である

入力

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

N K Q
A_1 A_2 ... A_N

出力

X-Y の最小値を出力せよ。


入力例 1

5 3 2
4 3 1 5 2

出力例 1

1

1 回目の操作では、どの長さ 3 の連続する部分列を選んでも、そのうち最小の要素は 1 です。 よって、1 回目の操作によって A_3=1 が取り除かれ、A=(4,3,5,2) となります。 2 回目の操作では、連続する長さ 3 の部分列として、(A_2,A_3,A_4)=(3,5,2) を選び、A_4=2 を取り除くのが最適です。 この場合、取り除いた要素の最大値は 2、最小値は 1 になるので、その差は 2-1=1 になります。


入力例 2

10 1 6
1 1 2 3 5 8 13 21 34 55

出力例 2

7

入力例 3

11 7 5
24979445 861648772 623690081 433933447 476190629 262703497 211047202 971407775 628894325 731963982 822804784

出力例 3

451211184

Score : 600 points

Problem Statement

You are given an integer sequence A of length N and an integer K. You will perform the following operation on this sequence Q times:

  • Choose a contiguous subsequence of length K, then remove the smallest element among the K elements contained in the chosen subsequence (if there are multiple such elements, choose one of them as you like).

Let X and Y be the values of the largest and smallest element removed in the Q operations. You would like X-Y to be as small as possible. Find the smallest possible value of X-Y when the Q operations are performed optimally.

Constraints

  • 1 \leq N \leq 2000
  • 1 \leq K \leq N
  • 1 \leq Q \leq N-K+1
  • 1 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K Q
A_1 A_2 ... A_N

Output

Print the smallest possible value of X-Y.


Sample Input 1

5 3 2
4 3 1 5 2

Sample Output 1

1

In the first operation, whichever contiguous subsequence of length 3 we choose, the minimum element in it is 1. Thus, the first operation removes A_3=1 and now we have A=(4,3,5,2). In the second operation, it is optimal to choose (A_2,A_3,A_4)=(3,5,2) as the contiguous subsequence of length 3 and remove A_4=2. In this case, the largest element removed is 2, and the smallest is 1, so their difference is 2-1=1.


Sample Input 2

10 1 6
1 1 2 3 5 8 13 21 34 55

Sample Output 2

7

Sample Input 3

11 7 5
24979445 861648772 623690081 433933447 476190629 262703497 211047202 971407775 628894325 731963982 822804784

Sample Output 3

451211184
F - Donation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 1000

問題文

N 頂点 M 辺の連結な単純無向グラフがあります。 頂点には 1 から N の番号が、辺には 1 から M の番号がついています。 辺 i は頂点 U_i と頂点 V_i を結んでいます。 また、頂点 i には二つの整数 A_i, B_i が定められています。 あなたは、このグラフ上で次のようなゲームをします。

まず初めに、W 円を持った状態で好きな頂点に立つ。 ただし、立つ頂点を s としたとき、A_s \leq W でなくてはならない。 その後、以下の 2 種類の操作を好きな順序で好きなだけ繰り返す。

  • 今いる頂点と直接辺で結ばれている頂点の中から一つ好きなものを選び、その頂点を v とする。そして、頂点 v に移動する。ただし、移動する際の所持金は A_v 円以上でなくてはならない。
  • 今いる頂点を v としたとき、B_v 円を頂点 v に寄付する。このとき、所持金が 0 円未満になってはならない。

このゲームは、すべての頂点に 1 度ずつ寄付をするとクリアになります。 このゲームのクリアが可能となる、最小の初期の所持金 W を求めてください。

制約

  • 1 \leq N \leq 10^5
  • N-1 \leq M \leq 10^5
  • 1 \leq A_i,B_i \leq 10^9
  • 1 \leq U_i < V_i \leq N
  • 与えられるグラフは連結かつ単純(どの 2 頂点を直接結ぶ辺も高々 1 つ)である

入力

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

N M
A_1 B_1
A_2 B_2
:
A_N B_N
U_1 V_1
U_2 V_2
:
U_M V_M

出力

このゲームのクリアが可能となる、最小の初期の所持金 W を出力せよ。


入力例 1

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

出力例 1

6

初期の所持金が 6 円のとき、以下のようにするとゲームをクリアできます。

  • 頂点 4 に立つ。所持金が 6 円以上なので立つことができる。
  • 頂点 42 円寄付し、所持金が 4 円になる。
  • 頂点 3 に移動する。所持金が 4 円以上なので移動できる。
  • 頂点 31 円寄付し、所持金が 3 円になる。
  • 頂点 2 に 移動する。所持金が 1 円以上なので移動できる。
  • 頂点 1 に 移動する。所持金が 3 円以上なので移動できる。
  • 頂点 11 円寄付し、所持金が 2 円になる。
  • 頂点 2 に 移動する。所持金が 1 円以上なので移動できる。
  • 頂点 22 円寄付し、所持金が 0 円になる。

初期の所持金が 6 円に満たない場合はゲームをクリアできないので、答えは 6 になります。


入力例 2

5 8
6 4
15 13
15 19
15 1
20 7
1 3
1 4
1 5
2 3
2 4
2 5
3 5
4 5

出力例 2

44

入力例 3

9 10
131 2
98 79
242 32
231 38
382 82
224 22
140 88
209 70
164 64
6 8
1 6
1 4
1 3
4 7
4 9
3 7
3 9
5 9
2 5

出力例 3

582

Score : 1000 points

Problem Statement

There is a simple undirected graph with N vertices and M edges. The vertices are numbered 1 through N, and the edges are numbered 1 through M. Edge i connects Vertex U_i and V_i. Also, Vertex i has two predetermined integers A_i and B_i. You will play the following game on this graph.

First, choose one vertex and stand on it, with W yen (the currency of Japan) in your pocket. Here, A_s \leq W must hold, where s is the vertex you choose. Then, perform the following two kinds of operations any number of times in any order:

  • Choose one vertex v that is directly connected by an edge to the vertex you are standing on, and move to vertex v. Here, you need to have at least A_v yen in your pocket when you perform this move.
  • Donate B_v yen to the vertex v you are standing on. Here, the amount of money in your pocket must not become less than 0 yen.

You win the game when you donate once to every vertex. Find the smallest initial amount of money W that enables you to win the game.

Constraints

  • 1 \leq N \leq 10^5
  • N-1 \leq M \leq 10^5
  • 1 \leq A_i,B_i \leq 10^9
  • 1 \leq U_i < V_i \leq N
  • The given graph is connected and simple (there is at most one edge between any pair of vertices).

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_N B_N
U_1 V_1
U_2 V_2
:
U_M V_M

Output

Print the smallest initial amount of money W that enables you to win the game.


Sample Input 1

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

Sample Output 1

6

If you have 6 yen initially, you can win the game as follows:

  • Stand on Vertex 4. This is possible since you have not less than 6 yen.
  • Donate 2 yen to Vertex 4. Now you have 4 yen.
  • Move to Vertex 3. This is possible since you have not less than 4 yen.
  • Donate 1 yen to Vertex 3. Now you have 3 yen.
  • Move to Vertex 2. This is possible since you have not less than 1 yen.
  • Move to Vertex 1. This is possible since you have not less than 3 yen.
  • Donate 1 yen to Vertex 1. Now you have 2 yen.
  • Move to Vertex 2. This is possible since you have not less than 1 yen.
  • Donate 2 yen to Vertex 2. Now you have 0 yen.

If you have less than 6 yen initially, you cannot win the game. Thus, the answer is 6.


Sample Input 2

5 8
6 4
15 13
15 19
15 1
20 7
1 3
1 4
1 5
2 3
2 4
2 5
3 5
4 5

Sample Output 2

44

Sample Input 3

9 10
131 2
98 79
242 32
231 38
382 82
224 22
140 88
209 70
164 64
6 8
1 6
1 4
1 3
4 7
4 9
3 7
3 9
5 9
2 5

Sample Output 3

582