E - Max Even

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

長さ N の非負整数列 A=(A_1,A_2,\ldots,A_N) が与えられます。

A の異なる 2 要素の和として表せる値の中に偶数が存在するか判定し、存在する場合その最大値を求めてください。

制約

  • 2\leq N \leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • A の要素は相異なる
  • 入力は全て整数

入力

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

N
A_1 A_2 \ldots A_N

出力

A の異なる 2 要素の和として表せる値の中に偶数が存在しない場合、-1 を出力せよ。

偶数が存在する場合、その最大値を出力せよ。


入力例 1

3
2 3 4

出力例 1

6

A の異なる 2 要素の和として表せる値は 5,6,7 です。この中に偶数は存在し、その最大値は 6 です。


入力例 2

2
1 0

出力例 2

-1

A の異なる 2 要素の和として表せる値は 1 です。この中に偶数は存在しないので、 -1 を出力してください。

Score : 300 points

Problem Statement

You are given a sequence A=(A_1,A_2,\ldots,A_N) of length N consisting of non-negative integers.

Determine if there is an even number represented as the sum of two different elements of A. If it exists, find the maximum such number.

Constraints

  • 2\leq N \leq 2\times 10^5
  • 0\leq A_i\leq 10^9
  • The elements of A are distinct.
  • All values in the input are integers.

Input

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

N
A_1 A_2 \ldots A_N

Output

Print -1 if there is no even number represented as the sum of two different elements of A.

If such an even number exists, print the maximum such number.


Sample Input 1

3
2 3 4

Sample Output 1

6

The values represented as the sum of two distinct elements of A are 5, 6, and 7. We have an even number here, and the maximum is 6.


Sample Input 2

2
1 0

Sample Output 2

-1

The value represented as the sum of two distinct elements of A is 1. We have no even number here, so -1 should be printed.

F - AtCoder Cards

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300

問題文

AtCoder社ではカードを使った 1 人ゲームが流行っています。
ゲームで使う各カードには、英小文字 1 文字または @ の文字が書かれており、いずれのカードも十分多く存在します。
ゲームは以下の手順で行います。

  1. カードを同じ枚数ずつ 2 列に並べる。
  2. @ のカードを、それぞれ a, t, c, o, d, e, r のいずれかのカードと置き換える。
  3. 2 つの列が一致していれば勝ち。そうでなければ負け。

このゲームに勝ちたいあなたは、次のようなイカサマをすることにしました。

  • 手順 1 以降の好きなタイミングで、列内のカードを自由に並び替えてよい。

手順 1 で並べられた 2 つの列を表す 2 つの文字列 S,T が与えられるので、イカサマをしてもよいときゲームに勝てるか判定してください。

制約

  • S,T は英小文字と @ からなる
  • S,T の長さは等しく 1 以上 2\times 10^5 以下

入力

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

S
T

出力

イカサマをしてもよいとき、ゲームに勝てるなら Yes、勝てないなら No と出力せよ。


入力例 1

ch@ku@ai
choku@@i

出力例 1

Yes

@ をうまく置き換えることによって、両方とも chokudai と一致させることが可能です。


入力例 2

ch@kud@i
akidu@ho

出力例 2

Yes

イカサマをし、@ をうまく置き換えることによって、両方とも chokudai と一致させることが可能です。


入力例 3

aoki
@ok@

出力例 3

No

イカサマをしても勝つことはできません。


入力例 4

aa
bb

出力例 4

No

Score : 300 points

Problem Statement

A single-player card game is popular in AtCoder Inc. Each card in the game has a lowercase English letter or the symbol @ written on it. There is plenty number of cards for each kind. The game goes as follows.

  1. Arrange the same number of cards in two rows.
  2. Replace each card with @ with one of the following cards: a, t, c, o, d, e, r.
  3. If the two rows of cards coincide, you win. Otherwise, you lose.

