apaz/codeforces-genesys huggingface dataset:
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 1.0 seconds
Memory limit: 256.0 MB

# Problem
You are given a function $$$f$$$ written in some basic language. The function accepts an integer value, which is immediately written into some variable $$$x$$$. $$$x$$$ is an integer variable and can be assigned values from $$$0$$$ to $$$2^{32}-1$$$. The function contains three types of commands:

- for $$$n$$$ — for loop;
- end — every command between "for $$$n$$$" and corresponding "end" is executed $$$n$$$ times;
- add — adds 1 to $$$x$$$.

After the execution of these commands, value of $$$x$$$ is returned.

Every "for $$$n$$$" is matched with "end", thus the function is guaranteed to be valid. "for $$$n$$$" can be immediately followed by "end"."add" command can be outside of any for loops.

Notice that "add" commands might overflow the value of $$$x$$$! It means that the value of $$$x$$$ becomes greater than $$$2^{32}-1$$$ after some "add" command.

Now you run $$$f(0)$$$ and wonder if the resulting value of $$$x$$$ is correct or some overflow made it incorrect.

If overflow happened then output "OVERFLOW!!!", otherwise print the resulting value of $$$x$$$.

## Input Format
The first line contains a single integer $$$l$$$ ($$$1 \le l \le 10^5$$$) — the number of lines in the function.

Each of the next $$$l$$$ lines contains a single command of one of three types:

- for $$$n$$$ ($$$1 \le n \le 100$$$) — for loop;
- end — every command between "for $$$n$$$" and corresponding "end" is executed $$$n$$$ times;
- add — adds 1 to $$$x$$$.

## Output Format
If overflow happened during execution of $$$f(0)$$$, then output "OVERFLOW!!!", otherwise print the resulting value of $$$x$$$.

## Examples
```input
9
add
for 43
end
for 10
for 15
add
end
add
end
```
```output
161
```
-----
```input
2
for 62
end
```
```output
0
```
-----
```input
11
for 100
for 100
for 100
for 100
for 100
add
end
end
end
end
end
```
```output
OVERFLOW!!!
```

## Note
In the first example the first "add" is executed 1 time, the second "add" is executed 150 times and the last "add" is executed 10 times. Note that "for $$$n$$$" can be immediately followed by "end" and that "add" can be outside of any for loops.

In the second example there are no commands "add", thus the returning value is 0.

In the third example "add" command is executed too many times, which causes $$$x$$$ to go over $$$2^{32}-1$$$.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 2.0 seconds
Memory limit: 256.0 MB

# Problem
At first, there was a legend related to the name of the problem, but now it's just a formal statement.

You are given $$$n$$$ points $$$a_1, a_2, \dots, a_n$$$ on the $$$OX$$$ axis. Now you are asked to find such an integer point $$$x$$$ on $$$OX$$$ axis that $$$f_k(x)$$$ is minimal possible.

The function $$$f_k(x)$$$ can be described in the following way:

- form a list of distances $$$d_1, d_2, \dots, d_n$$$ where $$$d_i = |a_i - x|$$$ (distance between $$$a_i$$$ and $$$x$$$);
- sort list $$$d$$$ in non-descending order;
- take $$$d_{k + 1}$$$ as a result.

If there are multiple optimal answers you can print any of them.

## Input Format
The first line contains single integer $$$T$$$ ($$$ 1 \le T \le 2 \cdot 10^5$$$) — number of queries. Next $$$2 \cdot T$$$ lines contain descriptions of queries. All queries are independent.

The first line of each query contains two integers $$$n$$$, $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$0 \le k < n$$$) — the number of points and constant $$$k$$$.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_1 < a_2 < \dots < a_n \le 10^9$$$) — points in ascending order.

It's guaranteed that $$$\sum{n}$$$ doesn't exceed $$$2 \cdot 10^5$$$.

## Output Format
Print $$$T$$$ integers — corresponding points $$$x$$$ which have minimal possible value of $$$f_k(x)$$$. If there are multiple answers you can print any of them.

## Examples
```input
3
3 2
1 2 5
2 1
1 1000000000
1 0
4
```
```output
3
500000000
4
```

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 2.0 seconds
Memory limit: 256.0 MB

# Problem
You are given an array $$$a_1, a_2, \dots, a_n$$$ and an integer $$$k$$$.

You are asked to divide this array into $$$k$$$ non-empty consecutive subarrays. Every element in the array should be included in exactly one subarray. Let $$$f(i)$$$ be the index of subarray the $$$i$$$-th element belongs to. Subarrays are numbered from left to right and from $$$1$$$ to $$$k$$$.

