A - camel Case

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 100

問題文

英大文字および英小文字からなる文字列 S が与えられます。
ここで、 S のうちちょうど 1 文字だけが英大文字であり、それ以外は全て英小文字です。
大文字が S の先頭から何文字目に登場するか出力してください。
ただし、S の先頭の文字を 1 文字目とします。

制約

  • S は英大文字および英小文字からなる長さ 2 以上 100 以下の文字列
  • S に大文字はちょうど 1 つ含まれる。

入力

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

S

出力

大文字が S の先頭から何文字目に登場するかを、整数で出力せよ。


入力例 1

aBc

出力例 1

2

aBc の先頭から 1 文字目は a , 2 文字目は B , 3 文字目は c であり、 このうち大文字は 2 文字目です。
よって、2 を出力します。


入力例 2

xxxxxxXxxx

出力例 2

7

S=xxxxxxXxxx7 文字目に、大文字である X が登場しています。よって、7 を出力します。


入力例 3

Zz

出力例 3

1

Score : 100 points

Problem Statement

You are given a string S consisting of uppercase and lowercase English letters.
Here, exactly one character of S is uppercase, and the others are all lowercase.
Find the integer x such that the x-th character of S is uppercase.
Here, the initial character of S is considered the 1-st one.

Constraints

  • S is a string of length between 2 and 100, inclusive, consisting of uppercase and lowercase English letters.
  • S has exactly one uppercase letter.

Input

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

S

Output

Print the integer x such that the x-th character of S is uppercase.


Sample Input 1

aBc

Sample Output 1

2

The 1-st character of aBc is a, the 2-nd is B, and the 3-rd is c; the 2-nd character is uppercase.
Thus, 2 should be printed.


Sample Input 2

xxxxxxXxxx

Sample Output 2

7

An uppercase letter X occurs as the 7-th character of S=xxxxxxXxxx, so 7 should be printed.


Sample Input 3

Zz

Sample Output 3

1
B - Trimmed Mean

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 200

問題文

高橋君は体操の大会に参加しています。
大会では、5N 人の審査員それぞれが高橋君の演技に対して評点をつけ、それらを元に次のように高橋君の得点が決定されます。

  • 高い評点をつけた方から順に N 人の審査員による評点を無効にする。
  • 低い評点をつけた方から順に N 人の審査員による評点を無効にする。
  • 残りの 3N 人の審査員による評点の平均点を高橋君の得点とする。

より厳密には、審査員がつけた得点の多重集合 S (|S|=5N) に対して次の操作を行って得られたものが高橋君の得点となります。

  • S の最大の要素(複数ある場合はそのうちの 1 つ)を選び、S から取り除く」という操作を N 回繰り返す。
  • S の最小の要素(複数ある場合はそのうちの 1 つ)を選び、S から取り除く」という操作を N 回繰り返す。
  • S に残った 3N 個の要素の平均を高橋君の得点とする。

高橋君の演技に対する、i 人目 (1\leq i\leq 5N) の審査員の評点は X_i 点でした。 高橋君の得点を計算してください。

制約

  • 1\leq N\leq 100
  • 0\leq X_i\leq 100
  • 入力はすべて整数

入力

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

N
X_1 X_2 \ldots X_{5N}

出力

高橋君の得点を出力せよ。
なお、真の値との絶対誤差または相対誤差が 10^{-5} 以下であれば正解として扱われる。


入力例 1

1
10 100 20 50 30

出力例 1

33.333333333333336

N=1 であるので、評点が高い方と低い方からそれぞれ 1 人ずつの審査員による評点を無効にします。
1 番高い評点をつけた審査員は 2 人目 (100 点) であるため、これを無効にします。
また、1 番低い評点をつけた審査員は 1 人目 (10 点) であるため、これも無効にします。
よって、最終的な平均点は \displaystyle\frac{20+50+30}{3}=33.333\cdots となります。

出力は、真の値との絶対誤差または相対誤差が 10^{-5} 以下であれば正解として扱われる事に注意してください。


入力例 2

2
3 3 3 4 5 6 7 8 99 100

出力例 2

5.500000000000000

