実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
N 問の問題が出題されるプログラミングコンテストがあります。 i = 1, 2, \ldots, N について、i 問目の配点は S_i です。
配点が X 以下である問題すべての配点の合計を出力してください。
制約
- 入力される値は全て整数
- 4 \leq N \leq 8
- 100 \leq S_i \leq 675
- 100 \leq X \leq 675
入力
入力は以下の形式で標準入力から与えられる。
N X S_1 S_2 \ldots S_N
出力
答えを出力せよ。
入力例 1
6 200 100 675 201 200 199 328
出力例 1
499
配点が 200 以下である問題は、1, 4, 5 問目の全 3 問であり、それらの配点の合計は S_1 + S_4 + S_5 = 100 + 200 + 199 = 499 です。
入力例 2
8 675 675 675 675 675 675 675 675 675
出力例 2
5400
入力例 3
8 674 675 675 675 675 675 675 675 675
出力例 3
0
Score : 100 points
Problem Statement
There is a programming contest with N problems. For each i = 1, 2, \ldots, N, the score for the i-th problem is S_i.
Print the total score for all problems with a score of X or less.
Constraints
- All input values are integers.
- 4 \leq N \leq 8
- 100 \leq S_i \leq 675
- 100 \leq X \leq 675
Input
The input is given from Standard Input in the following format:
N X S_1 S_2 \ldots S_N
Output
Print the answer.
Sample Input 1
6 200 100 675 201 200 199 328
Sample Output 1
499
Three problems have a score of 200 or less: the first, fourth, and fifth, for a total score of S_1 + S_4 + S_5 = 100 + 200 + 199 = 499.
Sample Input 2
8 675 675 675 675 675 675 675 675 675
Sample Output 2
5400
Sample Input 3
8 674 675 675 675 675 675 675 675 675
Sample Output 3
0
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
AtCoder 国では、1 年が N か月からなる暦を使っています。 i 月 (1\leq i\leq N) は、i 月 1 日から i 月 D _ i 日までの D _ i 日からなります。
AtCoder 国において、1 年のうち日付がゾロ目になる日が何日あるか求めてください。
ただし、i 月 j 日 (1\leq i\leq N,1\leq j\leq D _ i) の日付がゾロ目になるとは、1 種類の数字だけを用いて i と j を十進法で表すことができることをいいます。
制約
- 1\leq N\leq100
- 1\leq D _ i\leq100\ (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N D _ 1 D _ 2 \ldots D _ N
出力
答えを出力せよ。
入力例 1
12 31 29 31 30 31 30 31 31 30 31 30 31
出力例 1
13
AtCoder 国では、 1 月 1 日、1 月 11 日、2 月 2 日、2 月 22 日、3 月 3 日、4 月 4 日、5 月 5 日、6 月 6 日、7 月 7 日、8 月 8 日、9 月 9 日、11 月 1 日、11 月 11 日の合計 13 日の日付がゾロ目になります。
入力例 2
10 10 1 2 3 4 5 6 7 8 100
出力例 2
1
AtCoder 国では、1 月 1 日のみが日付がゾロ目になります。
入力例 3
30 73 8 55 26 97 48 37 47 35 55 5 17 62 2 60 23 99 73 34 75 7 46 82 84 29 41 32 31 52 32
出力例 3
15
Score : 200 points
Problem Statement
AtCoder Kingdom uses a calendar whose year has N months. Month i (1\leq i\leq N) has D _ i days, from day 1 of month i to day D _ i of month i.
How many days in a year of AtCoder have "repdigits" dates?
Here, day j of month i (1\leq i\leq N,1\leq j\leq D _ i) is said to have a repdigit date if and only if all digits in the decimal notations of i and j are the same.
Constraints
- 1\leq N\leq100
- 1\leq D _ i\leq100\ (1\leq i\leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N D _ 1 D _ 2 \ldots D _ N
Output
Print the answer.
Sample Input 1
12 31 29 31 30 31 30 31 31 30 31 30 31
Sample Output 1
13
In AtCoder Kingdom, the days that have repdigit dates are January 1, January 11, February 2, February 22, March 3, April 4, May 5, June 6, July 7, August 8, September 9, November 1, and November 11, for a total of 13 days.
Sample Input 2
10 10 1 2 3 4 5 6 7 8 100
Sample Output 2
1
In AtCoder Kingdom, only January 1 has a repdigit date.
Sample Input 3
30 73 8 55 26 97 48 37 47 35 55 5 17 62 2 60 23 99 73 34 75 7 46 82 84 29 41 32 31 52 32
Sample Output 3
15
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
英小文字のみからなる長さ N の文字列 S = S_1S_2\ldots S_N が与えられます。
また、S に関する Q 個の質問が与えられます。 i = 1, 2, \ldots, Q について、i 番目の質問は 2 つの整数 l_i, r_i で表される下記の質問です。
S の l_i 文字目から r_i 文字目までからなる部分文字列 S_{l_i}S_{l_i+1}\ldots S_{r_i} において、 同じ英小文字が 2 つ隣りあう箇所は何個ありますか? すなわち、l_i \leq p \leq r_i-1 かつ S_p = S_{p+1}を満たす整数 p は何個ありますか?
Q 個の質問それぞれの答えを出力してください。
制約
- N, Q は整数
- 1 \leq N, Q \leq 3 \times 10^5
- S は英小文字のみからなる長さ N の文字列
- l_i, r_i は整数
- 1 \leq l_i \leq r_i \leq N
入力
入力は以下の形式で標準入力から与えられる。
N Q S l_1 r_1 l_2 r_2 \vdots l_Q r_Q
出力
Q 行出力せよ。 i = 1, 2, \ldots, Q について、i 行目には i 番目の質問に対する答えを出力せよ。
入力例 1
11 4 mississippi 3 9 4 10 4 6 7 7
出力例 1
2 2 0 0
4 個の質問それぞれに対する答えは下記の通りです。
- 1 個目の質問に関して、S_3S_4\ldots S_9 =
ssissipで同じ英小文字が隣り合う箇所は、S_3S_4 =ssと S_6S_7 =ssの 2 個です。 - 2 個目の質問に関して、S_4S_5\ldots S_{10} =
sissippで同じ英小文字が隣り合う箇所は、S_6S_7 =ssと S_9S_{10} =ppの 2 個です。 - 3 個目の質問に関して、S_4S_5S_6 =
sisで同じ英小文字が隣り合う箇所は 0 個です。 - 4 個目の質問に関して、S_7 =
sで同じ英小文字が隣り合う箇所は 0 個です。
入力例 2
5 1 aaaaa 1 5
出力例 2
4
S_1S_2\ldots S_5 = aaaaa で同じ英小文字が隣り合う箇所は、
S_1S_2 = aa 、S_2S_3 = aa 、S_3S_4 = aa 、S_4S_5 = aa の 4 個です。
Score : 300 points
Problem Statement
You are given a string S = S_1S_2\ldots S_N of length N consisting of lowercase English letters.
Additionally, you are given Q queries about the string S. For i = 1, 2, \ldots, Q, the i-th query is represented by two integers l_i, r_i and asks the following.
In the substring S_{l_i}S_{l_i+1}\ldots S_{r_i} of S, which ranges from the l_i-th to the r_i-th character, how many places are there where the same lowercase English letter occurs twice in a row? In other words, how many integers p satisfy l_i \leq p \leq r_i-1 and S_p = S_{p+1}?
Print the answer for each of the Q queries.
Constraints
- N and Q are integers.
- 1 \leq N, Q \leq 3 \times 10^5
- S is a string of length N consisting of lowercase English letters.
- l_i and r_i are integers.
- 1 \leq l_i \leq r_i \leq N
Input
The input is given from Standard Input in the following format:
N Q S l_1 r_1 l_2 r_2 \vdots l_Q r_Q
Output
Print Q lines. For i = 1, 2, \ldots, Q, the i-th line should contain the answer to the i-th query.
Sample Input 1
11 4 mississippi 3 9 4 10 4 6 7 7
Sample Output 1
2 2 0 0
The answers to the four queries are as follows.
- For the first query, S_3S_4\ldots S_9 =
ssissiphas two places where the same lowercase English letter occurs twice in a row: S_3S_4 =ssand S_6S_7 =ss. - For the second query, S_4S_5\ldots S_{10} =
sissipphas two places where the same lowercase English letter occurs twice in a row: S_6S_7 =ssand S_9S_{10} =pp. - For the third query, S_4S_5S_6 =
sishas zero places where the same lowercase English letter occurs twice in a row. - For the fourth query, S_7 =
shas zero places where the same lowercase English letter occurs twice in a row.
Sample Input 2
5 1 aaaaa 1 5
Sample Output 2
4
S_1S_2\ldots S_5 = aaaaa has four places where the same lowercase English letter occurs twice in a row:
S_1S_2 = aa, S_2S_3 = aa, S_3S_4 = aa, and S_4S_5 = aa.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
A , B , C の 3 種類の文字のみからなる文字列 S が与えられます。
S が連続な部分文字列として文字列 ABC を含む限り、下記の操作を繰り返します。
S に連続な部分文字列として含まれる文字列
ABCのうち、S の中で最も左にあるものを、S から削除する。
上記の手順を行った後の、最終的な S を出力してください。
制約
- S は
A,B,Cのみからなる長さ 1 以上 2\times 10^5 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
BAABCBCCABCAC
出力例 1
BCAC
与えられた文字列 S = BAABCBCCABCAC に対して、下記の通りに操作が行われます。
- 1 回目の操作で、S =
BAABCBCCABCACの 3 文字目から 5 文字目のABCが削除され、その結果 S =BABCCABCACとなります。 - 2 回目の操作で、S =
BABCCABCACの 2 文字目から 4 文字目のABCが削除され、その結果 S =BCABCACとなります。 - 3 回目の操作で、S =
BCABCACの 3 文字目から 5 文字目のABCが削除され、その結果 S =BCACとなります。
よって、最終的な S は BCAC です。
入力例 2
ABCABC
出力例 2
この入力例では、最終的な S は空文字列です。
入力例 3
AAABCABCABCAABCABCBBBAABCBCCCAAABCBCBCC
出力例 3
AAABBBCCC
Score : 425 points
Problem Statement
You are given a string S consisting of three different characters: A, B, and C.
As long as S contains the string ABC as a consecutive substring, repeat the following operation:
Remove the leftmost occurrence of the substring
ABCfrom S.
Print the final string S after performing the above procedure.
Constraints
- S is a string of length between 1 and 2 \times 10^5, inclusive, consisting of the characters
A,B, andC.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
BAABCBCCABCAC
Sample Output 1
BCAC
For the given string S = BAABCBCCABCAC, the operations are performed as follows.
- In the first operation, the
ABCfrom the 3-rd to the 5-th character in S =BAABCBCCABCACis removed, resulting in S =BABCCABCAC. - In the second operation, the
ABCfrom the 2-nd to the 4-th character in S =BABCCABCACis removed, resulting in S =BCABCAC. - In the third operation, the
ABCfrom the 3-rd to the 5-th character in S =BCABCACis removed, resulting in S =BCAC.
Therefore, the final S is BCAC.
Sample Input 2
ABCABC
Sample Output 2
In this example, the final S is an empty string.
Sample Input 3
AAABCABCABCAABCABCBBBAABCBCCCAAABCBCBCC
Sample Output 3
AAABBBCCC
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 475 点
問題文
頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の重み付き単純連結無向グラフと正整数 K が与えられます。
辺 i\ (1\leq i\leq M) は頂点 u _ i と頂点 v _ i を結んでおり、重みは w _ i です。
このグラフの全域木 T に対して、T のコストを T に含まれる辺の重みの総和を K で割ったあまりで定めます。 このグラフの全域木のコストの最小値を求めてください。
制約
- 2\leq N\leq8
- N-1\leq M\leq\dfrac{N(N-1)}2
- 1\leq K\leq10^{15}
- 1\leq u _ i\lt v _ i\leq N\ (1\leq i\leq M)
- 0\leq w _ i\lt K\ (1\leq i\leq M)
- 与えられるグラフは単純かつ連結
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M K u _ 1 v _ 1 w _ 1 u _ 2 v _ 2 w _ 2 \vdots u _ M v _ M w _ M
出力
答えを出力せよ。
入力例 1
5 6 328 1 2 99 1 3 102 2 3 86 2 4 94 2 5 95 3 4 81
出力例 1
33
与えられるグラフは次のようになります。

辺 1,3,5,6 の 4 本を含む全域木のコストは (99+86+81+95)\bmod{328}=361\bmod{328}=33 となります。 このグラフの全域木のコストはすべて 33 以上であるため、33 を出力してください。
入力例 2
6 5 998244353 1 2 337361568 1 6 450343304 2 3 61477244 2 5 745383438 4 5 727360840
出力例 2
325437688
このグラフのただ一つの全域木のコスト 325437688 を出力してください。
入力例 3
8 28 936294041850197 1 2 473294720906780 1 3 743030800139244 1 4 709363019414774 1 5 383643612490312 1 6 557102781022861 1 7 623179288538138 1 8 739618599410809 2 3 857687812294404 2 4 893923168139714 2 5 581822471860662 2 6 740549363586558 2 7 307226438833222 2 8 447399029952998 3 4 636318083622768 3 5 44548707643622 3 6 307262781240755 3 7 12070267388230 3 8 700247263184082 4 5 560567890325333 4 6 704726113717147 4 7 588263818615687 4 8 549007536393172 5 6 779230871080408 5 7 825982583786498 5 8 713928998174272 6 7 751331074538826 6 8 449873635430228 7 8 11298381761479
出力例 3
11360716373
入力や答えが 32\operatorname{bit} 整数に収まらない場合があることに注意してください。
Score : 475 points
Problem Statement
You are given a weighted simple connected undirected graph with N vertices and M edges, where vertices are numbered 1 to N, and edges are numbered 1 to M. Additionally, a positive integer K is given.
Edge i\ (1\leq i\leq M) connects vertices u_i and v_i and has a weight of w_i.
For a spanning tree T of this graph, the cost of T is defined as the sum, modulo K, of the weights of the edges in T. Find the minimum cost of a spanning tree of this graph.
Constraints
- 2\leq N\leq8
- N-1\leq M\leq\dfrac{N(N-1)}2
- 1\leq K\leq10^{15}
- 1\leq u_i\lt v_i\leq N\ (1\leq i\leq M)
- 0\leq w_i\lt K\ (1\leq i\leq M)
- The given graph is simple and connected.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N M K u_1 v_1 w_1 u_2 v_2 w_2 \vdots u_M v_M w_M
Output
Print the answer.
Sample Input 1
5 6 328 1 2 99 1 3 102 2 3 86 2 4 94 2 5 95 3 4 81
Sample Output 1
33
The given graph is shown below:

The cost of the spanning tree containing edges 1,3,5,6 is (99+86+81+95)\bmod{328}=361\bmod{328}=33. The cost of every spanning tree of this graph is at least 33, so print 33.
Sample Input 2
6 5 998244353 1 2 337361568 1 6 450343304 2 3 61477244 2 5 745383438 4 5 727360840
Sample Output 2
325437688
Print the cost of the only spanning tree of this graph, which is 325437688.
Sample Input 3
8 28 936294041850197 1 2 473294720906780 1 3 743030800139244 1 4 709363019414774 1 5 383643612490312 1 6 557102781022861 1 7 623179288538138 1 8 739618599410809 2 3 857687812294404 2 4 893923168139714 2 5 581822471860662 2 6 740549363586558 2 7 307226438833222 2 8 447399029952998 3 4 636318083622768 3 5 44548707643622 3 6 307262781240755 3 7 12070267388230 3 8 700247263184082 4 5 560567890325333 4 6 704726113717147 4 7 588263818615687 4 8 549007536393172 5 6 779230871080408 5 7 825982583786498 5 8 713928998174272 6 7 751331074538826 6 8 449873635430228 7 8 11298381761479
Sample Output 3
11360716373
Note that the input and the answer may not fit into a 32\operatorname{bit} integer.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 525 点
問題文
Q 個の整数の 3 つ組 (a_1, b_1, d_1), (a_2, b_2, d_2), \ldots, (a_Q, b_Q, d_Q) が与えられます。
集合 \lbrace 1, 2, \ldots, Q\rbrace の部分集合 S が良い集合であることを、 下記の条件を満たす長さ N の整数列 (X_1, X_2, \ldots, X_N) が存在することと定めます。
すべての i \in S について、X_{a_i} - X_{b_i} = d_i が成り立つ。
S が空集合である状態から始め、i = 1, 2, \ldots, Q の順に下記の操作を行います。
もし S \cup \lbrace i \rbrace が良い集合なら、S を S \cup \lbrace i \rbrace で置き換える。
最終的な S のすべての要素を昇順に出力してください。
制約
- 入力される値はすべて整数
- 1 \leq N, Q \leq 2 \times 10^5
- 1 \leq a_i, b_i \leq N
- -10^9 \leq d_i \leq 10^9
入力
入力は以下の形式で標準入力から与えられる。
N Q a_1 b_1 d_1 a_2 b_2 d_2 \vdots a_Q b_Q d_Q
出力
最終的な S のすべての要素を昇順に並べた列 (s_1, s_2, \ldots, s_k) を、下記の形式で空白区切りで出力せよ。
s_1 s_2 \ldots s_k
入力例 1
3 5 1 2 2 3 2 -3 2 1 -1 3 3 0 1 3 5
出力例 1
1 2 4 5
S が空集合である状態から始め、i = 1, 2, 3, 4, 5 の順に問題文中の操作を下記の通り行います。
- i = 1 について、集合 S \cup \lbrace i \rbrace = \lbrace 1 \rbrace は良い集合です。なぜなら、例えば (X_1, X_2, X_3) = (3, 1, 4) が問題文中の条件を満たすからです。よって、S を \lbrace 1\rbrace で置き換えます。
- i = 2 について、集合 S \cup \lbrace i \rbrace = \lbrace 1, 2 \rbrace は良い集合です。なぜなら、例えば (X_1, X_2, X_3) = (3, 1, -2) が問題文中の条件を満たすからです。よって、S を \lbrace 1, 2\rbrace で置き換えます。
- i = 3 について、集合 S \cup \lbrace i \rbrace = \lbrace 1, 2, 3 \rbrace は良い集合ではありません。
- i = 4 について、集合 S \cup \lbrace i \rbrace = \lbrace 1, 2, 4 \rbrace は良い集合です。なぜなら、例えば (X_1, X_2, X_3) = (3, 1, -2) が問題文中の条件を満たすからです。よって、S を \lbrace 1, 2, 4\rbrace で置き換えます。
- i = 5 について、集合 S \cup \lbrace i \rbrace = \lbrace 1, 2, 4, 5 \rbrace は良い集合です。なぜなら、例えば (X_1, X_2, X_3) = (3, 1, -2) が問題文中の条件を満たすからです。よって、S を \lbrace 1, 2, 4, 5\rbrace で置き換えます。
よって、最終的な S は \lbrace 1, 2, 4, 5\rbrace です。
入力例 2
200000 1 1 1 1
出力例 2
最終的な S は空集合です。
入力例 3
5 20 4 2 125421359 2 5 -191096267 3 4 -42422908 3 5 -180492387 3 3 174861038 2 3 -82998451 3 4 -134761089 3 1 -57159320 5 2 191096267 2 4 -120557647 4 2 125421359 2 3 142216401 4 5 -96172984 3 5 -108097816 1 5 -50938496 1 2 140157771 5 4 65674908 4 3 35196193 4 4 0 3 4 188711840
出力例 3
1 2 3 6 8 9 11 14 15 16 17 19
Score : 525 points
Problem Statement
You are given Q triples of integers (a_1, b_1, d_1), (a_2, b_2, d_2), \ldots, (a_Q, b_Q, d_Q).
A subset S of the set \lbrace 1, 2, \ldots, Q\rbrace is defined to be a good set if there exists an integer sequence (X_1, X_2, \ldots, X_N) of length N that satisfies:
X_{a_i} - X_{b_i} = d_i for all i \in S.
Starting with S as an empty set, perform the following operation for i = 1, 2, \ldots, Q in this order:
If S \cup \lbrace i \rbrace is a good set, then replace S with S \cup \lbrace i \rbrace.
Print all elements of the final set S in ascending order.
Constraints
- All input values are integers.
- 1 \leq N, Q \leq 2 \times 10^5
- 1 \leq a_i, b_i \leq N
- -10^9 \leq d_i \leq 10^9
Input
The input is given from Standard Input in the following format:
N Q a_1 b_1 d_1 a_2 b_2 d_2 \vdots a_Q b_Q d_Q
Output
Print the sequence (s_1, s_2, \ldots, s_k) of all elements of the final set S in ascending order, separated by spaces, in the following format:
s_1 s_2 \ldots s_k
Sample Input 1
3 5 1 2 2 3 2 -3 2 1 -1 3 3 0 1 3 5
Sample Output 1
1 2 4 5
Starting with S as an empty set, perform the operation described in the problem statement for i = 1, 2, 3, 4, 5 in this order, as follows.
- For i = 1, the set S \cup \lbrace i \rbrace = \lbrace 1 \rbrace is a good set, because (X_1, X_2, X_3) = (3, 1, 4) satisfies the condition in the problem statement, for example, so replace S with \lbrace 1\rbrace.
- For i = 2, the set S \cup \lbrace i \rbrace = \lbrace 1, 2 \rbrace is a good set, because (X_1, X_2, X_3) = (3, 1, -2) satisfies the condition in the problem statement, for example, so replace S with \lbrace 1, 2\rbrace.
- For i = 3, the set S \cup \lbrace i \rbrace = \lbrace 1, 2, 3 \rbrace is not a good set.
- For i = 4, the set S \cup \lbrace i \rbrace = \lbrace 1, 2, 4 \rbrace is a good set, because (X_1, X_2, X_3) = (3, 1, -2) satisfies the condition in the problem statement, for example, so replace S with \lbrace 1, 2, 4\rbrace.
- For i = 5, the set S \cup \lbrace i \rbrace = \lbrace 1, 2, 4, 5 \rbrace is a good set, because (X_1, X_2, X_3) = (3, 1, -2) satisfies the condition in the problem statement, for example, so replace S with \lbrace 1, 2, 4, 5\rbrace.
Therefore, the final set S is \lbrace 1, 2, 4, 5\rbrace.
Sample Input 2
200000 1 1 1 1
Sample Output 2
The final set S is empty.
Sample Input 3
5 20 4 2 125421359 2 5 -191096267 3 4 -42422908 3 5 -180492387 3 3 174861038 2 3 -82998451 3 4 -134761089 3 1 -57159320 5 2 191096267 2 4 -120557647 4 2 125421359 2 3 142216401 4 5 -96172984 3 5 -108097816 1 5 -50938496 1 2 140157771 5 4 65674908 4 3 35196193 4 4 0 3 4 188711840
Sample Output 3
1 2 3 6 8 9 11 14 15 16 17 19
実行時間制限: 2.8 sec / メモリ制限: 512 MiB
配点 : 575 点
問題文
長さ N の数列 A=(A _ 1,A _ 2,\ldots,A _ N),B=(B _ 1,B _ 2,\ldots,B _ N) が与えられます。
あなたは、数列 A に対して次の 2 種類の操作を好きな順番で好きな回数行うことができます。
- A を好きな位置で分割し、分割された列を自由に並べ替える。分割した位置 1 つにつきコストが C かかる。 厳密には、(X-1)C のコストをかけて長さ X+1 の列 (i _ 0,i _ 1,i _ 2,\ldots,i _ X)\ (0=i _ 0\lt i _ 1\lt i _ 2\lt\cdots\lt i _ X=N) と (1,2,\ldots,X) の順列 p を自由にとり、(A _ {i _ {p _ j-1}+1},A _ {i _ {p _ j-1}+2},\ldots,A _ {i _ {p _ j}}) を j の昇順に連結したものを新しい A とする。
- 整数 k と A の好きな要素を 1 つ選び、選んだ要素の値に k を加える。コストが |k| かかる。
操作をすべて終えたときに A と B が等しくなるように操作を行うとき、必要なコストの合計の最小値を求めてください。
制約
- 1\leq N\leq22
- 1\leq C\leq10 ^ {15}
- 1\leq A _ i\leq10 ^ {15}\ (1\leq i\leq N)
- 1\leq B _ i\leq10 ^ {15}\ (1\leq i\leq N)
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N C A _ 1 A _ 2 \ldots A _ N B _ 1 B _ 2 \ldots B _ N
出力
答えを出力せよ。
入力例 1
5 1 3 1 4 1 5 9 2 6 5 3
出力例 1
12
例えば、次のように操作をすることで A と B を等しくすることができます。
- A _ 2 に 2 を加える。コストが 2 かかり、A=(3,3,4,1,5) となる。
- A _ 4 に 1 を加える。コストが 1 かかり、A=(3,3,4,2,5) となる。
- A _ 3 に 3 を加える。コストが 3 かかり、A=(3,3,7,2,5) となる。
- A を (3,3) と (7,2,5) に分割し、順番を入れ替える。コストが 1 かかり、A=(7,2,5,3,3) となる。
- A _ 3 に 1 を加える。コストが 1 かかり、A=(7,2,6,3,3) となる。
- A _ 4 に 2 を加える。コストが 2 かかり、A=(7,2,6,5,3) となる。
- A _ 1 に 2 を加える。コストが 2 かかり、A=(9,2,6,5,3) となる。
かかるコストの合計は 2+1+3+1+1+2+2=12 となります。
コストの合計を 11 以下にして A と B を等しくすることはできないため、12 と出力してください。
入力例 2
5 1000000000 3 1 4 1 5 9 2 6 5 3
出力例 2
15
例えば、次のように操作をすることで A と B を等しくすることができます。
- A _ 1 に 6 を加える。コストが 6 かかり、A=(9,1,4,1,5) となる。
- A _ 2 に 1 を加える。コストが 1 かかり、A=(9,2,4,1,5) となる。
- A _ 3 に 2 を加える。コストが 2 かかり、A=(9,2,6,1,5) となる。
- A _ 4 に 4 を加える。コストが 4 かかり、A=(9,2,6,5,5) となる。
- A _ 5 に -2 を加える。コストが 2 かかり、A=(9,2,6,5,3) となる。
かかるコストの合計は 15 となります。
コストの合計を 14 以下にして A と B を等しくすることはできないため、15 と出力してください。
入力例 3
22 467772225675200 814424018890229 837987908732596 281175505732576 405797525366223 319378664987871 305374284356649 519144936694626 316916938328237 590332737480143 506785561790072 945769796193819 365498597798550 5386616044591 672368930784037 478017750715806 340276460237787 176509793332130 2734777402752 677509027289850 250325127275409 260270543315523 103584313625431 720386673780641 77160494100361 540947273460639 255177791002759 969333325196025 477751866935037 369600749728569 466236682780196 343161112138696 541310338013515 42740499599240 165778332156355 618106559852784 16582487395877 591851763813728 221861304303645 982850624742022 728669467505250 337968530842725 746724490610504 61587851254728 451153536869240
出力例 3
4370668608634071
入力や答えが 32\operatorname{bit} 整数に収まらない場合があります。
Score : 575 points
Problem Statement
You are given two sequences of length N: A=(A_1,A_2,\ldots,A_N) and B=(B_1,B_2,\ldots,B_N).
You can perform the following two types of operations on the sequence A any number of times in any order.
- Split A at any positions and freely rearrange the split sequences. Each split position incurs a cost of C. More formally, at a cost of (X-1)C, take a sequence of length X+1, (i_0,i_1,i_2,\ldots,i_X)\ (0=i_0\lt i_1\lt i_2\lt\cdots\lt i_X=N), and a permutation p of (1,2,\ldots,X), and replace A with the concatenation of (A_{i_{p_j-1}+1},A_{i_{p_j-1}+2},\ldots,A_{i_{p_j}}) in ascending order of j.
- Choose an integer k and any element of A, and add k to the value of the chosen element, for a cost of |k|.
Find the minimum total cost required to make A and B equal by performing the operations.
Constraints
- 1\leq N\leq22
- 1\leq C\leq10^{15}
- 1\leq A_i\leq10^{15}\ (1\leq i\leq N)
- 1\leq B_i\leq10^{15}\ (1\leq i\leq N)
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N C A_1 A_2 \ldots A_N B_1 B_2 \ldots B_N
Output
Print the answer.
Sample Input 1
5 1 3 1 4 1 5 9 2 6 5 3
Sample Output 1
12
For example, you can make A and B equal by performing the following operations.
- Add 2 to A_2. It costs 2, and A becomes (3,3,4,1,5).
- Add 1 to A_4. It costs 1, and A becomes (3,3,4,2,5).
- Add 3 to A_3. It costs 3, and A becomes (3,3,7,2,5).
- Split A into (3,3) and (7,2,5), and swap their order. It costs 1, and A becomes (7,2,5,3,3).
- Add 1 to A_3. It costs 1, and A becomes (7,2,6,3,3).
- Add 2 to A_4. It costs 2, and A becomes (7,2,6,5,3).
- Add 2 to A_1. It costs 2, and A becomes (9,2,6,5,3).
The total cost incurred is 2+1+3+1+1+2+2=12.
It is impossible to make A and B equal with a total cost of 11 or less, so print 12.
Sample Input 2
5 1000000000 3 1 4 1 5 9 2 6 5 3
Sample Output 2
15
For example, you can make A and B equal by performing the following operations.
- Add 6 to A_1. It costs 6, and A becomes (9,1,4,1,5).
- Add 1 to A_2. It costs 1, and A becomes (9,2,4,1,5).
- Add 2 to A_3. It costs 2, and A becomes (9,2,6,1,5).
- Add 4 to A_4. It costs 4, and A becomes (9,2,6,5,5).
- Add -2 to A_5. It costs 2, and A becomes (9,2,6,5,3).
The total cost incurred is 15.
It is impossible to make A and B equal with a total cost of 14 or less, so print 15.
Sample Input 3
22 467772225675200 814424018890229 837987908732596 281175505732576 405797525366223 319378664987871 305374284356649 519144936694626 316916938328237 590332737480143 506785561790072 945769796193819 365498597798550 5386616044591 672368930784037 478017750715806 340276460237787 176509793332130 2734777402752 677509027289850 250325127275409 260270543315523 103584313625431 720386673780641 77160494100361 540947273460639 255177791002759 969333325196025 477751866935037 369600749728569 466236682780196 343161112138696 541310338013515 42740499599240 165778332156355 618106559852784 16582487395877 591851763813728 221861304303645 982850624742022 728669467505250 337968530842725 746724490610504 61587851254728 451153536869240
Sample Output 3
4370668608634071
Note that the input and the answer may not fit into a 32\operatorname{bit} integer.