E - One Time Swap

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

配点 : 350

問題文

文字列 S が与えられます。次の操作を ちょうど 1 行った後の文字列としてあり得るものがいくつあるか求めてください。

  • S の長さを N とする。 1\leq i<j\leq N をみたす整数の組 (i,j) を選び、Si 文字目と j 文字目を入れ替える。

なお、この問題の制約下で操作を必ず行うことができることが証明できます。

制約

  • S は英小文字からなる長さ 2 以上 10^6 以下の文字列

入力

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

S

出力

S に対して問題文中の操作をちょうど 1 回行った後の文字列としてあり得るものの個数を出力せよ。


入力例 1

abc

出力例 1

3

S の長さは 3 であるため、1\leq i<j\leq 3 をみたす整数の組 (i,j) としては、 (1,2), (1,3), (2,3)3 通りが考えられます。

  • S1 文字目と 2 文字目を入れ替えた時、Sbac となります。
  • S1 文字目と 3 文字目を入れ替えた時、Scba となります。
  • S2 文字目と 3 文字目を入れ替えた時、Sacb となります。

よって、abc に対して操作を行った後の文字列としては、bac, cba, acb3 つがあり得るため、3 を出力します。


入力例 2

aaaaa

出力例 2

1

どの 2 つの文字を入れ替えても Saaaaa のままです。よって、操作後の文字列としてあり得るものは 1 つです。

Points: 350 points

Problem Statement

You are given a string S. Find the number of strings that can result from performing the following operation exactly once.

  • Let N be the length of S. Choose a pair of integers (i,j) such that 1\leq i<j\leq N and swap the i-th and j-th characters of S.

It can be proved that you can always perform it under the constraints of this problem.

Constraints

  • S is a string of length between 2 and 10^6, inclusive, consisting of lowercase English letters.

Input

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

S

Output

Print the number of strings that can result from performing the operation in the problem statement exactly once on S.


Sample Input 1

abc

Sample Output 1

3

The length of S is 3, so 1\leq i<j\leq 3 is satisfied by three pairs of integers (i,j): (1,2), (1,3), and (2,3).

  • Swapping the 1-st and 2-nd characters of S results in S being bac.
  • Swapping the 1-st and 3-rd characters of S results in S being cba.
  • Swapping the 2-nd and 3-rd characters of S results in S being acb.

Therefore, the operation on abc results in one of the three strings: bac, cba, and acb, so print 3.


Sample Input 2

aaaaa

Sample Output 2

1

Swapping any two characters results in S remaining aaaaa. Thus, only one string can result from the operation.

F - Many Balls

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

配点 : 300

問題文

空の箱があります。
髙橋君は以下の 2 種類の魔法を好きな順番で好きな回数使えます。

  • 魔法 A :箱の中にボールを 1 つ増やす
  • 魔法 B :箱の中のボールの数を 2 倍にする

合計 \mathbf{120} 回以内の魔法で、箱の中のボールの数をちょうど N 個にする方法を 1 つ教えてください。
なお、与えられた制約のもとで条件を満たす方法が必ず存在することが示せます。

魔法以外の方法でボールの数を変化させることはできません。

制約

  • 1 \leq N \leq 10^{18}
  • 入力は全て整数

入力

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

N

出力

A , B のみからなる文字列 S を出力せよ。
Si 文字目が A ならば、髙橋君が i 回目に使う魔法が魔法 A であることを表し、B ならば魔法 B であることを表す。

S の長さは \mathbf{120} 以下でなければならない。


入力例 1

5

出力例 1

AABA

ボールの数は、0 \xrightarrow{A} 1\xrightarrow{A} 2 \xrightarrow{B}4\xrightarrow{A} 5 と変化します。
AAAAA などの答えも正解になります。


入力例 2

14

出力例 2

BBABBAAAB

ボールの数は、0 \xrightarrow{B} 0 \xrightarrow{B} 0 \xrightarrow{A}1 \xrightarrow{B} 2 \xrightarrow{B} 4 \xrightarrow{A}5 \xrightarrow{A}6 \xrightarrow{A} 7 \xrightarrow{B}14 と変化します。
S の長さを最小化する必要はありません。

Score : 300 points

Problem Statement

