Posters
Time limit2sMemory limit512 MB
On a cycle of n posters, choose a subset with no run of four consecutive chosen positions, maximizing total colorfulness, and answer after each of q point updates.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Segment tree, Matrix, Array
- Solved
- No attempts yet
Problem
A group of friends is getting ready to welcome the national team returning from the International Olympiad in Informatics. They have prepared many colorful posters. Only the details of the celebration remain to be worked out.
To greet the team, friends stand in a circle. Number them from 1 to in the order they are arranged around the circle. Then for every with , friends and stand next to each other, and friends and 1 also stand next to each other. Each friend has a poster. Each poster is described by its colorfulness, a non-negative integer. The poster of friend has colorfulness .
When the celebration begins, some of the friends raise their posters and show them to the team. So that the team members do not get confused and can see all the posters, there must not be four or more consecutive friends holding up a poster.
The friends plan to change posters during the meeting. In total changes will be made. After the -th change, the poster of friend has colorfulness . After each change, the friends want to determine the maximum total colorfulness of the raised posters they can achieve without violating the restriction.
You must write a program that, given the initial colorfulness of the posters and the sequence of changes, determines at the start and after each change the maximum total colorfulness of the raised posters that can be achieved without violating the condition that no more than three posters in a row are raised.
Input
The first line of input contains the integer (), the number of friends.
The second line contains integers (), the initial colorfulness values of the friends' posters.
The third line contains a single integer (), the number of poster changes the friends made.
Each of the following lines contains two integers and (; ), the number of the friend whose poster changed and the new colorfulness of that poster.
Output
Output numbers. Before the first change and after each poster change, output a single integer: the maximum total colorfulness of the raised posters under the condition that no more than three posters in a row may be raised.
Notes
Consider the test from the sample.
Before the first change, friends 2, 4, 5, 6 should raise their posters. The total colorfulness of the raised posters is 17.
After the first change, the poster of friend 6 has colorfulness 0. Now friends 1, 3, 4, 5 should raise their posters. The total colorfulness is 13.
After the second change, the poster of friend 2 has colorfulness 5. Friends 1, 2, 4, 5 should raise their posters. The total colorfulness is 15.