Let the cost of division be equal to $$$\sum\limits_{i=1}^{n} (a_i \cdot f(i))$$$. For example, if $$$a = [1, -2, -3, 4, -5, 6, -7]$$$ and we divide it into $$$3$$$ subbarays in the following way: $$$[1, -2, -3], [4, -5], [6, -7]$$$, then the cost of division is equal to $$$1 \cdot 1 - 2 \cdot 1 - 3 \cdot 1 + 4 \cdot 2 - 5 \cdot 2 + 6 \cdot 3 - 7 \cdot 3 = -9$$$.

Calculate the maximum cost you can obtain by dividing the array $$$a$$$ into $$$k$$$ non-empty consecutive subarrays.

## Input Format
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le k \le n \le 3 \cdot 10^5$$$).

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$ |a_i| \le 10^6$$$).

## Output Format
Print the maximum cost you can obtain by dividing the array $$$a$$$ into $$$k$$$ nonempty consecutive subarrays.

## Examples
```input
5 2
-1 -2 5 -4 8
```
```output
15
```
-----
```input
7 6
-3 0 -1 -2 -2 -4 -1
```
```output
-45
```
-----
```input
4 1
3 -1 6 0
```
```output
8
```

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 2.0 seconds
Memory limit: 256.0 MB

# Problem
You are given $$$n$$$ intervals in form $$$[l; r]$$$ on a number line.

You are also given $$$m$$$ queries in form $$$[x; y]$$$. What is the minimal number of intervals you have to take so that every point (not necessarily integer) from $$$x$$$ to $$$y$$$ is covered by at least one of them?

If you can't choose intervals so that every point from $$$x$$$ to $$$y$$$ is covered, then print -1 for that query.

## Input Format
The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2 \cdot 10^5$$$) — the number of intervals and the number of queries, respectively.

Each of the next $$$n$$$ lines contains two integer numbers $$$l_i$$$ and $$$r_i$$$ ($$$0 \le l_i < r_i \le 5 \cdot 10^5$$$) — the given intervals.

Each of the next $$$m$$$ lines contains two integer numbers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i < y_i \le 5 \cdot 10^5$$$) — the queries.

## Output Format
Print $$$m$$$ integer numbers. The $$$i$$$-th number should be the answer to the $$$i$$$-th query: either the minimal number of intervals you have to take so that every point (not necessarily integer) from $$$x_i$$$ to $$$y_i$$$ is covered by at least one of them or -1 if you can't choose intervals so that every point from $$$x_i$$$ to $$$y_i$$$ is covered.

## Examples
```input
2 3
1 3
2 4
1 3
1 4
3 4
```
```output
1
2
1
```
-----
```input
3 4
1 3
1 3
4 5
1 2
1 3
1 4
1 5
```
```output
1
1
-1
-1
```

## Note
In the first example there are three queries:

1. query $$$[1; 3]$$$ can be covered by interval $$$[1; 3]$$$;
2. query $$$[1; 4]$$$ can be covered by intervals $$$[1; 3]$$$ and $$$[2; 4]$$$. There is no way to cover $$$[1; 4]$$$ by a single interval;
3. query $$$[3; 4]$$$ can be covered by interval $$$[2; 4]$$$. It doesn't matter that the other points are covered besides the given query.

In the second example there are four queries:

1. query $$$[1; 2]$$$ can be covered by interval $$$[1; 3]$$$. Note that you can choose any of the two given intervals $$$[1; 3]$$$;
2. query $$$[1; 3]$$$ can be covered by interval $$$[1; 3]$$$;
3. query $$$[1; 4]$$$ can't be covered by any set of intervals;
4. query $$$[1; 5]$$$ can't be covered by any set of intervals. Note that intervals $$$[1; 3]$$$ and $$$[4; 5]$$$ together don't cover $$$[1; 5]$$$ because even non-integer points should be covered. Here $$$3.5$$$, for example, isn't covered.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 2.0 seconds
Memory limit: 256.0 MB

# Problem
You have an array $$$a_1, a_2, \dots, a_n$$$.

Let's call some subarray $$$a_l, a_{l + 1}, \dots , a_r$$$ of this array a subpermutation if it contains all integers from $$$1$$$ to $$$r-l+1$$$ exactly once. For example, array $$$a = [2, 2, 1, 3, 2, 3, 1]$$$ contains $$$6$$$ subarrays which are subpermutations: $$$[a_2 \dots a_3]$$$, $$$[a_2 \dots a_4]$$$, $$$[a_3 \dots a_3]$$$, $$$[a_3 \dots a_5]$$$, $$$[a_5 \dots a_7]$$$, $$$[a_7 \dots a_7]$$$.