To win this game, you will do the following cheat.

  • Freely rearrange the cards within a row whenever you want after step 1.

You are given two strings S and T, representing the two rows you have after step 1. Determine whether it is possible to win with cheating allowed.

Constraints

  • S and T consist of lowercase English letters and @.
  • The lengths of S and T are equal and between 1 and 2\times 10^5, inclusive.

Input

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

S
T

Output

If it is possible to win with cheating allowed, print Yes; otherwise, print No.


Sample Input 1

ch@ku@ai
choku@@i

Sample Output 1

Yes

You can replace the @s so that both rows become chokudai.


Sample Input 2

ch@kud@i
akidu@ho

Sample Output 2

Yes

You can cheat and replace the @s so that both rows become chokudai.


Sample Input 3

aoki
@ok@

Sample Output 3

No

You cannot win even with cheating.


Sample Input 4

aa
bb

Sample Output 4

No
G - General Weighted Max Matching

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400

問題文

頂点に 1 から N の番号が付いた N 頂点の重み付き無向完全グラフが与えられます。頂点 i と頂点 j\ (i< j) を結ぶ辺の重みは D_{i,j} です。

以下の条件を満たすように何本かの辺を選ぶとき、選んだ辺の重みの総和としてあり得る最大値を求めてください。

  • 選んだ辺の端点はどの 2 個も相異なる。

制約

  • 2\leq N\leq 16
  • 1\leq D_{i,j} \leq 10^9
  • 入力される数値は全て整数

入力

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

N 
D_{1,2} D_{1,3} \ldots D_{1,N}
D_{2,3} \ldots D_{2,N}
\vdots
D_{N-1,N}

出力

答えを整数として出力せよ。


入力例 1

4
1 5 4
7 8
6

出力例 1

13

頂点 1 と頂点 3 を結ぶ辺、頂点 2 と頂点 4 を結ぶ辺を選ぶと、辺の重みの総和が 5+8=13 となります。

これが達成可能な最大値であることが示せます。


入力例 2

3
1 2
3

出力例 2

3

N が奇数の場合もあります。


入力例 3

16
5 6 5 2 1 7 9 7 2 5 5 2 4 7 6
8 7 7 9 8 1 9 6 10 8 8 6 10 3
10 5 8 1 10 7 8 4 8 6 5 1 10
7 4 1 4 5 4 5 10 1 5 1 2
2 9 9 7 6 2 2 8 3 5 2
9 10 3 1 1 2 10 7 7 5
10 6 1 8 9 3 2 4 2
10 10 8 9 2 10 7 9
5 8 8 7 5 8 2
4 2 2 6 8 3
2 7 3 10 3
5 7 10 3
8 5 7
9 1
4

出力例 3

75

Score : 400 points

Problem Statement

You are given a weighted undirected complete graph with N vertices numbered from 1 to N. The edge connecting vertices i and j (i< j) has a weight of D_{i,j}.

When choosing some number of edges under the following condition, find the maximum possible total weight of the chosen edges.

  • The endpoints of the chosen edges are pairwise distinct.

Constraints

  • 2\leq N\leq 16
  • 1\leq D_{i,j} \leq 10^9
  • All input values are integers.

Input

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

N 
D_{1,2} D_{1,3} \ldots D_{1,N}
D_{2,3} \ldots D_{2,N}
\vdots
D_{N-1,N}

Output

Print the answer as an integer.


Sample Input 1

4
1 5 4
7 8
6

Sample Output 1

13

If you choose the edge connecting vertices 1 and 3, and the edge connecting vertices 2 and 4, the total weight of the edges is 5+8=13.

It can be shown that this is the maximum achievable value.


Sample Input 2

3
1 2
3

Sample Output 2

3

N can be odd.


Sample Input 3