N=2 であるので、評点が高い方と低い方からそれぞれ 2 人ずつの審査員による評点を無効にします。
1,2 番目に高い評点をつけた審査員は順に 10 人目 (100 点), 9 人目 (99 点) であるため、これを無効にします。
1 番低い評点をつけた審査員は 1,2,3 人目 (3 点) の 3 人がいるため、このうち 2 人を無効とします。
よって、答えは \displaystyle\frac{3+4+5+6+7+8}{6}=5.5 となります。

1 番低い評点をつけた 3 人のうちどの 2 人を無効にしたかは、答えに影響しない事に注意してください。

Score : 200 points

Problem Statement

Takahashi is participating in a gymnastic competition.
In the competition, each of 5N judges grades Takahashi's performance, and his score is determined as follows:

  • Invalidate the grades given by the N judges who gave the highest grades.
  • Invalidate the grades given by the N judges who gave the lowest grades.
  • Takahashi's score is defined as the average of the remaining 3N judges' grades.

More formally, Takahashi's score is obtained by performing the following procedure on the multiset of the judges' grades S (|S|=5N):

  • Repeat the following operation N times: choose the maximum element (if there are multiple such elements, choose one of them) and remove it from S.
  • Repeat the following operation N times: choose the minimum element (if there are multiple such elements, choose one of them) and remove it from S.
  • Takahashi's score is defined as the average of the 3N elements remaining in S.

The i-th (1\leq i\leq 5N) judge's grade for Takahashi's performance was X_i points. Find Takahashi's score.

Constraints

  • 1\leq N\leq 100
  • 0\leq X_i\leq 100
  • All values in the input are integers.

Input

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

N
X_1 X_2 \ldots X_{5N}

Output

Print Takahashi's score.
Your answer will be considered correct if the absolute or relative error from the true value is at most 10^{-5}.


Sample Input 1

1
10 100 20 50 30

Sample Output 1

33.333333333333336

Since N=1, the grade given by one judge who gave the highest grade, and one with the lowest, are invalidated.
The 2-nd judge gave the highest grade (100 points), which is invalidated.
Additionally, the 1-st judge gave the lowest grade (10 points), which is also invalidated.
Thus, the average is \displaystyle\frac{20+50+30}{3}=33.333\cdots.

Note that the output will be considered correct if the absolute or relative error from the true value is at most 10^{-5}.


Sample Input 2

2
3 3 3 4 5 6 7 8 99 100

Sample Output 2

5.500000000000000

Since N=2, the grades given by the two judges who gave the highest grades, and two with the lowest, are invalidated.
The 10-th and 9-th judges gave the highest grades (100 and 99 points, respectively), which are invalidated.
Three judges, the 1-st, 2-nd, and 3-rd, gave the lowest grade (3 points), so two of them are invalidated.
Thus, the average is \displaystyle\frac{3+4+5+6+7+8}{6}=5.5.

Note that the choice of the two invalidated judges from the three with the lowest grades does not affect the answer.

C - LRUD Instructions 2

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 300

問題文

二次元平面上に高橋君がいます。高橋君は原点から移動を N 回行いました。

N 回の移動は長さ N の文字列で表され、意味は次の通りです。

  • i 回目の高橋君の移動後の座標は、移動前の座標を (x,y) として、
    • Si 文字目が R であるとき (x+1,y)
    • Si 文字目が L であるとき (x-1,y)
    • Si 文字目が U であるとき (x,y+1)
    • Si 文字目が D であるとき (x,y-1)

N 回の移動 (始点と終点を含む) で、高橋君が同じ座標にいたことがあるかどうかを判定してください。

制約

  • 1 \leq N \leq 2\times 10^5
  • N は整数
  • SR, L, U, D のみからなる長さ N の文字列

入力

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

N
S

出力

N 回の移動 (始点と終点を含む) で、高橋君が同じ座標にいたことがあれば Yes、なければ No と出力せよ。


入力例 1

5
RLURU

出力例 1

Yes

高橋君のいる座標は (0,0)\to (1,0)\to (0,0)\to (0,1)\to (1,1)\to (1,2) と変化します。


入力例 2

20
URDDLLUUURRRDDDDLLLL

出力例 2

No

Score : 300 points

Problem Statement

Takahashi is on a two-dimensional plane. Starting from the origin, he made N moves.

The N moves are represented by a string of length N as described below:

  • Takahashi's coordinates after the i-th move are:

    • (x+1,y) if the i-th character of S is R;
    • (x-1,y) if the i-th character of S is L;
    • (x,y+1) if the i-th character of S is U; and
    • (x,y-1) if the i-th character of S is D,

    where (x,y) is his coordinates before the move.