You are asked to calculate the number of subpermutations.

## Input Format
The first line contains one integer $$$n$$$ ($$$1 \le n \le 3 \cdot 10^5$$$).

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots , a_n$$$ ($$$1 \le a_i \le n$$$).

This array can contain the same integers.

## Output Format
Print the number of subpermutations of the array $$$a$$$.

## Examples
```input
8
2 4 1 3 4 2 1 2
```
```output
7
```
-----
```input
5
1 1 2 1 2
```
```output
6
```

## Note
There are $$$7$$$ subpermutations in the first test case. Their segments of indices are $$$[1, 4]$$$, $$$[3, 3]$$$, $$$[3, 6]$$$, $$$[4, 7]$$$, $$$[6, 7]$$$, $$$[7, 7]$$$ and $$$[7, 8]$$$.

In the second test case $$$6$$$ subpermutations exist: $$$[1, 1]$$$, $$$[2, 2]$$$, $$$[2, 3]$$$, $$$[3, 4]$$$, $$$[4, 4]$$$ and $$$[4, 5]$$$.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 5.0 seconds
Memory limit: 512.0 MB

# Problem
You are given array $$$a_1, a_2, \dots, a_n$$$. You need to split it into $$$k$$$ subsegments (so every element is included in exactly one subsegment).

The weight of a subsegment $$$a_l, a_{l+1}, \dots, a_r$$$ is equal to $$$(r - l + 1) \cdot \max\limits_{l \le i \le r}(a_i)$$$. The weight of a partition is a total weight of all its segments.

Find the partition of minimal weight.

## Input Format
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^4$$$, $$$1 \le k \le \min(100, n)$$$) — the length of the array $$$a$$$ and the number of subsegments in the partition.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 2 \cdot 10^4$$$) — the array $$$a$$$.

## Output Format
Print single integer — the minimal weight among all possible partitions.

## Examples
```input
4 2
6 1 7 4
```
```output
25
```
-----
```input
4 3
6 1 7 4
```
```output
21
```
-----
```input
5 4
5 1 5 1 5
```
```output
21
```

## Note
The optimal partition in the first example is next: $$$6$$$ $$$1$$$ $$$7$$$ $$$\bigg|$$$ $$$4$$$.

The optimal partition in the second example is next: $$$6$$$ $$$\bigg|$$$ $$$1$$$ $$$\bigg|$$$ $$$7$$$ $$$4$$$.

One of the optimal partitions in the third example is next: $$$5$$$ $$$\bigg|$$$ $$$1$$$ $$$5$$$ $$$\bigg|$$$ $$$1$$$ $$$\bigg|$$$ $$$5$$$.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 1.0 seconds
Memory limit: 256.0 MB

# Problem
A class of students wrote a multiple-choice test.

There are $$$n$$$ students in the class. The test had $$$m$$$ questions, each of them had $$$5$$$ possible answers (A, B, C, D or E). There is exactly one correct answer for each question. The correct answer for question $$$i$$$ worth $$$a_i$$$ points. Incorrect answers are graded with zero points.

The students remember what answers they gave on the exam, but they don't know what are the correct answers. They are very optimistic, so they want to know what is the maximum possible total score of all students in the class.

## Input Format
The first line contains integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 1000$$$) — the number of students in the class and the number of questions in the test.

Each of the next $$$n$$$ lines contains string $$$s_i$$$ ($$$|s_i| = m$$$), describing an answer of the $$$i$$$-th student. The $$$j$$$-th character represents the student answer (A, B, C, D or E) on the $$$j$$$-th question.

The last line contains $$$m$$$ integers $$$a_1, a_2, \ldots, a_m$$$ ($$$1 \le a_i \le 1000$$$) — the number of points for the correct answer for every question.

## Output Format
Print a single integer — the maximum possible total score of the class.

## Examples
```input
2 4
ABCD
ABCE
1 2 3 4
```
```output
16
```
-----
```input
3 3
ABC
BCD
CDE
5 4 12
```
```output
21
```

## Note
In the first example, one of the most optimal test answers is "ABCD", this way the total number of points will be $$$16$$$.

In the second example, one of the most optimal test answers is "CCC", this way each question will be answered by exactly one student and the total number of points is $$$5 + 4 + 12 = 21$$$.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 1.0 seconds
Memory limit: 256.0 MB

# Problem
You are given an array $$$a_1, a_2, \ldots, a_n$$$.

In one operation you can choose two elements $$$a_i$$$ and $$$a_j$$$ ($$$i \ne j$$$) and decrease each of them by one.

You need to check whether it is possible to make all the elements equal to zero or not.

## Input Format
The first line contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the size of the array.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of the array.

## Output Format
Print "YES" if it is possible to make all elements zero, otherwise print "NO".

## Examples
```input
4
1 1 2 2
```
```output
YES
```
-----
```input
6
1 2 3 4 5 6
```
```output
NO
```

## Note
In the first example, you can make all elements equal to zero in $$$3$$$ operations:

- Decrease $$$a_1$$$ and $$$a_2$$$,
- Decrease $$$a_3$$$ and $$$a_4$$$,
- Decrease $$$a_3$$$ and $$$a_4$$$

In the second example, one can show that it is impossible to make all elements equal to zero.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 2.0 seconds
Memory limit: 256.0 MB

# Problem
You are given an array $$$a$$$ of $$$n$$$ integers, where $$$n$$$ is odd. You can make the following operation with it:

- Choose one of the elements of the array (for example $$$a_i$$$) and increase it by $$$1$$$ (that is, replace it with $$$a_i + 1$$$).

You want to make the median of the array the largest possible using at most $$$k$$$ operations.

The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array $$$[1, 5, 2, 3, 5]$$$ is $$$3$$$.

## Input Format
The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$n$$$ is odd, $$$1 \le k \le 10^9$$$) — the number of elements in the array and the largest number of operations you can make.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).

## Output Format
Print a single integer — the maximum possible median after the operations.

## Examples
```input
3 2
1 3 5
```
```output
5
```
-----
```input
5 5
1 2 1 1 1
```
```output
3
```
-----
```input
7 7
4 1 2 4 3 4 4
```
```output
5
```

## Note
In the first example, you can increase the second element twice. Than array will be $$$[1, 5, 5]$$$ and it's median is $$$5$$$.

In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is $$$3$$$.

In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be $$$[5, 1, 2, 5, 3, 5, 5]$$$ and the median will be $$$5$$$.

Now solve the problem and return the code.
You are an expert competitive programmer. You will be given a problem statement, test case constraints and example test inputs and outputs. Please reason step by step about the solution (that must respect memory and time limits), then provide a complete implementation in c++17.

Your solution must read input from standard input (cin), write output to standard output (cout).
Do not include any debug prints or additional output.

Put your final solution within a single code block:
```cpp
<your code here>
```

Execution time limit: 1.0 seconds
Memory limit: 256.0 MB

# Problem
You play your favourite game yet another time. You chose the character you didn't play before. It has $$$str$$$ points of strength and $$$int$$$ points of intelligence. Also, at start, the character has $$$exp$$$ free experience points you can invest either in strength or in intelligence (by investing one point you can either raise strength by $$$1$$$ or raise intelligence by $$$1$$$).

Since you'd like to make some fun you want to create a jock character, so it has more strength than intelligence points (resulting strength is strictly greater than the resulting intelligence).

Calculate the number of different character builds you can create (for the purpose of replayability) if you must invest all free points. Two character builds are different if their strength and/or intellect are different.

## Input Format
The first line contains the single integer $$$T$$$ ($$$1 \le T \le 100$$$) — the number of queries. Next $$$T$$$ lines contain descriptions of queries — one per line.

This line contains three integers $$$str$$$, $$$int$$$ and $$$exp$$$ ($$$1 \le str, int \le 10^8$$$, $$$0 \le exp \le 10^8$$$) — the initial strength and intelligence of the character and the number of free points, respectively.

## Output Format
Print $$$T$$$ integers — one per query. For each query print the number of different character builds you can create.

## Examples
```input
4
5 3 4
2 1 0
3 5 5
4 10 6
```
```output
3
1
2
0
```

## Note
In the first query there are only three appropriate character builds: $$$(str = 7, int = 5)$$$, $$$(8, 4)$$$ and $$$(9, 3)$$$. All other builds are either too smart or don't use all free points.

In the second query there is only one possible build: $$$(2, 1)$$$.

In the third query there are two appropriate builds: $$$(7, 6)$$$, $$$(8, 5)$$$.

In the fourth query all builds have too much brains.

Now solve the problem and return the code.