16
5 6 5 2 1 7 9 7 2 5 5 2 4 7 6
8 7 7 9 8 1 9 6 10 8 8 6 10 3
10 5 8 1 10 7 8 4 8 6 5 1 10
7 4 1 4 5 4 5 10 1 5 1 2
2 9 9 7 6 2 2 8 3 5 2
9 10 3 1 1 2 10 7 7 5
10 6 1 8 9 3 2 4 2
10 10 8 9 2 10 7 9
5 8 8 7 5 8 2
4 2 2 6 8 3
2 7 3 10 3
5 7 10 3
8 5 7
9 1
4

Sample Output 3

75
H - Isolation

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425

問題文

最初 N 頂点 0 辺の無向グラフがあり、各頂点には 1 から N まで番号がついています。
Q 個のクエリが与えられるので、順に処理し、各クエリの後における「他のどの頂点とも辺で結ばれていない頂点」の数を出力してください。

i 個目のクエリは \mathrm{query}_i であり、各クエリは次の 2 種類いずれかです。

  • 1 u v: 頂点 u と頂点 v を辺で結ぶ。このクエリが与えられる直前の時点で、頂点 u と頂点 v は辺で結ばれていない事が保証される。

  • 2 v : 頂点 v と他の頂点を結ぶ辺をすべて削除する。(頂点 v 自体は削除しない。)

制約

  • 2 \leq N\leq 3\times 10^5
  • 1 \leq Q\leq 3\times 10^5
  • 1 番目の種類のクエリにおいて、1\leq u,v\leq N, u\neq v
  • 2 番目の種類のクエリにおいて、1\leq v\leq N
  • 1 番目の種類のクエリの直前の時点で、そのクエリの u,v について頂点 u と頂点 v は辺で結ばれていない。
  • 入力はすべて整数

入力

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

N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

出力

Q 行出力せよ。
i 行目 (1\leq i\leq Q) には、i 個目のクエリを処理した後の「他のどの頂点とも辺で結ばれていない頂点」の数を出力せよ。


入力例 1

3 7
1 1 2
1 1 3
1 2 3
2 1
1 1 2
2 2
1 1 2

出力例 1

1
0
0
1
0
3
1

1 個目のクエリの後で、頂点 1 と頂点 2 は互いに結ばれており、頂点 3 のみが他のどの頂点とも辺で結ばれていません。
よって、1 行目には 1 を出力します。

また、3 個目のクエリの後でどの相異なる 2 頂点の間も辺で結ばれていますが、4 個目のクエリによって、 頂点 1 と他の頂点を結ぶ辺、すなわち 頂点 1 と頂点 2 を結ぶ辺および頂点 1 と頂点 3 を結ぶ辺が削除されます。 この結果として、頂点 2 と頂点 3 は互いに結ばれているが、頂点 1 は他のどの頂点とも辺で結ばれていない状態となります。
よって、3 行目には 0 を、4 行目には 1 を出力します。


入力例 2

2 1
2 1

出力例 2

2

2 番目の種類のクエリを行う直前の時点で、すでにその頂点と他の頂点を結ぶ辺が 1 本も存在しないこともあります。

Score : 425 points

Problem Statement

There is an undirected graph with N vertices numbered 1 through N, and initially with 0 edges.
Given Q queries, process them in order. After processing each query, print the number of vertices that are not connected to any other vertices by an edge.

The i-th query, \mathrm{query}_i, is of one of the following two kinds.

  • 1 u v: connect vertex u and vertex v with an edge. It is guaranteed that, when this query is given, vertex u and vertex v are not connected by an edge.

  • 2 v: remove all edges that connect vertex v and the other vertices. (Vertex v itself is not removed.)

Constraints

  • 2 \leq N\leq 3\times 10^5
  • 1 \leq Q\leq 3\times 10^5
  • For each query of the first kind, 1\leq u,v\leq N and u\neq v.
  • For each query of the second kind, 1\leq v\leq N.
  • Right before a query of the first kind is given, there is no edge between vertices u and v.
  • All values in the input are integers.

Input

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

