EtashGuha commited on
Commit
6342472
·
verified ·
1 Parent(s): e30eb6f

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. code_contests-0142/instruction.md +59 -0
  2. code_contests-0145/instruction.md +88 -0
  3. code_contests-0173/instruction.md +40 -0
  4. code_contests-0174/instruction.md +53 -0
  5. code_contests-0180/instruction.md +62 -0
  6. code_contests-0341/instruction.md +77 -0
  7. code_contests-0346/instruction.md +84 -0
  8. code_contests-0370/instruction.md +117 -0
  9. code_contests-0377/instruction.md +68 -0
  10. code_contests-0383/instruction.md +42 -0
  11. code_contests-0510/instruction.md +140 -0
  12. code_contests-0519/instruction.md +51 -0
  13. code_contests-0521/instruction.md +53 -0
  14. code_contests-0528/instruction.md +67 -0
  15. code_contests-0713/instruction.md +98 -0
  16. code_contests-0725/instruction.md +129 -0
  17. code_contests-0948/instruction.md +54 -0
  18. code_contests-0970/instruction.md +81 -0
  19. code_contests-0984/instruction.md +95 -0
  20. code_contests-10122/instruction.md +73 -0
  21. code_contests-10125/instruction.md +61 -0
  22. code_contests-10317/instruction.md +72 -0
  23. code_contests-10319/instruction.md +83 -0
  24. code_contests-10326/instruction.md +59 -0
  25. code_contests-10328/instruction.md +77 -0
  26. code_contests-1052/instruction.md +73 -0
  27. code_contests-10541/instruction.md +77 -0
  28. code_contests-10546/instruction.md +84 -0
  29. code_contests-1055/instruction.md +68 -0
  30. code_contests-10570/instruction.md +67 -0
  31. code_contests-10577/instruction.md +84 -0
  32. code_contests-10583/instruction.md +80 -0
  33. code_contests-1063/instruction.md +68 -0
  34. code_contests-10742/instruction.md +88 -0
  35. code_contests-10745/instruction.md +92 -0
  36. code_contests-10780/instruction.md +48 -0
  37. code_contests-10787/instruction.md +63 -0
  38. code_contests-10789/instruction.md +77 -0
  39. code_contests-1090/instruction.md +50 -0
  40. code_contests-10917/instruction.md +43 -0
  41. code_contests-10919/instruction.md +85 -0
  42. code_contests-10926/instruction.md +142 -0
  43. code_contests-1097/instruction.md +50 -0
  44. code_contests-11003/instruction.md +92 -0
  45. code_contests-11004/instruction.md +50 -0
  46. code_contests-11035/instruction.md +86 -0
  47. code_contests-11207/instruction.md +87 -0
  48. code_contests-11231/instruction.md +79 -0
  49. code_contests-11236/instruction.md +77 -0
  50. code_contests-11451/instruction.md +42 -0
