#abc249f. F - Ignore Operations

F - Ignore Operations

Score : 500500 points

问题描述

Takahashi 持有一个整数 xx。初始时,x=0x = 0

NN 个操作。第 ii 个操作 (1iN)(1 \leq i \leq N) 由两个整数 tit_iyiy_i 表示,如下所示:

  • ti=1t_i = 1,则将 xx 替换为 yiy_i
  • ti=2t_i = 2,则将 xx 替换为 x+yix + y_i

Takahashi 可以跳过任意从 00KK(包括 00KK)的若干个操作。当他按照原顺序仅执行剩余的操作一次时,找出 xx 的最大可能最终值。

以上为通义千问 qwen-max 翻译,仅供参考。

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.

Sample Input 1

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

Sample Output 1

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.

Sample Input 2

1 0
2 -1000000000

Sample Output 2

-1000000000

Sample Input 3

10 3
2 3
2 -1
1 4
2 -1
2 5
2 -9
2 2
1 -6
2 5
2 -3

Sample Output 3

15

update @ 2024/3/10 10:39:20