#abc341b. B - Foreign Exchange

B - Foreign Exchange

Score: 150150 points

问题描述

NN 个国家,编号从 11NN。对于每个 i=1,2,,Ni = 1, 2, \ldots, N,Takahashi 持有 AiA_i 单位的第 ii 国货币。

Takahashi 可以重复执行以下操作任意次数,包括零次:

  • 首先,在 11N1N-1(包含)之间选择一个整数 ii
  • 然后,如果 Takahashi 持有至少 SiS_i 单位的第 ii 国货币,他可以执行以下动作一次:
    • 支付 SiS_i 单位的第 ii 国货币,并获得 TiT_i 单位的第 (i+1)(i+1) 国货币。

输出 Takahashi 最终可能拥有的第 NN 国货币的最大数量单位。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There are NN countries numbered 11 to NN. For each i=1,2,,Ni = 1, 2, \ldots, N, Takahashi has AiA_i units of the currency of country ii.

Takahashi can repeat the following operation any number of times, possibly zero:

  • First, choose an integer ii between 11 and N1N-1, inclusive.
  • Then, if Takahashi has at least SiS_i units of the currency of country ii, he performs the following action once:
    • Pay SiS_i units of the currency of country ii and gain TiT_i units of the currency of country (i+1)(i+1).

Print the maximum possible number of units of the currency of country NN that Takahashi could have in the end.

Constraints

  • All input values are integers.
  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 0Ai1090 \leq A_i \leq 10^9
  • 1TiSi1091 \leq T_i \leq S_i \leq 10^9

Input

The input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \ldots ANA_N

S1S_1 T1T_1

S2S_2 T2T_2

\vdots

SN1S_{N-1} TN1T_{N-1}

Output

Print the answer.

Sample Input 1

4
5 7 0 3
2 2
4 3
5 2

Sample Output 1

5

In the following explanation, let the sequence A=(A1,A2,A3,A4)A = (A_1, A_2, A_3, A_4) represent the numbers of units of the currencies of the countries Takahashi has. Initially, A=(5,7,0,3)A = (5, 7, 0, 3).

Consider performing the operation four times as follows:

  • Choose i=2i = 2, pay four units of the currency of country 22, and gain three units of the currency of country 33. Now, A=(5,3,3,3)A = (5, 3, 3, 3).
  • Choose i=1i = 1, pay two units of the currency of country 11, and gain two units of the currency of country 22. Now, A=(3,5,3,3)A = (3, 5, 3, 3).
  • Choose i=2i = 2, pay four units of the currency of country 22, and gain three units of the currency of country 33. Now, A=(3,1,6,3)A = (3, 1, 6, 3).
  • Choose i=3i = 3, pay five units of the currency of country 33, and gain two units of the currency of country 44. Now, A=(3,1,1,5)A = (3, 1, 1, 5).

At this point, Takahashi has five units of the currency of country 44, which is the maximum possible number.

Sample Input 2

10
32 6 46 9 37 8 33 14 31 5
5 5
3 1
4 3
2 2
3 2
3 2
4 4
3 3
3 1

Sample Output 2

45

update @ 2024/3/10 01:34:05