We have an empty box.
Takahashi can cast the following two spells any number of times in any order.

  • Spell A: puts one new ball into the box.
  • Spell B: doubles the number of balls in the box.

Tell us a way to have exactly N balls in the box with at most \mathbf{120} casts of spells.
It can be proved that there always exists such a way under the Constraints given.

There is no way other than spells to alter the number of balls in the box.

Constraints

  • 1 \leq N \leq 10^{18}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N

Output

Print a string S consisting of A and B. The i-th character of S should represent the spell for the i-th cast.

S must have at most \mathbf{120} characters.


Sample Input 1

5

Sample Output 1

AABA

This changes the number of balls as follows: 0 \xrightarrow{A} 1\xrightarrow{A} 2 \xrightarrow{B}4\xrightarrow{A} 5.
There are also other acceptable outputs, such as AAAAA.


Sample Input 2

14

Sample Output 2

BBABBAAAB

This changes the number of balls as follows: 0 \xrightarrow{B} 0 \xrightarrow{B} 0 \xrightarrow{A}1 \xrightarrow{B} 2 \xrightarrow{B} 4 \xrightarrow{A}5 \xrightarrow{A}6 \xrightarrow{A} 7 \xrightarrow{B}14.
It is not required to minimize the length of S.

G - Set Menu

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

配点 : 400

問題文

AtCoder 食堂では N 種類の主菜と M 種類の副菜が提供されており、i 種類目の主菜の価格は A_ij 種類目の副菜の価格は B_j です。 AtCoder 食堂では新たに定食のメニューを設けることを検討しています。 定食は 1 種類の主菜と 1 種類の副菜から構成され、主菜と副菜の価格の和を s としたとき、定食の価格は \min(s,P) です。 ここで、P は入力で与えられる定数です。

定食を構成する主菜と副菜の選び方は NM 通りありますが、それらすべてに対する定食の価格の総和を求めてください。

制約

  • 1\leq N,M \leq 2\times 10^5
  • 1\leq A_i,B_j \leq 10^8
  • 1\leq P \leq 2\times 10^8
  • 入力は全て整数

入力

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

N M P
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

出力

答えを整数として出力せよ。 なお、本問題の制約下では、答えは 64 bit 符号付き整数型の範囲に収まることが証明できる。


入力例 1

2 2 7
3 5
6 1

出力例 1

24
  • 1 種類目の主菜と 1 種類目の副菜を選んだ場合、定食の価格は \min(3+6,7)=7 です。
  • 1 種類目の主菜と 2 種類目の副菜を選んだ場合、定食の価格は \min(3+1,7)=4 です。
  • 2 種類目の主菜と 1 種類目の副菜を選んだ場合、定食の価格は \min(5+6,7)=7 です。
  • 2 種類目の主菜と 2 種類目の副菜を選んだ場合、定食の価格は \min(5+1,7)=6 です。

よって、答えは 7+4+7+6=24 です。


入力例 2

1 3 2
1
1 1 1

出力例 2

6

入力例 3

7 12 25514963
2436426 24979445 61648772 23690081 33933447 76190629 62703497
11047202 71407775 28894325 31963982 22804784 50968417 30302156 82631932 61735902 80895728 23078537 7723857

出力例 3

2115597124

Score : 400 points

Problem Statement

AtCoder cafeteria offers N main dishes and M side dishes. The price of the i-th main dish is A_i, and that of the j-th side dish is B_j. The cafeteria is considering introducing a new set meal menu. A set meal consists of one main dish and one side dish. Let s be the sum of the prices of the main dish and the side dish, then the price of the set meal is \min(s,P). Here, P is a constant given in the input.

There are NM ways to choose a main dish and a side dish for a set meal. Find the total price of all these set meals.

Constraints

  • 1\leq N,M \leq 2\times 10^5
  • 1\leq A_i,B_j \leq 10^8
  • 1\leq P \leq 2\times 10^8
  • All input values are integers.

Input

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

N M P
A_1 A_2 \dots A_N
B_1 B_2 \dots B_M

Output

Print the answer as an integer. Under the constraints of this problem, it can be proved that the answer fits into a 64-bit signed integer.


Sample Input 1

2 2 7
3 5
6 1

Sample Output 1