Determine if Takahashi visited the same coordinates multiple times in the course of the N moves (including the starting and ending points).

Constraints

  • 1 \leq N \leq 2\times 10^5
  • N is an integer.
  • S is a string of length N consisting of R, L, U, and D.

Input

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

N
S

Output

Print Yes if Takahashi visited the same coordinates multiple times in the course of the N moves; print No otherwise.


Sample Input 1

5
RLURU

Sample Output 1

Yes

Takahashi's coordinates change as follows: (0,0)\to (1,0)\to (0,0)\to (0,1)\to (1,1)\to (1,2).


Sample Input 2

20
URDDLLUUURRRDDDDLLLL

Sample Output 2

No
D - Flip Cards

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

1 から N までの番号がついた N 枚のカードが一列に並んでいて、各 i\ (1\leq i < N) に対してカード i とカード i+1 が隣り合っています。 カード i の表には A_i が、裏には B_i が書かれており、最初全てのカードは表を向いています。

今から、N 枚のカードのうち好きな枚数 (0 枚でも良い) を選んで裏返すことを考えます。 裏返すカードの選び方は 2^N 通りありますが、そのうち以下の条件を満たすものの数を 998244353 で割った余りを求めてください。

  • 選んだカードを裏返した後、どの隣り合う 2 枚のカードについても、向いている面に書かれた数が相異なる。

制約

  • 1\leq N \leq 2\times 10^5
  • 1\leq A_i,B_i \leq 10^9
  • 入力は全て整数

入力

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

出力

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


入力例 1

3
1 2
4 2
3 4

出力例 1

4

裏返すカードの番号の集合を S とします。

例えば S=\{2,3\} を選ぶと、向いている面に書かれた数はカード 1 から順に 1,2,4 となるため条件を満たします。

一方 S=\{3\} を選ぶと、向いている面に書かれた数はカード 1 から順に 1,4,4 となり、カード 2 とカード 3 の数が一致するため条件を満たしません。

条件を満たす S\{\},\{1\},\{2\},\{2,3\}4 通りです。


入力例 2

4
1 5
2 6
3 7
4 8

出力例 2

16

入力例 3

8
877914575 602436426
861648772 623690081
476190629 262703497
971407775 628894325
822804784 450968417
161735902 822804784
161735902 822804784
822804784 161735902

出力例 3

48

Score : 400 points

Problem Statement

N cards, numbered 1 through N, are arranged in a line. For each i\ (1\leq i < N), card i and card (i+1) are adjacent to each other. Card i has A_i written on its front, and B_i written on its back. Initially, all cards are face up.

Consider flipping zero or more cards chosen from the N cards. Among the 2^N ways to choose the cards to flip, find the number, modulo 998244353, of such ways that:

  • when the chosen cards are flipped, for every pair of adjacent cards, the integers written on their face-up sides are different.

Constraints

  • 1\leq N \leq 2\times 10^5
  • 1\leq A_i,B_i \leq 10^9
  • All values in the input are integers.

Input

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

N
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the answer as an integer.


Sample Input 1

3
1 2
4 2
3 4

Sample Output 1

4

Let S be the set of card numbers to flip.

For example, when S=\{2,3\} is chosen, the integers written on their visible sides are 1,2, and 4, from card 1 to card 3, so it satisfies the condition.

On the other hand, when S=\{3\} is chosen, the integers written on their visible sides are 1,4, and 4, from card 1 to card 3, where the integers on card 2 and card 3 are the same, violating the condition.

Four S satisfy the conditions: \{\},\{1\},\{2\}, and \{2,3\}.


Sample Input 2

4
1 5
2 6
3 7
4 8

Sample Output 2

16

Sample Input 3

8
877914575 602436426
861648772 623690081
476190629 262703497
971407775 628894325
822804784 450968417
161735902 822804784
161735902 822804784
822804784 161735902

Sample Output 3

48
E - Find Permutation

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

1,\ldots,N の並び替えである長さ N の数列 A=(A_1,\ldots,A_N) があります。

あなたは A を知りませんが、M 個の整数の組 (X_i,Y_i) について、A_{X_i}<A_{Y_i} が成り立つことを知っています。

