d2p3 — CCO Canadian_Computing_Olympiad
Task type: batch | Time limit: 1 s | Memory limit: 1024 MB | Difficulty: 22
Algorithms: data structures, greedy, prefix sum, segment tree

# 2023 Canadian Computing Olympiad
Day 2, Problem 3
Triangle Collection

**Time Limit:** 4 seconds

**Problem Description**

Alice has a collection of sticks. Initially, she has $c_l$ sticks of length $l$ for each $l = 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 $l$, and a third stick with a length between 1 and $2l - 1$ 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 $l_i$ and $d_i$, representing that the number of sticks of length $l_i$ changes by $d_i$. Note that $d_i$ may be positive, negative, or even 0, but Alice will never have a negative number or more than $10^9$ 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 $c_1, c_2, ..., c_N$ ($0 \le c_i \le 10^9$), representing Alice's initial collection.

The next $Q$ lines of input each contain two space-separated integers $l_i$ and $d_i$ ($1 \le l_i \le N$, $-10^9 \le d_i \le 10^9$), representing an event.

Initially and after each event, the number of sticks of length $l$ is between 0 and $10^9$ for all $l = 1, ..., N$.

| Marks Awarded | Bounds on $N, Q$ | Additional Constraints                                                                 |
| :------------ | :--------------- | :--------------------------------------------------------------------------------------- |
| 5 marks       | $1 \le N, Q \le 2000$    | There are at most 2000 sticks in total initially and after each event.                |
| 5 marks       | $1 \le N, Q \le 2000$    | No additional constraints.                                                            |
| 5 marks       | $1 \le N, Q \le 200000$  | The number of sticks of each length is either 0, 1, or 2 initially and after each event. |
| 5 marks       | $1 \le N, Q \le 200000$  | For each event, $|d_i| = 1$.                                                         |
| 5 marks       | $1 \le N, Q \le 200000$  | No additional constraints.                                                            |

**Output Specification**

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

**Sample Input**

```
4 3
3 1 4 1
3 -3
1 6
2 1
```

**Sample Output**
```
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).

Starter code: