E - Leftover Recipes

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

配点 : 300

問題文

冷蔵庫に N 種類の材料があります。これらを材料 1\dots、材料 N と呼びます。材料 iQ_i グラムあります。

あなたは 2 種類の料理を作れます。料理 A は、1 人分を作るのに各材料 i (1 \leq i \leq N)A_i グラム必要です。料理 B は、1 人分を作るのに各材料 iB_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 グラムの材料 1300 グラムの材料 2 があります。

100 グラムの材料 1100 グラムの材料 2 で料理 A を 1 人分作れ、200 グラムの材料 110 グラムの材料 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
F - Index × A(Continuous ver.)

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

配点 : 300

問題文

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

長さ MA の連続部分列 B=(B_1,B_2,\dots,B_M) に対する、\displaystyle \sum_{i=1}^{M} i \times B_i の最大値を求めてください。

注記

数列の連続部分列とは、数列の先頭から 0 個以上、末尾から 0 個以上の要素を削除して得られる列のことをいいます。

例えば (2, 3)(1, 2, 3)(1, 2, 3, 4) の連続部分列ですが、(1, 3)(3,2,1)(1, 2, 3, 4) の連続部分列ではありません。

制約

  • 1 \le M \le N \le 2 \times 10^5
  • - 2 \times 10^5 \le A_i \le 2 \times 10^5
  • 入力は全て整数。

入力

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

N M
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

4 2
5 4 -1 8

出力例 1

15

B=(A_3,A_4) とした場合、\displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15 となります。16 以上の値を達成することはできないため、解は 15 です。

B=(A_1,A_4) などを選ぶことができないことに注意してください。


入力例 2

10 4
-3 1 -4 1 -5 9 -2 6 -5 3

出力例 2

31

Score : 300 points

Problem Statement

You are given an integer sequence A=(A_1,A_2,\dots,A_N) of length N.

Find the maximum value of \displaystyle \sum_{i=1}^{M} i \times B_i for a contiguous subarray B=(B_1,B_2,\dots,B_M) of A of length M.

Notes

A contiguous subarray of a number sequence is a sequence that is obtained by removing 0 or more initial terms and 0 or more final terms from the original number sequence.

For example, (2, 3) and (1, 2, 3) are contiguous subarrays of (1, 2, 3, 4), but (1, 3) and (3,2,1) are not contiguous subarrays of (1, 2, 3, 4).

Constraints

  • 1 \le M \le N \le 2 \times 10^5
  • - 2 \times 10^5 \le A_i \le 2 \times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

4 2
5 4 -1 8

Sample Output 1

15

When B=(A_3,A_4), we have \displaystyle \sum_{i=1}^{M} i \times B_i = 1 \times (-1) + 2 \times 8 = 15. Since it is impossible to achieve 16 or a larger value, the solution is 15.

Note that you are not allowed to choose, for instance, B=(A_1,A_4).


Sample Input 2

10 4
-3 1 -4 1 -5 9 -2 6 -5 3

Sample Output 2

31
G - Range Count Query

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

配点 : 400

問題文

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

以下の形式で与えられる Q 個のクエリに答えてください。

  • 整数 L,R,X が与えられる。 A_L, \ldots,A_R のうち、値が X に等しいものの個数を求めよ。

制約

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq N
  • 1 \leq Q \leq 2\times 10^5
  • 各クエリについて、 1\le L \leq R \leq N, 1 \leq X \leq N
  • 入力は全て整数

入力

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

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

ただし、\mathrm{Query}_ii 個目のクエリを表す。

各クエリは以下の形式で与えられる。

L R X

出力

Q 行出力せよ。i 行目には、i 個目のクエリに対する答えを出力せよ。


入力例 1

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

出力例 1

2
0
0
1

1 個目のクエリでは、 (A_1,A_2,A_3,A_4,A_5) =(3,1,4,1,5) のうち値が 1 に等しいものの個数は 2 です。

2 個目のクエリでは、 (A_2,A_3,A_4) =(1,4,1) のうち値が 3 に等しいものの個数は 0 です。

Score : 400 points

Problem Statement

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

Answer Q queries given in the following format.

  • You are given integers L, R, and X. Find the number of elements among A_L, \ldots, A_R whose values are equal to X.

Constraints

  • 1 \leq N \leq 2\times 10^5
  • 1 \leq A_i \leq N
  • 1 \leq Q \leq 2\times 10^5
  • 1\le L \leq R \leq N, 1 \leq X \leq N, for each query.
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Here, \mathrm{Query}_i represents the i-th query.

Each query is in the following format:

L R X

Output

Print Q lines, the i-th of which contains the answer to the i-th query.


Sample Input 1

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

Sample Output 1

2
0
0
1

In the first query, two of (A_1,A_2,A_3,A_4,A_5) =(3,1,4,1,5) have values equal to 1.

In the second query, zero of (A_2,A_3,A_4) =(1,4,1) have values equal to 3.

H - Count Simple Paths

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

配点 : 500

問題文

頂点に 1 から N の番号が、辺に 1 から M の番号がついた N 頂点 M 辺の単純無向グラフが与えられます。辺 i は頂点 u_i と頂点 v_i を結んでいます。また、各頂点の次数は 10 以下です。
頂点 1 を始点とする単純パス(同じ頂点を複数回通らないパス)の個数を K とします。\min(K, 10^6) を出力してください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq \min \left(2 \times 10^5, \frac{N(N-1)}{2}\right)
  • 1 \leq u_i, v_i \leq N
  • 入力で与えられるグラフは単純グラフ
  • 入力で与えられるグラフの頂点の次数はすべて 10 以下
  • 入力される値は全て整数

入力

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

N M
u_1 v_1
u_2 v_2
\vdots
u_M v_M

出力

答えを出力せよ。


入力例 1

4 2
1 2
2 3

出力例 1

3

条件を満たすパスは次の 3 個です。(長さが 0 のパスも数えるのに注意してください。)

  • 頂点 1
  • 頂点 1, 頂点 2
  • 頂点 1, 頂点 2, 頂点 3

入力例 2

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

出力例 2

16

入力例 3

8 21
2 6
1 3
5 6
3 8
3 6
4 7
4 6
3 4
1 5
2 4
1 2
2 7
1 4
3 5
2 5
2 3
4 5
3 7
6 7
5 7
2 8

出力例 3

2023

Score : 500 points

Problem Statement

You are given a simple undirected graph with N vertices numbered 1 to N and M edges numbered 1 to M. Edge i connects vertex u_i and vertex v_i. The degree of each vertex is at most 10.
Let K be the number of simple paths (paths without repeated vertices) starting from vertex 1. Print \min(K, 10^6).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 0 \leq M \leq \min \left(2 \times 10^5, \frac{N(N-1)}{2}\right)
  • 1 \leq u_i, v_i \leq N
  • The given graph is simple.
  • The degree of each vertex in the given graph is at most 10.
  • 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

Output

Print the answer.


Sample Input 1

4 2
1 2
2 3

Sample Output 1

3

We have the following three paths that count. (Note that a path of length 0 also counts.)

  • Vertex 1;
  • vertex 1, vertex 2;
  • vertex 1, vertex 2, vertex 3.

Sample Input 2

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

Sample Output 2

16

Sample Input 3

8 21
2 6
1 3
5 6
3 8
3 6
4 7
4 6
3 4
1 5
2 4
1 2
2 7
1 4
3 5
2 5
2 3
4 5
3 7
6 7
5 7
2 8

Sample Output 3

2023
I - Common Prefixes

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

配点 : 500

問題文

2 つの文字列 X,Y に対して、その類似度 f(X,Y) を、XY を先頭から見て一致している文字数とします。
例えば abcaxbc の類似度は 1aaaaaaa の類似度は 3 です。

長さ N の文字列 S が与えられます。Si 文字目以降からなる文字列を S_i とします。k=1,\ldots,N のそれぞれについて、f(S_k,S_1)+f(S_k,S_2)+\ldots+f(S_k,S_N) を求めてください。

制約

  • 1 \leq N \leq 10^6
  • S は英小文字のみからなる長さ N の文字列

入力

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

N
S

出力

N 行出力せよ。

i 行目には k=i の問題の答えを出力せよ。


入力例 1

3
abb

出力例 1

3
3
2

S_1,S_2,S_3 はそれぞれ abb, bb, b です。

  • k=1 のとき f(S_1,S_1)+f(S_1,S_2)+f(S_1,S_3)=3+0+0=3
  • k=2 のとき f(S_2,S_1)+f(S_2,S_2)+f(S_2,S_3)=0+2+1=3
  • k=3 のとき f(S_3,S_1)+f(S_3,S_2)+f(S_3,S_3)=0+1+1=2

入力例 2

11
mississippi

出力例 2

11
16
14
12
13
11
9
7
4
3
4

Score : 500 points

Problem Statement

Let the similarity f(X,Y) between two strings X and Y be the length of their longest common prefix.
For example, the similarity between abc and axbc is 1, and the similarity between aaa and aaaa is 3.

You are given a string S of length N. Let S_i be the suffix of S beginning with the i-th character of S. For each k=1,\ldots,N, find f(S_k,S_1)+f(S_k,S_2)+\ldots+f(S_k,S_N).

Constraints

  • 1 \leq N \leq 10^6
  • S is a string of length N consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print N lines.

The i-th line should contain the answer for k=i.


Sample Input 1

3
abb

Sample Output 1

3
3
2

S_1 is abb, S_2 is bb, and S_3 is b.

  • For k=1: f(S_1,S_1)+f(S_1,S_2)+f(S_1,S_3)=3+0+0=3.
  • For k=2: f(S_2,S_1)+f(S_2,S_2)+f(S_2,S_3)=0+2+1=3.
  • For k=3: f(S_3,S_1)+f(S_3,S_2)+f(S_3,S_3)=0+1+1=2.

Sample Input 2

11
mississippi

Sample Output 2

11
16
14
12
13
11
9
7
4
3
4