The 16th Heilongjiang Provincial Collegiate Programming Contest
A. And RMQ
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a sequence consisting of $$$N$$$ integers $$$\lbrace a_i \rbrace$$$. There will be $$$m$$$ different events.

  • $$$\texttt{AND} ~ l ~ r ~ v$$$ – for all elements $$$a_i ~ (l \leq i \leq r)$$$, change $$$a_i$$$ to $$$a_i ~ \& ~ v$$$, where $$$\&$$$ means bitwise AND

  • $$$\texttt{UPD} ~ x ~ v$$$ – change $$$a_x$$$ to $$$v$$$

  • $$$\texttt{QUE} ~ l ~ r$$$ – ask for the maximum value of $$$a_i$$$ where $$$l \leq i \leq r$$$

Please write a program to support these events efficiently.

Input

The first line contains two integers $$$N, M ~ (1 \leq N, M \leq 400\,000)$$$ denoting the length of sequence and the number of events.

The second line contains $$$N$$$ integers $$$a_1, a_2, \cdots, a_n ~ (1 \leq a_i \leq 2^{30}-1)$$$ denoting the initial elements of the sequence.

For the next $$$M$$$ lines, each line describes an event. It is guaranteed that $$$1 \leq l, r, x \leq n$$$ and $$$1 \leq v \leq 2^{30}-1$$$.

Output

For each $$$\texttt{QUE}$$$ event, print a single line containing an integer, denoting the answer.

It is guaranteed that there is at least one $$$\texttt{QUE}$$$ event.

Example
Input
5 11
6 3 5 2 3
AND 2 4 10
UPD 1 8
AND 3 4 7
UPD 1 6
QUE 1 4
AND 3 5 10
AND 3 5 2
UPD 5 1
QUE 2 5
UPD 3 6
QUE 1 5
Output
6
2
6
Note

Since the input file is large, please use 'fread' (strongly recommended) or 'scanf' instead of 'cin'.

B. Bo Bing
time limit per test
2 seconds
memory limit per test
128 megabytes
input
standard input
output
standard output

Moon cake betting (博饼) is a traditional Mid-autumn festival activity originated in Xiamen and then spread to southern Fujian. It began in the early Qing Dynasty. It was invented by Zheng Chenggong when he stationed in order to resolve the Mid-autumn lovesickness of the soldiers and inspire their morale. As a result, it has become a unique folk custom in Southern Fujian.

However, no one around wor knows how to play Moon cake betting, he have to play it with his brain girlfriend.

Moon cake betting is a game of dice. Each person rolls 6 dice at a time and gets corresponding prizes according to the result type.

