#P9790. [ROIR 2020] 海报 (Day 2)

[ROIR 2020] 海报 (Day 2)

Problem Description

Translated from ROIR 2020 Day2 T4. Плакаты, translator: ksyx.

Your friends have prepared many nice posters to welcome the national team members returning from IOI, and now they only need to work out some details.

To welcome these contestants, your nn friends will stand in a circle holding posters. For convenience, they are numbered as friends 1n1\ldots n. For i[1,n1]i\in [1,n-1], friend ii stands next to friend i+1i+1, and friend nn stands next to friend 11.

Each poster has a beauty value. The beauty value of the poster held by friend ii is aia_i. When the celebration starts, some friends will raise their posters. For appearance, there must not be 44 or more consecutive friends raising their posters at the same time.

To make the celebration more interesting, your friends also plan to change posters qq times during the celebration. After each change, the beauty value of poster pip_i will become viv_i. Your friends want to know, after each change, the maximum possible sum of beauty values under the rule above.

Your task is: given the initial beauty values, output the maximum sum for the initial state and after each change.

Translator’s note: The statement omits some unnecessary details that are hard to understand.

Input Format

The first line contains an integer nn, the number of friends.

The next line contains nn integers aia_i, the initial beauty values.

The third line contains an integer qq, the number of poster changes.

The next qq lines each contain two integers pi, vip_i,~v_i, describing one change.

Output Format

Output q+1q+1 lines, representing the maximum sum of beauty values for the initial state and after each change.

6
1 2 3 4 5 6
2
6 0
2 5
17
13
15

Hint

Sample 1 Explanation

In the initial state, the best plan is to let friends 2, 4, 5, 62,~4,~5,~6 raise their posters, and the sum of beauty values is 1717.

After the first change, the beauty value of friend 66’s poster becomes 00. In this case, the best plan is to let friends 1, 3, 4, 51,~3,~4,~5 raise their posters, and the sum is 1313.

After the second change, the beauty value of friend 22’s poster becomes 55. In this case, the best plan is to let friends 1, 2, 4, 51,~2,~4,~5 raise their posters, and the sum is 1515.

Constraints

For 100%100\% of the testdata, $4\le n\le 40000,~0\le a_i,~v_i\le 10^9,~1\le p_i\le n,~0\le q\le 40000$.

The subtasks are as follows:

Subtask ID Points Limits
11 1111 4n10, q=04 \le n \le 10,~q=0
22 1212 4n10, 0q104 \le n \le 10,~0\le q\le 10
33 1313 4n1000, 0q10004 \le n \le 1000,~0\le q\le 1000
44 1717 4n40000, q=04 \le n \le 40000,~q=0
55 4747 4n40000,0q400004 \le n \le 40000, 0\le q\le 40000

Translated by ChatGPT 5