A を一意に特定できるかどうか判定し、できるなら A を求めてください。

制約

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • 1\leq X_i,Y_i \leq N
  • 入力は全て整数である
  • 入力に矛盾しない A が存在する

入力

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

N M
X_1 Y_1
\vdots
X_M Y_M

出力

A を一意に特定できるとき、1行目に Yes と出力し、2行目に A_1,\ldots,A_N をこの順に空白区切りで出力せよ。

A を一意に特定できないとき、No とのみ出力せよ。


入力例 1

3 2
3 1
2 3

出力例 1

Yes
3 1 2

A=(3,1,2) であると一意に特定できます。


入力例 2

3 2
3 1
3 2

出力例 2

No

A として (2,3,1),(3,2,1)2 通りが考えられます。


入力例 3

4 6
1 2
1 2
2 3
2 3
3 4
3 4

出力例 3

Yes
1 2 3 4

Score : 500 points

Problem Statement

There is a length-N sequence A=(A_1,\ldots,A_N) that is a permutation of 1,\ldots,N.

While you do not know A, you know that A_{X_i}<A_{Y_i} for M pairs of integers (X_i,Y_i).

Can A be uniquely determined? If it is possible, find A.

Constraints

  • 2 \leq N \leq 2\times 10^5
  • 1 \leq M \leq 2\times 10^5
  • 1\leq X_i,Y_i \leq N
  • All values in the input are integers.
  • There is an A consistent with the input.

Input

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

N M
X_1 Y_1
\vdots
X_M Y_M

Output

If A can be uniquely determined, print Yes in the first line. Then, print A_1,\ldots,A_N in the second line, separated by spaces.

If A cannot be uniquely determined, just print No.


Sample Input 1

3 2
3 1
2 3

Sample Output 1

Yes
3 1 2

We can uniquely determine that A=(3,1,2).


Sample Input 2

3 2
3 1
3 2

Sample Output 2

No

Two sequences (2,3,1) and (3,2,1) can be A.


Sample Input 3

4 6
1 2
1 2
2 3
2 3
3 4
3 4

Sample Output 3

Yes
1 2 3 4
F - Teleporter and Closed off

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500

問題文

N 個の都市があり、都市 1, 都市 2, \ldots, 都市 N と番号づけられています。
いくつかの異なる都市の間は一方通行のテレポーターによって移動できます。 都市 i (1\leq i\leq N) からテレポーターによって直接移動できる都市は 01 からなる長さ M の文字列 S_i によって表されます。具体的には、1\leq j\leq N に対して、

  • 1\leq j-i\leq M かつ S_i(j-i) 文字目が 1 ならば、都市 i から都市 j に直接移動できる。
  • そうでない時、都市 i から都市 j へは直接移動できない。

k=2,3,\ldots, N-1 に対して次の問題を解いてください。

テレポータを繰り返し使用することによって、都市 k を通らずに都市 1 から 都市 N へ移動できるか判定し、 できるならばそのために必要なテレポーターの使用回数の最小値を、 できないならば -1 を出力せよ。

制約

  • 3 \leq N \leq 10^5
  • 1\leq M\leq 10
  • M<N
  • S_i01 のみからなる長さ M の文字列
  • i+j>N ならば S_ij 文字目は 0
  • N,M は整数

入力

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

N M
S_1
S_2
\vdots
S_N

出力

N-2 個の整数を空白区切りで一行に出力せよ。 i (1\leq i\leq N-2) 番目には、k=i+1 に対する問題の答えを出力せよ。


入力例 1

5 2
11
01
11
10
00

出力例 1

2 3 2

テレポータによって各都市からはそれぞれ以下の都市へ直接移動する事ができます。

  • 都市 1 からは都市 2,3 へ移動できる。
  • 都市 2 からは都市 4 へ移動できる。
  • 都市 3 からは都市 4,5 へ移動できる。
  • 都市 4 からは都市 5 へ移動できる。
  • 都市 5 から移動できる都市は存在しない。

よって、都市 1 から都市 5 へ移動する方法は、

  • 経路 1 : 都市 1 \to 都市 2 \to 都市 4 \to 都市 5
  • 経路 2 : 都市 1 \to 都市 3 \to 都市 4 \to 都市 5
  • 経路 3 : 都市 1 \to 都市 3 \to 都市 5