code_contests-0142/instruction.md ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 339_A. Helpful Maths
2
+
3
+ ## Problem Description
4
+ Xenia the beginner mathematician is a third year student at elementary school. She is now learning the addition operation.
5
+
6
+ The teacher has written down the sum of multiple numbers. Pupils should calculate the sum. To make the calculation easier, the sum only contains numbers 1, 2 and 3. Still, that isn't enough for Xenia. She is only beginning to count, so she can calculate a sum only if the summands follow in non-decreasing order. For example, she can't calculate sum 1+3+2+1 but she can calculate sums 1+1+2 and 3+3.
7
+
8
+ You've got the sum that was written on the board. Rearrange the summans and print the sum in such a way that Xenia can calculate the sum.
9
+
10
+ Input
11
+
12
+ The first line contains a non-empty string s — the sum Xenia needs to count. String s contains no spaces. It only contains digits and characters "+". Besides, string s is a correct sum of numbers 1, 2 and 3. String s is at most 100 characters long.
13
+
14
+ Output
15
+
16
+ Print the new sum that Xenia can count.
17
+
18
+ Examples
19
+
20
+ Input
21
+
22
+ 3+2+1
23
+
24
+
25
+ Output
26
+
27
+ 1+2+3
28
+
29
+
30
+ Input
31
+
32
+ 1+1+3+1+3
33
+
34
+
35
+ Output
36
+
37
+ 1+1+1+3+3
38
+
39
+
40
+ Input
41
+
42
+ 2
43
+
44
+
45
+ Output
46
+
47
+ 2
48
+
49
+ ## Contest Information
50
+ - **Contest ID**: 339
51
+ - **Problem Index**: A
52
+ - **Points**: 500.0
53
+ - **Rating**: 800
54
+ - **Tags**: greedy, implementation, sortings, strings
55
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
56
+ - **Memory Limit**: 256000000 bytes
57
+
58
+ ## Task
59
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0145/instruction.md ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 405_E. Graph Cutting
2
+
3
+ ## Problem Description
4
+ Little Chris is participating in a graph cutting contest. He's a pro. The time has come to test his skills to the fullest.
5
+
6
+ Chris is given a simple undirected connected graph with n vertices (numbered from 1 to n) and m edges. The problem is to cut it into edge-distinct paths of length 2. Formally, Chris has to partition all edges of the graph into pairs in such a way that the edges in a single pair are adjacent and each edge must be contained in exactly one pair.
7
+
8
+ For example, the figure shows a way Chris can cut a graph. The first sample test contains the description of this graph.
9
+
10
+ <image>
11
+
12
+ You are given a chance to compete with Chris. Find a way to cut the given graph or determine that it is impossible!
13
+
14
+ Input
15
+
16
+ The first line of input contains two space-separated integers n and m (1 ≤ n, m ≤ 105), the number of vertices and the number of edges in the graph. The next m lines contain the description of the graph's edges. The i-th line contains two space-separated integers ai and bi (1 ≤ ai, bi ≤ n; ai ≠ bi), the numbers of the vertices connected by the i-th edge. It is guaranteed that the given graph is simple (without self-loops and multi-edges) and connected.
17
+
18
+ Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
19
+
20
+ Output
21
+
22
+ If it is possible to cut the given graph into edge-distinct paths of length 2, output <image> lines. In the i-th line print three space-separated integers xi, yi and zi, the description of the i-th path. The graph should contain this path, i.e., the graph should contain edges (xi, yi) and (yi, zi). Each edge should appear in exactly one path of length 2. If there are multiple solutions, output any of them.
23
+
24
+ If it is impossible to cut the given graph, print "No solution" (without quotes).
25
+
26
+ Examples
27
+
28
+ Input
29
+
30
+ 8 12
31
+ 1 2
32
+ 2 3
33
+ 3 4
34
+ 4 1
35
+ 1 3
36
+ 2 4
37
+ 3 5
38
+ 3 6
39
+ 5 6
40
+ 6 7
41
+ 6 8
42
+ 7 8
43
+
44
+
45
+ Output
46
+
47
+ 1 2 4
48
+ 1 3 2
49
+ 1 4 3
50
+ 5 3 6
51
+ 5 6 8
52
+ 6 7 8
53
+
54
+ Input
55
+
56
+ 3 3
57
+ 1 2
58
+ 2 3
59
+ 3 1
60
+
61
+
62
+ Output
63
+
64
+ No solution
65
+
66
+
67
+ Input
68
+
69
+ 3 2
70
+ 1 2
71
+ 2 3
72
+
73
+
74
+ Output
75
+
76
+ 1 2 3
77
+
78
+ ## Contest Information
79
+ - **Contest ID**: 405
80
+ - **Problem Index**: E
81
+ - **Points**: 3000.0
82
+ - **Rating**: 2300
83
+ - **Tags**: dfs and similar, graphs
84
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
85
+ - **Memory Limit**: 256000000 bytes
86
+
87
+ ## Task
88
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0173/instruction.md ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # even-from-end-1
2
+
3
+ ## Problem Description
4
+ Problem Description
5
+
6
+ Given a list of integers, find and display all even numbers from the end of the list.
7
+
8
+ Input Format
9
+
10
+ Each line of input begins with an integer N indicating the number of integer n that follow which comprises a list.
11
+
12
+ Output Format
13
+
14
+ All even numbers from the end of the list, each separated by a single space. Separate output for each test case with the newline character. Otherwise, display the words None to display
15
+
16
+ Constraints
17
+
18
+ 2 ≤ N ≤ 100
19
+
20
+ -1000 ≤ n ≤ 1000
21
+
22
+ SAMPLE INPUT
23
+ 5 10 25 12 4 1
24
+ 3 16 28 100
25
+
26
+ SAMPLE OUTPUT
27
+ 4 12 10
28
+ 100 28 16
29
+
30
+ ## Contest Information
31
+ - **Contest ID**: 0
32
+ - **Problem Index**:
33
+ - **Points**: 0.0
34
+ - **Rating**: 0
35
+ - **Tags**: None
36
+ - **Time Limit**: None seconds
37
+ - **Memory Limit**: 0 bytes
38
+
39
+ ## Task
40
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0174/instruction.md ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # hermione-vs-draco
2
+
3
+ ## Problem Description
4
+ Draco Malfoy and Hermione Granger have gotten into a "battle of brains". Draco was foolish enough to challenge her to a Arithmancy problem. Septima Vector, Arithmancy teacher at Hogwarts, has agreed to give them both a problem which they should solve overnight.
5
+
6
+ The problem is as follows :-
7
+
8
+ Firstly, a function F (from naturals to naturals), which is strictly increasing, is defined as follows:-
9
+
10
+ F(0) = F(1) = 1
11
+
12
+ F(x) = x * (x - 1) * F(x - 2) ; x > 1
13
+
14
+ Now, define another function, Z (from naturals to naturals) as
15
+
16
+ Z(x) = highest value of n such that 10^n divides x
17
+
18
+ Draco has realized his folly and has come to you asking for help. You don't like him, but you have accepted the challenge as he has agreed to accept your prowess at the your Muggle stuff if you can help him out with this. He doesn't understand computers, so it's time to show him some Muggle-magic
19
+
20
+ INPUT FORMAT :
21
+ Single positive integer on the first line, T ( ≤ 100000), which indicates the number of lines that follow. Each of the next T lines contain a positive integer, N ( ≤ 1000000000).
22
+
23
+ OUTPUT FORMAT :
24
+ For every number N in the input, you are expected to output a single number that is equal to Z(F(N)).
25
+
26
+ SAMPLE INPUT
27
+ 6
28
+ 3
29
+ 60
30
+ 100
31
+ 1024
32
+ 23456
33
+ 8735373
34
+
35
+ SAMPLE OUTPUT
36
+ 0
37
+ 14
38
+ 24
39
+ 253
40
+ 5861
41
+ 2183837
42
+
43
+ ## Contest Information
44
+ - **Contest ID**: 0
45
+ - **Problem Index**:
46
+ - **Points**: 0.0
47
+ - **Rating**: 0
48
+ - **Tags**: None
49
+ - **Time Limit**: None seconds
50
+ - **Memory Limit**: 0 bytes
51
+
52
+ ## Task
53
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0180/instruction.md ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # xenny-and-range-sums
2
+
3
+ ## Problem Description
4
+ PowerShell had N natural numbers. He wanted to test Xenny's speed in finding the sum and difference of several numbers.
5
+
6
+ He decided to ask Xenny several questions. In each question, he gave him two positive integers L and R. He asked him to find the sum of all integers from index L to index R (inclusive) and the difference of all integers from index R to index L (inclusive).
7
+
8
+ (Numbers are 1-indexed)
9
+
10
+ He asked Xenny Q such questions.
11
+
12
+ Your task is to report the answer that Xenny gave to each of the Q questions.
13
+
14
+ Input Format:
15
+
16
+ First line contains 2 space-separated integers - N and Q.
17
+
18
+ Second line contains N space-separated natural numbers.
19
+
20
+ Q lines follow - each line contains 2 space-separated postiive integers L and R.
21
+
22
+ Output Format:
23
+
24
+ Output Q lines - the answer to each question.
25
+
26
+ Each line should contain 2 space-separated integers - the required sum and difference.
27
+
28
+ Constraints:
29
+
30
+ 1 ≤ N ≤ 10^6
31
+
32
+ 1 ≤ L ≤ R ≤ 10^6
33
+
34
+ 1 ≤ Q ≤ 10^5
35
+
36
+ -1000000 ≤ Numbers ≤ 1000000
37
+
38
+ SAMPLE INPUT
39
+ 4 1
40
+ 1 2 3 4
41
+ 1 3
42
+
43
+ SAMPLE OUTPUT
44
+ 6 0
45
+
46
+ Explanation
47
+
48
+ 1 + 2 + 3 = 6
49
+
50
+ 3 - 2 - 1 = 0
51
+
52
+ ## Contest Information
53
+ - **Contest ID**: 0
54
+ - **Problem Index**:
55
+ - **Points**: 0.0
56
+ - **Rating**: 0
57
+ - **Tags**: None
58
+ - **Time Limit**: None seconds
59
+ - **Memory Limit**: 0 bytes
60
+
61
+ ## Task
62
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0341/instruction.md ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1506_F. Triangular Paths
2
+
3
+ ## Problem Description
4
+ Consider an infinite triangle made up of layers. Let's number the layers, starting from one, from the top of the triangle (from top to bottom). The k-th layer of the triangle contains k points, numbered from left to right. Each point of an infinite triangle is described by a pair of numbers (r, c) (1 ≤ c ≤ r), where r is the number of the layer, and c is the number of the point in the layer. From each point (r, c) there are two directed edges to the points (r+1, c) and (r+1, c+1), but only one of the edges is activated. If r + c is even, then the edge to the point (r+1, c) is activated, otherwise the edge to the point (r+1, c+1) is activated. Look at the picture for a better understanding.
5
+
6
+ <image> Activated edges are colored in black. Non-activated edges are colored in gray.
7
+
8
+ From the point (r_1, c_1) it is possible to reach the point (r_2, c_2), if there is a path between them only from activated edges. For example, in the picture above, there is a path from (1, 1) to (3, 2), but there is no path from (2, 1) to (1, 1).
9
+
10
+ Initially, you are at the point (1, 1). For each turn, you can:
11
+
12
+ * Replace activated edge for point (r, c). That is if the edge to the point (r+1, c) is activated, then instead of it, the edge to the point (r+1, c+1) becomes activated, otherwise if the edge to the point (r+1, c+1), then instead if it, the edge to the point (r+1, c) becomes activated. This action increases the cost of the path by 1;
13
+ * Move from the current point to another by following the activated edge. This action does not increase the cost of the path.
14
+
15
+
16
+
17
+ You are given a sequence of n points of an infinite triangle (r_1, c_1), (r_2, c_2), …, (r_n, c_n). Find the minimum cost path from (1, 1), passing through all n points in arbitrary order.
18
+
19
+ Input
20
+
21
+ The first line contains one integer t (1 ≤ t ≤ 10^4) is the number of test cases. Then t test cases follow.
22
+
23
+ Each test case begins with a line containing one integer n (1 ≤ n ≤ 2 ⋅ 10^5) is the number of points to visit.
24
+
25
+ The second line contains n numbers r_1, r_2, …, r_n (1 ≤ r_i ≤ 10^9), where r_i is the number of the layer in which i-th point is located.
26
+
27
+ The third line contains n numbers c_1, c_2, …, c_n (1 ≤ c_i ≤ r_i), where c_i is the number of the i-th point in the r_i layer.
28
+
29
+ It is guaranteed that all n points are distinct.
30
+
31
+ It is guaranteed that there is always at least one way to traverse all n points.
32
+
33
+ It is guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5.
34
+
35
+ Output
36
+
37
+ For each test case, output the minimum cost of a path passing through all points in the corresponding test case.
38
+
39
+ Example
40
+
41
+ Input
42
+
43
+
44
+ 4
45
+ 3
46
+ 1 4 2
47
+ 1 3 1
48
+ 2
49
+ 2 4
50
+ 2 3
51
+ 2
52
+ 1 1000000000
53
+ 1 1000000000
54
+ 4
55
+ 3 10 5 8
56
+ 2 5 2 4
57
+
58
+
59
+ Output
60
+
61
+
62
+ 0
63
+ 1
64
+ 999999999
65
+ 2
66
+
67
+ ## Contest Information
68
+ - **Contest ID**: 1506
69
+ - **Problem Index**: F
70
+ - **Points**: 0.0
71
+ - **Rating**: 2000
72
+ - **Tags**: constructive algorithms, graphs, math, shortest paths, sortings
73
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
74
+ - **Memory Limit**: 256000000 bytes
75
+
76
+ ## Task
77
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0346/instruction.md ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 228_D. Zigzag
2
+
3
+ ## Problem Description
4
+ The court wizard Zigzag wants to become a famous mathematician. For that, he needs his own theorem, like the Cauchy theorem, or his sum, like the Minkowski sum. But most of all he wants to have his sequence, like the Fibonacci sequence, and his function, like the Euler's totient function.
5
+
6
+ The Zigag's sequence with the zigzag factor z is an infinite sequence Siz (i ≥ 1; z ≥ 2), that is determined as follows:
7
+
8
+ * Siz = 2, when <image>;
9
+ * <image>, when <image>;
10
+ * <image>, when <image>.
11
+
12
+
13
+
14
+ Operation <image> means taking the remainder from dividing number x by number y. For example, the beginning of sequence Si3 (zigzag factor 3) looks as follows: 1, 2, 3, 2, 1, 2, 3, 2, 1.
15
+
16
+ Let's assume that we are given an array a, consisting of n integers. Let's define element number i (1 ≤ i ≤ n) of the array as ai. The Zigzag function is function <image>, where l, r, z satisfy the inequalities 1 ≤ l ≤ r ≤ n, z ≥ 2.
17
+
18
+ To become better acquainted with the Zigzag sequence and the Zigzag function, the wizard offers you to implement the following operations on the given array a.
19
+
20
+ 1. The assignment operation. The operation parameters are (p, v). The operation denotes assigning value v to the p-th array element. After the operation is applied, the value of the array element ap equals v.
21
+ 2. The Zigzag operation. The operation parameters are (l, r, z). The operation denotes calculating the Zigzag function Z(l, r, z).
22
+
23
+
24
+
25
+ Explore the magical powers of zigzags, implement the described operations.
26
+
27
+ Input
28
+
29
+ The first line contains integer n (1 ≤ n ≤ 105) — The number of elements in array a. The second line contains n space-separated integers: a1, a2, ..., an (1 ≤ ai ≤ 109) — the elements of the array.
30
+
31
+ The third line contains integer m (1 ≤ m ≤ 105) — the number of operations. Next m lines contain the operations' descriptions. An operation's description starts with integer ti (1 ≤ ti ≤ 2) — the operation type.
32
+
33
+ * If ti = 1 (assignment operation), then on the line follow two space-separated integers: pi, vi (1 ≤ pi ≤ n; 1 ≤ vi ≤ 109) — the parameters of the assigning operation.
34
+ * If ti = 2 (Zigzag operation), then on the line follow three space-separated integers: li, ri, zi (1 ≤ li ≤ ri ≤ n; 2 ≤ zi ≤ 6) — the parameters of the Zigzag operation.
35
+
36
+
37
+
38
+ You should execute the operations in the order, in which they are given in the input.
39
+
40
+ Output
41
+
42
+ For each Zigzag operation print the calculated value of the Zigzag function on a single line. Print the values for Zigzag functions in the order, in which they are given in the input.
43
+
44
+ Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.
45
+
46
+ Examples
47
+
48
+ Input
49
+
50
+ 5
51
+ 2 3 1 5 5
52
+ 4
53
+ 2 2 3 2
54
+ 2 1 5 3
55
+ 1 3 5
56
+ 2 1 5 3
57
+
58
+
59
+ Output
60
+
61
+ 5
62
+ 26
63
+ 38
64
+
65
+ Note
66
+
67
+ Explanation of the sample test:
68
+
69
+ * Result of the first operation is Z(2, 3, 2) = 3·1 + 1·2 = 5.
70
+ * Result of the second operation is Z(1, 5, 3) = 2·1 + 3·2 + 1·3 + 5·2 + 5·1 = 26.
71
+ * After the third operation array a is equal to 2, 3, 5, 5, 5.
72
+ * Result of the forth operation is Z(1, 5, 3) = 2·1 + 3·2 + 5·3 + 5·2 + 5·1 = 38.
73
+
74
+ ## Contest Information
75
+ - **Contest ID**: 228
76
+ - **Problem Index**: D
77
+ - **Points**: 2000.0
78
+ - **Rating**: 2100
79
+ - **Tags**: data structures
80
+ - **Time Limit**: {'seconds': 3, 'nanos': 0} seconds
81
+ - **Memory Limit**: 256000000 bytes
82
+
83
+ ## Task
84
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0370/instruction.md ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 793_F. Julia the snail
2
+
3
+ ## Problem Description
4
+ After hard work Igor decided to have some rest.
5
+
6
+ He decided to have a snail. He bought an aquarium with a slippery tree trunk in the center, and put a snail named Julia into the aquarium.
7
+
8
+ Igor noticed that sometimes Julia wants to climb onto the trunk, but can't do it because the trunk is too slippery. To help the snail Igor put some ropes on the tree, fixing the lower end of the i-th rope on the trunk on the height li above the ground, and the higher end on the height ri above the ground.
9
+
10
+ For some reason no two ropes share the same position of the higher end, i.e. all ri are distinct. Now Julia can move down at any place of the trunk, and also move up from the lower end of some rope to its higher end. Igor is proud of his work, and sometimes think about possible movements of the snail. Namely, he is interested in the following questions: «Suppose the snail is on the trunk at height x now. What is the highest position on the trunk the snail can get on if it would never be lower than x or higher than y?» Please note that Julia can't move from a rope to the trunk before it reaches the higher end of the rope, and Igor is interested in the highest position on the tree trunk.
11
+
12
+ Igor is interested in many questions, and not always can answer them. Help him, write a program that answers these questions.
13
+
14
+ Input
15
+
16
+ The first line contains single integer n (1 ≤ n ≤ 100000) — the height of the trunk.
17
+
18
+ The second line contains single integer m (1 ≤ m ≤ 100000) — the number of ropes.
19
+
20
+ The next m lines contain information about the ropes.
21
+
22
+ The i-th of these lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n) — the heights on which the lower and the higher ends of the i-th rope are fixed, respectively. It is guaranteed that all ri are distinct.
23
+
24
+ The next line contains single integer q (1 ≤ q ≤ 100000) — the number of questions.
25
+
26
+ The next q lines contain information about the questions.
27
+
28
+ Each of these lines contain two integers x and y (1 ≤ x ≤ y ≤ n), where x is the height where Julia starts (and the height Julia can't get lower than), and y is the height Julia can't get higher than.
29
+
30
+ Output
31
+
32
+ For each question print the maximum reachable for Julia height.
33
+
34
+ Examples
35
+
36
+ Input
37
+
38
+ 8
39
+ 4
40
+ 1 2
41
+ 3 4
42
+ 2 5
43
+ 6 7
44
+ 5
45
+ 1 2
46
+ 1 4
47
+ 1 6
48
+ 2 7
49
+ 6 8
50
+
51
+
52
+ Output
53
+
54
+ 2
55
+ 2
56
+ 5
57
+ 5
58
+ 7
59
+
60
+
61
+ Input
62
+
63
+ 10
64
+ 10
65
+ 3 7
66
+ 1 4
67
+ 1 6
68
+ 5 5
69
+ 1 1
70
+ 3 9
71
+ 7 8
72
+ 1 2
73
+ 3 3
74
+ 7 10
75
+ 10
76
+ 2 4
77
+ 1 7
78
+ 3 4
79
+ 3 5
80
+ 2 8
81
+ 2 5
82
+ 5 5
83
+ 3 5
84
+ 7 7
85
+ 3 10
86
+
87
+
88
+ Output
89
+
90
+ 2
91
+ 7
92
+ 3
93
+ 3
94
+ 2
95
+ 2
96
+ 5
97
+ 3
98
+ 7
99
+ 10
100
+
101
+ Note
102
+
103
+ The picture of the first sample is on the left, the picture of the second sample is on the right. Ropes' colors are just for clarity, they don't mean anything.
104
+
105
+ <image>
106
+
107
+ ## Contest Information
108
+ - **Contest ID**: 793
109
+ - **Problem Index**: F
110
+ - **Points**: 3000.0
111
+ - **Rating**: 3000
112
+ - **Tags**: data structures, divide and conquer, dp
113
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
114
+ - **Memory Limit**: 512000000 bytes
115
+
116
+ ## Task
117
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0377/instruction.md ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 958_E2. Guard Duty (medium)
2
+
3
+ ## Problem Description
4
+ Princess Heidi decided to give orders to all her K Rebel ship commanders in person. Unfortunately, she is currently travelling through hyperspace, and will leave it only at N specific moments t1, t2, ..., tN. The meetings with commanders must therefore start and stop at those times. Namely, each commander will board her ship at some time ti and disembark at some later time tj. Of course, Heidi needs to meet with all commanders, and no two meetings can be held during the same time. Two commanders cannot even meet at the beginnings/endings of the hyperspace jumps, because too many ships in one position could give out their coordinates to the enemy.
5
+
6
+ Your task is to find minimum time that Princess Heidi has to spend on meetings, with her schedule satisfying the conditions above.
7
+
8
+ Input
9
+
10
+ The first line contains two integers K, N (2 ≤ 2K ≤ N ≤ 500000, K ≤ 5000). The second line contains N distinct integers t1, t2, ..., tN (1 ≤ ti ≤ 109) representing the times when Heidi leaves hyperspace.
11
+
12
+ Output
13
+
14
+ Output only one integer: the minimum time spent on meetings.
15
+
16
+ Examples
17
+
18
+ Input
19
+
20
+ 2 5
21
+ 1 4 6 7 12
22
+
23
+
24
+ Output
25
+
26
+ 4
27
+
28
+
29
+ Input
30
+
31
+ 3 6
32
+ 6 3 4 2 5 1
33
+
34
+
35
+ Output
36
+
37
+ 3
38
+
39
+
40
+ Input
41
+
42
+ 4 12
43
+ 15 7 4 19 3 30 14 1 5 23 17 25
44
+
45
+
46
+ Output
47
+
48
+ 6
49
+
50
+ Note
51
+
52
+ In the first example, there are five valid schedules: [1, 4], [6, 7] with total time 4, [1, 4], [6, 12] with total time 9, [1, 4], [7, 12] with total time 8, [1, 6], [7, 12] with total time 10, and [4, 6], [7, 12] with total time 7. So the answer is 4.
53
+
54
+ In the second example, there is only 1 valid schedule: [1, 2], [3, 4], [5, 6].
55
+
56
+ For the third example, one possible schedule with total time 6 is: [1, 3], [4, 5], [14, 15], [23, 25].
57
+
58
+ ## Contest Information
59
+ - **Contest ID**: 958
60
+ - **Problem Index**: E2
61
+ - **Points**: 0.0
62
+ - **Rating**: 2200
63
+ - **Tags**: binary search, dp, greedy, sortings
64
+ - **Time Limit**: {'seconds': 3, 'nanos': 0} seconds
65
+ - **Memory Limit**: 256000000 bytes
66
+
67
+ ## Task
68
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0383/instruction.md ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # jumping-frog
2
+
3
+ ## Problem Description
4
+ There is a frog known as "CHAMELEON" because he has a special feature to change its body's color similar to stone's color on which he sits. There are N colorful stones lying in a row, but of only 1 to 100 different colors. Frog can hopp on another stone if the stone has same color as its body or (i-1)th stone if it is currently on ith stone. Frog needs to hopp from position 'S' to 'E' via 'M'. Finds the minimum no. of jumps he needs to take to reach E from S via M.
5
+
6
+ INPUT:
7
+
8
+ First line contain a integer N. Next line contain N stones(0- based index) each ith stone is denoted by a number which is color of the stone.
9
+ Next line contain Q (no. of queries). Each query contain S,M,E.
10
+
11
+ 3 ≤ N ≤ 10^5.
12
+
13
+ 1 ≤ Q ≤ 50
14
+
15
+ 0 ≤ S,M,E<N
16
+
17
+ OUTPUT:
18
+
19
+ Answer of each query. Print -1 if it will not able to reach the destination point.
20
+
21
+ SAMPLE INPUT
22
+ 6
23
+ 2 3 1 3 2 4
24
+ 2
25
+ 1 0 4
26
+ 2 3 5
27
+
28
+ SAMPLE OUTPUT
29
+ 2
30
+ -1
31
+
32
+ ## Contest Information
33
+ - **Contest ID**: 0
34
+ - **Problem Index**:
35
+ - **Points**: 0.0
36
+ - **Rating**: 0
37
+ - **Tags**: None
38
+ - **Time Limit**: None seconds
39
+ - **Memory Limit**: 0 bytes
40
+
41
+ ## Task
42
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0510/instruction.md ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # p01017 Yu-kun Likes Rectangles
2
+
3
+ ## Problem Description
4
+ Background
5
+
6
+ The kindergarten attached to the University of Aizu is a kindergarten where children who love programming gather. Yu, one of the kindergarten children, loves rectangles as much as programming. Yu-kun decided to write a program to calculate the maximum score that can be obtained, thinking of a new play to get points using three rectangles.
7
+
8
+ Problem
9
+
10
+ Given the rectangles A and B of the H × W cells and the rectangle C of the h × w cells. (H and W represent the number of vertical and horizontal squares of rectangles A and B, respectively, and h and w represent the number of vertical and horizontal squares of rectangle C, respectively.)
11
+
12
+ As shown in Fig. 1, an integer is written in each cell of A, and each cell of B is colored white or black. Each cell in C is also colored white or black.
13
+
14
+
15
+ Figure 1
16
+ Figure 1
17
+
18
+
19
+ If there is a rectangle in B that has exactly the same pattern as C, you can get the sum of the integers written in the corresponding squares in A as a score.
20
+
21
+ For example, when the rectangles A, B, and C as shown in Fig. 1 are used, the same pattern as C is included in B as shown by the red line in Fig. 2, so 193 points can be obtained from that location.
22
+
23
+
24
+ Figure 2
25
+ Figure 2
26
+
27
+
28
+ If there is one or more rectangles in B that have exactly the same pattern as C, how many points can be obtained in such rectangles?
29
+
30
+ However, if there is no rectangle in B that has the same pattern as C, output "NA" (excluding "). Also, the rectangle cannot be rotated or inverted.
31
+
32
+ Constraints
33
+
34
+ The input satisfies the following conditions.
35
+
36
+ * All inputs are integers
37
+ * 1 ≤ H, W ≤ 50
38
+ * 1 ≤ h ≤ H
39
+ * 1 ≤ w ≤ W
40
+ * -100 ≤ a (i, j) ≤ 100
41
+ * b (i, j), c (i, j) is 0 or 1
42
+
43
+ Input
44
+
45
+ The input format is as follows.
46
+
47
+
48
+ H W
49
+ a (1,1) a (1,2) ... a (1, W)
50
+ a (2,1) a (2,2) ... a (2, W)
51
+ ::
52
+ a (H, 1) a (H, 2) ... a (H, W)
53
+ b (1,1) b (1,2) ... b (1, W)
54
+ b (2,1) b (2,2) ... b (2, W)
55
+ ::
56
+ b (H, 1) b (H, 2) ... b (H, W)
57
+ h w
58
+ c (1,1) c (1,2) ... c (1, w)
59
+ c (2,1) c (2,2) ... c (2, w)
60
+ ::
61
+ c (h, 1) c (h, 2) ... c (h, w)
62
+
63
+
64
+ a (i, j) is the integer written in the square (i, j) of the rectangle A, b (i, j) is the color of the square (i, j) of the rectangle B, and c (i, j) Represents the color of the square (i, j) of the rectangle C. As for the color of the square, 0 is white and 1 is black.
65
+
66
+ Output
67
+
68
+ If there is a place in rectangle B that has exactly the same pattern as rectangle C, output the one with the highest score among such places.
69
+
70
+ If no such location exists, print “NA” (excluding “).
71
+
72
+ Examples
73
+
74
+ Input
75
+
76
+ 4 4
77
+ 10 2 -1 6
78
+ 8 1 -100 41
79
+ 22 47 32 11
80
+ -41 99 12 -8
81
+ 1 0 0 1
82
+ 0 0 1 0
83
+ 0 1 0 1
84
+ 0 0 1 0
85
+ 2 3
86
+ 1 0 1
87
+ 0 1 0
88
+
89
+
90
+ Output
91
+
92
+ 193
93
+
94
+
95
+ Input
96
+
97
+ 3 3
98
+ 5 1 3
99
+ 2 5 9
100
+ 0 1 5
101
+ 1 0 0
102
+ 0 1 1
103
+ 0 1 1
104
+ 1 1
105
+ 1
106
+
107
+
108
+ Output
109
+
110
+ 9
111
+
112
+
113
+ Input
114
+
115
+ 3 4
116
+ 4 1 9 1
117
+ 9 1 -1 3
118
+ 2 -4 1 10
119
+ 1 1 0 1
120
+ 0 1 1 1
121
+ 1 1 0 0
122
+ 1 4
123
+ 1 1 1 1
124
+
125
+
126
+ Output
127
+
128
+ NA
129
+
130
+ ## Contest Information
131
+ - **Contest ID**: 0
132
+ - **Problem Index**:
133
+ - **Points**: 0.0
134
+ - **Rating**: 0
135
+ - **Tags**:
136
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
137
+ - **Memory Limit**: 268435456 bytes
138
+
139
+ ## Task
140
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0519/instruction.md ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # p02327 Largest Rectangle
2
+
3
+ ## Problem Description
4
+ Given a matrix (H × W) which contains only 1 and 0, find the area of the largest rectangle which only contains 0s.
5
+
6
+ Constraints
7
+
8
+ * 1 ≤ H, W ≤ 1,400
9
+
10
+ Input
11
+
12
+
13
+ H W
14
+ c1,1 c1,2 ... c1,W
15
+ c2,1 c2,2 ... c2,W
16
+ :
17
+ cH,1 cH,2 ... cH,W
18
+
19
+
20
+ In the first line, two integers H and W separated by a space character are given. In the following H lines, ci,j, elements of the H × W matrix, are given.
21
+
22
+ Output
23
+
24
+ Print the area (the number of 0s) of the largest rectangle.
25
+
26
+ Example
27
+
28
+ Input
29
+
30
+ 4 5
31
+ 0 0 1 0 0
32
+ 1 0 0 0 0
33
+ 0 0 0 1 0
34
+ 0 0 0 1 0
35
+
36
+
37
+ Output
38
+
39
+ 6
40
+
41
+ ## Contest Information
42
+ - **Contest ID**: 0
43
+ - **Problem Index**:
44
+ - **Points**: 0.0
45
+ - **Rating**: 0
46
+ - **Tags**:
47
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
48
+ - **Memory Limit**: 134217728 bytes
49
+
50
+ ## Task
51
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0521/instruction.md ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # bico
2
+
3
+ ## Problem Description
4
+ The much anticipated video game "BiCo Grid" has been released. The rules of "Bico Grid" are very simple.
5
+ The game field is a 100x100 matrix, where each cell is either a blocked cell, or a cell with some number of coins. For a regular player the look of the field seems pretty random, but the programmer in you recognizes the following pattern: the i-th cell on the n-th row contains C(n, i) coins if and only if 0 ≤ i ≤ n, all other cells are blocked. Record C(n, i) denotes binomial coefficient "n choose i".
6
+ The player starts from the cell situated at row R and column C in the matrix. The objective is to collect exactly G number of coins from matrix in several moves. There are some rules:
7
+
8
+ On each move the player must collect all the coins from some unblocked cell in the current column.
9
+ The rules of the game state, that player mustn't be really greedy, so the number of coins he collected must not increase. In other words, if at some move the player collected X coins then further he cannot collect more than X coins in a single move.
10
+ After each move, the player is immediately moved to some cell of the column W-1 (where W denotes the current column of the player). If the current column of the player has index 0, the game ends.
11
+ The game ends when player collects exactly G number of coins.
12
+
13
+ You are given the description of the game. Please, output the sequence of moves that win the game (collect exactly G coins)! It is guaranteed that if the player will play optimally it is possible to win the game.
14
+
15
+ Input
16
+ The first line of the input contains an integer T denoting the number of test cases. Then T lines follows. Each containing three integers, R denoting the starting row, C, denoting the starting column, and G, denoting the number of coins to be collected.
17
+
18
+ Output
19
+ For each test case, output two lines. First line contains K, the number of column visited before completion of game. Second line contains K space separated integers, the number of coins collected from the cells, in the order they were collected.
20
+ It is guaranteed that a solution exists. And if there are multiple solutions, print any of them.
21
+
22
+ Constraints
23
+ 1 ≤ T ≤ 100000 ≤ C ≤ 490 ≤ R ≤ 991 ≤ G ≤ 10^12
24
+
25
+ Example
26
+ Input:
27
+ 3
28
+ 3 2 5
29
+ 3 3 10
30
+ 5 4 7
31
+
32
+ Output:
33
+ 2
34
+ 3 2
35
+ 1
36
+ 10
37
+ 3
38
+ 5 1 1
39
+
40
+ Explanation
41
+ Example case 1. We first pick 3 coins from [3, 2] then we pick 2 coins from [2, 1]Example case 2. As 3rd column contains 10 coins in cell [5, 3] we pick it.Example case 3. We first pick 5 coins from [5, 4] then we pick 1 coin from [3, 3] and again we pick 1 coin from [2, 2].
42
+
43
+ ## Contest Information
44
+ - **Contest ID**: 0
45
+ - **Problem Index**:
46
+ - **Points**: 0.0
47
+ - **Rating**: 0
48
+ - **Tags**: None
49
+ - **Time Limit**: None seconds
50
+ - **Memory Limit**: 0 bytes
51
+
52
+ ## Task
53
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0528/instruction.md ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1036_B. Diagonal Walking v.2
2
+
3
+ ## Problem Description
4
+ Mikhail walks on a Cartesian plane. He starts at the point (0, 0), and in one move he can go to any of eight adjacent points. For example, if Mikhail is currently at the point (0, 0), he can go to any of the following points in one move:
5
+
6
+ * (1, 0);
7
+ * (1, 1);
8
+ * (0, 1);
9
+ * (-1, 1);
10
+ * (-1, 0);
11
+ * (-1, -1);
12
+ * (0, -1);
13
+ * (1, -1).
14
+
15
+
16
+
17
+ If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 ≠ x2 and y1 ≠ y2, then such a move is called a diagonal move.
18
+
19
+ Mikhail has q queries. For the i-th query Mikhail's target is to go to the point (n_i, m_i) from the point (0, 0) in exactly k_i moves. Among all possible movements he want to choose one with the maximum number of diagonal moves. Your task is to find the maximum number of diagonal moves or find that it is impossible to go from the point (0, 0) to the point (n_i, m_i) in k_i moves.
20
+
21
+ Note that Mikhail can visit any point any number of times (even the destination point!).
22
+
23
+ Input
24
+
25
+ The first line of the input contains one integer q (1 ≤ q ≤ 10^4) — the number of queries.
26
+
27
+ Then q lines follow. The i-th of these q lines contains three integers n_i, m_i and k_i (1 ≤ n_i, m_i, k_i ≤ 10^{18}) — x-coordinate of the destination point of the query, y-coordinate of the destination point of the query and the number of moves in the query, correspondingly.
28
+
29
+ Output
30
+
31
+ Print q integers. The i-th integer should be equal to -1 if Mikhail cannot go from the point (0, 0) to the point (n_i, m_i) in exactly k_i moves described above. Otherwise the i-th integer should be equal to the the maximum number of diagonal moves among all possible movements.
32
+
33
+ Example
34
+
35
+ Input
36
+
37
+ 3
38
+ 2 2 3
39
+ 4 3 7
40
+ 10 1 9
41
+
42
+
43
+ Output
44
+
45
+ 1
46
+ 6
47
+ -1
48
+
49
+ Note
50
+
51
+ One of the possible answers to the first test case: (0, 0) → (1, 0) → (1, 1) → (2, 2).
52
+
53
+ One of the possible answers to the second test case: (0, 0) → (0, 1) → (1, 2) → (0, 3) → (1, 4) → (2, 3) → (3, 2) → (4, 3).
54
+
55
+ In the third test case Mikhail cannot reach the point (10, 1) in 9 moves.
56
+
57
+ ## Contest Information
58
+ - **Contest ID**: 1036
59
+ - **Problem Index**: B
60
+ - **Points**: 0.0
61
+ - **Rating**: 1600
62
+ - **Tags**: math
63
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
64
+ - **Memory Limit**: 256000000 bytes
65
+
66
+ ## Task
67
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0713/instruction.md ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # p00184 Tsuruga Castle
2
+
3
+ ## Problem Description
4
+ Tsuruga Castle, a symbol of Aizuwakamatsu City, was named "Tsuruga Castle" after Gamo Ujisato built a full-scale castle tower. You can overlook the Aizu basin from the castle tower. On a clear day, you can see Tsuruga Castle from the summit of Mt. Iimori, which is famous for Byakkotai.
5
+
6
+ <image>
7
+
8
+
9
+
10
+ We decided to conduct a dating survey of visitors to Tsuruga Castle to use as a reference for future public relations activities in Aizuwakamatsu City. Please create a program that inputs the age of visitors and outputs the number of people by age group below.
11
+
12
+ Category | Age
13
+ --- | ---
14
+ Under 10 years old | 0 ~ 9
15
+ Teens | 10 ~ 19
16
+ 20s | 20 ~ 29
17
+ 30s | 30 ~ 39
18
+ 40s | 40 ~ 49
19
+ 50s | 50 ~ 59
20
+ Over 60 years old | 60 ~
21
+
22
+
23
+
24
+ Input
25
+
26
+ A sequence of multiple datasets is given as input. The end of the input is indicated by a single line of zeros. Each dataset is given in the following format:
27
+
28
+
29
+ n
30
+ a1
31
+ a2
32
+ ::
33
+ an
34
+
35
+
36
+ The first line gives the number of visitors n (1 ≤ n ≤ 1000000), and the following n lines give the age of the i-th visitor ai (0 ≤ ai ≤ 120).
37
+
38
+ Output
39
+
40
+ The number of people is output in the following format for each data set.
41
+
42
+ Line 1: Number of people under 10
43
+ Line 2: Number of teens
44
+ Line 3: Number of people in their 20s
45
+ Line 4: Number of people in their 30s
46
+ Line 5: Number of people in their 40s
47
+ Line 6: Number of people in their 50s
48
+ Line 7: Number of people over 60
49
+
50
+ Example
51
+
52
+ Input
53
+
54
+ 8
55
+ 71
56
+ 34
57
+ 65
58
+ 11
59
+ 41
60
+ 39
61
+ 6
62
+ 5
63
+ 4
64
+ 67
65
+ 81
66
+ 78
67
+ 65
68
+ 0
69
+
70
+
71
+ Output
72
+
73
+ 2
74
+ 1
75
+ 0
76
+ 2
77
+ 1
78
+ 0
79
+ 2
80
+ 0
81
+ 0
82
+ 0
83
+ 0
84
+ 0
85
+ 0
86
+ 4
87
+
88
+ ## Contest Information
89
+ - **Contest ID**: 0
90
+ - **Problem Index**:
91
+ - **Points**: 0.0
92
+ - **Rating**: 0
93
+ - **Tags**:
94
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
95
+ - **Memory Limit**: 134217728 bytes
96
+
97
+ ## Task
98
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0725/instruction.md ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # p01991 Namo.. Cut
2
+
3
+ ## Problem Description
4
+ C: Namo .. Cut
5
+
6
+ problem
7
+
8
+ -Defeat the mysterious giant jellyfish, codenamed "Nari"-
9
+
10
+ "Nari" has a very strong vitality, so if you don't keep cutting quickly, it will be revived in a blink of an eye. We are making trial and error every day to find out how to cut "Nari" efficiently. In the process, you needed the help of a programmer.
11
+
12
+ "Na ◯ ri" can be represented by a connected undirected graph consisting of N vertices and N edges. From now on, suppose each vertex is named with a different number from 1 to N.
13
+
14
+ We ask Q questions about "Nari". I want you to create a program that answers all of them.
15
+
16
+ Questions have numbers from 1 to Q, and each question is structured as follows:
17
+
18
+ * Question i specifies two vertices a_i and b_i. Answer the minimum number of edges that need to be deleted in order to unlink a_i and b_i.
19
+
20
+
21
+
22
+ Here, the fact that the vertices u and v are unconnected means that there is no route that can go back and forth between u and v.
23
+
24
+ Input format
25
+
26
+
27
+ N
28
+ u_1 v_1
29
+ u_2 v_2
30
+ ...
31
+ u_N v_N
32
+ Q
33
+ a_1 b_1
34
+ a_2 b_2
35
+ ...
36
+ a_Q b_Q
37
+
38
+
39
+ All inputs are integers.
40
+
41
+ The number of vertices N is given in the first line. The i-th line of the following N lines is given the numbers u_i and v_i of the two vertices connected by the i-th edge, separated by blanks.
42
+
43
+ Then the number of questions Q is given. The i-th line of the following Q lines is given the numbers a_i and b_i of the two vertices specified in the i-th question, separated by blanks.
44
+
45
+ Constraint
46
+
47
+ * 3 \ leq N \ leq 100,000
48
+ * 1 \ leq Q \ leq 100,000
49
+ * There are no self-loops or multiple edges in the graph
50
+ * 1 \ leq a_i, b_i \ leq N and a_i \ neq b_i (1 \ leq i \ leq Q)
51
+
52
+
53
+
54
+ Output format
55
+
56
+ The output consists of Q lines. On line i, output an integer that represents the minimum number of edges that need to be deleted in order to unlink a_i and b_i.
57
+
58
+ Input example 1
59
+
60
+
61
+ 3
62
+ 1 2
63
+ 13
64
+ twenty three
65
+ 1
66
+ 13
67
+
68
+
69
+ Output example 1
70
+
71
+
72
+ 2
73
+
74
+ Input example 2
75
+
76
+
77
+ 7
78
+ 1 2
79
+ 1 6
80
+ 3 5
81
+ twenty five
82
+ 5 4
83
+ 14
84
+ 3 7
85
+ 3
86
+ twenty four
87
+ 3 1
88
+ 6 7
89
+
90
+
91
+ Output example 2
92
+
93
+
94
+ 2
95
+ 1
96
+ 1
97
+
98
+
99
+
100
+
101
+
102
+
103
+ Example
104
+
105
+ Input
106
+
107
+ 3
108
+ 1 2
109
+ 1 3
110
+ 2 3
111
+ 1
112
+ 1 3
113
+
114
+
115
+ Output
116
+
117
+ 2
118
+
119
+ ## Contest Information
120
+ - **Contest ID**: 0
121
+ - **Problem Index**:
122
+ - **Points**: 0.0
123
+ - **Rating**: 0
124
+ - **Tags**:
125
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
126
+ - **Memory Limit**: 268435456 bytes
127
+
128
+ ## Task
129
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0948/instruction.md ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1090_C. New Year Presents
2
+
3
+ ## Problem Description
4
+ Santa has prepared boxes with presents for n kids, one box for each kid. There are m kinds of presents: balloons, sweets, chocolate bars, toy cars... A child would be disappointed to receive two presents of the same kind, so all kinds of presents in one box are distinct.
5
+
6
+ Having packed all the presents, Santa realized that different boxes can contain different number of presents. It would be unfair to the children, so he decided to move some presents between boxes, and make their sizes similar. After all movements, the difference between the maximal and the minimal number of presents in a box must be as small as possible. All presents in each box should still be distinct. Santa wants to finish the job as fast as possible, so he wants to minimize the number of movements required to complete the task.
7
+
8
+ Given the sets of presents in each box, find the shortest sequence of movements of presents between boxes that minimizes the difference of sizes of the smallest and the largest box, and keeps all presents in each box distinct.
9
+
10
+ Input
11
+
12
+ The first line of input contains two integers n, m (1 ≤ n, m ≤ 100\ 000), the number of boxes and the number of kinds of the presents. Denote presents with integers from 1 to m.
13
+
14
+ Each of the following n lines contains the description of one box. It begins with an integer s_i (s_i ≥ 0), the number of presents in the box, s_i distinct integers between 1 and m follow, denoting the kinds of presents in that box.
15
+
16
+ The total number of presents in all boxes does not exceed 500 000.
17
+
18
+ Output
19
+
20
+ Print one integer k at the first line of output, the number of movements in the shortest sequence that makes the sizes of the boxes differ by at most one. Then print k lines that describe movements in the same order in which they should be performed. Each movement is described by three integers from_i, to_i, kind_i. It means that the present of kind kind_i is moved from the box with number from_i to the box with number to_i. Boxes are numbered from one in the order they are given in the input.
21
+
22
+ At the moment when the movement is performed the present with kind kind_i must be present in the box with number from_i. After performing all moves each box must not contain two presents of the same kind.
23
+
24
+ If there are several optimal solutions, output any of them.
25
+
26
+ Example
27
+
28
+ Input
29
+
30
+
31
+ 3 5
32
+ 5 1 2 3 4 5
33
+ 2 1 2
34
+ 2 3 4
35
+
36
+
37
+ Output
38
+
39
+
40
+ 2
41
+ 1 3 5
42
+ 1 2 3
43
+
44
+ ## Contest Information
45
+ - **Contest ID**: 1090
46
+ - **Problem Index**: C
47
+ - **Points**: 0.0
48
+ - **Rating**: 2400
49
+ - **Tags**: constructive algorithms, data structures
50
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
51
+ - **Memory Limit**: 512000000 bytes
52
+
53
+ ## Task
54
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0970/instruction.md ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 177_C1. Party
2
+
3
+ ## Problem Description
4
+ To celebrate the second ABBYY Cup tournament, the Smart Beaver decided to throw a party. The Beaver has a lot of acquaintances, some of them are friends with each other, and some of them dislike each other. To make party successful, the Smart Beaver wants to invite only those of his friends who are connected by friendship relations, and not to invite those who dislike each other. Both friendship and dislike are mutual feelings.
5
+
6
+ More formally, for each invited person the following conditions should be fulfilled:
7
+
8
+ * all his friends should also be invited to the party;
9
+ * the party shouldn't have any people he dislikes;
10
+ * all people who are invited to the party should be connected with him by friendship either directly or through a chain of common friends of arbitrary length. We'll say that people a1 and ap are connected through a chain of common friends if there exists a sequence of people a2, a3, ..., ap - 1 such that all pairs of people ai and ai + 1 (1 ≤ i < p) are friends.
11
+
12
+
13
+
14
+ Help the Beaver find the maximum number of acquaintances he can invite.
15
+
16
+ Input
17
+
18
+ The first line of input contains an integer n — the number of the Beaver's acquaintances.
19
+
20
+ The second line contains an integer k <image> — the number of pairs of friends. Next k lines contain space-separated pairs of integers ui, vi <image> — indices of people who form the i-th pair of friends.
21
+
22
+ The next line contains an integer m <image> — the number of pairs of people who dislike each other. Next m lines describe pairs of people who dislike each other in the same format as the pairs of friends were described.
23
+
24
+ Each pair of people is mentioned in the input at most once <image>. In particular, two persons cannot be friends and dislike each other at the same time.
25
+
26
+ The input limitations for getting 30 points are:
27
+
28
+ * 2 ≤ n ≤ 14
29
+
30
+
31
+
32
+ The input limitations for getting 100 points are:
33
+
34
+ * 2 ≤ n ≤ 2000
35
+
36
+ Output
37
+
38
+ Output a single number — the maximum number of people that can be invited to the party. If a group of people that meets all the requirements is impossible to select, output 0.
39
+
40
+ Examples
41
+
42
+ Input
43
+
44
+ 9
45
+ 8
46
+ 1 2
47
+ 1 3
48
+ 2 3
49
+ 4 5
50
+ 6 7
51
+ 7 8
52
+ 8 9
53
+ 9 6
54
+ 2
55
+ 1 6
56
+ 7 9
57
+
58
+
59
+ Output
60
+
61
+ 3
62
+
63
+ Note
64
+
65
+ Let's have a look at the example.
66
+
67
+ <image>
68
+
69
+ Two groups of people can be invited: {1, 2, 3} and {4, 5}, thus the answer will be the size of the largest of these groups. Group {6, 7, 8, 9} doesn't fit, since it includes people 7 and 9 who dislike each other. Group {1, 2, 3, 4, 5} also doesn't fit, because not all of its members are connected by a chain of common friends (for example, people 2 and 5 aren't connected).
70
+
71
+ ## Contest Information
72
+ - **Contest ID**: 177
73
+ - **Problem Index**: C1
74
+ - **Points**: 30.0
75
+ - **Rating**: 1500
76
+ - **Tags**: dfs and similar, dsu, graphs
77
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
78
+ - **Memory Limit**: 256000000 bytes
79
+
80
+ ## Task
81
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-0984/instruction.md ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 505_B. Mr. Kitayuta's Colorful Graph
2
+
3
+ ## Problem Description
4
+ Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.
5
+
6
+ Mr. Kitayuta wants you to process the following q queries.
7
+
8
+ In the i-th query, he gives you two integers — ui and vi.
9
+
10
+ Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.
11
+
12
+ Input
13
+
14
+ The first line of the input contains space-separated two integers — n and m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100), denoting the number of the vertices and the number of the edges, respectively.
15
+
16
+ The next m lines contain space-separated three integers — ai, bi (1 ≤ ai < bi ≤ n) and ci (1 ≤ ci ≤ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i ≠ j, (ai, bi, ci) ≠ (aj, bj, cj).
17
+
18
+ The next line contains a integer — q (1 ≤ q ≤ 100), denoting the number of the queries.
19
+
20
+ Then follows q lines, containing space-separated two integers — ui and vi (1 ≤ ui, vi ≤ n). It is guaranteed that ui ≠ vi.
21
+
22
+ Output
23
+
24
+ For each query, print the answer in a separate line.
25
+
26
+ Examples
27
+
28
+ Input
29
+
30
+ 4 5
31
+ 1 2 1
32
+ 1 2 2
33
+ 2 3 1
34
+ 2 3 3
35
+ 2 4 3
36
+ 3
37
+ 1 2
38
+ 3 4
39
+ 1 4
40
+
41
+
42
+ Output
43
+
44
+ 2
45
+ 1
46
+ 0
47
+
48
+
49
+ Input
50
+
51
+ 5 7
52
+ 1 5 1
53
+ 2 5 1
54
+ 3 5 1
55
+ 4 5 1
56
+ 1 2 2
57
+ 2 3 2
58
+ 3 4 2
59
+ 5
60
+ 1 5
61
+ 5 1
62
+ 2 5
63
+ 1 5
64
+ 1 4
65
+
66
+
67
+ Output
68
+
69
+ 1
70
+ 1
71
+ 1
72
+ 1
73
+ 2
74
+
75
+ Note
76
+
77
+ Let's consider the first sample.
78
+
79
+ <image> The figure above shows the first sample.
80
+
81
+ * Vertex 1 and vertex 2 are connected by color 1 and 2.
82
+ * Vertex 3 and vertex 4 are connected by color 3.
83
+ * Vertex 1 and vertex 4 are not connected by any single color.
84
+
85
+ ## Contest Information
86
+ - **Contest ID**: 505
87
+ - **Problem Index**: B
88
+ - **Points**: 1000.0
89
+ - **Rating**: 1400
90
+ - **Tags**: dfs and similar, dp, dsu, graphs
91
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
92
+ - **Memory Limit**: 256000000 bytes
93
+
94
+ ## Task
95
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10122/instruction.md ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1311_A. Add Odd or Subtract Even
2
+
3
+ ## Problem Description
4
+ You are given two positive integers a and b.
5
+
6
+ In one move, you can change a in the following way:
7
+
8
+ * Choose any positive odd integer x (x > 0) and replace a with a+x;
9
+ * choose any positive even integer y (y > 0) and replace a with a-y.
10
+
11
+
12
+
13
+ You can perform as many such operations as you want. You can choose the same numbers x and y in different moves.
14
+
15
+ Your task is to find the minimum number of moves required to obtain b from a. It is guaranteed that you can always obtain b from a.
16
+
17
+ You have to answer t independent test cases.
18
+
19
+ Input
20
+
21
+ The first line of the input contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases.
22
+
23
+ Then t test cases follow. Each test case is given as two space-separated integers a and b (1 ≤ a, b ≤ 10^9).
24
+
25
+ Output
26
+
27
+ For each test case, print the answer — the minimum number of moves required to obtain b from a if you can perform any number of moves described in the problem statement. It is guaranteed that you can always obtain b from a.
28
+
29
+ Example
30
+
31
+ Input
32
+
33
+
34
+ 5
35
+ 2 3
36
+ 10 10
37
+ 2 4
38
+ 7 4
39
+ 9 3
40
+
41
+
42
+ Output
43
+
44
+
45
+ 1
46
+ 0
47
+ 2
48
+ 2
49
+ 1
50
+
51
+ Note
52
+
53
+ In the first test case, you can just add 1.
54
+
55
+ In the second test case, you don't need to do anything.
56
+
57
+ In the third test case, you can add 1 two times.
58
+
59
+ In the fourth test case, you can subtract 4 and add 1.
60
+
61
+ In the fifth test case, you can just subtract 6.
62
+
63
+ ## Contest Information
64
+ - **Contest ID**: 1311
65
+ - **Problem Index**: A
66
+ - **Points**: 0.0
67
+ - **Rating**: 800
68
+ - **Tags**: greedy, implementation, math
69
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
70
+ - **Memory Limit**: 256000000 bytes
71
+
72
+ ## Task
73
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10125/instruction.md ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1373_G. Pawns
2
+
3
+ ## Problem Description
4
+ You are given a chessboard consisting of n rows and n columns. Rows are numbered from bottom to top from 1 to n. Columns are numbered from left to right from 1 to n. The cell at the intersection of the x-th column and the y-th row is denoted as (x, y). Furthermore, the k-th column is a special column.
5
+
6
+ Initially, the board is empty. There are m changes to the board. During the i-th change one pawn is added or removed from the board. The current board is good if we can move all pawns to the special column by the followings rules:
7
+
8
+ * Pawn in the cell (x, y) can be moved to the cell (x, y + 1), (x - 1, y + 1) or (x + 1, y + 1);
9
+ * You can make as many such moves as you like;
10
+ * Pawns can not be moved outside the chessboard;
11
+ * Each cell can not contain more than one pawn.
12
+
13
+
14
+
15
+ The current board may not always be good. To fix it, you can add new rows to the board. New rows are added at the top, i. e. they will have numbers n+1, n+2, n+3, ....
16
+
17
+ After each of m changes, print one integer — the minimum number of rows which you have to add to make the board good.
18
+
19
+ Input
20
+
21
+ The first line contains three integers n, k and m (1 ≤ n, m ≤ 2 ⋅ 10^5; 1 ≤ k ≤ n) — the size of the board, the index of the special column and the number of changes respectively.
22
+
23
+ Then m lines follow. The i-th line contains two integers x and y (1 ≤ x, y ≤ n) — the index of the column and the index of the row respectively. If there is no pawn in the cell (x, y), then you add a pawn to this cell, otherwise — you remove the pawn from this cell.
24
+
25
+ Output
26
+
27
+ After each change print one integer — the minimum number of rows which you have to add to make the board good.
28
+
29
+ Example
30
+
31
+ Input
32
+
33
+
34
+ 5 3 5
35
+ 4 4
36
+ 3 5
37
+ 2 4
38
+ 3 4
39
+ 3 5
40
+
41
+
42
+ Output
43
+
44
+
45
+ 0
46
+ 1
47
+ 2
48
+ 2
49
+ 1
50
+
51
+ ## Contest Information
52
+ - **Contest ID**: 1373
53
+ - **Problem Index**: G
54
+ - **Points**: 0.0
55
+ - **Rating**: 2600
56
+ - **Tags**: data structures, divide and conquer, greedy
57
+ - **Time Limit**: {'seconds': 3, 'nanos': 0} seconds
58
+ - **Memory Limit**: 256000000 bytes
59
+
60
+ ## Task
61
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10317/instruction.md ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 102_C. Homework
2
+
3
+ ## Problem Description
4
+ Once when Gerald studied in the first year at school, his teacher gave the class the following homework. She offered the students a string consisting of n small Latin letters; the task was to learn the way the letters that the string contains are written. However, as Gerald is too lazy, he has no desire whatsoever to learn those letters. That's why he decided to lose some part of the string (not necessarily a connected part). The lost part can consist of any number of segments of any length, at any distance from each other. However, Gerald knows that if he loses more than k characters, it will be very suspicious.
5
+
6
+ Find the least number of distinct characters that can remain in the string after no more than k characters are deleted. You also have to find any possible way to delete the characters.
7
+
8
+ Input
9
+
10
+ The first input data line contains a string whose length is equal to n (1 ≤ n ≤ 105). The string consists of lowercase Latin letters. The second line contains the number k (0 ≤ k ≤ 105).
11
+
12
+ Output
13
+
14
+ Print on the first line the only number m — the least possible number of different characters that could remain in the given string after it loses no more than k characters.
15
+
16
+ Print on the second line the string that Gerald can get after some characters are lost. The string should have exactly m distinct characters. The final string should be the subsequence of the initial string. If Gerald can get several different strings with exactly m distinct characters, print any of them.
17
+
18
+ Examples
19
+
20
+ Input
21
+
22
+ aaaaa
23
+ 4
24
+
25
+
26
+ Output
27
+
28
+ 1
29
+ aaaaa
30
+
31
+
32
+ Input
33
+
34
+ abacaba
35
+ 4
36
+
37
+
38
+ Output
39
+
40
+ 1
41
+ aaaa
42
+
43
+
44
+ Input
45
+
46
+ abcdefgh
47
+ 10
48
+
49
+
50
+ Output
51
+
52
+ 0
53
+
54
+ Note
55
+
56
+ In the first sample the string consists of five identical letters but you are only allowed to delete 4 of them so that there was at least one letter left. Thus, the right answer is 1 and any string consisting of characters "a" from 1 to 5 in length.
57
+
58
+ In the second sample you are allowed to delete 4 characters. You cannot delete all the characters, because the string has length equal to 7. However, you can delete all characters apart from "a" (as they are no more than four), which will result in the "aaaa" string.
59
+
60
+ In the third sample you are given a line whose length is equal to 8, and k = 10, so that the whole line can be deleted. The correct answer is 0 and an empty string.
61
+
62
+ ## Contest Information
63
+ - **Contest ID**: 102
64
+ - **Problem Index**: C
65
+ - **Points**: 500.0
66
+ - **Rating**: 1200
67
+ - **Tags**: greedy
68
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
69
+ - **Memory Limit**: 256000000 bytes
70
+
71
+ ## Task
72
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10319/instruction.md ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1073_D. Berland Fair
2
+
3
+ ## Problem Description
4
+ XXI Berland Annual Fair is coming really soon! Traditionally fair consists of n booths, arranged in a circle. The booths are numbered 1 through n clockwise with n being adjacent to 1. The i-th booths sells some candies for the price of a_i burles per item. Each booth has an unlimited supply of candies.
5
+
6
+ Polycarp has decided to spend at most T burles at the fair. However, he has some plan in mind for his path across the booths:
7
+
8
+ * at first, he visits booth number 1;
9
+ * if he has enough burles to buy exactly one candy from the current booth, then he buys it immediately;
10
+ * then he proceeds to the next booth in the clockwise order (regardless of if he bought a candy or not).
11
+
12
+
13
+
14
+ Polycarp's money is finite, thus the process will end once he can no longer buy candy at any booth.
15
+
16
+ Calculate the number of candies Polycarp will buy.
17
+
18
+ Input
19
+
20
+ The first line contains two integers n and T (1 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ T ≤ 10^{18}) — the number of booths at the fair and the initial amount of burles Polycarp has.
21
+
22
+ The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9) — the price of the single candy at booth number i.
23
+
24
+ Output
25
+
26
+ Print a single integer — the total number of candies Polycarp will buy.
27
+
28
+ Examples
29
+
30
+ Input
31
+
32
+ 3 38
33
+ 5 2 5
34
+
35
+
36
+ Output
37
+
38
+ 10
39
+
40
+
41
+ Input
42
+
43
+ 5 21
44
+ 2 4 100 2 6
45
+
46
+
47
+ Output
48
+
49
+ 6
50
+
51
+ Note
52
+
53
+ Let's consider the first example. Here are Polycarp's moves until he runs out of money:
54
+
55
+ 1. Booth 1, buys candy for 5, T = 33;
56
+ 2. Booth 2, buys candy for 2, T = 31;
57
+ 3. Booth 3, buys candy for 5, T = 26;
58
+ 4. Booth 1, buys candy for 5, T = 21;
59
+ 5. Booth 2, buys candy for 2, T = 19;
60
+ 6. Booth 3, buys candy for 5, T = 14;
61
+ 7. Booth 1, buys candy for 5, T = 9;
62
+ 8. Booth 2, buys candy for 2, T = 7;
63
+ 9. Booth 3, buys candy for 5, T = 2;
64
+ 10. Booth 1, buys no candy, not enough money;
65
+ 11. Booth 2, buys candy for 2, T = 0.
66
+
67
+
68
+
69
+ No candy can be bought later. The total number of candies bought is 10.
70
+
71
+ In the second example he has 1 burle left at the end of his path, no candy can be bought with this amount.
72
+
73
+ ## Contest Information
74
+ - **Contest ID**: 1073
75
+ - **Problem Index**: D
76
+ - **Points**: 0.0
77
+ - **Rating**: 1700
78
+ - **Tags**: binary search, brute force, data structures, greedy
79
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
80
+ - **Memory Limit**: 256000000 bytes
81
+
82
+ ## Task
83
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10326/instruction.md ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1219_A. BubbleReactor
2
+
3
+ ## Problem Description
4
+ You are in charge of the BubbleReactor. It consists of N BubbleCores connected with N lines of electrical wiring. Each electrical wiring connects two distinct BubbleCores. There are no BubbleCores connected with more than one line of electrical wiring.
5
+
6
+ Your task is to start the BubbleReactor by starting each BubbleCore. In order for a BubbleCore to be started it needs to be receiving power from a directly connected BubbleCore which is already started. However, you can kick-start one BubbleCore manually without needing power. It is guaranteed that all BubbleCores can be started.
7
+
8
+ Before the BubbleCore boot up procedure its potential is calculated as the number of BubbleCores it can power on (the number of inactive BubbleCores which are connected to it directly or with any number of inactive BubbleCores in between, itself included)
9
+
10
+ Start the BubbleReactor so that the sum of all BubbleCores' potentials is maximum.
11
+
12
+ Input
13
+
14
+ First line contains one integer N (3 ≤ N ≤ 15.000), the number of BubbleCores.
15
+
16
+ The following N lines contain two integers U, V (0 ≤ U ≠ V < N) denoting that there exists electrical wiring between BubbleCores U and V.
17
+
18
+ Output
19
+
20
+ Single integer, the maximum sum of all BubbleCores' potentials.
21
+
22
+ Example
23
+
24
+ Input
25
+
26
+
27
+ 10
28
+ 0 1
29
+ 0 3
30
+ 0 4
31
+ 0 9
32
+ 1 2
33
+ 2 3
34
+ 2 7
35
+ 4 5
36
+ 4 6
37
+ 7 8
38
+
39
+
40
+ Output
41
+
42
+
43
+ 51
44
+
45
+ Note
46
+
47
+ If we start by kickstarting BubbleCup 8 and then turning on cores 7, 2, 1, 3, 0, 9, 4, 5, 6 in that order we get potentials 10 + 9 + 8 + 7 + 6 + 5 + 1 + 3 + 1 + 1 = 51
48
+
49
+ ## Contest Information
50
+ - **Contest ID**: 1219
51
+ - **Problem Index**: A
52
+ - **Points**: 0.0
53
+ - **Rating**: 2800
54
+ - **Tags**: dp, graphs
55
+ - **Time Limit**: {'seconds': 1, 'nanos': 500000000} seconds
56
+ - **Memory Limit**: 256000000 bytes
57
+
58
+ ## Task
59
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10328/instruction.md ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1261_F. Xor-Set
2
+
3
+ ## Problem Description
4
+ You are given two sets of integers: A and B. You need to output the sum of elements in the set C = \\{x | x = a ⊕ b, a ∈ A, b ∈ B\} modulo 998244353, where ⊕ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). Each number should be counted only once.
5
+
6
+ For example, if A = \{2, 3\} and B = \{2, 3\} you should count integer 1 only once, despite the fact that you can get it as 3 ⊕ 2 and as 2 ⊕ 3. So the answer for this case is equal to 1 + 0 = 1.
7
+
8
+ Let's call a segment [l; r] a set of integers \\{l, l+1, ..., r\}.
9
+
10
+ The set A is given as a union of n_A segments, the set B is given as a union of n_B segments.
11
+
12
+ Input
13
+
14
+ The first line contains a single integer n_A (1 ≤ n_A ≤ 100).
15
+
16
+ The i-th of the next n_A lines contains two integers l_i and r_i (1 ≤ l_i ≤ r_i ≤ 10^{18}), describing a segment of values of set A.
17
+
18
+ The next line contains a single integer n_B (1 ≤ n_B ≤ 100).
19
+
20
+ The i-th of the next n_B lines contains two integers l_j and r_j (1 ≤ l_j ≤ r_j ≤ 10^{18}), describing a segment of values of set B.
21
+
22
+ Note that segments in both sets may intersect.
23
+
24
+ Output
25
+
26
+ Print one integer — the sum of all elements in set C = \\{x | x = a ⊕ b, a ∈ A, b ∈ B\} modulo 998244353.
27
+
28
+ Examples
29
+
30
+ Input
31
+
32
+
33
+ 2
34
+ 3 5
35
+ 5 8
36
+ 3
37
+ 1 2
38
+ 1 9
39
+ 2 9
40
+
41
+
42
+ Output
43
+
44
+
45
+ 112
46
+
47
+
48
+ Input
49
+
50
+
51
+ 1
52
+ 1 9
53
+ 2
54
+ 2 4
55
+ 2 10
56
+
57
+
58
+ Output
59
+
60
+
61
+ 120
62
+
63
+ Note
64
+
65
+ In the second example, we can discover that the set C = \{0,1,...,15\}, which means that all numbers between 0 and 15 can be represented as a ⊕ b.
66
+
67
+ ## Contest Information
68
+ - **Contest ID**: 1261
69
+ - **Problem Index**: F
70
+ - **Points**: 2500.0
71
+ - **Rating**: 3100
72
+ - **Tags**: bitmasks, divide and conquer, math
73
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
74
+ - **Memory Limit**: 256000000 bytes
75
+
76
+ ## Task
77
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-1052/instruction.md ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1070_D. Garbage Disposal
2
+
3
+ ## Problem Description
4
+ Enough is enough. Too many times it happened that Vasya forgot to dispose of garbage and his apartment stank afterwards. Now he wants to create a garbage disposal plan and stick to it.
5
+
6
+ For each of next n days Vasya knows a_i — number of units of garbage he will produce on the i-th day. Each unit of garbage must be disposed of either on the day it was produced or on the next day. Vasya disposes of garbage by putting it inside a bag and dropping the bag into a garbage container. Each bag can contain up to k units of garbage. It is allowed to compose and drop multiple bags into a garbage container in a single day.
7
+
8
+ Being economical, Vasya wants to use as few bags as possible. You are to compute the minimum number of bags Vasya needs to dispose of all of his garbage for the given n days. No garbage should be left after the n-th day.
9
+
10
+ Input
11
+
12
+ The first line of the input contains two integers n and k (1 ≤ n ≤ 2⋅10^5, 1 ≤ k ≤ 10^9) — number of days to consider and bag's capacity. The second line contains n space separated integers a_i (0 ≤ a_i ≤ 10^9) — the number of units of garbage produced on the i-th day.
13
+
14
+ Output
15
+
16
+ Output a single integer — the minimum number of bags Vasya needs to dispose of all garbage. Each unit of garbage should be disposed on the day it was produced or on the next day. No garbage can be left after the n-th day. In a day it is allowed to compose and drop multiple bags.
17
+
18
+ Examples
19
+
20
+ Input
21
+
22
+ 3 2
23
+ 3 2 1
24
+
25
+
26
+ Output
27
+
28
+ 3
29
+
30
+
31
+ Input
32
+
33
+ 5 1
34
+ 1000000000 1000000000 1000000000 1000000000 1000000000
35
+
36
+
37
+ Output
38
+
39
+ 5000000000
40
+
41
+
42
+ Input
43
+
44
+ 3 2
45
+ 1 0 1
46
+
47
+
48
+ Output
49
+
50
+ 2
51
+
52
+
53
+ Input
54
+
55
+ 4 4
56
+ 2 8 4 1
57
+
58
+
59
+ Output
60
+
61
+ 4
62
+
63
+ ## Contest Information
64
+ - **Contest ID**: 1070
65
+ - **Problem Index**: D
66
+ - **Points**: 0.0
67
+ - **Rating**: 1300
68
+ - **Tags**: greedy
69
+ - **Time Limit**: {'seconds': 3, 'nanos': 0} seconds
70
+ - **Memory Limit**: 256000000 bytes
71
+
72
+ ## Task
73
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10541/instruction.md ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 137_D. Palindromes
2
+
3
+ ## Problem Description
4
+ Friday is Polycarpus' favourite day of the week. Not because it is followed by the weekend, but because the lessons on Friday are 2 IT lessons, 2 math lessons and 2 literature lessons. Of course, Polycarpus has prepared to all of them, unlike his buddy Innocentius. Innocentius spent all evening playing his favourite game Fur2 and didn't have enough time to do the literature task. As Innocentius didn't want to get an F, he decided to do the task and read the book called "Storm and Calm" during the IT and Math lessons (he never used to have problems with these subjects). When the IT teacher Mr. Watkins saw this, he decided to give Innocentius another task so that the boy concentrated more on the lesson and less — on the staff that has nothing to do with IT.
5
+
6
+ Mr. Watkins said that a palindrome is a string that can be read the same way in either direction, from the left to the right and from the right to the left. A concatenation of strings a, b is a string ab that results from consecutive adding of string b to string a. Of course, Innocentius knew it all but the task was much harder than he could have imagined. Mr. Watkins asked change in the "Storm and Calm" the minimum number of characters so that the text of the book would also be a concatenation of no more than k palindromes. Innocentius can't complete the task and therefore asks you to help him.
7
+
8
+ Input
9
+
10
+ The first input line contains a non-empty string s which is the text of "Storm and Calm" (without spaces). The length of the string s does not exceed 500 characters. String s consists of uppercase and lowercase Latin letters. The second line contains a single number k (1 ≤ k ≤ |s|, where |s| represents the length of the string s).
11
+
12
+ Output
13
+
14
+ Print on the first line the minimum number of changes that Innocentius will have to make. Print on the second line the string consisting of no more than k palindromes. Each palindrome should be non-empty and consist of uppercase and lowercase Latin letters. Use the character "+" (ASCII-code 43) to separate consecutive palindromes. If there exist several solutions, print any of them.
15
+
16
+ The letters' case does matter, that is an uppercase letter is not considered equivalent to the corresponding lowercase letter.
17
+
18
+ Examples
19
+
20
+ Input
21
+
22
+ abacaba
23
+ 1
24
+
25
+
26
+ Output
27
+
28
+ 0
29
+ abacaba
30
+
31
+
32
+ Input
33
+
34
+ abdcaba
35
+ 2
36
+
37
+
38
+ Output
39
+
40
+ 1
41
+ abdcdba
42
+
43
+
44
+ Input
45
+
46
+ abdcaba
47
+ 5
48
+
49
+
50
+ Output
51
+
52
+ 0
53
+ a+b+d+c+aba
54
+
55
+
56
+ Input
57
+
58
+ abacababababbcbabcd
59
+ 3
60
+
61
+
62
+ Output
63
+
64
+ 1
65
+ abacaba+babab+bcbabcb
66
+
67
+ ## Contest Information
68
+ - **Contest ID**: 137
69
+ - **Problem Index**: D
70
+ - **Points**: 2000.0
71
+ - **Rating**: 1900
72
+ - **Tags**: dp, strings
73
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
74
+ - **Memory Limit**: 256000000 bytes
75
+
76
+ ## Task
77
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10546/instruction.md ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1494_C. 1D Sokoban
2
+
3
+ ## Problem Description
4
+ You are playing a game similar to Sokoban on an infinite number line. The game is discrete, so you only consider integer positions on the line.
5
+
6
+ You start on a position 0. There are n boxes, the i-th box is on a position a_i. All positions of the boxes are distinct. There are also m special positions, the j-th position is b_j. All the special positions are also distinct.
7
+
8
+ In one move you can go one position to the left or to the right. If there is a box in the direction of your move, then you push the box to the next position in that direction. If the next position is taken by another box, then that box is also pushed to the next position, and so on. You can't go through the boxes. You can't pull the boxes towards you.
9
+
10
+ You are allowed to perform any number of moves (possibly, zero). Your goal is to place as many boxes on special positions as possible. Note that some boxes can be initially placed on special positions.
11
+
12
+ Input
13
+
14
+ The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of testcases.
15
+
16
+ Then descriptions of t testcases follow.
17
+
18
+ The first line of each testcase contains two integers n and m (1 ≤ n, m ≤ 2 ⋅ 10^5) — the number of boxes and the number of special positions, respectively.
19
+
20
+ The second line of each testcase contains n distinct integers in the increasing order a_1, a_2, ..., a_n (-10^9 ≤ a_1 < a_2 < ... < a_n ≤ 10^9; a_i ≠ 0) — the initial positions of the boxes.
21
+
22
+ The third line of each testcase contains m distinct integers in the increasing order b_1, b_2, ..., b_m (-10^9 ≤ b_1 < b_2 < ... < b_m ≤ 10^9; b_i ≠ 0) — the special positions.
23
+
24
+ The sum of n over all testcases doesn't exceed 2 ⋅ 10^5. The sum of m over all testcases doesn't exceed 2 ⋅ 10^5.
25
+
26
+ Output
27
+
28
+ For each testcase print a single integer — the maximum number of boxes that can be placed on special positions.
29
+
30
+ Example
31
+
32
+ Input
33
+
34
+
35
+ 5
36
+ 5 6
37
+ -1 1 5 11 15
38
+ -4 -3 -2 6 7 15
39
+ 2 2
40
+ -1 1
41
+ -1000000000 1000000000
42
+ 2 2
43
+ -1000000000 1000000000
44
+ -1 1
45
+ 3 5
46
+ -1 1 2
47
+ -2 -1 1 2 5
48
+ 2 1
49
+ 1 2
50
+ 10
51
+
52
+
53
+ Output
54
+
55
+
56
+ 4
57
+ 2
58
+ 0
59
+ 3
60
+ 1
61
+
62
+ Note
63
+
64
+ In the first testcase you can go 5 to the right: the box on position 1 gets pushed to position 6 and the box on position 5 gets pushed to position 7. Then you can go 6 to the left to end up on position -1 and push a box to -2. At the end, the boxes are on positions [-2, 6, 7, 11, 15], respectively. Among them positions [-2, 6, 7, 15] are special, thus, the answer is 4.
65
+
66
+ In the second testcase you can push the box from -1 to -10^9, then the box from 1 to 10^9 and obtain the answer 2.
67
+
68
+ The third testcase showcases that you are not allowed to pull the boxes, thus, you can't bring them closer to special positions.
69
+
70
+ In the fourth testcase all the boxes are already on special positions, so you can do nothing and still obtain the answer 3.
71
+
72
+ In the fifth testcase there are fewer special positions than boxes. You can move either 8 or 9 to the right to have some box on position 10.
73
+
74
+ ## Contest Information
75
+ - **Contest ID**: 1494
76
+ - **Problem Index**: C
77
+ - **Points**: 0.0
78
+ - **Rating**: 1900
79
+ - **Tags**: binary search, dp, greedy, implementation, two pointers
80
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
81
+ - **Memory Limit**: 256000000 bytes
82
+
83
+ ## Task
84
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-1055/instruction.md ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1140_C. Playlist
2
+
3
+ ## Problem Description
4
+ You have a playlist consisting of n songs. The i-th song is characterized by two numbers t_i and b_i — its length and beauty respectively. The pleasure of listening to set of songs is equal to the total length of the songs in the set multiplied by the minimum beauty among them. For example, the pleasure of listening to a set of 3 songs having lengths [5, 7, 4] and beauty values [11, 14, 6] is equal to (5 + 7 + 4) ⋅ 6 = 96.
5
+
6
+ You need to choose at most k songs from your playlist, so the pleasure of listening to the set of these songs them is maximum possible.
7
+
8
+ Input
9
+
10
+ The first line contains two integers n and k (1 ≤ k ≤ n ≤ 3 ⋅ 10^5) – the number of songs in the playlist and the maximum number of songs you can choose, respectively.
11
+
12
+ Each of the next n lines contains two integers t_i and b_i (1 ≤ t_i, b_i ≤ 10^6) — the length and beauty of i-th song.
13
+
14
+ Output
15
+
16
+ Print one integer — the maximum pleasure you can get.
17
+
18
+ Examples
19
+
20
+ Input
21
+
22
+
23
+ 4 3
24
+ 4 7
25
+ 15 1
26
+ 3 6
27
+ 6 8
28
+
29
+
30
+ Output
31
+
32
+
33
+ 78
34
+
35
+
36
+ Input
37
+
38
+
39
+ 5 3
40
+ 12 31
41
+ 112 4
42
+ 100 100
43
+ 13 55
44
+ 55 50
45
+
46
+
47
+ Output
48
+
49
+
50
+ 10000
51
+
52
+ Note
53
+
54
+ In the first test case we can choose songs {1, 3, 4}, so the total pleasure is (4 + 3 + 6) ⋅ 6 = 78.
55
+
56
+ In the second test case we can choose song 3. The total pleasure will be equal to 100 ⋅ 100 = 10000.
57
+
58
+ ## Contest Information
59
+ - **Contest ID**: 1140
60
+ - **Problem Index**: C
61
+ - **Points**: 0.0
62
+ - **Rating**: 1600
63
+ - **Tags**: brute force, data structures, sortings
64
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
65
+ - **Memory Limit**: 256000000 bytes
66
+
67
+ ## Task
68
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10570/instruction.md ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 667_B. Coat of Anticubism
2
+
3
+ ## Problem Description
4
+ <image>
5
+
6
+ As some of you know, cubism is a trend in art, where the problem of constructing volumetrical shape on a plane with a combination of three-dimensional geometric shapes comes to the fore.
7
+
8
+ A famous sculptor Cicasso, whose self-portrait you can contemplate, hates cubism. He is more impressed by the idea to transmit two-dimensional objects through three-dimensional objects by using his magnificent sculptures. And his new project is connected with this. Cicasso wants to make a coat for the haters of anticubism. To do this, he wants to create a sculpture depicting a well-known geometric primitive — convex polygon.
9
+
10
+ Cicasso prepared for this a few blanks, which are rods with integer lengths, and now he wants to bring them together. The i-th rod is a segment of length li.
11
+
12
+ The sculptor plans to make a convex polygon with a nonzero area, using all rods he has as its sides. Each rod should be used as a side to its full length. It is forbidden to cut, break or bend rods. However, two sides may form a straight angle <image>.
13
+
14
+ Cicasso knows that it is impossible to make a convex polygon with a nonzero area out of the rods with the lengths which he had chosen. Cicasso does not want to leave the unused rods, so the sculptor decides to make another rod-blank with an integer length so that his problem is solvable. Of course, he wants to make it as short as possible, because the materials are expensive, and it is improper deed to spend money for nothing.
15
+
16
+ Help sculptor!
17
+
18
+ Input
19
+
20
+ The first line contains an integer n (3 ≤ n ≤ 105) — a number of rod-blanks.
21
+
22
+ The second line contains n integers li (1 ≤ li ≤ 109) — lengths of rods, which Cicasso already has. It is guaranteed that it is impossible to make a polygon with n vertices and nonzero area using the rods Cicasso already has.
23
+
24
+ Output
25
+
26
+ Print the only integer z — the minimum length of the rod, so that after adding it it can be possible to construct convex polygon with (n + 1) vertices and nonzero area from all of the rods.
27
+
28
+ Examples
29
+
30
+ Input
31
+
32
+ 3
33
+ 1 2 1
34
+
35
+
36
+ Output
37
+
38
+ 1
39
+
40
+
41
+ Input
42
+
43
+ 5
44
+ 20 4 3 2 1
45
+
46
+
47
+ Output
48
+
49
+ 11
50
+
51
+ Note
52
+
53
+ In the first example triangle with sides {1 + 1 = 2, 2, 1} can be formed from a set of lengths {1, 1, 1, 2}.
54
+
55
+ In the second example you can make a triangle with lengths {20, 11, 4 + 3 + 2 + 1 = 10}.
56
+
57
+ ## Contest Information
58
+ - **Contest ID**: 667
59
+ - **Problem Index**: B
60
+ - **Points**: 1000.0
61
+ - **Rating**: 1100
62
+ - **Tags**: constructive algorithms, geometry
63
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
64
+ - **Memory Limit**: 256000000 bytes
65
+
66
+ ## Task
67
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10577/instruction.md ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 827_C. DNA Evolution
2
+
3
+ ## Problem Description
4
+ Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A", "T", "G", "C". A DNA strand is a sequence of nucleotides. Scientists decided to track evolution of a rare species, which DNA strand was string s initially.
5
+
6
+ Evolution of the species is described as a sequence of changes in the DNA. Every change is a change of some nucleotide, for example, the following change can happen in DNA strand "AAGC": the second nucleotide can change to "T" so that the resulting DNA strand is "ATGC".
7
+
8
+ Scientists know that some segments of the DNA strand can be affected by some unknown infections. They can represent an infection as a sequence of nucleotides. Scientists are interested if there are any changes caused by some infections. Thus they sometimes want to know the value of impact of some infection to some segment of the DNA. This value is computed as follows:
9
+
10
+ * Let the infection be represented as a string e, and let scientists be interested in DNA strand segment starting from position l to position r, inclusive.
11
+ * Prefix of the string eee... (i.e. the string that consists of infinitely many repeats of string e) is written under the string s from position l to position r, inclusive.
12
+ * The value of impact is the number of positions where letter of string s coincided with the letter written under it.
13
+
14
+
15
+
16
+ Being a developer, Innokenty is interested in bioinformatics also, so the scientists asked him for help. Innokenty is busy preparing VK Cup, so he decided to delegate the problem to the competitors. Help the scientists!
17
+
18
+ Input
19
+
20
+ The first line contains the string s (1 ≤ |s| ≤ 105) that describes the initial DNA strand. It consists only of capital English letters "A", "T", "G" and "C".
21
+
22
+ The next line contains single integer q (1 ≤ q ≤ 105) — the number of events.
23
+
24
+ After that, q lines follow, each describes one event. Each of the lines has one of two formats:
25
+
26
+ * 1 x c, where x is an integer (1 ≤ x ≤ |s|), and c is a letter "A", "T", "G" or "C", which means that there is a change in the DNA: the nucleotide at position x is now c.
27
+ * 2 l r e, where l, r are integers (1 ≤ l ≤ r ≤ |s|), and e is a string of letters "A", "T", "G" and "C" (1 ≤ |e| ≤ 10), which means that scientists are interested in the value of impact of infection e to the segment of DNA strand from position l to position r, inclusive.
28
+
29
+ Output
30
+
31
+ For each scientists' query (second type query) print a single integer in a new line — the value of impact of the infection on the DNA.
32
+
33
+ Examples
34
+
35
+ Input
36
+
37
+ ATGCATGC
38
+ 4
39
+ 2 1 8 ATGC
40
+ 2 2 6 TTT
41
+ 1 4 T
42
+ 2 2 6 TA
43
+
44
+
45
+ Output
46
+
47
+ 8
48
+ 2
49
+ 4
50
+
51
+
52
+ Input
53
+
54
+ GAGTTGTTAA
55
+ 6
56
+ 2 3 4 TATGGTG
57
+ 1 1 T
58
+ 1 6 G
59
+ 2 5 9 AGTAATA
60
+ 1 10 G
61
+ 2 2 6 TTGT
62
+
63
+
64
+ Output
65
+
66
+ 0
67
+ 3
68
+ 1
69
+
70
+ Note
71
+
72
+ Consider the first example. In the first query of second type all characters coincide, so the answer is 8. In the second query we compare string "TTTTT..." and the substring "TGCAT". There are two matches. In the third query, after the DNA change, we compare string "TATAT..."' with substring "TGTAT". There are 4 matches.
73
+
74
+ ## Contest Information
75
+ - **Contest ID**: 827
76
+ - **Problem Index**: C
77
+ - **Points**: 1500.0
78
+ - **Rating**: 2100
79
+ - **Tags**: data structures, strings
80
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
81
+ - **Memory Limit**: 512000000 bytes
82
+
83
+ ## Task
84
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10583/instruction.md ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 96_C. Hockey
2
+
3
+ ## Problem Description
4
+ Petya loves hockey very much. One day, as he was watching a hockey match, he fell asleep. Petya dreamt of being appointed to change a hockey team's name. Thus, Petya was given the original team name w and the collection of forbidden substrings s1, s2, ..., sn. All those strings consist of uppercase and lowercase Latin letters. String w has the length of |w|, its characters are numbered from 1 to |w|.
5
+
6
+ First Petya should find all the occurrences of forbidden substrings in the w string. During the search of substrings the case of letter shouldn't be taken into consideration. That is, strings "aBC" and "ABc" are considered equal.
7
+
8
+ After that Petya should perform the replacement of all letters covered by the occurrences. More formally: a letter in the position i should be replaced by any other one if for position i in string w there exist pair of indices l, r (1 ≤ l ≤ i ≤ r ≤ |w|) such that substring w[l ... r] is contained in the collection s1, s2, ..., sn, when using case insensitive comparison. During the replacement the letter's case should remain the same. Petya is not allowed to replace the letters that aren't covered by any forbidden substring.
9
+
10
+ Letter letter (uppercase or lowercase) is considered lucky for the hockey players. That's why Petya should perform the changes so that the letter occurred in the resulting string as many times as possible. Help Petya to find such resulting string. If there are several such strings, find the one that comes first lexicographically.
11
+
12
+ Note that the process of replacements is not repeated, it occurs only once. That is, if after Petya's replacements the string started to contain new occurrences of bad substrings, Petya pays no attention to them.
13
+
14
+ Input
15
+
16
+ The first line contains the only integer n (1 ≤ n ≤ 100) — the number of forbidden substrings in the collection. Next n lines contain these substrings. The next line contains string w. All those n + 1 lines are non-empty strings consisting of uppercase and lowercase Latin letters whose length does not exceed 100. The last line contains a lowercase letter letter.
17
+
18
+ Output
19
+
20
+ Output the only line — Petya's resulting string with the maximum number of letters letter. If there are several answers then output the one that comes first lexicographically.
21
+
22
+ The lexicographical comparison is performed by the standard < operator in modern programming languages. The line a is lexicographically smaller than the line b, if a is a prefix of b, or there exists such an i (1 ≤ i ≤ |a|), that ai < bi, and for any j (1 ≤ j < i) aj = bj. |a| stands for the length of string a.
23
+
24
+ Examples
25
+
26
+ Input
27
+
28
+ 3
29
+ bers
30
+ ucky
31
+ elu
32
+ PetrLoveLuckyNumbers
33
+ t
34
+
35
+
36
+ Output
37
+
38
+ PetrLovtTttttNumtttt
39
+
40
+
41
+ Input
42
+
43
+ 4
44
+ hello
45
+ party
46
+ abefglghjdhfgj
47
+ IVan
48
+ petrsmatchwin
49
+ a
50
+
51
+
52
+ Output
53
+
54
+ petrsmatchwin
55
+
56
+
57
+ Input
58
+
59
+ 2
60
+ aCa
61
+ cba
62
+ abAcaba
63
+ c
64
+
65
+
66
+ Output
67
+
68
+ abCacba
69
+
70
+ ## Contest Information
71
+ - **Contest ID**: 96
72
+ - **Problem Index**: C
73
+ - **Points**: 500.0
74
+ - **Rating**: 1600
75
+ - **Tags**: implementation, strings
76
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
77
+ - **Memory Limit**: 256000000 bytes
78
+
79
+ ## Task
80
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-1063/instruction.md ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1301_C. Ayoub's function
2
+
3
+ ## Problem Description
4
+ Ayoub thinks that he is a very smart person, so he created a function f(s), where s is a binary string (a string which contains only symbols "0" and "1"). The function f(s) is equal to the number of substrings in the string s that contains at least one symbol, that is equal to "1".
5
+
6
+ More formally, f(s) is equal to the number of pairs of integers (l, r), such that 1 ≤ l ≤ r ≤ |s| (where |s| is equal to the length of string s), such that at least one of the symbols s_l, s_{l+1}, …, s_r is equal to "1".
7
+
8
+ For example, if s = "01010" then f(s) = 12, because there are 12 such pairs (l, r): (1, 2), (1, 3), (1, 4), (1, 5), (2, 2), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5).
9
+
10
+ Ayoub also thinks that he is smarter than Mahmoud so he gave him two integers n and m and asked him this problem. For all binary strings s of length n which contains exactly m symbols equal to "1", find the maximum value of f(s).
11
+
12
+ Mahmoud couldn't solve the problem so he asked you for help. Can you help him?
13
+
14
+ Input
15
+
16
+ The input consists of multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 10^5) — the number of test cases. The description of the test cases follows.
17
+
18
+ The only line for each test case contains two integers n, m (1 ≤ n ≤ 10^{9}, 0 ≤ m ≤ n) — the length of the string and the number of symbols equal to "1" in it.
19
+
20
+ Output
21
+
22
+ For every test case print one integer number — the maximum value of f(s) over all strings s of length n, which has exactly m symbols, equal to "1".
23
+
24
+ Example
25
+
26
+ Input
27
+
28
+
29
+ 5
30
+ 3 1
31
+ 3 2
32
+ 3 3
33
+ 4 0
34
+ 5 2
35
+
36
+
37
+ Output
38
+
39
+
40
+ 4
41
+ 5
42
+ 6
43
+ 0
44
+ 12
45
+
46
+ Note
47
+
48
+ In the first test case, there exists only 3 strings of length 3, which has exactly 1 symbol, equal to "1". These strings are: s_1 = "100", s_2 = "010", s_3 = "001". The values of f for them are: f(s_1) = 3, f(s_2) = 4, f(s_3) = 3, so the maximum value is 4 and the answer is 4.
49
+
50
+ In the second test case, the string s with the maximum value is "101".
51
+
52
+ In the third test case, the string s with the maximum value is "111".
53
+
54
+ In the fourth test case, the only string s of length 4, which has exactly 0 symbols, equal to "1" is "0000" and the value of f for that string is 0, so the answer is 0.
55
+
56
+ In the fifth test case, the string s with the maximum value is "01010" and it is described as an example in the problem statement.
57
+
58
+ ## Contest Information
59
+ - **Contest ID**: 1301
60
+ - **Problem Index**: C
61
+ - **Points**: 1250.0
62
+ - **Rating**: 1700
63
+ - **Tags**: binary search, combinatorics, greedy, math, strings
64
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
65
+ - **Memory Limit**: 256000000 bytes
66
+
67
+ ## Task
68
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10742/instruction.md ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1237_C1. Balanced Removals (Easier)
2
+
3
+ ## Problem Description
4
+ This is an easier version of the problem. In this version, n ≤ 2000.
5
+
6
+ There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.
7
+
8
+ You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.
9
+
10
+ Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate.
11
+
12
+ Find a way to remove all points in n/2 snaps.
13
+
14
+ Input
15
+
16
+ The first line contains a single integer n (2 ≤ n ≤ 2000; n is even), denoting the number of points.
17
+
18
+ Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.
19
+
20
+ No two points coincide.
21
+
22
+ Output
23
+
24
+ Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.
25
+
26
+ We can show that it is always possible to remove all points. If there are many solutions, output any of them.
27
+
28
+ Examples
29
+
30
+ Input
31
+
32
+
33
+ 6
34
+ 3 1 0
35
+ 0 3 0
36
+ 2 2 0
37
+ 1 0 0
38
+ 1 3 0
39
+ 0 1 0
40
+
41
+
42
+ Output
43
+
44
+
45
+ 3 6
46
+ 5 1
47
+ 2 4
48
+
49
+
50
+ Input
51
+
52
+
53
+ 8
54
+ 0 1 1
55
+ 1 0 1
56
+ 1 1 0
57
+ 1 1 1
58
+ 2 2 2
59
+ 3 2 2
60
+ 2 3 2
61
+ 2 2 3
62
+
63
+
64
+ Output
65
+
66
+
67
+ 4 5
68
+ 1 6
69
+ 2 7
70
+ 3 8
71
+
72
+ Note
73
+
74
+ In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed.
75
+
76
+ <image>
77
+
78
+ ## Contest Information
79
+ - **Contest ID**: 1237
80
+ - **Problem Index**: C1
81
+ - **Points**: 750.0
82
+ - **Rating**: 1700
83
+ - **Tags**: constructive algorithms, geometry, greedy
84
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
85
+ - **Memory Limit**: 512000000 bytes
86
+
87
+ ## Task
88
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10745/instruction.md ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 1299_D. Around the World
2
+
3
+ ## Problem Description
4
+ Guy-Manuel and Thomas are planning 144 trips around the world.
5
+
6
+ You are given a simple weighted undirected connected graph with n vertexes and m edges with the following restriction: there isn't any simple cycle (i. e. a cycle which doesn't pass through any vertex more than once) of length greater than 3 which passes through the vertex 1. The cost of a path (not necessarily simple) in this graph is defined as the [XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of weights of all edges in that path with each edge being counted as many times as the path passes through it.
7
+
8
+ But the trips with cost 0 aren't exciting.
9
+
10
+ You may choose any subset of edges incident to the vertex 1 and remove them. How many are there such subsets, that, when removed, there is not any nontrivial cycle with the cost equal to 0 which passes through the vertex 1 in the resulting graph? A cycle is called nontrivial if it passes through some edge odd number of times. As the answer can be very big, output it modulo 10^9+7.
11
+
12
+ Input
13
+
14
+ The first line contains two integers n and m (1 ≤ n,m ≤ 10^5) — the number of vertexes and edges in the graph. The i-th of the next m lines contains three integers a_i, b_i and w_i (1 ≤ a_i, b_i ≤ n, a_i ≠ b_i, 0 ≤ w_i < 32) — the endpoints of the i-th edge and its weight. It's guaranteed there aren't any multiple edges, the graph is connected and there isn't any simple cycle of length greater than 3 which passes through the vertex 1.
15
+
16
+ Output
17
+
18
+ Output the answer modulo 10^9+7.
19
+
20
+ Examples
21
+
22
+ Input
23
+
24
+
25
+ 6 8
26
+ 1 2 0
27
+ 2 3 1
28
+ 2 4 3
29
+ 2 6 2
30
+ 3 4 8
31
+ 3 5 4
32
+ 5 4 5
33
+ 5 6 6
34
+
35
+
36
+ Output
37
+
38
+
39
+ 2
40
+
41
+
42
+ Input
43
+
44
+
45
+ 7 9
46
+ 1 2 0
47
+ 1 3 1
48
+ 2 3 9
49
+ 2 4 3
50
+ 2 5 4
51
+ 4 5 7
52
+ 3 6 6
53
+ 3 7 7
54
+ 6 7 8
55
+
56
+
57
+ Output
58
+
59
+
60
+ 1
61
+
62
+
63
+ Input
64
+
65
+
66
+ 4 4
67
+ 1 2 27
68
+ 1 3 1
69
+ 1 4 1
70
+ 3 4 0
71
+
72
+
73
+ Output
74
+
75
+
76
+ 6
77
+
78
+ Note
79
+
80
+ The pictures below represent the graphs from examples. <image> In the first example, there aren't any nontrivial cycles with cost 0, so we can either remove or keep the only edge incident to the vertex 1. <image> In the second example, if we don't remove the edge 1-2, then there is a cycle 1-2-4-5-2-1 with cost 0; also if we don't remove the edge 1-3, then there is a cycle 1-3-2-4-5-2-3-1 of cost 0. The only valid subset consists of both edges. <image> In the third example, all subsets are valid except for those two in which both edges 1-3 and 1-4 are kept.
81
+
82
+ ## Contest Information
83
+ - **Contest ID**: 1299
84
+ - **Problem Index**: D
85
+ - **Points**: 1750.0
86
+ - **Rating**: 3000
87
+ - **Tags**: bitmasks, combinatorics, dfs and similar, dp, graphs, graphs, math, trees
88
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
89
+ - **Memory Limit**: 512000000 bytes
90
+
91
+ ## Task
92
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10780/instruction.md ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 717_G. Underfail
2
+
3
+ ## Problem Description
4
+ You have recently fallen through a hole and, after several hours of unconsciousness, have realized you are in an underground city. On one of your regular, daily walks through the unknown, you have encountered two unusually looking skeletons called Sanz and P’pairus, who decided to accompany you and give you some puzzles for seemingly unknown reasons.
5
+
6
+ One day, Sanz has created a crossword for you. Not any kind of crossword, but a 1D crossword! You are given m words and a string of length n. You are also given an array p, which designates how much each word is worth — the i-th word is worth pi points. Whenever you find one of the m words in the string, you are given the corresponding number of points. Each position in the crossword can be used at most x times. A certain word can be counted at different places, but you cannot count the same appearance of a word multiple times. If a word is a substring of another word, you can count them both (presuming you haven’t used the positions more than x times).
7
+
8
+ In order to solve the puzzle, you need to tell Sanz what’s the maximum achievable number of points in the crossword. There is no need to cover all postions, just get the maximal score! Crossword and words contain only lowercase English letters.
9
+
10
+ Input
11
+
12
+ The first line of the input contains a single integer n (1 ≤ n ≤ 500) — the length of the crossword. The second line contains the crossword string. The third line contains a single integer m (1 ≤ m ≤ 100) — the number of given words, and next m lines contain description of words: each line will have a string representing a non-empty word (its length doesn't exceed the length of the crossword) and integer pi (0 ≤ pi ≤ 100). Last line of the input will contain x (1 ≤ x ≤ 100) — maximum number of times a position in crossword can be used.
13
+
14
+ Output
15
+
16
+ Output single integer — maximum number of points you can get.
17
+
18
+ Example
19
+
20
+ Input
21
+
22
+ 6
23
+ abacba
24
+ 2
25
+ aba 6
26
+ ba 3
27
+ 3
28
+
29
+
30
+ Output
31
+
32
+ 12
33
+
34
+ Note
35
+
36
+ For example, with the string "abacba", words "aba" (6 points) and "ba" (3 points), and x = 3, you can get at most 12 points - the word "aba" appears once ("abacba"), while "ba" appears two times ("abacba"). Note that for x = 1, you could get at most 9 points, since you wouldn’t be able to count both "aba" and the first appearance of "ba".
37
+
38
+ ## Contest Information
39
+ - **Contest ID**: 717
40
+ - **Problem Index**: G
41
+ - **Points**: 0.0
42
+ - **Rating**: 2400
43
+ - **Tags**: flows
44
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
45
+ - **Memory Limit**: 256000000 bytes
46
+
47
+ ## Task
48
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10787/instruction.md ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 876_F. High Cry
2
+
3
+ ## Problem Description
4
+ Disclaimer: there are lots of untranslateable puns in the Russian version of the statement, so there is one more reason for you to learn Russian :)
5
+
6
+ Rick and Morty like to go to the ridge High Cry for crying loudly — there is an extraordinary echo. Recently they discovered an interesting acoustic characteristic of this ridge: if Rick and Morty begin crying simultaneously from different mountains, their cry would be heard between these mountains up to the height equal the bitwise OR of mountains they've climbed and all the mountains between them.
7
+
8
+ Bitwise OR is a binary operation which is determined the following way. Consider representation of numbers x and y in binary numeric system (probably with leading zeroes) x = xk... x1x0 and y = yk... y1y0. Then z = x | y is defined following way: z = zk... z1z0, where zi = 1, if xi = 1 or yi = 1, and zi = 0 otherwise. In the other words, digit of bitwise OR of two numbers equals zero if and only if digits at corresponding positions is both numbers equals zero. For example bitwise OR of numbers 10 = 10102 and 9 = 10012 equals 11 = 10112. In programming languages C/C++/Java/Python this operation is defined as «|», and in Pascal as «or».
9
+
10
+ Help Rick and Morty calculate the number of ways they can select two mountains in such a way that if they start crying from these mountains their cry will be heard above these mountains and all mountains between them. More formally you should find number of pairs l and r (1 ≤ l < r ≤ n) such that bitwise OR of heights of all mountains between l and r (inclusive) is larger than the height of any mountain at this interval.
11
+
12
+ Input
13
+
14
+ The first line contains integer n (1 ≤ n ≤ 200 000), the number of mountains in the ridge.
15
+
16
+ Second line contains n integers ai (0 ≤ ai ≤ 109), the heights of mountains in order they are located in the ridge.
17
+
18
+ Output
19
+
20
+ Print the only integer, the number of ways to choose two different mountains.
21
+
22
+ Examples
23
+
24
+ Input
25
+
26
+ 5
27
+ 3 2 1 6 5
28
+
29
+
30
+ Output
31
+
32
+ 8
33
+
34
+
35
+ Input
36
+
37
+ 4
38
+ 3 3 3 3
39
+
40
+
41
+ Output
42
+
43
+ 0
44
+
45
+ Note
46
+
47
+ In the first test case all the ways are pairs of mountains with the numbers (numbering from one):
48
+
49
+ (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 4), (3, 5), (4, 5)
50
+
51
+ In the second test case there are no such pairs because for any pair of mountains the height of cry from them is 3, and this height is equal to the height of any mountain.
52
+
53
+ ## Contest Information
54
+ - **Contest ID**: 876
55
+ - **Problem Index**: F
56
+ - **Points**: 1750.0
57
+ - **Rating**: 2200
58
+ - **Tags**: binary search, bitmasks, combinatorics, data structures, divide and conquer
59
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
60
+ - **Memory Limit**: 512000000 bytes
61
+
62
+ ## Task
63
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10789/instruction.md ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 922_E. Birds
2
+
3
+ ## Problem Description
4
+ Apart from plush toys, Imp is a huge fan of little yellow birds!
5
+
6
+ <image>
7
+
8
+ To summon birds, Imp needs strong magic. There are n trees in a row on an alley in a park, there is a nest on each of the trees. In the i-th nest there are ci birds; to summon one bird from this nest Imp needs to stay under this tree and it costs him costi points of mana. However, for each bird summoned, Imp increases his mana capacity by B points. Imp summons birds one by one, he can summon any number from 0 to ci birds from the i-th nest.
9
+
10
+ Initially Imp stands under the first tree and has W points of mana, and his mana capacity equals W as well. He can only go forward, and each time he moves from a tree to the next one, he restores X points of mana (but it can't exceed his current mana capacity). Moving only forward, what is the maximum number of birds Imp can summon?
11
+
12
+ Input
13
+
14
+ The first line contains four integers n, W, B, X (1 ≤ n ≤ 103, 0 ≤ W, B, X ≤ 109) — the number of trees, the initial points of mana, the number of points the mana capacity increases after a bird is summoned, and the number of points restored when Imp moves from a tree to the next one.
15
+
16
+ The second line contains n integers c1, c2, ..., cn (0 ≤ ci ≤ 104) — where ci is the number of birds living in the i-th nest. It is guaranteed that <image>.
17
+
18
+ The third line contains n integers cost1, cost2, ..., costn (0 ≤ costi ≤ 109), where costi is the mana cost to summon a bird from the i-th nest.
19
+
20
+ Output
21
+
22
+ Print a single integer — the maximum number of birds Imp can summon.
23
+
24
+ Examples
25
+
26
+ Input
27
+
28
+ 2 12 0 4
29
+ 3 4
30
+ 4 2
31
+
32
+
33
+ Output
34
+
35
+ 6
36
+
37
+
38
+ Input
39
+
40
+ 4 1000 10 35
41
+ 1 2 4 5
42
+ 1000 500 250 200
43
+
44
+
45
+ Output
46
+
47
+ 5
48
+
49
+
50
+ Input
51
+
52
+ 2 10 7 11
53
+ 2 10
54
+ 6 1
55
+
56
+
57
+ Output
58
+
59
+ 11
60
+
61
+ Note
62
+
63
+ In the first sample base amount of Imp's mana is equal to 12 (with maximum capacity also equal to 12). After he summons two birds from the first nest, he loses 8 mana points, although his maximum capacity will not increase (since B = 0). After this step his mana will be 4 of 12; during the move you will replenish 4 mana points, and hence own 8 mana out of 12 possible. Now it's optimal to take 4 birds from the second nest and spend 8 mana. The final answer will be — 6.
64
+
65
+ In the second sample the base amount of mana is equal to 1000. The right choice will be to simply pick all birds from the last nest. Note that Imp's mana doesn't restore while moving because it's initially full.
66
+
67
+ ## Contest Information
68
+ - **Contest ID**: 922
69
+ - **Problem Index**: E
70
+ - **Points**: 2000.0
71
+ - **Rating**: 2200
72
+ - **Tags**: dp
73
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
74
+ - **Memory Limit**: 256000000 bytes
75
+
76
+ ## Task
77
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-1090/instruction.md ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 530_D. Set subtraction
2
+
3
+ ## Problem Description
4
+ You are given a starting set consisting of all integers from 1 to 1000, inclusive. You are also given several sets which need to be subtracted from the starting set (i.e., each number which is in at least one of these sets needs to be removed from the starting set). Each subtracted set is represented as an interval of integers from A to B, inclusive. Output the result after all subtractions.
5
+
6
+ Input
7
+
8
+ The first line of input contains an integer N (0 ≤ N ≤ 100) — the number of intervals to be subtracted. The following N lines contain pairs of integers A and B (1 ≤ A ≤ B ≤ 1000) — lower and upper bounds of the intervals. Intervals can intersect. An interval can consist of a single number.
9
+
10
+ Output
11
+
12
+ Output the result of subtractions in the following format: in one line output first the number of integers in the resulting set and then the integers of the set, sorted in increasing order, separated by single space.
13
+
14
+ Examples
15
+
16
+ Input
17
+
18
+ 2
19
+ 1 900
20
+ 902 999
21
+
22
+
23
+ Output
24
+
25
+ 2 901 1000
26
+
27
+
28
+ Input
29
+
30
+ 3
31
+ 1 500
32
+ 200 746
33
+ 150 1000
34
+
35
+
36
+ Output
37
+
38
+ 0
39
+
40
+ ## Contest Information
41
+ - **Contest ID**: 530
42
+ - **Problem Index**: D
43
+ - **Points**: 0.0
44
+ - **Rating**: 1600
45
+ - **Tags**: *special
46
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
47
+ - **Memory Limit**: 256000000 bytes
48
+
49
+ ## Task
50
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10917/instruction.md ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # p00005 GCD and LCM
2
+
3
+ ## Problem Description
4
+ Write a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given a and b.
5
+
6
+ Constraints
7
+
8
+ * 0 < a, b ≤ 2,000,000,000
9
+ * LCM(a, b) ≤ 2,000,000,000
10
+ * The number of data sets ≤ 50
11
+
12
+ Input
13
+
14
+ Input consists of several data sets. Each data set contains a and b separated by a single space in a line. The input terminates with EOF.
15
+
16
+ Output
17
+
18
+ For each data set, print GCD and LCM separated by a single space in a line.
19
+
20
+ Example
21
+
22
+ Input
23
+
24
+ 8 6
25
+ 50000000 30000000
26
+
27
+
28
+ Output
29
+
30
+ 2 24
31
+ 10000000 150000000
32
+
33
+ ## Contest Information
34
+ - **Contest ID**: 0
35
+ - **Problem Index**:
36
+ - **Points**: 0.0
37
+ - **Rating**: 0
38
+ - **Tags**:
39
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
40
+ - **Memory Limit**: 134217728 bytes
41
+
42
+ ## Task
43
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10919/instruction.md ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # p00270 Railroad
2
+
3
+ ## Problem Description
4
+ We have decided to introduce an automatic ticket gate to the railway network of a certain country. One of the difficult issues to implement is determining whether a given ticket can move between designated stations. Each ticket has a boarding station and a getting-off station. With this ticket, you can not only "get on at the boarding station and get off at the getting off station", but you are also allowed to get on and off the train.
5
+
6
+ There are S stations on this rail network, of which Group R stations are adjacent and can be traversed in both directions without going through other stations. There is only one railroad track connecting adjacent stations. The distance between adjacent stations is the distance measured along this railroad track. There are multiple possible routes from one station to another, depending on the shape of the railway network, but the route with the shortest distance is called the shortest route. If there are multiple such routes, both are accepted as the shortest route.
7
+
8
+ You can move from station c to station d with a ticket for boarding station a and getting off station b if there is a route p that meets all of the following conditions.
9
+
10
+ * Route p is the shortest route from station a to station b.
11
+ * Route p is a route that starts from station a, goes through station c, then station d, and ends at station b. The section from station c to station d is the shortest route between these two stations.
12
+
13
+
14
+
15
+ You will be given route map and ticket information. Next, you will be given several pairs of start and end points, so write a program that determines whether you can move from the start point to the end point with that ticket.
16
+
17
+
18
+
19
+ input
20
+
21
+ The input consists of one dataset. Input data is given in the following format.
22
+
23
+
24
+ S R
25
+ u1 v1 w1
26
+ u2 v2 w2
27
+ ::
28
+ uR vR wR
29
+ a b Q
30
+ c1 d1
31
+ ::
32
+ cQ dQ
33
+
34
+
35
+ The numbers given on each line are separated by a single space.
36
+
37
+ The first line consists of two integers. S (2 ≤ S ≤ 100000) is the number of stations that appear on the railroad map, and R (1 ≤ R ≤ 200000) is the number of pairs of adjacent stations. The following R line is given information on the railroad tracks that directly connect adjacent stations. ui and vi (1 ≤ ui, vi ≤ S) indicate the station numbers at both ends of the i-th line. wi (1 ≤ wi ≤ 1000) is an integer representing the distance between these stations. However, numbers from 1 to S are assigned to each station without duplication, and ui ≠ vi.
38
+
39
+ The next line consists of three integers. The first two integers represent the ticket sections, where a is the boarding station and b is the getting-off station (1 ≤ a, b ≤ S). The third integer Q (1 ≤ Q ≤ 40000) indicates the number of questions. The question is given on the following Q line. ci and di (1 ≤ ci, di ≤ S) indicate the boarding and alighting stations of the i-th question. However, a ≠ b and ci ≠ di.
40
+
41
+ output
42
+
43
+ For each question, print Yes if you can move with the given ticket, or No if you can't.
44
+
45
+ Example
46
+
47
+ Input
48
+
49
+ 6 7
50
+ 1 2 3
51
+ 1 4 1
52
+ 2 3 5
53
+ 4 3 1
54
+ 3 6 2
55
+ 4 5 2
56
+ 5 6 1
57
+ 1 6 6
58
+ 1 6
59
+ 4 3
60
+ 4 6
61
+ 5 6
62
+ 2 6
63
+ 2 5
64
+
65
+
66
+ Output
67
+
68
+ Yes
69
+ Yes
70
+ Yes
71
+ Yes
72
+ No
73
+ No
74
+
75
+ ## Contest Information
76
+ - **Contest ID**: 0
77
+ - **Problem Index**:
78
+ - **Points**: 0.0
79
+ - **Rating**: 0
80
+ - **Tags**:
81
+ - **Time Limit**: {'seconds': 8, 'nanos': 0} seconds
82
+ - **Memory Limit**: 134217728 bytes
83
+
84
+ ## Task
85
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-10926/instruction.md ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # p01326 UTF-8
2
+
3
+ ## Problem Description
4
+ UTF-8 is one of the methods for coding multibyte characters.
5
+
6
+ Characters are treated as bytes on the computer. If it is only English, it can be expressed in 1 byte even if the Latin alphabet, numbers and symbols are combined, but unfortunately it is not possible to express the characters used all over the world in 1 byte, so the characters are expressed using multiple bytes. There is a need.
7
+
8
+ Here, for example, when a 2-byte string of 12354 (0x3042) is assigned to the character "a", if the byte strings are arranged as they are, it is indistinguishable whether it is 2 characters with 0x30,0x42 or 1 character with 0x3042.
9
+
10
+ For this reason, UTF-8, which is a multi-byte character encoding method, overcomes this problem by making it possible to know the length of the byte string that continues in the first byte. The specific bit pattern is as follows.
11
+
12
+ Byte length | Bit pattern
13
+ --- | ---
14
+ 1 | 0xxxxxxx
15
+ 2 | 110yyyyx 10xxxxxx
16
+ 3 | 1110yyyy 10yxxxxx 10xxxxxx
17
+ 4 | 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx
18
+
19
+ Here, x is an arbitrary bit of 0/1. Also, y can be any bit of 0/1, but one of them must be 1. It is assumed that all characters are 1, 2, 3, or 4-byte characters.
20
+
21
+ Here, the restriction that at least one bit of y is 1 is due to the following reasons. For example, when encoding'/' (0x2f) into a byte string, there are two methods, 0x2f and 1-byte coding, or 0xc0 0xaf and 2-byte coding, but allowing such ambiguity is for security reasons. This is because it may be a source of threats.
22
+
23
+ After hearing the above story, you were wondering how much freedom UTF-8 has. Specifically, how many combinations of bytes are possible as UTF-8 when some bits of a given byte sequence are unknown.
24
+
25
+ At this time, find the number of possible combinations of byte strings and output the remainder divided by 1,000,000.
26
+
27
+ Notes on Test Cases
28
+
29
+ Multiple datasets are given in the above input format. Create a program that outputs each data set in the above output format.
30
+
31
+ When N is 0, it indicates the end of input.
32
+
33
+ <!-
34
+
35
+
36
+
37
+ Input
38
+
39
+
40
+ N
41
+ b1
42
+ b2
43
+ ...
44
+ bN
45
+
46
+ N is the number of bytes and bi is the bytes. 1 ≤ N ≤ 1000 is satisfied. For bi, eight symbols {0/1 / x} are lined up. x indicates that the bit is unknown.
47
+
48
+ Output
49
+
50
+ Output the remainder of dividing the number of possible byte string combinations by 1,000,000.
51
+
52
+ Examples
53
+
54
+ Input
55
+
56
+ 1
57
+ xxxxxxxx
58
+ 3
59
+ 11100000
60
+ 10x00000
61
+ 10111111
62
+ 3
63
+ 11100000
64
+ 10000000
65
+ 10111111
66
+ 4
67
+ xxxxxxxx
68
+ xxxxxxxx
69
+ xxxxxxxx
70
+ xxxxxxxx
71
+ 0
72
+
73
+
74
+ Output
75
+
76
+ 128
77
+ 1
78
+ 0
79
+ 778240
80
+
81
+
82
+ Input
83
+
84
+ 1
85
+ xxxxxxxx
86
+
87
+
88
+ Output
89
+
90
+ 128
91
+
92
+
93
+ Input
94
+
95
+ 3
96
+ 11100000
97
+ 10x00000
98
+ 10111111
99
+
100
+
101
+ Output
102
+
103
+ 1
104
+
105
+
106
+ Input
107
+
108
+ 3
109
+ 11100000
110
+ 10000000
111
+ 10111111
112
+
113
+
114
+ Output
115
+
116
+ 0
117
+
118
+
119
+ Input
120
+
121
+ 4
122
+ xxxxxxxx
123
+ xxxxxxxx
124
+ xxxxxxxx
125
+ xxxxxxxx
126
+
127
+
128
+ Output
129
+
130
+ 778240
131
+
132
+ ## Contest Information
133
+ - **Contest ID**: 0
134
+ - **Problem Index**:
135
+ - **Points**: 0.0
136
+ - **Rating**: 0
137
+ - **Tags**:
138
+ - **Time Limit**: {'seconds': 8, 'nanos': 0} seconds
139
+ - **Memory Limit**: 134217728 bytes
140
+
141
+ ## Task
142
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-1097/instruction.md ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 69_A. Young Physicist
2
+
3
+ ## Problem Description
4
+ A guy named Vasya attends the final grade of a high school. One day Vasya decided to watch a match of his favorite hockey team. And, as the boy loves hockey very much, even more than physics, he forgot to do the homework. Specifically, he forgot to complete his physics tasks. Next day the teacher got very angry at Vasya and decided to teach him a lesson. He gave the lazy student a seemingly easy task: You are given an idle body in space and the forces that affect it. The body can be considered as a material point with coordinates (0; 0; 0). Vasya had only to answer whether it is in equilibrium. "Piece of cake" — thought Vasya, we need only to check if the sum of all vectors is equal to 0. So, Vasya began to solve the problem. But later it turned out that there can be lots and lots of these forces, and Vasya can not cope without your help. Help him. Write a program that determines whether a body is idle or is moving by the given vectors of forces.
5
+
6
+ Input
7
+
8
+ The first line contains a positive integer n (1 ≤ n ≤ 100), then follow n lines containing three integers each: the xi coordinate, the yi coordinate and the zi coordinate of the force vector, applied to the body ( - 100 ≤ xi, yi, zi ≤ 100).
9
+
10
+ Output
11
+
12
+ Print the word "YES" if the body is in equilibrium, or the word "NO" if it is not.
13
+
14
+ Examples
15
+
16
+ Input
17
+
18
+ 3
19
+ 4 1 7
20
+ -2 4 -1
21
+ 1 -5 -3
22
+
23
+
24
+ Output
25
+
26
+ NO
27
+
28
+ Input
29
+
30
+ 3
31
+ 3 -1 7
32
+ -5 2 -4
33
+ 2 -1 -3
34
+
35
+
36
+ Output
37
+
38
+ YES
39
+
40
+ ## Contest Information
41
+ - **Contest ID**: 69
42
+ - **Problem Index**: A
43
+ - **Points**: 500.0
44
+ - **Rating**: 1000
45
+ - **Tags**: implementation, math
46
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
47
+ - **Memory Limit**: 256000000 bytes
48
+
49
+ ## Task
50
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-11003/instruction.md ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # end-game
2
+
3
+ ## Problem Description
4
+ Problem:
5
+
6
+ Black and White are playing a game of chess on a chess board of n X n dimensions. The game is nearing its end. White has his King and a Pawn left. Black has only his King left. So, definitely Black cannot win the game. However, Black can drag the game towards a draw, and, he can do this only if he captures White's only left pawn. White knows exactly what he must do so that he wins the game. He is aware of the fact that if his Pawn reaches Row 'n' (topmost row as seen by White) , he is allowed to replace that Pawn by a Queen. Since White plays chess regularly, he knows how to beat the opponent in a situation in which the opponent has only a King left and he (White) has a King as well as a Queen left. The current scenario of the game will be given to you. You must tell whether this game will end with a Draw or will it end with White winning.
7
+
8
+ If Move = 0, it is currently White's move and if Move = 1, it is currently Black's move.
9
+
10
+ Black and White play alternately.
11
+
12
+ White's piece will be captured by Black if Black's King manages to reach the same square in which White's piece is currently in.
13
+
14
+ White's king is currently placed at the the point (1,n). White is in a hurry to convert his pawn into a queen, so , in every move of his, he only plays his pawn forward ( towards row n ), ignoring his king.
15
+
16
+ So, naturally, Black plays his King towards White's Pawn in order to capture it.
17
+
18
+ White's Pawn is currently located at the point (a,b).
19
+
20
+ Black's King is currently located at the point (c,d).
21
+
22
+ Pawn movement on a chess board : Pawn can move only one step forward, provided no piece is blocking it from doing so. ie. it can move only from (u,v) to (u+1,v), provided there is no piece (white or black) at (u+1,v)
23
+
24
+ King movement on a chess board : King can move one step in any direction. ie if it is at (u,v) currently, then it can move to (u+1,v) , (u-1,v) , (u,v+1) , (u,v-1) , (u+1,v+1) , (u+1, v-1) , (u-1,v+1) , (u-1,v-1), provided it remains inside the chess board
25
+
26
+ If at any moment the White Pawn is blocked by the black king from moving forward, white will be forced to play his king in such a case.
27
+
28
+ Input:
29
+
30
+ First line consists of T, the number of test cases. Every test case comprises of a single line containing space separated integers n, a, b, c, d, Move.
31
+
32
+ Output:
33
+
34
+ For every test case, print a single line, 'Draw' is the game ends as a draw or 'White Wins' if the game ends in white winning.
35
+
36
+ Constraints :
37
+
38
+ 1 ≤ T ≤ 100000
39
+
40
+ 5 ≤ n ≤ 10^9
41
+
42
+ 2 ≤ a ≤ n
43
+
44
+ 1 ≤ b,c,d ≤ n
45
+
46
+ 0 ≤ Move ≤ 1
47
+
48
+ (c,d) != { (1,n) , (2,n) , (1,n-1) , (2,n-1) , (a,b) }
49
+
50
+ SAMPLE INPUT
51
+ 2
52
+ 10 6 2 10 2 0
53
+ 10 5 2 1 3 0
54
+
55
+ SAMPLE OUTPUT
56
+ Draw
57
+ White Wins
58
+
59
+ Explanation
60
+
61
+ For first test case :
62
+ white plays the first move since Move=0.
63
+ white : Pawn moves from (6,2) to (7,2).
64
+ black : King moves from (10,2) to (9,2).
65
+ white : Pawn moves from (7,2) to (8,2).
66
+ black : King moves from (9,2) to (8,2).
67
+ white Pawn has been captured, so the game ends in a draw.
68
+
69
+ For second test case :
70
+ white plays the first move since Move=0.
71
+ white : Pawn moves from (5,2) to (6,2).
72
+ black : King moves from (1,3) to (2,2).
73
+ white : Pawn moves from (6,2) to (7,2).
74
+ black : King moves from (2,2) to (3,2).
75
+ white : Pawn moves from (7,2) to (8,2).
76
+ black : King moves from (3,2) to (4,2).
77
+ white : Pawn moves from (8,2) to (9,2).
78
+ black : King moves from (4,2) to (5,2).
79
+ white : Pawn moves from (9,2) to (10,2).
80
+ white Pawn has reached the topmost row (nth row), so, white will convert it into a Queen. Black King cannot eliminate this Queen as it is too far. So, finally, White Wins.
81
+
82
+ ## Contest Information
83
+ - **Contest ID**: 0
84
+ - **Problem Index**:
85
+ - **Points**: 0.0
86
+ - **Rating**: 0
87
+ - **Tags**: None
88
+ - **Time Limit**: None seconds
89
+ - **Memory Limit**: 0 bytes
90
+
91
+ ## Task
92
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-11004/instruction.md ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # help-ashu-1
2
+
3
+ ## Problem Description
4
+ Ashu and Shanu are best buddies. One day Shanu gives Ashu a problem to test his intelligence.He gives him an array of N natural numbers and asks him to solve the following queries:-
5
+
6
+ Query 0:- modify the element present at index i to x.
7
+ Query 1:- count the number of even numbers in range l to r inclusive.
8
+ Query 2:- count the number of odd numbers in range l to r inclusive.
9
+
10
+ input:
11
+ First line of the input contains the number N. Next line contains N natural numbers.
12
+ Next line contains an integer Q followed by Q queries.
13
+ 0 x y - modify the number at index x to y.
14
+ 1 x y - count the number of even numbers in range l to r inclusive.
15
+ 2 x y - count the number of odd numbers in range l to r inclusive.
16
+
17
+ Constraints:
18
+ 1 ≤ N,Q ≤ 10^5
19
+ 1 ≤ l ≤ r ≤ N
20
+ 0 ≤ Ai ≤ 10^9
21
+ 1 ≤ x ≤ N
22
+ 0 ≤ y ≤ 10^9
23
+
24
+ Note:- indexing starts from 1.
25
+
26
+ SAMPLE INPUT
27
+ 6
28
+ 1 2 3 4 5 6
29
+ 4
30
+ 1 2 5
31
+ 2 1 4
32
+ 0 5 4
33
+ 1 1 6
34
+
35
+ SAMPLE OUTPUT
36
+ 2
37
+ 2
38
+ 4
39
+
40
+ ## Contest Information
41
+ - **Contest ID**: 0
42
+ - **Problem Index**:
43
+ - **Points**: 0.0
44
+ - **Rating**: 0
45
+ - **Tags**: None
46
+ - **Time Limit**: None seconds
47
+ - **Memory Limit**: 0 bytes
48
+
49
+ ## Task
50
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-11035/instruction.md ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # p02152 Tunnel
2
+
3
+ ## Problem Description
4
+ Problem
5
+
6
+ There are $ n $ rectangles on the $ xy $ plane with $ x $ on the horizontal axis and $ y $ on the vertical axis. The $ i $ th rectangle is $ h_i $ in height and $ 1 $ in width, and the vertices are point $ (i-1, 0) $, point $ (i-1, h_i) $, and point $ (i, h_i). ) $, Point $ (i, 0) $.
7
+ You choose $ n $ positive integers $ y_1, y_2, ... y_n $ and perform the following $ n $ operations.
8
+ (1) In the first operation, draw a line segment connecting the point $ (0, 1) $ and the point $ (1, y_1) $.
9
+ (2) In the operation of $ i $ th time $ (2 \ le i \ le n) $, a line segment connecting the point $ (i-1, y_ {i-1}) $ and the point $ (i, y_i) $ pull.
10
+
11
+ For the $ i $ operation, set $ S_i $ as follows.
12
+ Let A be the endpoint with the smaller $ x $ coordinates of the line segment drawn in the $ i $ operation, and B be the endpoint with the larger $ x $ coordinates.
13
+ Also, point $ (i-1, 0) $ is C, point $ (i-1, h_i) $ is D, point $ (i, h_i) $ is E, and point $ (i, 0) $ is F. To do.
14
+ Let $ S_i $ be the area of ​​the non-intersection between the trapezoid ABFC and the rectangle DEFC.
15
+ Five examples are shown below.
16
+ <image>
17
+ $ S_i $ in each example is the total area of ​​the area containing the star mark. Find $ y_1, y_2, ... y_n $ that minimizes $ S = S_1 + S_2 +… + S_n $, and output the minimum value of $ S $.
18
+
19
+ Constraints
20
+
21
+ The input satisfies the following conditions.
22
+
23
+ * $ 2 \ leq n \ leq 150 $
24
+ * $ 1 \ leq h_i \ leq 150 $
25
+
26
+ Input
27
+
28
+ The input is given in the following format.
29
+
30
+
31
+ $ n $
32
+ $ h_1 $ $ h_2 $ $ ... $ $ h_n $
33
+
34
+
35
+ All inputs are given as integers.
36
+
37
+ Output
38
+
39
+ Output the minimum value of $ S $ on one line.
40
+ However, absolute or relative errors up to $ 10 ^ {-5} $ are acceptable.
41
+
42
+ Examples
43
+
44
+ Input
45
+
46
+ 2
47
+ 5 3
48
+
49
+
50
+ Output
51
+
52
+ 2.83333333
53
+
54
+
55
+ Input
56
+
57
+ 3
58
+ 6 1 4
59
+
60
+
61
+ Output
62
+
63
+ 6.25000000
64
+
65
+
66
+ Input
67
+
68
+ 7
69
+ 3 1 9 2 2 5 1
70
+
71
+
72
+ Output
73
+
74
+ 12.37500000
75
+
76
+ ## Contest Information
77
+ - **Contest ID**: 0
78
+ - **Problem Index**:
79
+ - **Points**: 0.0
80
+ - **Rating**: 0
81
+ - **Tags**:
82
+ - **Time Limit**: {'seconds': 3, 'nanos': 0} seconds
83
+ - **Memory Limit**: 268435456 bytes
84
+
85
+ ## Task
86
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-11207/instruction.md ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 94_D. End of Exams
2
+
3
+ ## Problem Description
4
+ Students love to celebrate their holidays. Especially if the holiday is the day of the end of exams!
5
+
6
+ Despite the fact that Igor K., unlike his groupmates, failed to pass a programming test, he decided to invite them to go to a cafe so that each of them could drink a bottle of... fresh cow milk. Having entered the cafe, the m friends found n different kinds of milk on the menu, that's why they ordered n bottles — one bottle of each kind. We know that the volume of milk in each bottle equals w.
7
+
8
+ When the bottles were brought in, they decided to pour all the milk evenly among the m cups, so that each got a cup. As a punishment for not passing the test Igor was appointed the person to pour the milk. He protested that he was afraid to mix something up and suggested to distribute the drink so that the milk from each bottle was in no more than two different cups. His friends agreed but they suddenly faced the following problem — and what is actually the way to do it?
9
+
10
+ Help them and write the program that will help to distribute the milk among the cups and drink it as quickly as possible!
11
+
12
+ Note that due to Igor K.'s perfectly accurate eye and unswerving hands, he can pour any fractional amount of milk from any bottle to any cup.
13
+
14
+ Input
15
+
16
+ The only input data file contains three integers n, w and m (1 ≤ n ≤ 50, 100 ≤ w ≤ 1000, 2 ≤ m ≤ 50), where n stands for the number of ordered bottles, w stands for the volume of each of them and m stands for the number of friends in the company.
17
+
18
+ Output
19
+
20
+ Print on the first line "YES" if it is possible to pour the milk so that the milk from each bottle was in no more than two different cups. If there's no solution, print "NO".
21
+
22
+ If there is a solution, then print m more lines, where the i-th of them describes the content of the i-th student's cup. The line should consist of one or more pairs that would look like "b v". Each such pair means that v (v > 0) units of milk were poured into the i-th cup from bottle b (1 ≤ b ≤ n). All numbers b on each line should be different.
23
+
24
+ If there are several variants to solve the problem, print any of them. Print the real numbers with no less than 6 digits after the decimal point.
25
+
26
+ Examples
27
+
28
+ Input
29
+
30
+ 2 500 3
31
+
32
+
33
+ Output
34
+
35
+ YES
36
+ 1 333.333333
37
+ 2 333.333333
38
+ 2 166.666667 1 166.666667
39
+
40
+
41
+ Input
42
+
43
+ 4 100 5
44
+
45
+
46
+ Output
47
+
48
+ YES
49
+ 3 20.000000 4 60.000000
50
+ 1 80.000000
51
+ 4 40.000000 2 40.000000
52
+ 3 80.000000
53
+ 2 60.000000 1 20.000000
54
+
55
+
56
+ Input
57
+
58
+ 4 100 7
59
+
60
+
61
+ Output
62
+
63
+ NO
64
+
65
+
66
+ Input
67
+
68
+ 5 500 2
69
+
70
+
71
+ Output
72
+
73
+ YES
74
+ 4 250.000000 5 500.000000 2 500.000000
75
+ 3 500.000000 1 500.000000 4 250.000000
76
+
77
+ ## Contest Information
78
+ - **Contest ID**: 94
79
+ - **Problem Index**: D
80
+ - **Points**: 1000.0
81
+ - **Rating**: 1900
82
+ - **Tags**: greedy
83
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
84
+ - **Memory Limit**: 256000000 bytes
85
+
86
+ ## Task
87
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-11231/instruction.md ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # p00212 Highway Express Bus
2
+
3
+ ## Problem Description
4
+ Mr. A is planning to travel alone on a highway bus (hereinafter referred to as "bus") during his high school holidays. First, Mr. A chose the town he wanted to visit the most and made it his destination. Next, you have to decide the route to transfer the bus from the departure point to the destination. When connecting, you will need a ticket for each bus as you will need to get off the bus and then transfer to another bus.
5
+
6
+ Mr. A got some discount tickets for the bus from his relatives' uncle. If you use one ticket, you can buy one ticket at half price. For example, when going from the departure point 5 in Fig. 1 to the destination 1, there are two possible routes: 5 → 4 → 6 → 2 → 1 and 5 → 3 → 1. Assuming that there are two discount tickets, the cheapest way to travel is to follow the route 5 → 4 → 6 → 2 → 1, and use the discount on the routes 4 → 6 and 6 → 2 for the total price. Will be 4600 yen. On the other hand, if you follow the route 5 → 3 → 1, you will get a discount on the routes 5 → 3 and 3 → 1, and the total charge will be 3750 yen.
7
+
8
+ Mr. A wants to spend money on sightseeing, so he wants to keep transportation costs as low as possible. Therefore, Mr. A decided to create a program to find the cheapest transportation cost from the starting point to the destination.
9
+
10
+
11
+ <image>
12
+
13
+ Figure 1
14
+
15
+ Enter the number of discount tickets, the number of towns connected by buses, the number of bus routes, and the route information of each bus, and create a program that outputs the cheapest transportation cost from the departure point to the destination. Each bus runs in both directions at the same rate. Also, assuming that the number of towns is n, each town is numbered differently from 1 to n. There must always be a route from the origin to the destination.
16
+
17
+
18
+
19
+ Input
20
+
21
+ A sequence of multiple datasets is given as input. The end of the input is indicated by five lines of zeros. Each dataset is given in the following format:
22
+
23
+
24
+ c n m s d
25
+ a1 b1 f1
26
+ a2 b2 f2
27
+ ::
28
+ am bm fm
29
+
30
+
31
+ Number of discount tickets on the first line c (1 ≤ c ≤ 10), number of towns connected by buses n (2 ≤ n ≤ 100), number of bus routes m (1 ≤ m ≤ 500), town number of departure Given s and the destination town number d (s ≠ d).
32
+
33
+ The following m lines are given the route information ai, bi, fi (1 ≤ ai, bi ≤ n, 1000 ≤ fi ≤ 10000) for the i-th bus. ai and bi are the town numbers of the start and end points of the bus route, and fi is an integer in increments of 100 that represents the fare for this route.
34
+
35
+ The number of datasets does not exceed 100.
36
+
37
+ Output
38
+
39
+ Outputs the cheapest transportation cost on one line for each input dataset.
40
+
41
+ Example
42
+
43
+ Input
44
+
45
+ 1 3 3 1 3
46
+ 1 3 2000
47
+ 1 2 1000
48
+ 2 3 1000
49
+ 2 3 3 1 3
50
+ 1 3 2300
51
+ 1 2 1000
52
+ 2 3 1200
53
+ 2 6 6 5 1
54
+ 1 2 1500
55
+ 1 3 4500
56
+ 2 6 2000
57
+ 5 4 1000
58
+ 6 4 2200
59
+ 3 5 3000
60
+ 0 0 0 0 0
61
+
62
+
63
+ Output
64
+
65
+ 1000
66
+ 1100
67
+ 3750
68
+
69
+ ## Contest Information
70
+ - **Contest ID**: 0
71
+ - **Problem Index**:
72
+ - **Points**: 0.0
73
+ - **Rating**: 0
74
+ - **Tags**:
75
+ - **Time Limit**: {'seconds': 1, 'nanos': 0} seconds
76
+ - **Memory Limit**: 134217728 bytes
77
+
78
+ ## Task
79
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-11236/instruction.md ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # p00997 Dungeon (II)
2
+
3
+ ## Problem Description
4
+ You are involved in the development of a certain game. The game is for players to explore randomly generated dungeons. As a specification of the game, I want to show the player the danger level of the dungeon in advance and select whether to search for the generated dungeon or to regenerate a new dungeon.
5
+
6
+ There are n rooms in the dungeon generated by this game, and they are numbered from 0 to n-1. The rooms are connected by a passage. There are a total of n-1 passages connecting rooms. The passage can go in either direction. In addition, a distance is set between the rooms. In the generated dungeon, it is possible to go from one room to all other rooms via several passages. Then, when the player plays the game, two different rooms are selected as the start point and the goal point.
7
+
8
+ You decide to decide how to evaluate the risk in order to evaluate the dungeon. First, the risk level when moving from one room to another is set to the value of the most costly passage among the passages used to move between rooms in the shortest time. Then, we decided to set the risk level of the dungeon as the sum of the risk levels when moving between a pair of rooms where i <j.
9
+
10
+ A randomly generated dungeon is given as input. First, calculate the risk of moving for all room pairs where i <j. Then output the sum as the answer to the problem.
11
+
12
+
13
+
14
+ Input
15
+
16
+ The input is given in the following format.
17
+
18
+
19
+ n
20
+ a1 b1 c1
21
+ ..
22
+ ..
23
+ ..
24
+ an-1 bn-1 cn-1
25
+
26
+
27
+ ai bi ci means that the distance of the passage connecting the rooms ai and bi is ci.
28
+
29
+ Input meets the following constraints
30
+ 2 ≤ n ≤ 200,000
31
+ 0 ≤ ai, bi <n
32
+ 0 ≤ ci ≤ 100,000
33
+
34
+ Output
35
+
36
+ Print the answer value on one line
37
+
38
+ Examples
39
+
40
+ Input
41
+
42
+ 4
43
+ 0 1 3
44
+ 1 2 5
45
+ 1 3 2
46
+
47
+
48
+ Output
49
+
50
+ 23
51
+
52
+
53
+ Input
54
+
55
+ 6
56
+ 0 2 5
57
+ 2 1 1
58
+ 2 3 10
59
+ 3 5 4
60
+ 3 4 2
61
+
62
+
63
+ Output
64
+
65
+ 111
66
+
67
+ ## Contest Information
68
+ - **Contest ID**: 0
69
+ - **Problem Index**:
70
+ - **Points**: 0.0
71
+ - **Rating**: 0
72
+ - **Tags**:
73
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
74
+ - **Memory Limit**: 268435456 bytes
75
+
76
+ ## Task
77
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.
code_contests-11451/instruction.md ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # p01906 Weight Range
2
+
3
+ ## Problem Description
4
+ problem
5
+
6
+ There are many $ N $ colored balls of different weights in the queue. The queue is in ascending order from the beginning: $ 1,2,3, \ dots, N-1, N, 1,2,3, \ dots, N-1, N, 1,2,3, \ dots $ The balls are lined up, followed by the balls of color $ N $, followed by the balls of color $ 1 $. Balls of the same color weigh the same, and balls of color $ i $ weigh $ A_i $.
7
+
8
+ From this state, take out $ M $ of balls from the beginning of the queue and repeat the process of grouping them. Then stop forming groups when the total number of balls of each color removed from the cue is equal. Please note that the cue contains a sufficient number of balls and the cue will not be empty by the time you stop forming groups.
9
+
10
+ For example, when $ N = 8, M = 2 $, there are 4 groups of {color 1, color 2}, {color 3, color 4}, {color 5, color 6}, {color 7, color 8}. (At this time, there is one ball of each color). When $ N = 4, M = 3 $, {color 1, color 2, color 3}, {color 4, color 1, color 2}, {color 3, color 4, color 1}, {color 2, color There will be a $ 4 $ group of 3, colors 4} (there are 3 balls of each color each).
11
+
12
+ At this time, in each group, the difference between the maximum value and the minimum value of the weight of the balls included is called the weight range of that group. Output the sum of the weight ranges for each group.
13
+
14
+
15
+
16
+ output
17
+
18
+ Print the answer in one line. Also, output a line break at the end.
19
+
20
+ Example
21
+
22
+ Input
23
+
24
+ 8 2
25
+ 23 61 57 13 91 41 79 41
26
+
27
+
28
+ Output
29
+
30
+ 170
31
+
32
+ ## Contest Information
33
+ - **Contest ID**: 0
34
+ - **Problem Index**:
35
+ - **Points**: 0.0
36
+ - **Rating**: 0
37
+ - **Tags**:
38
+ - **Time Limit**: {'seconds': 2, 'nanos': 0} seconds
39
+ - **Memory Limit**: 268435456 bytes
40
+
41
+ ## Task
42
+ Solve this competitive programming problem. Provide a complete solution that handles all the given constraints and edge cases.