• 2018-09-08
• 14
• 0

# A. Function Height

## Description:

You are given a set of 2n+1 integer points on a Cartesian plane. Points are numbered from 0 to 2n inclusive. Let P_i be the i-th point. The x-coordinate of the point P_i equals i. The y-coordinate of the point P_i equals zero (initially). Thus, initially P_i=(i,0).
The given points are vertices of a plot of a piecewise function. The j-th piece of the function is the segment P_{j}P_{j + 1}.
In one move you can increase the y-coordinate of any point with odd x-coordinate (i.e. such points are P_1, P_3, \dots, P_{2n-1}) by 1. Note that the corresponding segments also change.
For example, the following plot shows a function for n=3 (i.e. number of points is 2\cdot3+1=7) in which we increased the y-coordinate of the point P_1 three times and y-coordinate of the point P_5 one time:

Let the area of the plot be the area below this plot and above the coordinate axis OX. For example, the area of the plot on the picture above is 4 (the light blue area on the picture above is the area of the plot drawn on it).
Let the height of the plot be the maximum y-coordinate among all initial points in the plot (i.e. points P_0, P_1, \dots, P_{2n}). The height of the plot on the picture above is 3.
Your problem is to say which minimum possible height can have the plot consisting of 2n+1 vertices and having an area equal to k. Note that it is unnecessary to minimize the number of moves.
It is easy to see that any answer which can be obtained by performing moves described above always exists and is an integer number not exceeding 10^{18}.

## Input:

The first line of the input contains two integers n and k (1 \le n, k \le 10^{18}) — the number of vertices in a plot of a piecewise function and the area we need to obtain.

## Output

Print one integer — the minimum possible height of a plot consisting of 2n+1 vertices and with an area equals k. It is easy to see that any answer which can be obtained by performing moves described above always exists and is an integer number not exceeding 10^{18}.

4 3

1

4 12

3

## Sample Input:

999999999999999999 999999999999999986

1

# B. Diagonal Walking v.2

## Description:

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:

• (1, 0);

• (1, 1);

• (0, 1);

• (-1, 1);

• (-1, 0);

• (-1, -1);

• (0, -1);

(1, -1). If Mikhail goes from the point (x1, y1) to the point (x2, y2) in one move, and x1 \ne x2 and y1 \ne y2, then such a move is called a diagonal move.
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.
Note that Mikhail can visit any point any number of times (even the destination point!).

## Input:

The first line of the input contains one integer q (1 \le q \le 10^4) — the number of queries.
Then q​ lines follow. The i​-th of these q​ lines contains three integers n_i​, m_i​ and k_i​ (1 \le n_i, m_i, k_i \le 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.

## Output

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.

3
2 2 3
4 3 7
10 1 9

1
6
-1

# D. Vasya and Arrays

## Description:

Vasya has two arrays A and B of lengths n and m, respectively.
He can perform the following operation arbitrary number of times (possibly zero): he takes some consecutive subsegment of the array and replaces it with a single element, equal to the sum of all elements on this subsegment. For example, from the array [1, 10, 100, 1000, 10000] Vasya can obtain array [1, 1110, 10000], and from array [1, 2, 3] Vasya can obtain array [6].
Two arrays A and B are considered equal if and only if they have the same length and for each valid i A_i = B_i.
Vasya wants to perform some of these operations on array A, some on array B, in such a way that arrays A and B become equal. Moreover, the lengths of the resulting arrays should be maximal possible.
Help Vasya to determine the maximum length of the arrays that he can achieve or output that it is impossible to make arrays A and B equal.

## Input:

The first line contains a single integer n~(1 \le n \le 3 \cdot 10^5) — the length of the first array.
The second line contains n integers a_1, a_2, \cdots, a_n~(1 \le a_i \le 10^9) — elements of the array A.
The third line contains a single integer m~(1 \le m \le 3 \cdot 10^5) — the length of the second array.
The fourth line contains m integers b_1, b_2, \cdots, b_m~(1 \le b_i \le 10^9) – elements of the array B.

## Output

Print a single integer — the maximum length of the resulting arrays after some operations were performed on arrays A and B in such a way that they became equal.
If there is no way to make array equal, print “-1”.

5
11 2 3 5 7
4
11 7 3 7

3

2
1 2
1
100

-1

3
1 2 3
3
1 2 3

3