24
  • If you choose the first main dish and the first side dish, the price of the set meal is \min(3+6,7)=7.
  • If you choose the first main dish and the second side dish, the price of the set meal is \min(3+1,7)=4.
  • If you choose the second main dish and the first side dish, the price of the set meal is \min(5+6,7)=7.
  • If you choose the second main dish and the second side dish, the price of the set meal is \min(5+1,7)=6.

Thus, the answer is 7+4+7+6=24.


Sample Input 2

1 3 2
1
1 1 1

Sample Output 2

6

Sample Input 3

7 12 25514963
2436426 24979445 61648772 23690081 33933447 76190629 62703497
11047202 71407775 28894325 31963982 22804784 50968417 30302156 82631932 61735902 80895728 23078537 7723857

Sample Output 3

2115597124
H - Insert or Erase

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

配点 : 475

問題文

長さ N の数列 A=(A_1,\ldots,A_N) が与えられます。A の要素は相異なります。

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

  • 1 x yA 内の要素 x の直後に y を挿入する。このクエリが与えられる時点で、A には x が存在することが保証される。
  • 2 xA 内の要素 x を削除する。このクエリが与えられる時点で、A には x が存在することが保証される。

各クエリの処理後、A は空でなく、要素は相異なることが保証されます。

全てのクエリを処理した後の A を出力してください。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq Q \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • A_i \neq A_j
  • 1 種類目のクエリにおいて、1 \leq x,y \leq 10^9
  • 1 種類目のクエリが与えられる時点で、A には x が存在する
  • 2 種類目のクエリにおいて、1 \leq x \leq 10^9
  • 2 種類目のクエリが与えられる時点で、A には x が存在する
  • 各クエリの処理後、A は空でなく、要素は相異なる
  • 入力は全て整数である

入力

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

N
A_1 \ldots A_N
Q
\mathrm{Query}_1
\vdots
\mathrm{Query}_Q

ここで \mathrm{Query}_ii 番目のクエリを表し、次の形式で与えられる。

1 x y
2 x

出力

全てのクエリを処理したあとの数列 A(A_1,\ldots,A_K) とする。A_1,\ldots,A_K をこの順に空白区切りで出力せよ。


入力例 1

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

出力例 1

4 5 1 3

クエリは次のように処理されます。

  • 最初 A=(2,1,4,3) である。
  • 1 番目のクエリにより 1 を削除する。A=(2,4,3) となる。
  • 2 番目のクエリにより、4 の直後に 5 を挿入する。A=(2,4,5,3) となる。
  • 3 番目のクエリにより 2 を削除する。A=(4,5,3) となる。
  • 4 番目のクエリにより、5 の直後に 1 を挿入する。A=(4,5,1,3) となる。

入力例 2

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

出力例 2

5 1 7 2 3

Score: 475 points

Problem Statement

You are given a sequence A=(A_1,\ldots,A_N) of length N. The elements of A are distinct.

Process Q queries in the order they are given. Each query is of one of the following two types:

  • 1 x y : Insert y immediately after the element x in A. It is guaranteed that x exists in A when this query is given.
  • 2 x : Remove the element x from A. It is guaranteed that x exists in A when this query is given.

It is guaranteed that after processing each query, A will not be empty, and its elements will be distinct.

Print A after processing all the queries.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq Q \leq 2\times 10^5
  • 1 \leq A_i \leq 10^9
  • A_i \neq A_j
  • For queries of the first type, 1 \leq x,y \leq 10^9.
  • When a query of the first type is given, x exists in A.
  • For queries of the second type, 1 \leq x \leq 10^9.
  • When a query of the second type is given, x exists in A.
  • After processing each query, A is not empty, and its elements are distinct.
  • All input values are integers.

Input

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

N 
A_1 \ldots A_N
Q
\mathrm{Query}_1
\vdots 
\mathrm{Query}_Q

Here, \mathrm{Query}_i represents the i-th query and is given in one of the following formats:

1 x y
2 x 

Output

Let A=(A_1,\ldots,A_K) be the sequence after processing all the queries. Print A_1,\ldots,A_K in this order, separated by spaces.


Sample Input 1

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

Sample Output 1

4 5 1 3

The queries are processed as follows:

  • Initially, A=(2,1,4,3).
  • The first query removes 1, making A=(2,4,3).
  • The second query inserts 5 immediately after 4, making A=(2,4,5,3).
  • The third query removes 2, making A=(4,5,3).
  • The fourth query inserts 1 immediately after 5, making A=(4,5,1,3).

