CCO '23 P6 - Triangle Collection

View as PDF

Submit solution


Points: 25 (partial)
Time limit: 4.0s
Memory limit: 1G

Author:
Problem types
Canadian Computing Olympiad: 2023 Day 2, Problem 3

Alice has a collection of sticks. Initially, she has c sticks of length for each =1,,N.

Alice would like to use her sticks to make some isosceles triangles. An isosceles triangle is made of two sticks of the same length, say , and a third stick with a length between 1 and 21 inclusive. Note that the triangles must strictly obey the triangle inequality, and equilateral triangles are okay. Each stick may be used in at most one triangle. Alice would like to know the maximum number of isosceles triangles she can make with her sticks.

There are Q events that change the collection of sticks she has. The i-th event consists of two integers i and di, representing that the number of sticks of length i changes by di. Note that di may be positive, negative, or even 0, but Alice will never have a negative number or more than 109 sticks of each length.

Your task is to determine the maximum number of isosceles triangles Alice can make after each event if she uses her sticks optimally.

Input Specification

The first line of input contains two space-separated integers N and Q.

The second line of input contains N space-separated integers c1,c2,,cN (0ci109), representing Alice's initial collection.

The next Q lines of input each contain two space-separated integers i and di (1iN,109di109), representing an event.

Initially and after each event, the number of sticks of length is between 0 and 109 for all =1,,N.

Marks AwardedBounds on N,QAdditional Constraints
5 marks1N,Q2000There are at most 2000 sticks in
total initially and after each event
5 marks1N,Q2000No additional constraints.
5 marks1N,Q200000The number of sticks of each length is either
0, 1, or 2 initially and after each event.
5 marks1N,Q200000For each event, |di|=1
5 marks1N,Q200000No additional constraints.

Output Specification

Output Q lines each containing a single integer, the answer after each event.

Sample Input

Copy
4 3
3 1 4 1
3 -3
1 6
2 1

Output for Sample Input

Copy
1
3
4

Explanation of Output for Sample Input

After the first event, Alice can make a single triangle with sticks of lengths (1,1,1).

After the second event, Alice can make 3 triangles with sticks of lengths (1,1,1).

After the third event, Alice can make 3 triangles with sticks of lengths (1,1,1) and a triangle with sticks of lengths (2,2,3).


Comments

There are no comments at the moment.