3 つがあり、

  • 都市 2 を通らない経路は経路 2, 経路 32つであり、そのうちテレポーターの使用回数が最小となるのは経路 3 で、この時 2 回使用する。
  • 都市 3 を通らない経路は経路 1 のみであり、この時テレポーターは 3 回使用する。
  • 都市 4 を通らない経路は経路 3 のみであり、この時テレポーターは 2 回使用する。

となります。よって、2,3,2 をこの順に空白区切りで出力します。


入力例 2

6 3
101
001
101
000
100
000

出力例 2

-1 3 3 -1

都市 1 から都市 6 へ移動する方法は、都市 1 \to 都市 2 \to 都市 5 \to 都市 6 のみであるため、
k=2,5 の場合には都市 k を通らずに都市 1 から都市 6 へ移動する方法は存在せず、
k=3,4 の場合には上の方法が条件をみたし、テレポーターを 3 回使用します。

よって、-1,3,3,-1 をこの順に空白区切りで出力します。

テレポーターは一方通行であるため、 都市 3 から都市 4 へはテレポーターによって移動できますが、 都市 4 から都市 3 へは移動できず、
都市 1 \to 都市 4 \to 都市 3 \to 都市 6 のような移動はできない事に注意してください。

Score : 500 points

Problem Statement

There are N cities numbered city 1, city 2, \ldots, and city N.
There are also one-way teleporters that send you to different cities. Whether a teleporter can send you directly from city i (1\leq i\leq N) to another is represented by a length-M string S_i consisting of 0 and 1. Specifically, for 1\leq j\leq N,

  • if 1\leq j-i\leq M and the (j-i)-th character of S_i is 1, then a teleporter can send you directly from city i to city j;
  • otherwise, it cannot send you directly from city i to city j.

Solve the following problem for k=2,3,\ldots, N-1:

Can you travel from city 1 to city N without visiting city k by repeatedly using a teleporter? If you can, print the minimum number of times you need to use a teleporter; otherwise, print -1.

Constraints

  • 3 \leq N \leq 10^5
  • 1\leq M\leq 10
  • M<N
  • S_i is a string of length M consisting of 0 and 1.
  • If i+j>N, then the j-th character of S_i is 0.
  • N and M are integers.

Input

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

N M
S_1
S_2
\vdots
S_N

Output

Print (N-2) integers, separated by spaces, in a single line. The i-th (1\leq i\leq N-2) integer should be the answer to the problem for k=i+1.


Sample Input 1

5 2
11
01
11
10
00

Sample Output 1

2 3 2

A teleporter sends you

  • from city 1 to cities 2 and 3;
  • from city 2 to city 4;
  • from city 3 to cities 4 and 5;
  • from city 4 to city 5; and
  • from city 5 to nowhere.

Therefore, there are three paths to travel from city 1 to city 5:

  • path 1 : city 1 \to city 2 \to city 4 \to city 5;
  • path 2 : city 1 \to city 3 \to city 4 \to city 5; and
  • path 3 : city 1 \to city 3 \to city 5.

Among these paths,

  • two paths, path 2 and path 3, do not visit city 2. Among them, path 3 requires the minimum number of teleporter uses (twice).
  • Path 1 is the only path without city 3. It requires using a teleporter three times.
  • Path 3 is the only path without city 4. It requires using a teleporter twice.

Thus, 2, 3, and 2, separated by spaces, should be printed.


Sample Input 2

6 3
101
001
101
000
100
000

Sample Output 2

-1 3 3 -1

The only path from city 1 to city 6 is city 1 \to city 2 \to city 5 \to city 6.
For k=2,5, there is no way to travel from city 1 to city 6 without visiting city k.
For k=3,4, the path above satisfies the condition; it requires using a teleporter three times.

Thus, -1, 3, 3, and -1, separated by spaces, should be printed.

Note that a teleporter is one-way; a teleporter can send you from city 3 to city 4, but not from city 4 to city 3,
so the following path, for example, is invalid: city 1 \to city 4 \to city 3 \to city 6.

G - OR Sum

Time Limit: 8 sec / Memory Limit: 1024 MB

配点 : 600

問題文