All result types are as follows:

  • ZhuangYuan: There are six sub-situations.

    • SiHong: Four $$$\texttt{#4}$$$ + two arbitrary non $$$\texttt{#4}$$$ (non $$$\texttt{#1#1}$$$), which can not form a Multi-award special case.
    • WuZiDengKe: Five arbitrary (non $$$\texttt{#4#4#4#4#4}$$$) + one arbitrary different from the previous ones, which can form a Multi-award special case.
    • WuHong: Five $$$\texttt{#4}$$$ + one arbitrary non $$$\texttt{#4}$$$, which can not form a Multi-award special case.
    • ManDiJin: Six arbitrary (non $$$\texttt{#4#4#4#4#4#4}$$$), which can not form a Multi-award special case.
    • ManTangCai: Six $$$\texttt{#4}$$$, which can not form a Multi-award special case.
    • ZhuangYuanChaJinHua: Four $$$\texttt{#4}$$$ + two $$$\texttt{#1}$$$, which can not form a Multi-award special case.

    The rank of ZhuangYuan is compared according to the following rules:

    1. First compare the type. SiHong < WuZiDengKe < WuHong < ManDiJin < ManTangCai < ZhuangYuanChaJinHua
    2. If the types are same, then compare according to the following rules:

      1. If they are same SiHong, then compare the sum of remaining two dices points.
      2. If they are same WuZiDengKe, then compare the points of dice that form the WuZiDengKe. If still the same, then compare the remaining one dice points.
      3. If they are same WuHong, then compare the remaining one dice points.
      4. If they are same ManDiJin, then compare the points of dice that form the ManDiJin.
      5. If still same after compared by above rules, then the first one who rolled will get it.

  • DuiTang: $$$\texttt{#1#2#3#4#5#6}$$$, which can not form a Multi-award special case.

  • SanHong: Three $$$\texttt{#4}$$$ + three arbitrary non $$$\texttt{#4}$$$, which can not form a Multi-award special case.
  • SiJin: Four arbitrary non $$$\texttt{#4}$$$ + two arbitrary different from the previous, which can form a Multi-award special case.
  • ErJu: Two $$$\texttt{#4}$$$ + four arbitrary non $$$\texttt{#4}$$$ and doesn't constitute any other form, which can not form a Multi-award special case.

  • YiXiu: One $$$\texttt{#4}$$$ + five arbitrary non $$$\texttt{#4}$$$ and doesn't constitute any other form, which can not form a Multi-award special case.
  • LuoBang: Doesn't constitute any other form.

There are also several special cases:

  • TanChu: If the dice is thrown out of range (concretely, get $$$\texttt{#0}$$$), the next round will stop, and this roll is invalid.
  • ManTangHei: Six $$$\texttt{#6}$$$, a part of ManDiJin, except for the ZhuangYuan prize, the rest of the other prize all will be taken away.

  • Multi-award: May appear in the SiJin and WuZiDengKe, for example $$$\texttt{#3#3#3#3#4#2}$$$ is SiJin with YiXiu, in this case, player can get the award of SiJin and YiXiu at the same time; $$$\texttt{#5#5#5#5#5#4}$$$ is WuZiDengKe with YiXiu, in this case, player can get the award of YiXiu and record the ZhuangYuan at the same time.

Now it is known that there are $$$1$$$ ZhuangYuan prize, $$$2$$$ DuiTang prizes, $$$a$$$ SanHong prizes, $$$b$$$ SiJin prizes, $$$c$$$ ErJu prizes, $$$d$$$ YiXiu prizes.

The prize rules are as follows:

  • For ZhuangYuan prize, at the end of game, the biggest one who rolled will win it, and every one will take the last time that rolled the ZhuangYuan to compared.

  • For other prizes, first come, first served. If anyone roll a prize but there is no residue then he get nothing. For example player A roll the YiXiu and there is only one YiXiu residue then A can get the last YiXiu. After that, player B roll the YiXiu. But now there is no YiXiu residue so B can not get anything.

Now we know that $$$n$$$ player sit together in a circle and play $$$m$$$ rounds. Player 1 starts to roll. They roll in turn $$$m$$$ times in the order of $$$(1, 2, 3, \cdots, n-1, n, 1, 2, \cdots)$$$.

If any player throw dice out of the range and get TanChu, then this roll is invalid, and he will do nothing the next round (Caution, skipped round is counted) and let the next player roll.

Given the game situation, can you find out the final prize situation of every player?

Input

The first line contains six integers $$$n, m, a, b, c, d ~ (1 \leq n \le 10^6; ~ 0 \le m \le 10^6; ~ 1 \le a, b, c, d \le 10^9)$$$ denoting the number of players, the numbers of rounds to play, $$$a$$$ SanHong prizes, $$$b$$$ SiJin prizes, $$$c$$$ ErJu prizes, $$$d$$$ YiXiu prizes respectively.

Following $$$m$$$ lines each contains a string $$$\texttt{#}g_1\texttt{#}g_2\texttt{#}g_3\texttt{#}g_4\texttt{#}g_5\texttt{#}g_6$$$ representing the result of this throw.

If it is $$$\texttt{#}0$$$, it means that the dice is thrown out of range.

If it is between $$$\texttt{#}1$$$ and $$$\texttt{#}6$$$, it is the thrown number of dice.

Note that if someone stops for a round, this line will be blank.

Output

Output $$$n$$$ lines.

Each line contains $$$6$$$ integers representing the final prize situation of every player, in the order of ZhuangYuan, DuiTang, SanHong, SiJin, ErJu, YiXiu.

Examples
Input
2 10 10 10 10 10
#4#4#4#4#1#1
#1#2#3#4#5#6
#1#2#3#5#6#6
#0#4#4#4#4#4
#4#4#4#4#2#2

#4#1#4#2#4#3
#5#5#5#5#5#1
#1#1#1#1#4#4
#6#6#6#6#6#6
Output
0 0 1 1 1 0
1 2 9 9 9 10
Input
2 20 10 10 10 10
#2#5#5#6#6#6
#5#6#3#2#4#3
#6#3#4#1#1#3
#4#3#2#5#6#6
#2#3#3#4#4#3
#2#6#4#6#1#2
#4#4#6#4#2#1
#6#5#6#5#5#6
#6#3#1#5#2#2
#4#6#2#3#2#5
#4#1#3#3#6#6
#3#1#3#3#2#2
#6#3#5#6#1#1
#5#2#6#2#3#3
#4#1#6#6#2#5
#5#2#3#5#5#4
#5#1#1#4#3#4
#4#2#6#5#4#4
#5#2#6#3#4#4
#2#2#6#3#3#5
Output
0 0 1 0 3 3
0 0 1 0 0 5

C. Cookie
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little G loves cookies.

One day, he bought a bag of cookies and started eating. The shape of each cookie can be approximately regarded as a convex polygon in 2D coordinate system. Unconsciously, a few cookies fell to the ground and each of them broke into two pieces. The broken pieces can be regarded as simple polygons.

Little G didn't want them to go to waste. So, he randomly picked two pieces from the ground and try to restore the original cookie by translating one of the pieces.

Little G is poor at geometry, it takes long time for him to do this or figure out it's just impossible to form a convex polygon using the two simple polygons. Please write a program to help him.

Note that after combining, the overlapping edges of two polygons need to correspond one to one. One edge matching two or more edges of another polygon is illegal.

The graph above shows one illegal situation for matching (a). The situation (b) of the graph is illegal because the right edge of the first square matches two edges of the other polygon. The situation (c) is legal.

Input

The first line contains an integer $$$n ~ (3 \leq n \leq 2000)$$$, indicating the number of the vertexes of the first simple polygon.

Then the following $$$n$$$ lines describes the coordinates of the $$$n$$$ vertexes in counter-clockwise order. Each line contains only two integers $$$x_i, y_i ~ (0 \leq x_i,y_i \leq 10^9)$$$.

The next line contains an integer $$$m ~ (3 \leq m \leq 2000)$$$, indicating the number of the vertexes of the second simple polygon.

Then the following $$$m$$$ lines describes the coordinates of the $$$m$$$ vertexes in counter-clockwise order. Each line contains only two integers $$$x_i, y_i ~ (0 \leq x_i,y_i \leq 10^9)$$$.

Output

One line with a single string yes or no. Print yes if these two polygons can form a convex polygon by translating one of them, otherwise print no.

Example
Input
8
20 0
18 15
12 17
6 9
10 6
12 8
12 16
17 8
9
11 18
2 15
9 1
19 1
16 9
11 17
11 9
9 7
5 10
Output
yes
Note

The graph below describes the sample where $$$P_1, P_2, \cdots, P_8$$$ is the $$$8$$$ points for the first polygon and $$$Q_1, Q_2, \cdots, Q_9$$$ is the $$$9$$$ points for the second polygon respectively.

D. Doin' Time
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One day, Fop_zz is playing a boring game merge numbers though he is busy.

There is a sequence $$$\{a_i\}$$$ of length $$$N$$$ and he should play this game for $$$n-1$$$ turns. In one turn, he chooses an index $$$x$$$ and then he merges $$$a_x$$$ and $$$a_{x+1}$$$ to $$$a_x \times a_{x+1} \bmod 1\,000\,003$$$ and gets $$$(a_x-a_{x+1})^2$$$ scores.

So, after one turn, the length of sequence will decrease exactly one, and after the last turn there's only $$$1$$$ integer in the sequence and he stops.

Now he want to know the maximum total scores he can get. Can you help him?

Input

The first line contains one integer $$$N ~ (1 \leq N \leq 300)$$$ denoting the length of sequence.

The second line contains $$$N$$$ integers $$$a_1, a_2, \cdots, a_n ~ (1 \leq a_i \leq 10^6)$$$.

Output

Output one line with only one integer denoting the answer.

Example
Input
3
1 2 3
Output
26

E. Elastic Search
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given $$$n$$$ strings $$$s_1, s_2, \cdots, s_n$$$. We call $$$t$$$ a substring of $$$s$$$ if we can get $$$t$$$ by deleting several consecutive characters (probably zero) from the beginning and/or the end of the string $$$s$$$.

We define a multinest of strings as a sequence $$$p_1, p_2, \cdots, p_m ~ (m \leq n)$$$, where $$$p_i$$$ are pair-wise distinct and $$$s_{p_i}$$$ is a substring of $$$s_{p_{i-1}}$$$ for any $$$2 \leq i \leq m$$$. The length of the multinest is the number of elements in the sequence.

Now you need to calculate the maximum possible length of the multinest among the $$$n$$$ strings.

Input

The first line contains an integer $$$n ~ (1 \leq n \leq 500000)$$$, indicating the number of strings.

Then $$$n$$$ lines follows. Each line contains a string $$$s_i$$$ which consists of only lowercase letters.

It is guaranteed that $$$\sum_{i = 1}^n |s_i| \leq 500000$$$.

Output

Only one integer $$$L$$$ which represents the maximum length of the multinest.

Example
Input
6
bcba
cba
cbcb
cba
cb
bcb
Output
4
Note

The longest multinest in the sample is $$$(bcba, cba, cba, cb)$$$ of which the the index sequence is $$$(1, 2, 4, 5)$$$ which has length $$$4$$$.

F. Function
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One day, Little Y came cross a function that confused her a lot.

$$$$$$ f(n) = \begin{cases} 1, & n \in \{1\} \bigcup \operatorname{Prime} \\ p\,f(p^{k-2}), & n = p^k ~ (p \in \operatorname{Prime}; ~ k \gt 1) \\ f(p_1^{e_1})\prod_{i=2}^r {p_i}^{e_i}, & n = \prod_{i=1}^r {p_i} ^ {e_i} ~ (p_1 \lt p_2 \lt \cdots \lt p_r; ~ p_i \in \operatorname{Prime}; ~ r \ge 2) \end{cases} $$$$$$

Now Little Y is interested in $$$\sum_{i=1}^n f(i)$$$. Can you help her?

Input

One line contains one integer denoting $$$n ~ (1 \le n \le {10}^{7})$$$.

Output

One line, the number denoting the answer.

Examples
Input
10
Output
20
Input
100
Output
1487
Note

For the first sample, $$$f(1) = 1$$$, $$$f(2) = 1$$$, $$$f(3) = 1$$$, $$$f(4) = 2$$$, $$$f(5) = 1$$$, $$$f(6) = 3$$$, $$$f(7) = 1$$$, $$$f(8) = 2$$$, $$$f(9) = 3$$$, $$$f(10) = 5$$$.

It is guaranteed that the answer will always be less than $$$2^{64}-1$$$.

Little Y wonders whether you can solve $$$n$$$ up to $$${10}^{10}$$$.

G. Go? No
time limit per test
1.5 seconds
memory limit per test
512 МБ
input
standard input
output
standard output

NoGo is a game whose rules are the reverse of Go. In this game, the player who takes opponent's chess pieces or kills his own pieces will lose the game.

Little G is writing a bot to play the game. And he is now working on a simplified version of this game. In the simplified version, Little G has a chess board of size $$$n \times m$$$. There are only two kinds of state in each intersection: empty or occupied by his stone. Two intersections are considered connected if they are orthogonally adjacent (up, down, left, or right). He believes that the more connected components of empty positions his stones can separate, the greater chance he will have to win.

Now you are given the state of chess board at some point. You need to place one stone into an empty intersection and make the number of separated empty connected components as large as possible after placing your stone.

Input

The first line contains two integers $$$n$$$ and $$$m ~ (1 \leq n, m \leq 2000)$$$, indicating the number of rows and columns of the chess board.

Then $$$n$$$ lines follows, each line contains a string of length $$$m$$$ indicating the state of each position in the chess board. A character of "$$$\texttt{#}$$$" represents an occupied position and a character of "$$$\texttt{.}$$$" represents an empty position. It is guaranteed that at least one position is empty.

Output

Output one line with only one integer – the maximum empty connected component you can get after placing one stone into current chess board.

Examples
Input
3 3
#..
#.#
..#
Output
2
Input
4 4
#.##
.#.#
...#
.#.#
Output
4
Note

For the first sample, one possible method is to place the stone into intersection $$$(2, 2)$$$ (indexed form $$$1$$$) and get two empty connected components.

For the second sample, one possible method is to place the stone into intersection $$$(3, 1)$$$ and get four empty connected components.

H. Hack DSU!
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

As we all know, disjoint set union is a useful tool. However, sometimes we must apply the strategy of union by rank or path compression to make it quick. In general, if we have $$$n$$$ operations of merge, we may do all these operations in time complexity of $$$O(n \alpha(n))$$$, where $$$\alpha(n) \leq 4$$$ for $$$n \leq 10^5$$$.

Little Y is the teaching assistant of Data Structure class. One day, one problem was left for practice. There are $$$n$$$ sets numbered from $$$1$$$ to $$$n$$$. Then there are $$$n$$$ pairs of integer $$$(A_i,B_i)$$$. We should merge set $$$A$$$ and $$$B$$$ for each pair $$$(A,B)$$$. The task is to get the count of disjoint sets after all operations.

When reading the homework codes from students, Little Y noticed one implementation of find operation without recursion. Then she thinks this code is not implemented correctly, which may lead to about $$$O(n^2)$$$ times of read or write operations to the array $$$\texttt{parent}$$$. To verify this, she adds a temporary variable called $$$\texttt{counter}$$$ to track the count of write operations.

#include <iostream>
using namespace std;
const int MAXN = 1e5+10;
int parent[MAXN];
long long counter = 0;

int find(int x) {
while (x != parent[x]) {
if (x < parent[x]) {
// Merge-by-rank and Path-compression
parent[x] = parent[parent[x]];
}
x = parent[x];
counter++;
}

return x;
}

void merge(int a, int b) {
a = find(a);
b = find(b);
parent[a] = b;
}

int main() {
int n, A, B, ans = 0;
cin >> n;

for (int i = 1; i <= n; i++)
parent[i] = i;

for (int i = 1; i <= n; i++) {
cin >> A >> B;
merge(A, B);
}

for (int i = 1; i <= n; i++)
if (i == find(i))
ans++;

cout << ans << endl;
return 0;
}

Little Y thinks there exists one input data that makes the $$$\texttt{counter}$$$ large enough. Can you help her to provide one input data to prove that she is right?

For a given $$$n$$$, you should construct $$$n$$$ pairs of integers $$$(A_i, B_i)$$$. When the number $$$n$$$ and $$$n$$$ pairs of integers $$$(A_i, B_i)$$$ is sent to this program, you should guarantee that the final value of $$$\texttt{counter}$$$ should be greater than a certain number $$$T$$$, which means that this program cannot stop within $$$T$$$ write operations.

Input

One line contains two integer $$$n ~ (10 \leq n \leq 10^4)$$$ and $$$T ~ (n \leq T \leq \frac{n^2}{\log_2 n})$$$ denoting the count of initial sets and the least write operations that this program must take.

Output

Output $$$n$$$ lines.

The $$$i$$$-th line contains two integers $$$A_i, B_i ~ (1 \leq A_i, B_i \leq n)$$$ denoting the $$$i$$$-th pair of number.

Example
Input
10 10
Output
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 1
Note

The $$$\texttt{counter}$$$ of sample input is $$$18$$$.

I. ICU4C
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

ICU for Car! is an exciting racing game. Two players take turns to make a move. In one move, the player chooses one car and push this car one grid right-forward. Cars satisfying all the conditions will be also pushed forward too.

  • In the same racetrack with the selected car.
  • In front of the selected car.
  • Connected with the selected car. Formally, two cars $$$x$$$ and $$$y$$$ are connected, if there is no gap between $$$x$$$ and $$$y$$$, or they are both connected with car $$$z$$$.

Note that if the right-most car in this move has already touched the finishing line, this move is not valid and you cannot process it. The player who can't make a move loses.

There are $$$n$$$ racetracks holding the cars in one game. For each racetrack, the number of cars $$$m_i$$$, the position of each car $$$x_j$$$ and the position of finishing line $$$e_i$$$ are given.

Now Alice and Bob wants to play this game and Alice takes the first turn. Can you tell wor the winner?

Input

The first line contains one integer $$$T ~ (1 \leq T \leq 20)$$$ denoting the number of test cases.

For each testcase,

The first line contains one integer $$$n ~ (1 \leq n \leq 1\,000)$$$ denoting the number of racetracks.

The $$$2i$$$-th line contains two integer $$$m_i, e_i ~ (1 \leq m_i \leq 1\,000; 0 \lt e_i \leq 1\,000\,000\,000)$$$ denoting there are $$$m_i$$$ cars on the racetrack $$$i$$$ and the finishing line is at $$$e_i$$$. It is guaranteed that $$$\sum m \leq 1\,500\,000$$$.

The $$$2i+1$$$-th line contains $$$m_i$$$ integers $$$x_1, x_2, \cdots, x_{m_i} ~ (0 \lt x_j \leq e_i)$$$ representing the initial position of each car on the racetrack $$$i$$$. It is guaranteed that $$$x_i \lt x_j$$$ for $$$i \lt j$$$.

Output

For each testcase, output one line containing the name of winner (either Alice or Bob).

Example
Input
2
1
3 5
2 3 5
2
3 5
2 3 5
3 5
2 3 5
Output
Alice
Bob

J. JOJO's Factory
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Aki is the prime minister in JOJO's Factory.

There are two types of machines in the factory, which are called A-machine and B-machine respectively. Both the number of two types of machines are $$$N$$$. One A-machine should work with exactly one B-machine and one B-machine should work with exactly one A-machine.

Unfortunately, some pairs of machines can not work together due to some unknown reason. A pair $$$(i,j)$$$ means $$$i$$$-th A-machine can not work with $$$j$$$-th B-machine.

In order to improve the efficiency, you, his staff, should find a plan that the most A-machines can run normally.

Luckily, Aki is a talented prime minister, so the number of un-working pairs is no more than $$$2N-3$$$.

Input

The first line contains two integers $$$N, M ~ (5 \leq N \leq 5 \times 10^5; ~ 0 \leq M \leq 2N-3)$$$ denoting the count of machines and the count of pairs of machines that don't work together.

Then next $$$M$$$ lines follows, each containing two integers $$$i, j ~ (1 \leq i, j \leq N)$$$ denoting the un-working pairs. It is guaranteed that all pairs of $$$(i,j)$$$ are different.

Output

Output one line containing the maximal number of normally running A-machines.

Example
Input
4 5
1 2
3 1
1 3
1 4
1 1
Output
3

K. Keep Eating
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Megumi is a girl who likes to eat.

Today there will be a party at Aki's home. There are $$$N$$$ cakes for Megumi of different weight on the table. She will choose one cake and eat no more than half of it every time. But out of conscience, she won't eat those cakes whose weight is smaller than $$$K$$$.

However, she knows a magic to merge two cakes. That means she can put two cakes together and combine them into a larger cake whose weight is equal to the sum of weight of previous cakes.

Now Aki wants to know the maximum total weight of cakes Megumi can eat. Please note that she can eat as many times as she can, and each time the weight she eat is a positive integer no more than the half of the weight of cake.

Input

The first line contains two integers $$$N, K ~ (1 \leq N \leq 200\,000; ~ 2 \leq K \leq 10^6)$$$.

The second line contains $$$N$$$ integers $$$a_1, a_2, \cdots, a_n ~ (1 \leq a_i \leq 10^6)$$$ denoting the weight of each cake.

Output

Output one integer denoting the maximum total weight of cakes she can eat.

Example
Input
5 10
9 8 7 10 10
Output
39

L. Labi-Ribi
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Labi-ribi is a newly published STG. As everyone knows, wor is a TouHouProject-Player. As a TouHouProject-Player, he must want to play such funny STG.

Labi-ribi has $$$n$$$ stages. Each stage has a boss at level $$$h_i$$$. Only when player's level is greater than or equal to boss's level, the player can beat boss and clear this stage. After beating $$$i$$$-th boss, the level of all remaining bosses will be increased $$$a_i$$$ points, and the player's level will be increased $$$b_i$$$ points.

wor want to challenge the hardest. Therefore, he wants to use the minimun initial level to clear all stages. Can you tell him the minimun initial level that he need to clear all stages?

Labi-ribi is a great game, so there is an additional DLC annually. Now there has been $$$q$$$ DLCs. For one DLC, it provides a brand new stage. wor is so patient that every time an additional DLC was provided, he would play Labi-ribi again. Similarly, for each additional DLC, wor wants to know the minimun initial level that can clear all stages. Can you help him?

Input

The first line contains one integer $$$n ~ (1 \le n \le 10^5)$$$ denoting the number of initial stages.

The second line contains $$$n$$$ integers $$$h_1, h_2, \cdots, h_n ~ (-10^9 \le h_i \le 10^9)$$$ denoting the initial level of $$$i$$$-th boss

Then $$$n$$$ lines follows, the $$$i$$$-th of which contains two integers $$$a_i, b_i ~ (-10^9 \le a_i, b_i \le 10^9)$$$.

The $$$n+3$$$-th line contains one integer $$$q ~ (0 \le q \le 1000)$$$ denoting the number of additional DLCs.

Then $$$q$$$ lines follows, the $$$i$$$-th of which contains three integers $$$h_i, a_i, b_i$$$ denoting the arguments for the $$$i$$$-th DLC.

Output

Output $$$q+1$$$ lines in total.

The first line contains one integer denoting the minimum initial level that he needs to clear all stages.

Then $$$q$$$ lines follows, the $$$i$$$-th of which contains one integer for the minimum initial level that he needs to clear all stages after acquiring the $$$i$$$-th additional DLC.

Examples
Input
3
1 2 3
1 2
1 1
1 0
1
4 1 0
Output
2
3
Input
1
100
2 1
2
200 102 1
1 1 100
Output
100
201
102