Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
以下の条件を全て満たす長さ N の文字列を求めてください。
- 各文字は
-または=である - 回文である
- 文字列中に
=は 1 個または 2 個含まれる。 2 個含まれる場合、それらの=は隣接している
なお、そのような文字列はちょうど 1 つ存在します。
制約
- 1 \leq N \leq 100
- N は整数である
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
4
出力例 1
-==-
入力例 2
7
出力例 2
---=---
Score : 100 points
Problem Statement
Find a length-N string that satisfies all of the following conditions:
- Each character is
-or=. - It is a palindrome.
- It contains exactly one or exactly two
=s. If it contains two=s, they are adjacent.
Such a string is unique.
Constraints
- 1 \leq N \leq 100
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
4
Sample Output 1
-==-
Sample Input 2
7
Sample Output 2
---=---
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 100 点
問題文
N 個の商品があります。高橋君と青木君がどの商品を欲しがっているかを表す長さ N の文字列 T,A が与えられます。T,A の i\ (1\leq i\leq N) 文字目をそれぞれ T_i,A_i とします。
高橋君は T_i が o のとき i 番目の商品を欲しがっており、T_i が x のとき i 番目の商品を欲しがっていません。
同様に、青木君は A_i が o のとき i 番目の商品を欲しがっており、A_i が x のとき i 番目の商品を欲しがっていません。
2 人ともが欲しがっている商品が存在するか判定してください。
制約
- 1\leq N\leq 100
- N は整数
- T,A は
oおよびxからなる長さ N の文字列
入力
入力は以下の形式で標準入力から与えられる。
N T A
出力
2 人とも欲しがっている商品が存在するならば Yes を、存在しないならば No を出力せよ。
入力例 1
4 oxoo xoox
出力例 1
Yes
3 つ目の商品は 2 人ともが欲しがっているため、Yes を出力します。
入力例 2
5 xxxxx ooooo
出力例 2
No
2 人とも欲しがっている商品は存在しないため、No を出力します。
入力例 3
10 xoooxoxxxo ooxooooxoo
出力例 3
Yes
Score : 100 points
Problem Statement
There are N items. You are given strings T and A of length N that represent which items Takahashi and Aoki want, respectively. Let T_i and A_i be the i-th (1\leq i\leq N) characters of T and A, respectively.
Takahashi wants the i-th item when T_i is o, and does not want the i-th item when T_i is x.
Similarly, Aoki wants the i-th item when A_i is o, and does not want the i-th item when A_i is x.
Determine whether there exists an item that both of them want.
Constraints
- 1\leq N\leq 100
- N is an integer.
- T and A are strings of length N consisting of
oandx.
Input
The input is given from Standard Input in the following format:
N T A
Output
If there exists an item that both of them want, output Yes; otherwise, output No.
Sample Input 1
4 oxoo xoox
Sample Output 1
Yes
The third item is wanted by both of them, so output Yes.
Sample Input 2
5 xxxxx ooooo
Sample Output 2
No
There is no item that both of them want, so output No.
Sample Input 3
10 xoooxoxxxo ooxooooxoo
Sample Output 3
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
高橋君はゲームの中で洞窟を探索しています。
洞窟は N 個の部屋が一列に並んだ構造であり、入り口から順に部屋 1,2,\ldots,N と番号がついています。
最初、高橋君は部屋 1 におり、持ち時間 は T です。
各 1 \leq i \leq N-1 について、持ち時間を A_i 消費することで、部屋 i から部屋 i+1 へ移動することができます。これ以外に部屋を移動する方法はありません。
また、持ち時間が 0 以下になるような移動は行うことができません。
洞窟内には M 個のボーナス部屋があります。i 番目のボーナス部屋は部屋 X_i であり、この部屋に到達すると持ち時間が Y_i 増加します。
高橋君は部屋 N にたどりつくことができますか?
制約
- 2 \leq N \leq 10^5
- 0 \leq M \leq N-2
- 1 \leq T \leq 10^9
- 1 \leq A_i \leq 10^9
- 1 < X_1 < \ldots < X_M < N
- 1 \leq Y_i \leq 10^9
- 入力に含まれる値は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N M T
A_1 A_2 \ldots A_{N-1}
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M
出力
高橋君が部屋 N にたどりつくことができるなら Yes を、できないなら No を出力せよ。
入力例 1
4 1 10 5 7 5 2 10
出力例 1
Yes
- 高橋君は最初、部屋 1 にいて持ち時間は 10 です。
- 持ち時間を 5 消費して部屋 2 に移動します。持ち時間は 5 になります。その後、持ち時間が 10 増え 15 になります。
- 持ち時間を 7 消費して部屋 3 に移動します。持ち時間は 8 になります。
- 持ち時間を 5 消費して部屋 4 に移動します。持ち時間は 3 になります。
入力例 2
4 1 10 10 7 5 2 10
出力例 2
No
部屋 1 から部屋 2 へ移動することができません。
Score : 200 points
Problem Statement
Takahashi is exploring a cave in a video game.
The cave consists of N rooms arranged in a row. The rooms are numbered Room 1,2,\ldots,N from the entrance.
Takahashi is initially in Room 1, and the time limit is T.
For each 1 \leq i \leq N-1, he may consume a time of A_i to move from Room i to Room (i+1). There is no other way to move between rooms.
He cannot make a move that makes the time limit 0 or less.
There are M bonus rooms in the cave. The i-th bonus room is Room X_i; when he arrives at the room, the time limit increases by Y_i.
Can Takahashi reach Room N?
Constraints
- 2 \leq N \leq 10^5
- 0 \leq M \leq N-2
- 1 \leq T \leq 10^9
- 1 \leq A_i \leq 10^9
- 1 < X_1 < \ldots < X_M < N
- 1 \leq Y_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M T
A_1 A_2 \ldots A_{N-1}
X_1 Y_1
X_2 Y_2
\vdots
X_M Y_M
Output
If Takahashi can reach Room N, print Yes; otherwise, print No.
Sample Input 1
4 1 10 5 7 5 2 10
Sample Output 1
Yes
- Takahashi is initially in Room 1, and the time limit is 10.
- He consumes a time of 5 to move to Room 2. Now the time limit is 5. Then, the time limit increases by 10; it is now 15.
- He consumes a time of 7 to move to Room 3. Now the time limit is 8.
- He consumes a time of 5 to move to Room 4. Now the time limit is 3.
Sample Input 2
4 1 10 10 7 5 2 10
Sample Output 2
No
He cannot move from Room 1 to Room 2.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 200 点
問題文
いろはちゃんは長さ N ( N \ge 1 ) の正整数列 A=(A_1,A_2,\dots,A_N) を持っています。
いろはちゃんは A を使って、次のように文字列 S を生成しました。
- S=
|から始める。 - i=1,2,\dots,N の順に、次の操作を行う。
- S の末尾に
-を A_i 個追加する。 - その後、 S の末尾に
|を 1 個追加する。
- S の末尾に
生成された文字列 S が与えられるので、正整数列 A を復元してください。
制約
- S は問題文中の方法で生成された長さ 3 以上 100 以下の文字列
- A は長さ 1 以上の正整数列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを以下の形式で、 1 行に空白区切りで出力せよ。
A_1 A_2 \dots A_N
入力例 1
|---|-|----|-|-----|
出力例 1
3 1 4 1 5
S= |---|-|----|-|-----| が生成されるような A は (3,1,4,1,5) です。
入力例 2
|----------|
出力例 2
10
入力例 3
|-|-|-|------|
出力例 3
1 1 1 6
Score : 200 points
Problem Statement
Iroha has a sequence of positive integers A = (A_1, A_2, \dots, A_N) of length N (N \ge 1).
She generated a string S using A as follows:
- Start with S =
|. - For i = 1, 2, \dots, N, perform the following operations in order:
- Append A_i copies of
-to the end of S. - Then, append one
|to the end of S.
- Append A_i copies of
Given the generated string S, reconstruct the sequence A.
Constraints
- S is a string of length between 3 and 100, inclusive, generated by the method in the problem statement.
- A is a sequence of positive integers of length at least 1.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer in the following format, with elements separated by spaces in a single line:
A_1 A_2 \dots A_N
Sample Input 1
|---|-|----|-|-----|
Sample Output 1
3 1 4 1 5
S = |---|-|----|-|-----| is generated by A = (3, 1, 4, 1, 5).
Sample Input 2
|----------|
Sample Output 2
10
Sample Input 3
|-|-|-|------|
Sample Output 3
1 1 1 6
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
英小文字のみからなる N 個の文字列 S_1,S_2,\dots,S_N が与えられます。
S_1,S_2,\dots,S_N から文字列を好きな個数選ぶことを考えます。
このとき、「選んだ文字列の中でちょうど K 個の文字列に登場する英小文字」の種類数としてあり得る最大値を求めてください。
制約
- 1 \le N \le 15
- 1 \le K \le N
- N,K は整数
- S_i は英小文字からなる空でない文字列である。
- 1 \le i \le N を満たす整数 i に対し、S_i に同じ文字は 2 個以上含まれない。
- i \neq j ならば S_i \neq S_j である。
入力
入力は以下の形式で標準入力から与えられる。
N K S_1 S_2 \vdots S_N
出力
答えを出力せよ。
入力例 1
4 2 abi aef bc acg
出力例 1
3
S_1,S_3,S_4 を選んだ場合、a,b,c がちょうど 2 個の文字列に含まれます。
4 個以上の文字がちょうど 2 個の文字列に含まれるような選び方は存在しないため、答えは 3 です。
入力例 2
2 2 a b
出力例 2
0
同じ文字列を複数回選ぶことはできません。
入力例 3
5 2 abpqxyz az pq bc cy
出力例 3
7
Score : 300 points
Problem Statement
You are given N strings S_1,S_2,\dots,S_N consisting of lowercase English alphabets.
Consider choosing some number of strings from S_1,S_2,\dots,S_N.
Find the maximum number of distinct alphabets that satisfy the following condition: "the alphabet is contained in exactly K of the chosen strings."
Constraints
- 1 \le N \le 15
- 1 \le K \le N
- N and K are integers.
- S_i is a non-empty string consisting of lowercase English alphabets.
- For each integer i such that 1 \le i \le N, S_i does not contain two or more same alphabets.
- If i \neq j, then S_i \neq S_j.
Input
Input is given from Standard Input in the following format:
N K S_1 S_2 \vdots S_N
Output
Print the answer.
Sample Input 1
4 2 abi aef bc acg
Sample Output 1
3
When S_1,S_3, and S_4 are chosen, a,b, and c occur in exactly two of the strings.
There is no way to choose strings so that 4 or more alphabets occur in exactly 2 of the strings, so the answer is 3.
Sample Input 2
2 2 a b
Sample Output 2
0
You cannot choose the same string more than once.
Sample Input 3
5 2 abpqxyz az pq bc cy
Sample Output 3
7
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 300 点
問題文
高橋くんは、頭パーツを 1 個と体パーツを 1 個組み合わせてロボットを 1 体作ることができます。 ロボットは頭パーツの重さが体パーツの重さより大きいと倒れてしまいます。
現在、高橋くんは頭パーツを N 個と体パーツを M 個持っています。 高橋くんが持っている i 番目 (1\le i\le N) の頭パーツの重さは H _ i グラム、i 番目 (1\le i\le M) の体パーツの重さは B _ i グラムです。
高橋くんは、持っているパーツを適切に組み合わせることで、倒れないロボットを合計 K 体作りたいです。 うまくパーツを組み合わせることで高橋くんが目標を達成することができるか判定してください。
ただし、1 個のパーツを複数のロボットを作るために利用したり、1 体のロボットを作るために頭パーツを 2 個以上(もしくは体パーツを 2 個以上)利用することはできません。
制約
- 1\le N\le2\times10 ^ 5
- 1\le M\le2\times10 ^ 5
- 1\le K\le\min\lbrace N,M\rbrace
- 1\le H _ i\le10 ^ 9\ (1\le i\le N)
- 1\le B _ i\le10 ^ 9\ (1\le i\le M)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M K H _ 1 H _ 2 \ldots H _ N B _ 1 B _ 2 \ldots B _ M
出力
高橋くんがパーツをうまく組み合わせて倒れないロボットを K 体作ることができるなら Yes を、そうでなければ No を出力せよ。
入力例 1
6 6 3 2 7 1 8 2 8 1 8 2 8 4 5
出力例 1
Yes
i 番目の頭パーツと j 番目の体パーツを組み合わせることを (i,j) と書くことにすると、高橋くんは例えば (1,2),(2,4),(3,6) のように組み合わせることで 3 体の倒れないロボットを作ることができます。
よって、Yes を出力してください。
入力例 2
1 1 1 43 1
出力例 2
No
高橋くんが持っている頭パーツが重すぎるため、高橋くんは倒れないロボットを作ることができません。
入力例 3
1 1 1 100 100
出力例 3
Yes
頭の重さと体の重さが等しい場合、ロボットは倒れないことに注意してください。
入力例 4
12 15 12 748 169 586 329 972 529 432 519 408 587 138 249 656 114 632 299 984 755 404 772 155 506 832 854 353 465 387
出力例 4
Yes
Score : 300 points
Problem Statement
Takahashi can combine a head part and a body part to create a robot. A robot falls over if the weight of the head part is greater than the weight of the body part.
Currently, he has N head parts and M body parts. The weight of the i-th (1\le i\le N) head part is H _ i grams, and the weight of the i-th (1\le i\le M) body part is B _ i grams.
He wants to create a total of K robots that do not fall over by appropriately combining the parts he has. Determine whether he can achieve his goal by combining the parts well.
Here, a part cannot be used to create multiple robots, and two or more head parts (or two or more body parts) cannot be used to create one robot.
Constraints
- 1\le N\le2\times10 ^ 5
- 1\le M\le2\times10 ^ 5
- 1\le K\le\min\lbrace N,M\rbrace
- 1\le H _ i\le10 ^ 9\ (1\le i\le N)
- 1\le B _ i\le10 ^ 9\ (1\le i\le M)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M K H _ 1 H _ 2 \ldots H _ N B _ 1 B _ 2 \ldots B _ M
Output
Print Yes if Takahashi can combine the parts well to create K robots that do not fall over; otherwise, print No.
Sample Input 1
6 6 3 2 7 1 8 2 8 1 8 2 8 4 5
Sample Output 1
Yes
If we denote combining the i-th head part and the j-th body part as (i,j), then Takahashi can create three robots that do not fall over by combining them as (1,2),(2,4),(3,6), for example.
Thus, print Yes.
Sample Input 2
1 1 1 43 1
Sample Output 2
No
His head part is too heavy, so he cannot create any robot that does not fall over.
Sample Input 3
1 1 1 100 100
Sample Output 3
Yes
Note that a robot does not fall over if the head and body have equal weights.
Sample Input 4
12 15 12 748 169 586 329 972 529 432 519 408 587 138 249 656 114 632 299 984 755 404 772 155 506 832 854 353 465 387
Sample Output 4
Yes
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 425 点
問題文
N 頂点 M 辺の単純連結無向グラフが与えられます。頂点 i\,(1\leq i \leq N) は重み A_i を持ちます。また、辺 j\,(1\leq j \leq M) は頂点 U_j,V_j を双方向に結び、重み B_j を持ちます。
このグラフ上のパスの重みを、パス上に現れる頂点の重みと辺の重みの総和と定義します。
各 i=2,3,\dots,N について、以下の問題を解いてください。
- 頂点 1 から頂点 i までのパスであって、重みが最小となるものの重みを求めよ。
制約
- 2 \leq N \leq 2 \times 10^5
- N-1 \leq M \leq 2 \times 10^5
- 1 \leq U_j < V_j \leq N
- i\neq j なら (U_i,V_i) \neq (U_j,V_j)
- グラフは連結である
- 0 \leq A_i \leq 10^9
- 0 \leq B_j \leq 10^9
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M A_1 A_2 \dots A_N U_1 V_1 B_1 U_2 V_2 B_2 \vdots U_M V_M B_M
出力
i=2,3,\dots,N に対する答えを、この順に空白区切りで一行で出力せよ。
入力例 1
3 3 1 2 3 1 2 1 1 3 6 2 3 2
出力例 1
4 9
頂点 1 から頂点 2 へのパスを考えます。 パス 1 \to 2 の重みは A_1+B_1+A_2=1+1+2=4、パス 1 \to 3 \to 2 の重みは A_1+B_2+A_3+B_3+A_2=1+6+3+2+2=14 であり、最小の重みは 4 です。
頂点 1 から頂点 3 へのパスを考えます。 パス 1 \to 3 の重みは A_1+B_2+A_3=1+6+3=10、パス 1 \to 2 \to 3 の重みは A_1+B_1+A_2+B_3+A_3=1+1+2+2+3=9 であり、最小の重みは 9 です。
入力例 2
2 1 0 1 1 2 3
出力例 2
4
入力例 3
5 8 928448202 994752369 906965437 942744902 907560126 2 5 975090662 1 2 908843627 1 5 969061140 3 4 964249326 2 3 957690728 2 4 942986477 4 5 948404113 1 3 988716403
出力例 3
2832044198 2824130042 4696218483 2805069468
答えが 32-bit 整数に収まらないことがあることに注意してください。
Score : 425 points
Problem Statement
You are given a simple connected undirected graph with N vertices and M edges. Each vertex i\,(1\leq i \leq N) has a weight A_i. Each edge j\,(1\leq j \leq M) connects vertices U_j and V_j bidirectionally and has a weight B_j.
The weight of a path in this graph is defined as the sum of the weights of the vertices and edges that appear on the path.
For each i=2,3,\dots,N, solve the following problem:
- Find the minimum weight of a path from vertex 1 to vertex i.
Constraints
- 2 \leq N \leq 2 \times 10^5
- N-1 \leq M \leq 2 \times 10^5
- 1 \leq U_j < V_j \leq N
- (U_i, V_i) \neq (U_j, V_j) if i \neq j.
- The graph is connected.
- 0 \leq A_i \leq 10^9
- 0 \leq B_j \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M A_1 A_2 \dots A_N U_1 V_1 B_1 U_2 V_2 B_2 \vdots U_M V_M B_M
Output
Print the answers for i=2,3,\dots,N in a single line, separated by spaces.
Sample Input 1
3 3 1 2 3 1 2 1 1 3 6 2 3 2
Sample Output 1
4 9
Consider the paths from vertex 1 to vertex 2. The weight of the path 1 \to 2 is A_1 + B_1 + A_2 = 1 + 1 + 2 = 4, and the weight of the path 1 \to 3 \to 2 is A_1 + B_2 + A_3 + B_3 + A_2 = 1 + 6 + 3 + 2 + 2 = 14. The minimum weight is 4.
Consider the paths from vertex 1 to vertex 3. The weight of the path 1 \to 3 is A_1 + B_2 + A_3 = 1 + 6 + 3 = 10, and the weight of the path 1 \to 2 \to 3 is A_1 + B_1 + A_2 + B_3 + A_3 = 1 + 1 + 2 + 2 + 3 = 9. The minimum weight is 9.
Sample Input 2
2 1 0 1 1 2 3
Sample Output 2
4
Sample Input 3
5 8 928448202 994752369 906965437 942744902 907560126 2 5 975090662 1 2 908843627 1 5 969061140 3 4 964249326 2 3 957690728 2 4 942986477 4 5 948404113 1 3 988716403
Sample Output 3
2832044198 2824130042 4696218483 2805069468
Note that the answers may not fit in a 32-bit integer.
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 450 点
問題文
高橋君が N 体のモンスターと順に戦うゲームで遊ぼうとしています。最初、高橋君の体力は H 、魔力は M です。
i 番目に戦うモンスターに対して、高橋君は以下のどちらかの行動を選べます。
- 魔法を使わずに戦う。体力が A_i 以上のときのみ選ぶことができ、体力が A_i 減少しモンスターを倒す。
- 魔法を使って戦う。魔力が B_i 以上のときのみ選ぶことができ、魔力が B_i 減少しモンスターを倒す。
N 体全てのモンスターを倒すか、高橋君が行動できなくなるとゲーム終了です。ゲーム終了までに最大で何体のモンスターを倒せますか。
制約
- 1 \leq N \leq 3000
- 1 \leq H,M \leq 3000
- 1 \leq A_i,B_i \leq 3000
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N H M A_1 B_1 A_2 B_2 \vdots A_N B_N
出力
答えを出力せよ。
入力例 1
4 10 14 5 8 5 6 7 9 99 99
出力例 1
3
次のように行動することで、ゲーム終了までに 3 体のモンスターを倒すことができます。
- 最初、高橋君の体力は 10、魔力は 14 です。
- 1 体目のモンスターと魔法を使わずに戦う。体力が 5 減少し、体力が 5、魔力が 14 となる。
- 2 体目のモンスターと魔法を使わずに戦う。体力が 5 減少し、体力が 0、魔力が 14 となる。
- 3 体目のモンスターと魔法を使って戦う。魔力が 9 減少し、体力が 0、魔力が 5 となる。
- 4 体目のモンスターとの戦いではどちらの行動も選べないためゲーム終了となる。
入力例 2
3 3000 3000 3 3 3 3 3 3
出力例 2
3
全てのモンスターを倒せることもあります。
入力例 3
10 8 8 2 2 2 3 2 2 1 2 2 3 1 2 3 3 3 2 3 1 3 2
出力例 3
9
Score : 450 points
Problem Statement
Takahashi is about to play a game where he fights N monsters in order. Initially, Takahashi's health is H and his magic power is M.
For the i-th monster he fights, he can choose one of the following actions:
- Fight without using magic. This can only be chosen when his health is at least A_i, and his health decreases by A_i and the monster is defeated.
- Fight using magic. This can only be chosen when his magic power is at least B_i, and his magic power decreases by B_i and the monster is defeated.
The game ends when all N monsters are defeated or when he cannot take any action. What is the maximum number of monsters he can defeat before the game ends?
Constraints
- 1 \leq N \leq 3000
- 1 \leq H,M \leq 3000
- 1 \leq A_i,B_i \leq 3000
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N H M A_1 B_1 A_2 B_2 \vdots A_N B_N
Output
Output the answer.
Sample Input 1
4 10 14 5 8 5 6 7 9 99 99
Sample Output 1
3
By taking the following actions, Takahashi can defeat 3 monsters before the game ends.
- Initially, his health is 10 and his magic power is 14.
- Fight the 1st monster without using magic. His health decreases by 5, becoming health 5 and magic power 14.
- Fight the 2nd monster without using magic. His health decreases by 5, becoming health 0 and magic power 14.
- Fight the 3rd monster using magic. His magic power decreases by 9, becoming health 0 and magic power 5.
- For the 4th monster, he cannot choose either action, so the game ends.
Sample Input 2
3 3000 3000 3 3 3 3 3 3
Sample Output 2
3
He may be able to defeat all monsters.
Sample Input 3
10 8 8 2 2 2 3 2 2 1 2 2 3 1 2 3 3 3 2 3 1 3 2
Sample Output 3
9
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 500 点
問題文
N+1 頂点の無向グラフが与えられます。
頂点には頂点 0 、頂点 1 、\ldots 、頂点 N と名前がついています。
i=1,2,\ldots,N について、頂点 0 と頂点 i を結ぶ重み A_i の無向辺があります。
また、i=1,2,\ldots,N について、頂点 i と頂点 i+1 を結ぶ重み B_i の無向辺があります。(ただし、頂点 N+1 は頂点 1 とみなします。)
上に述べた 2N 本の辺の他に辺はありません。
このグラフからいくつかの辺を削除して、グラフを二部グラフにします。
削除する辺の重みの総和の最小値はいくつですか?
制約
- 3 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- 入力は全て整数である
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
答えを出力せよ。
入力例 1
5 31 4 159 2 65 5 5 5 5 10
出力例 1
16