長さ N の数列 A=(A_0,A_1,\ldots,A_{N-1})B=(B_0,B_1,\ldots,B_{N-1}) が与えられます。
また、高橋君は数列 A に対して、次の操作を好きな回数 ( 0 回でもよい) 行う事ができます。

  • A1 つ左にシフトする、すなわち、A を、A'_i=A_{(i+1)\% N} で定義される A' で置き換える。ただし、x\% N で、xN で割った余りを表す。

高橋君の目的は \displaystyle\sum_{i=0}^{N-1} (A_i|B_i) を最大化することです。ただし、x|yxy のビット毎の論理和(bitwise OR)を表します。

\displaystyle\sum_{i=0}^{N-1} (A_i|B_i) の値としてあり得る最大の値を求めてください。

ビット毎の論理和(bitwise OR)とは 1 ビットの数字 (0 または 1) の組に対して下の表で定義される演算を論理和(またはOR演算)といいます。
ビット毎に論理和を適用する演算をビット毎の論理和(bitwise OR)といいます。
x y x|y
0 0 0
0 1 1
1 0 1
1 1 1

論理和ではビット x, y の少なくとも一方が 1 の場合に結果が 1 となります。 逆に言うと、共に 0 の場合のみ結果が 0 となります。

具体例
0110 | 0101 = 0111

制約

  • 2 \leq N \leq 5\times 10^5
  • 0\leq A_i,B_i \leq 31
  • 入力はすべて整数

入力

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

N
A_0 A_1 \ldots A_{N-1}
B_0 B_1 \ldots B_{N-1}

出力

\displaystyle\sum_{i=0}^{N-1} (A_i|B_i) の値としてあり得る最大の値を出力せよ。


入力例 1

3
0 1 3
0 2 3

出力例 1

8

高橋君が一度も操作を行わなかった時、A(0,1,3) のままであり、\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(0|0)+(1|2)+(3|3)=0+3+3=6,
高橋君が 1 回操作を行った時、A=(1,3,0) となり、\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(1|0)+(3|2)+(0|3)=1+3+3=7,
高橋君が 2 回操作を行った時、A=(3,0,1) となり、\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(3|0)+(0|2)+(1|3)=3+2+3=8
となります。3 回以上操作を行った時、 A は上記のいずれかの形になるため、\displaystyle\sum_{i=0}^{N-1} (A_i|B_i) の最大値は 8 であり、8 を出力します。


入力例 2

5
1 6 1 4 3
0 6 4 0 1

出力例 2

23

最大となるのは高橋君が 3 回操作を行った時であり、この時 A=(4,3,1,6,1) となり、
\displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(4|0)+(3|6)+(1|4)+(6|0)+(1|1)=4+7+5+6+1=23 となります。

Score : 600 points

Problem Statement

There are length-N sequences A=(A_0,A_1,\ldots,A_{N-1}) and B=(B_0,B_1,\ldots,B_{N-1}).
Takahashi may perform the following operation on A any number of times (possibly zero):

  • apply a left cyclic shift to the sequence A. In other words, replace A with A' defined by A'_i=A_{(i+1)\% N}, where x\% N denotes the remainder when x is divided by N.

Takahashi's objective is to maximize \displaystyle\sum_{i=0}^{N-1} (A_i|B_i), where x|y denotes the bitwise logical sum (bitwise OR) of x and y.

Find the maximum possible \displaystyle\sum_{i=0}^{N-1} (A_i|B_i).

What is the bitwise logical sum (bitwise OR)? The logical sum (or the OR operation) is an operation on two one-bit integers (0 or 1) defined by the table below.
The bitwise logical sum (bitwise OR) is an operation of applying the logical sum bitwise.
x y x|y
0 0 0
0 1 1
1 0 1
1 1 1

The logical sum yields 1 if at least one of the bits x and y is 1. Conversely, it yields 0 only if both of them are 0.

Example
0110 | 0101 = 0111

Constraints

  • 2 \leq N \leq 5\times 10^5
  • 0\leq A_i,B_i \leq 31
  • All values in the input are integers.

Input

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

N
A_0 A_1 \ldots A_{N-1}
B_0 B_1 \ldots B_{N-1}

Output

Print the maximum possible \displaystyle\sum_{i=0}^{N-1} (A_i|B_i).


Sample Input 1

3
0 1 3
0 2 3

Sample Output 1

8

