atcoder#ABC249F. [ABC249F] Ignore Operations

[ABC249F] Ignore Operations

Score : 500500 points

Problem Statement

Takahashi has an integer xx. Initially, x=0x = 0.

There are NN operations. The ii-th operation (1iN)(1 \leq i \leq N) is represented by two integers tit_i and yiy_i as follows:

  • If ti=1t_i = 1, replace xx with yiy_i.
  • If ti=2t_i = 2, replace xx with x+yix + y_i.

Takahashi may skip any number between 00 and KK (inclusive) of the operations. When he performs the remaining operations once each without changing the order, find the maximum possible final value of xx.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 0KN0 \leq K \leq N
  • ti{1,2}(1iN)t_i \in \{1,2\} \, (1 \leq i \leq N)
  • yi109(1iN)|y_i| \leq 10^9 \, (1 \leq i \leq N)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN KK

t1t_1 y1y_1

\vdots

tNt_N yNy_N

Output

Print the answer.

5 1
2 4
2 -3
1 2
2 1
2 -3
3

If he skips the 55-th operation, xx changes as $0 \rightarrow 4 \rightarrow 1 \rightarrow 2 \rightarrow 3$, so xx results in 33. This is the maximum.

1 0
2 -1000000000
-1000000000
10 3
2 3
2 -1
1 4
2 -1
2 5
2 -9
2 2
1 -6
2 5
2 -3
15