N Q
\mathrm{query}_1
\mathrm{query}_2
\vdots
\mathrm{query}_Q

Output

Print Q lines.
The i-th line (1\leq i\leq Q) should contain the number of vertices that are not connected to any other vertices by an edge.


Sample Input 1

3 7
1 1 2
1 1 3
1 2 3
2 1
1 1 2
2 2
1 1 2

Sample Output 1

1
0
0
1
0
3
1

After the first query, vertex 1 and vertex 2 are connected to each other by an edge, but vertex 3 is not connected to any other vertices.
Thus, 1 should be printed in the first line.

After the third query, all pairs of different vertices are connected by an edge.
However, the fourth query asks to remove all edges that connect vertex 1 and the other vertices, specifically to remove the edge between vertex 1 and vertex 2, and another between vertex 1 and vertex 3. As a result, vertex 2 and vertex 3 are connected to each other, while vertex 1 is not connected to any other vertices by an edge.
Thus, 0 and 1 should be printed in the third and fourth lines, respectively.


Sample Input 2

2 1
2 1

Sample Output 2

2

When the query of the second kind is given, there may be no edge that connects that vertex and the other vertices.

I - Problem where +s Separate Digits

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 500

問題文

1 から 9 までの数字のみで構成された文字列 S が与えられます。
この文字列 S から、以下の操作によって式 T を作ります。

  • はじめ、 T=S であるとする。
  • 各要素が 1 以上 |S|-1 以下の整数である、値に重複のない集合 A を選ぶ。なお、 A が空集合であってもよい。
  • A の全ての要素 x について、 x の降順に以下の操作を行う。
    • Tx 文字目と x+1 文字目の間に、 + を挿入する。

例えば、 S= 1234A= \lbrace 2,3 \rbrace であるとき、 T= 12+3+4 となります。

この操作によって得られる T としてあり得る全ての式に対して、式を計算したときの値の総和を 998244353 で割った余りを求めてください。

制約

  • 1 \le |S| \le 2 \times 10^5
  • S1, 2, 3, 4, 5, 6, 7, 8, 9 のみからなる。

入力

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

S

出力

答えを整数として出力せよ。


入力例 1

1234

出力例 1

1736

T としてあり得るものは 1234, 123+4, 12+34, 12+3+4, 1+234, 1+23+4, 1+2+34, 1+2+3+48 つです。
これらを計算した値の総和は 1736 です。


入力例 2

1

出力例 2

1

S の長さが 1 であることもあります。この場合、 A として指定可能なのは空集合のみです。


入力例 3

31415926535897932384626433832795

出力例 3

85607943

答えを 998244353 で割った余りを求めてください。

Score : 500 points

Problem Statement

Given is a string S consisting of digits from 1 through 9.
From this string S, let us make a formula T by the following operations.

  • Initially, let T=S.
  • Choose a (possibly empty) set A of different integers where each element is between 1 and |S|-1 (inclusive).
  • For each element x in descending order, do the following.
    • Insert a + between the x-th and (x+1)-th characters of T.

For example, when S= 1234 and A= \lbrace 2,3 \rbrace, we will have T= 12+3+4.

Consider evaluating all possible formulae T obtained by these operations. Find the sum, modulo 998244353, of the evaluations.

Constraints

  • 1 \le |S| \le 2 \times 10^5
  • S consists of 1, 2, 3, 4, 5, 6, 7, 8, and 9.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer.


Sample Input 1

1234

Sample Output 1

1736

There are eight formulae that can be obtained as T: 1234, 123+4, 12+34, 12+3+4, 1+234, 1+23+4, 1+2+34, and 1+2+3+4.
The sum of the evaluations of these formulae is 1736.


Sample Input 2

1

Sample Output 2

1

S may have a length of 1, in which case the only possible choice for A is the empty set.


Sample Input 3

31415926535897932384626433832795

Sample Output 3

85607943

Be sure to find the sum modulo 998244353.