If Takahashi does not perform the operation, A remains (0,1,3), and we have \displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(0|0)+(1|2)+(3|3)=0+3+3=6;
if he performs the operation once, making A=(1,3,0), we have \displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(1|0)+(3|2)+(0|3)=1+3+3=7; and
if he performs the operation twice, making A=(3,0,1), we have \displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(3|0)+(0|2)+(1|3)=3+2+3=8.
If he performs the operation three or more times, A becomes one of the sequences above, so the maximum possible \displaystyle\sum_{i=0}^{N-1} (A_i|B_i) is 8, which should be printed.


Sample Input 2

5
1 6 1 4 3
0 6 4 0 1

Sample Output 2

23

The value is maximized if he performs the operation three times, making A=(4,3,1,6,1),
where \displaystyle\sum_{i=0}^{N-1} (A_i|B_i)=(4|0)+(3|6)+(1|4)+(6|0)+(1|1)=4+7+5+6+1=23.

Ex - Balanced Tree

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600

問題文

N 頂点の木 T が与えられます。i 番目の辺は頂点 A_iB_i を結んでいます。

次の条件をともに満たす N 頂点の根付き木 R1 つ求めてください。

  • 1 \leq x < y \leq N を満たす全ての整数の組 (x,y) に対し次が成り立つ
    • R における頂点 x,y の最小共通祖先が頂点 z であるとき、T において 頂点 x,y を結ぶ単純パス上に頂点 z が存在する
  • R において、根以外の全ての頂点 v に対し、v を根とする部分木の頂点数の 2 倍は、 v の親を根とする部分木の頂点数以下である

なお、この条件を満たす根付き木は必ず存在することが証明できます。

制約

  • 1 \leq N \leq 10^5
  • 1\leq A_i,B_i \leq N
  • 入力は全て整数である
  • 与えられるグラフは木である

入力

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

N
A_1 B_1
\vdots
A_{N-1} B_{N-1}

出力

問題文中の条件を満たす根付き木を R とする。R における頂点 i の親を頂点 p_i とする(ただし、i が根のときは p_i=-1 と定める)。
N 個の整数 p_1,\ldots,p_N をこの順に空白区切りで出力せよ。


入力例 1

4
1 2
2 3
3 4

出力例 1

2 -1 4 2

例えば、R における頂点 1,3 の最小共通祖先は頂点 2 であり、T において、頂点 1,3 を結ぶ単純パス上に頂点 2 が存在します。
また、例えば、R における頂点 4 を根とする部分木の頂点数は 2 であり、その 2 倍は、頂点 2 を根とする部分木の頂点数 4 以下です。

図


入力例 2

5
1 2
1 3
1 4
1 5

出力例 2

-1 1 1 1 1

Score : 600 points

Problem Statement

You are given a tree T with N vertices. The i-th edge connects vertices A_i and B_i.

Construct an N-vertex rooted tree R that satisfies the following conditions.

  • For all integer pairs (x,y) such that 1 \leq x < y \leq N,
    • if the lowest common ancestor in R of vertices x and y is vertex z, then vertex z in T is on the simple path between vertices x and y.
  • In R, for all vertices v except for the root, the number of vertices of the subtree rooted at v, multiplied by 2, does not exceed the number of vertices of the subtree rooted at the parent of v.

We can prove that such a rooted tree always exists.

Constraints

  • 1 \leq N \leq 10^5
  • 1\leq A_i,B_i \leq N
  • All values in the input are integers.
  • The given graph is a tree.

Input

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

N
A_1 B_1
\vdots
A_{N-1} B_{N-1}

Output

Let R be a rooted tree that satisfies the conditions in the Problem Statement. Suppose that the parent of vertex i in R is vertex p_i. (If i is the root, let p_i=-1.)
Print N integers p_1,\ldots,p_N, with spaces in between.


Sample Input 1

4
1 2
2 3
3 4

Sample Output 1

2 -1 4 2

For example, the lowest common ancestor in R of vertices 1 and 3 is vertex 2; in T, vertex 2 is on the simple path between vertices 1 and 3.
Also, for example, in R, the subtree rooted at vertex 4 has two vertices, and the number multiplied by two does not exceed the number of vertices of the subtree rooted at vertex 2, which has 4 vertices.

Figure


Sample Input 2

5
1 2
1 3
1 4
1 5

Sample Output 2

-1 1 1 1 1