実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
文字列 S が与えられます。ここで、S の 1 文字目は英大文字、2 文字目以降は英小文字です。
S の 1 文字目と UPC をこの順に結合した文字列を出力してください。
制約
- S は長さ 1 以上 100 以下の文字列
- S の 1 文字目は英大文字
- S の 2 文字目以降は英小文字
入力
入力は以下の形式で標準入力から与えられる。
S
出力
S の 1 文字目と UPC をこの順に結合した文字列を出力せよ。
入力例 1
Kyoto
出力例 1
KUPC
Kyoto の 1 文字目は K であるため、K と UPC を結合した KUPC を出力します。
入力例 2
Tohoku
出力例 2
TUPC
Score : 100 points
Problem Statement
You are given a string S. Here, the first character of S is an uppercase English letter, and the second and subsequent characters are lowercase English letters.
Print the string formed by concatenating the first character of S and UPC in this order.
Constraints
- S is a string of length between 1 and 100, inclusive.
- The first character of S is an uppercase English letter.
- The second and subsequent characters of S are lowercase English letters.
Input
The input is given from Standard Input in the following format:
S
Output
Print the string formed by concatenating the first character of S and UPC in this order.
Sample Input 1
Kyoto
Sample Output 1
KUPC
The first character of Kyoto is K, so concatenate K and UPC, and print KUPC.
Sample Input 2
Tohoku
Sample Output 2
TUPC
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
AtCoder Beginner Contest は、今回で 214 回目の開催となりました。
今までの AtCoder Beginner Contest において、出題される問題数は次のように変化しました。
- 1 回目から 125 回目までは 4 問
- 126 回目から 211 回目までは 6 問
- 212 回目から 214 回目までは 8 問
N 回目の AtCoder Beginner Contest において出題された問題数を求めてください。
制約
- 1 \leq N \leq 214
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
214
出力例 1
8
入力例 2
1
出力例 2
4
入力例 3
126
出力例 3
6
Score : 100 points
Problem Statement
This is the 214-th AtCoder Beginner Contest (ABC).
The ABCs so far have had the following number of problems.
- The 1-st through 125-th ABCs had 4 problems each.
- The 126-th through 211-th ABCs had 6 problems each.
- The 212-th through 214-th ABCs have 8 problems each.
Find the number of problems in the N-th ABC.
Constraints
- 1 \leq N \leq 214
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
214
Sample Output 1
8
Sample Input 2
1
Sample Output 2
4
Sample Input 3
126
Sample Output 3
6
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
キーエンスでは良いことも悪いこともありのままに報告するという文化があります。
そこで、報告内容が元の文章のありのままであるかを確認したいです。
英小文字のみからなる文字列 S, T が与えられます。
S と T が等しいならば 0 を、そうでないならば異なっている文字のうち先頭のものが何文字目かを出力してください。
ただし、S,T の一方にのみ i 文字目が存在するときも、i 文字目は異なっているとみなすものとします。
より厳密には、S と T が等しくないならば次の条件のうちいずれかをみたす最小の整数 i を出力してください。
- 1\leq i\leq |S| かつ 1\leq i\leq |T| かつ S_i\neq T_i
- |S|< i\leq |T|
- |T|< i\leq |S|
ただし、|S|,|T| でそれぞれ S,T の長さを、S_i,T_i でそれぞれ S,T の i 文字目を表します。
制約
- S,T は英小文字のみからなる長さ 1 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
S と T が等しいならば 0 を、そうでないならば異なっている文字のうち先頭のものが何文字目かを出力せよ。
入力例 1
abcde abedc
出力例 1
3
S=abcde, T=abedc です。
S と T は 1,2 文字目は等しく、3 文字目が異なるため、 3 を出力します。
入力例 2
abcde abcdefg
出力例 2
6
S=abcde, T=abcdefg です。
S と T は 5 文字目まで等しく、T にのみ 6 文字目が存在するため、6 を出力します。
入力例 3
keyence keyence
出力例 3
0
S と T は等しいため、 0 を出力します。
Score : 200 points
Problem Statement
KEYENCE has a culture of reporting things as they are, whether good or bad.
So we want to check whether the reported content is exactly the same as the original text.
You are given two strings S and T, consisting of lowercase English letters.
If S and T are equal, print 0; otherwise, print the position of the first character where they differ.
Here, if the i-th character exists in only one of S and T, consider that the i-th characters are different.
More precisely, if S and T are not equal, print the smallest integer i satisfying one of the following conditions:
- 1\leq i\leq |S|, 1\leq i\leq |T|, and S_i\neq T_i.
- |S| < i \leq |T|.
- |T| < i \leq |S|.
Here, |S| and |T| denote the lengths of S and T, respectively, and S_i and T_i denote the i-th characters of S and T, respectively.
Constraints
- S and T are strings of length between 1 and 100, inclusive, consisting of lowercase English letters.
Input
The input is given from Standard Input in the following format:
S T
Output
If S and T are equal, print 0; otherwise, print the position of the first character where they differ.
Sample Input 1
abcde abedc
Sample Output 1
3
We have S= abcde and T= abedc.
S and T have the same first and second characters, but differ at the third character, so print 3.
Sample Input 2
abcde abcdefg
Sample Output 2
6
We have S= abcde and T= abcdefg.
S and T are equal up to the fifth character, but only T has a sixth character, so print 6.
Sample Input 3
keyence keyence
Sample Output 3
0
S and T are equal, so print 0.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
文字列 S が与えられます。
S の中に A, B, C がこの順に等間隔に並んでいる場所が何箇所あるか求めてください。
厳密には、3 つの整数の組 (i,j,k) であって、以下の条件をすべて満たすものの個数を求めてください。 ここで、|S| で S の長さを、S_x で S の x 文字目を表すものとします。
- 1\leq i<j<k\leq |S|
- j-i = k-j
- S_i=
A - S_j=
B - S_k=
C
制約
- S は英大文字からなる長さ 3 以上 100 以下の文字列
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
AABCC
出力例 1
2
(i,j,k)=(1,3,5),(2,3,4) の 2 つの組が条件を満たします。
入力例 2
ARC
出力例 2
0
入力例 3
AABAAABBAEDCCCD
出力例 3
4
Score : 200 points
Problem Statement
A string S is given.
Find how many places in S have A, B, and C in this order at even intervals.
Specifically, find the number of triples of integers (i,j,k) that satisfy all of the following conditions. Here, |S| denotes the length of S, and S_x denotes the x-th character of S.
- 1 \leq i < j < k \leq |S|
- j - i = k - j
- S_i =
A - S_j =
B - S_k =
C
Constraints
- S is an uppercase English string with length between 3 and 100, inclusive.
Input
The input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
AABCC
Sample Output 1
2
There are two triples (i,j,k) = (1,3,5) and (2,3,4) that satisfy the conditions.
Sample Input 2
ARC
Sample Output 2
0
Sample Input 3
AABAAABBAEDCCCD
Sample Output 3
4
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
非負整数 n が次の条件を満たすとき、n を 良い整数 と呼びます。
- n を 10 進法で表したときに、偶数の数字 (0, 2, 4, 6, 8) のみが登場する。
例えば 0、68 および 2024 は良い整数です。
整数 N が与えられます。良い整数のうち小さい方から N 番目の整数を求めてください。
制約
- 1 \leq N \leq 10^{12}
- N は整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
小さい方から N 番目の良い整数を出力せよ。
入力例 1
8
出力例 1
24
良い整数を小さい方から順に並べると 0, 2, 4, 6, 8, 20, 22, 24, 26, 28, \dots となります。
小さい方から 8 番目の良い整数は 24 なので、これを出力します。
入力例 2
133
出力例 2
2024
入力例 3
31415926535
出力例 3
2006628868244228
Score: 300 points
Problem Statement
A non-negative integer n is called a good integer when it satisfies the following condition:
- All digits in the decimal notation of n are even numbers (0, 2, 4, 6, and 8).
For example, 0, 68, and 2024 are good integers.
You are given an integer N. Find the N-th smallest good integer.
Constraints
- 1 \leq N \leq 10^{12}
- N is an integer.
Input
The input is given from Standard Input in the following format:
N
Output
Print the N-th smallest good integer.
Sample Input 1
8
Sample Output 1
24
The good integers in ascending order are 0, 2, 4, 6, 8, 20, 22, 24, 26, 28, \dots.
The eighth smallest is 24, which should be printed.
Sample Input 2
133
Sample Output 2
2024
Sample Input 3
31415926535
Sample Output 3
2006628868244228
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
冷蔵庫に N 種類の材料があります。これらを材料 1、\dots、材料 N と呼びます。材料 i は Q_i グラムあります。
あなたは 2 種類の料理を作れます。料理 A は、1 人分を作るのに各材料 i (1 \leq i \leq N) が A_i グラム必要です。料理 B は、1 人分を作るのに各材料 i が B_i グラム必要です。どちらも整数人分しか作れません。
冷蔵庫にある材料のみを使って、最大で合計何人分の料理を作れますか。
制約
- 1 \leq N \leq 10
- 1 \leq Q_i \leq 10^6
- 0 \leq A_i \leq 10^6
- A_i \geq 1 であるような i が存在する。
- 0 \leq B_i \leq 10^6
- B_i \geq 1 であるような i が存在する。
- 入力値はすべて整数である。
入力
入力は以下の形式で標準入力から与えられる。
N Q_1 Q_2 \dots Q_N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
出力
最大で合計 S 人分の料理を作れるとして、整数 S を出力せよ。
入力例 1
2 800 300 100 100 200 10
出力例 1
5
この冷蔵庫には、800 グラムの材料 1 と 300 グラムの材料 2 があります。
100 グラムの材料 1 と 100 グラムの材料 2 で料理 A を 1 人分作れ、200 グラムの材料 1 と 10 グラムの材料 2 で料理 B を 1 人分作れます。
料理 A を 2 人分、料理 B を 3 人分作るのに必要な材料 1 の量は 100 \times 2 + 200 \times 3 = 800 グラム、材料 2 の量は 100 \times 2 + 10 \times 3 = 230 グラムで、いずれも冷蔵庫にある量を超えません。このようにして合計 5 人分の料理を作ることができますが、6 人分を作る方法はなく、答えは 5 です。
入力例 2
2 800 300 100 0 0 10
出力例 2
38
800 グラムの材料 1 で料理 A を 8 人分、300 グラムの材料 2 で料理 B を 30 人分、合計 38 人分作れます。
入力例 3
2 800 300 801 300 800 301
出力例 3
0
何も作れません。
入力例 4
10 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
出力例 4
222222
Score: 300 points
Problem Statement
Your refrigerator has N kinds of ingredients. Let us call them ingredient 1, \dots, ingredient N. You have Q_i grams of ingredient i.
You can make two types of dishes. To make one serving of dish A, you need A_i grams of each ingredient i (1 \leq i \leq N). To make one serving of dish B, you need B_i grams of each ingredient i. You can only make an integer number of servings of each type of dish.
Using only the ingredients in the refrigerator, what is the maximum total number of servings of dishes you can make?
Constraints
- 1 \leq N \leq 10
- 1 \leq Q_i \leq 10^6
- 0 \leq A_i \leq 10^6
- There is an i such that A_i \geq 1.
- 0 \leq B_i \leq 10^6
- There is an i such that B_i \geq 1.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N Q_1 Q_2 \dots Q_N A_1 A_2 \dots A_N B_1 B_2 \dots B_N
Output
Assuming that you can make a maximum total of S servings of dishes, print the integer S.
Sample Input 1
2 800 300 100 100 200 10
Sample Output 1
5
This refrigerator has 800 grams of ingredient 1 and 300 grams of ingredient 2.
You can make one serving of dish A with 100 grams of ingredient 1 and 100 grams of ingredient 2, and one serving of dish B with 200 grams of ingredient 1 and 10 grams of ingredient 2.
To make two servings of dish A and three servings of dish B, you need 100 \times 2 + 200 \times 3 = 800 grams of ingredient 1, and 100 \times 2 + 10 \times 3 = 230 grams of ingredient 2, neither of which exceeds the amount available in the refrigerator. In this way, you can make a total of five servings of dishes, but there is no way to make six, so the answer is 5.
Sample Input 2
2 800 300 100 0 0 10
Sample Output 2
38
You can make 8 servings of dish A with 800 grams of ingredient 1, and 30 servings of dish B with 300 grams of ingredient 2, for a total of 38 servings.
Sample Input 3
2 800 300 801 300 800 301
Sample Output 3
0
You cannot make any dishes.
Sample Input 4
10 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 1000000 0 1 2 3 4 5 6 7 8 9 9 8 7 6 5 4 3 2 1 0
Sample Output 4
222222
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 400 点
問題文
N 以下の正整数のうち、正の約数をちょうど 9 個持つものの個数を求めてください。
制約
- 1\leq N\leq 4\times 10^{12}
- 入力される数値は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N
出力
答えを出力せよ。
入力例 1
200
出力例 1
3
条件を満たす正整数は 36,100,196 の 3 個です。
入力例 2
4000000000000
出力例 2
407073
Score : 400 points
Problem Statement
Find the number of positive integers not greater than N that have exactly 9 positive divisors.
Constraints
- 1 \leq N \leq 4 \times 10^{12}
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N
Output
Print the answer.
Sample Input 1
200
Sample Output 1
3
Three positive integers 36,100,196 satisfy the condition.
Sample Input 2
4000000000000
Sample Output 2
407073
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 個の頂点と M 本の辺からなる、単純(自己ループおよび多重辺を含まない)かつ連結な無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結ぶ無向辺です。
各頂点を白または黒で塗る方法であって、下記の 2 つの条件をともに満たすものが存在するかを判定し、存在する場合はその一例を示してください。
- 1 個以上の頂点が黒で塗られている。
- すべての i = 1, 2, \ldots, K について、下記の条件が成り立つ。
- 頂点 p_i と「黒で塗られた頂点のうち頂点 p_i からの距離が最小であるもの」の距離がちょうど d_i である。
ここで、頂点 u と頂点 v の距離は、u と v を結ぶパスの辺の本数としてあり得る最小値として定義されます。
制約
- 1 \leq N \leq 2000
- N-1 \leq M \leq \min\lbrace N(N-1)/2, 2000 \rbrace
- 1 \leq u_i, v_i \leq N
- 0 \leq K \leq N
- 1 \leq p_1 \lt p_2 \lt \cdots \lt p_K \leq N
- 0 \leq d_i \leq N
- 与えられるグラフは単純かつ連結
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M K p_1 d_1 p_2 d_2 \vdots p_K d_K
出力
問題文中の条件を満たすように各頂点を白または黒で塗る方法が存在しない場合は、No を出力せよ。
存在する場合は、下記の形式の通り、1 行目に Yes と出力し、2 行目に各頂点を塗る方法を表す文字列 S を出力せよ。
ここで、S は長さ N の文字列であり、i = 1, 2, \ldots, N について S の i 文字目は、頂点 i を黒で塗るとき 1 、白で塗るとき 0 である。
Yes S
答えが複数ある場合、どれを出力しても正解とみなされる。
入力例 1
5 5 1 2 2 3 3 1 3 4 4 5 2 1 0 5 2
出力例 1
Yes 10100
例えば、頂点 1, 3 を黒に、頂点 2, 4, 5 を白に塗るという方法が、問題文中の条件を満たします。
実際、i = 1, 2, 3, 4, 5 について、頂点 i と「黒で塗られた頂点のうち頂点 i からの距離が最小であるもの」の距離を A_i で表すと、
(A_1, A_2, A_3, A_4, A_5) = (0, 1, 0, 1, 2) であり、特に、A_1 = 0, A_5 = 2 が成り立ちます。
入力例 2
5 5 1 2 2 3 3 1 3 4 4 5 5 1 1 2 1 3 1 4 1 5 1
出力例 2
No
問題文中の条件を満たすように各頂点を白または黒で塗る方法が存在しないため、No を出力します。
入力例 3
1 0 0
出力例 3
Yes 1
Score : 500 points
Problem Statement
You are given a simple connected undirected graph with N vertices and M edges (a simple graph contains no self-loop and no multi-edges).
For i = 1, 2, \ldots, M, the i-th edge connects vertex u_i and vertex v_i bidirectionally.
Determine whether there is a way to paint each vertex black or white to satisfy both of the following conditions, and show one such way if it exists.
- At least one vertex is painted black.
- For every i = 1, 2, \ldots, K, the following holds:
- the minimum distance between vertex p_i and a vertex painted black is exactly d_i.
Here, the distance between vertex u and vertex v is the minimum number of edges in a path connecting u and v.
Constraints
- 1 \leq N \leq 2000
- N-1 \leq M \leq \min\lbrace N(N-1)/2, 2000 \rbrace
- 1 \leq u_i, v_i \leq N
- 0 \leq K \leq N
- 1 \leq p_1 \lt p_2 \lt \cdots \lt p_K \leq N
- 0 \leq d_i \leq N
- The given graph is simple and connected.
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M u_1 v_1 u_2 v_2 \vdots u_M v_M K p_1 d_1 p_2 d_2 \vdots p_K d_K
Output
If there is no way to paint each vertex black or white to satisfy the conditions, print No.
Otherwise, print Yes in the first line, and a string S representing a coloring of the vertices in the second line, as shown below.
Here, S is a string of length N such that, for each i = 1, 2, \ldots, N, the i-th character of S is 1 if vertex i is painted black and 0 if white.
Yes S
If multiple solutions exist, you may print any of them.
Sample Input 1
5 5 1 2 2 3 3 1 3 4 4 5 2 1 0 5 2
Sample Output 1
Yes 10100
One way to satisfy the conditions is to paint vertices 1, 3 black and vertices 2, 4, 5 white.
Indeed, for each i = 1, 2, 3, 4, 5, let A_i denote the minimum distance between vertex i and a vertex painted black, and we have (A_1, A_2, A_3, A_4, A_5) = (0, 1, 0, 1, 2), where A_1 = 0, A_5 = 2.
Sample Input 2
5 5 1 2 2 3 3 1 3 4 4 5 5 1 1 2 1 3 1 4 1 5 1
Sample Output 2
No
There is no way to satisfy the conditions by painting each vertex black or white, so you should print No.
Sample Input 3
1 0 0
Sample Output 3
Yes 1
実行時間制限: 4 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
N 個の頂点と M 本の辺からなる単純(自己ループおよび多重辺を持たない)かつ連結な無向グラフが与えられます。
i = 1, 2, \ldots, M について、i 番目の辺は頂点 u_i と頂点 v_i を結びます。
下記の 2 つの条件をともに満たす整数列 (A_1, A_2, \ldots, A_k) を長さ k のパスと呼びます。
- すべての i = 1, 2, \dots, k について、1 \leq A_i \leq N 。
- すべての i = 1, 2, \ldots, k-1 について、頂点 A_i と頂点 A_{i+1} は辺で直接結ばれている。
空列も長さ 0 のパスとみなします。
S = s_1s_2\ldots s_N を 0 と 1 のみからなる長さ N の文字列とします。 パス A = (A_1, A_2, \ldots, A_k) が下記を満たすとき、パス A を S に関する良いパスと呼びます。
- すべての i = 1, 2, \ldots, N について、次を満たす。
- s_i = 0 ならば、A に含まれる i の個数は偶数である。
- s_i = 1 ならば、A に含まれる i の個数は奇数である。
S として考えられる文字列(すなわち、0 と 1 のみからなる長さ N の文字列)は 2^N 個ありますが、そのすべてにわたる「 S に関する良いパスのうち最短のものの長さ」の総和を出力してください。
この問題の制約下において、0 と 1 からなる長さ N のどのような文字列を S として選んでも、S に関する良いパスが少なくとも 1 つ存在することが示せます。
制約
- 2 \leq N \leq 17
- N-1 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq u_i, v_i \leq N
- 与えられるグラフは単純かつ連結
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
出力
答えを出力せよ。
入力例 1
3 2 1 2 2 3
出力例 1
14
- S = 000 のとき、空列 () は S に関する最短の良いパスであり、その長さは 0 です。
- S = 100 のとき、(1) は S に関する最短の良いパスであり、その長さは 1 です。
- S = 010 のとき、(2) は S に関する最短の良いパスであり、その長さは 1 です。
- S = 110 のとき、(1, 2) は S に関する最短の良いパスであり、その長さは 2 です。
- S = 001 のとき、(3) は S に関する最短の良いパスであり、その長さは 1 です。
- S = 101 のとき、(1, 2, 3, 2) は S に関する最短の良いパスであり、その長さは 4 です。
- S = 011 のとき、(2, 3) は S に関する最短の良いパスであり、その長さは 2 です。
- S = 111 のとき、(1, 2, 3) は S に関する最短の良いパスであり、その長さは 3 です。
よって、求める答えは 0 + 1 + 1 + 2 + 1 + 4 + 2 + 3 = 14 です。
入力例 2
5 5 4 2 2 3 1 3 2 1 1 5
出力例 2
108
Score : 500 points
Problem Statement
You are given a simple connected undirected graph with N vertices and M edges. (A graph is said to be simple if it has no multi-edges and no self-loops.)
For i = 1, 2, \ldots, M, the i-th edge connects Vertex u_i and Vertex v_i.
A sequence (A_1, A_2, \ldots, A_k) is said to be a path of length k if both of the following two conditions are satisfied:
- For all i = 1, 2, \dots, k, it holds that 1 \leq A_i \leq N.
- For all i = 1, 2, \ldots, k-1, Vertex A_i and Vertex A_{i+1} are directly connected by an edge.
An empty sequence is regarded as a path of length 0.
Let S = s_1s_2\ldots s_N be a string of length N consisting of 0 and 1. A path A = (A_1, A_2, \ldots, A_k) is said to be a good path with respect to S if the following conditions are satisfied:
- For all i = 1, 2, \ldots, N, it holds that:
- if s_i = 0, then A has even number of i's.
- if s_i = 1, then A has odd number of i's.
There are 2^N possible S (in other words, there are 2^N strings of length N consisting of 0 and 1). Find the sum of "the length of the shortest good path with respect to S" over all those S.
Under the Constraints of this problem, it can be proved that, for any string S of length N consisting of 0 and 1, there is at least one good path with respect to S.
Constraints
- 2 \leq N \leq 17
- N-1 \leq M \leq \frac{N(N-1)}{2}
- 1 \leq u_i, v_i \leq N
- The given graph is simple and connected.
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N M u_1 v_1 u_2 v_2 \vdots u_M v_M
Output
Print the answer.
Sample Input 1
3 2 1 2 2 3
Sample Output 1
14
- For S = 000, the empty sequence () is the shortest good path with respect to S, whose length is 0.
- For S = 100, (1) is the shortest good path with respect to S, whose length is 1.
- For S = 010, (2) is the shortest good path with respect to S, whose length is 1.
- For S = 110, (1, 2) is the shortest good path with respect to S, whose length is 2.
- For S = 001, (3) is the shortest good path with respect to S, whose length is 1.
- For S = 101, (1, 2, 3, 2) is the shortest good path with respect to S, whose length is 4.
- For S = 011, (2, 3) is the shortest good path with respect to S, whose length is 2.
- For S = 111, (1, 2, 3) is the shortest good path with respect to S, whose length is 3.
Therefore, the sought answer is 0 + 1 + 1 + 2 + 1 + 4 + 2 + 3 = 14.
Sample Input 2
5 5 4 2 2 3 1 3 2 1 1 5
Sample Output 2
108