頂点 0,2 を結ぶ辺(重み 4 )、頂点 0,4 を結ぶ辺(重み 2 )、頂点 1,5 を結ぶ辺(重み 10 )を削除するとグラフは二部グラフになります。
入力例 2
4 100 100 100 1000000000 1 2 3 4
出力例 2
10
Score : 500 points
Problem Statement
Given is an undirected graph with N+1 vertices.
The vertices are called Vertex 0, Vertex 1, \ldots, Vertex N.
For each i=1,2,\ldots,N, the graph has an undirected edge with a weight of A_i connecting Vertex 0 and Vertex i.
Additionally, for each i=1,2,\ldots,N, the graph has an undirected edge with a weight of B_i connecting Vertex i and Vertex i+1. (Here, Vertex N+1 stands for Vertex 1.)
The graph has no edge other than these 2N edges above.
Let us delete some of the edges from this graph so that the graph will be bipartite.
What is the minimum total weight of the edges that have to be deleted?
Constraints
- 3 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 1 \leq B_i \leq 10^9
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
Output
Print the answer.
Sample Input 1
5 31 4 159 2 65 5 5 5 5 10
Sample Output 1
16

Deleting the edge connecting Vertices 0,2 (weight: 4), the edge connecting Vertices 0,4 (weight: 2), and the edge connecting Vertices 1,5 (weight: 10) makes the graph bipartite.
Sample Input 2
4 100 100 100 1000000000 1 2 3 4
Sample Output 2
10