CCO '18 P5 - Boring Lectures

View as PDF

Submit solution

Points: 30 (partial)
Time limit: 8.0s
Memory limit: 512M

Author:
Problem type
Canadian Computing Olympiad: 2018 Day 2, Problem 2

You have a schedule of N upcoming lectures that you have the option of attending. The lectures are numbered from 1 to N and are in chronological order. From the current schedule, you expect that the i^{th} lecture will have quality a_i. Since most of the lectures will be boring, you are only willing to attend some group of K consecutive lectures. You will skip the remaining lectures so that you can catch up on sleep and participate in programming contests. Since you don't like taking notes, you will only be able to remember the content from 2 of the lectures you attend. You want to choose the lectures you attend and the 2 lectures you remember as to maximize the sum of the lecture qualities of those 2 lectures.

There are Q changes that will be made to the schedule. The j^{th} change to the schedule is represented by two values i_j, x_j that indicate that the quality of the i_j^{th} lecture changes to x_j. For each of the Q+1 versions of the schedule, find the maximum possible sum of lecture qualities that you can attain.

Input Specification

The first line will contain three integers N, K, and Q\,(2 \le N \le 10^6, 2 \le K \le N, 0 \le Q \le 10^5). The second line contains N integers a_1, \ldots, a_N\,(0 \le a_i \le 10^9). The next Q lines each contain two integers i_j and x_j\,(1 \le i_j \le N, 0 \le x_j \le 10^9).

For 5 of the 25 available marks, Q = 0.

For an additional 10 of the 25 available marks, N \le 10^4.

Output Specification

Output Q + 1 lines, each containing a single integer. The j^{th} line that follows should contain the answer for the schedule obtained after the first j - 1 changes are made.

Sample Input

4 3 1
6 1 2 4
1 3

Output for Sample Input

8
6

Explanation for Output for Sample Input

For the original schedule, it is best to attend the first three lectures and remember the first and third, for an overall value of 6 + 2 = 8. After the update, it is best to attend the last three lectures and remember the last two, giving a value of 2 + 4 = 6.


Comments


  • 1
    Y  commented on June 5, 2018, 1:38 p.m.

    Is this problem solvable in java? 【just out of curiosity


    • 1
      wleung_bvg  commented on June 5, 2018, 5:45 p.m. edit 2

      Yes (surprisingly almost as fast as equivalent C++/C code in the worst case)


      • 1
        Y  commented on June 6, 2018, 11:55 a.m.

        Thank you ^^