Sample Input 2

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

Sample Output 2

5 1 7 2 3
I - Fuel Round Trip

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

配点 : 550

問題文

数直線上の座標 0 から座標 X_N まで行き、折り返して座標 0 まで帰ってくる計画を立てています。ただし、往路では正の方向、復路では負の方向にしか進めません。
移動は車で行います。車は距離 1 進むごとに 1 リットルの燃料を消費します。燃料は H リットルまで所持することができ、燃料を所持していない状態で進むことはできません。
i = 1, 2, \ldots, N-1 について、座標 X_i にはガソリンスタンドがあり、P_i 円払うと F_i リットルの燃料が得られます。ただし、H リットルを超えて燃料を所持することはできません。より厳密には、x リットルの燃料を持っているときに座標 X_i にあるガソリンスタンドを使うと P_i 円を払う必要があり、持っている燃料は \min(x + F_i, H) リットルとなります。 各ガソリンスタンドは、往路と復路で合わせて 1 回までしか使うことができません。
はじめに燃料を H リットル所持しているとき、この計画を達成することができるか判定し、可能ならば必要な金額の最小値を求めてください。

制約

  • 1 \leq N, H \leq 300
  • 0 < X_1 < X_2 < \ldots < X_N \leq 10^5
  • 1 \leq P_i \leq 10^5
  • 1 \leq F_i \leq H
  • 入力される数値はすべて整数

入力

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

N H
X_1 X_2 \ldots X_N
P_1 F_1
P_2 F_2
\vdots
P_{N-1} F_{N-1}

出力

計画を達成できる場合は必要な金額の最小値を、できない場合は -1 を出力せよ。


入力例 1

4 10
2 5 9 11
8 10
5 8
4 9

出力例 1

9

往路で座標 5 の、復路で座標 9 のガソリンスタンドを用いることにより合計 9 円払うことで計画を達成することができます。
計画を達成するためにかかる金額を 8 円以下にすることはできません。往路と復路で同じガソリンスタンドを使うことができないことに注意してください。


入力例 2

1 1
100000

出力例 2

-1

入力例 3

5 20
4 13 16 18 23
1 16
2 8
4 11
8 13

出力例 3

13

Score : 550 points

Problem Statement

You are planning to travel from coordinate 0 to coordinate X_N on a number line, then turn around and return to coordinate 0. Here, you can only move in the positive direction on the outbound trip and in the negative direction on the return trip.
You will travel by car. The car consumes one liter of fuel for every unit distance it travels. You can carry up to H liters of fuel and cannot move without fuel.
For each i = 1, 2, \ldots, N-1, there is a gas station at coordinate X_i, where you can get F_i liters of fuel for P_i yen. However, you cannot carry more than H liters of fuel. More precisely, if you have x liters of fuel and use the gas station at coordinate X_i, you must pay P_i yen, and your amount of fuel becomes \min(x + F_i, H) liters. Each gas station can be used at most once in total during the round trip.
Determine if you can achieve this plan when you initially have H liters of fuel, and if it is possible, find the minimum amount of money required.

Constraints

  • 1 \leq N, H \leq 300
  • 0 < X_1 < X_2 < \ldots < X_N \leq 10^5
  • 1 \leq P_i \leq 10^5
  • 1 \leq F_i \leq H
  • All input values are integers.

Input

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

N H
X_1 X_2 \ldots X_N
P_1 F_1
P_2 F_2
\vdots
P_{N-1} F_{N-1}

Output

If the plan can be achieved, print the minimum amount of money required; otherwise, print -1.


Sample Input 1

4 10
2 5 9 11
8 10
5 8
4 9

Sample Output 1

9

You can achieve the plan by using the gas station at coordinate 5 on the outbound trip and the one at coordinate 9 on the return trip, paying a total of 9 yen.
It is impossible to achieve the plan by paying 8 yen or less. Note that you cannot use the same gas station on both the outbound and return trips.


Sample Input 2

1 1
100000

Sample Output 2

-1

Sample Input 3

5 20
4 13 16 18 23
1 16
2 8
4 11
8 13

Sample Output 3

13