103063: [Atcoder]ABC306 D - Poisonous Full-Course

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:9 Solved:0

Description

Score : $400$ points

Problem Statement

Takahashi has decided to enjoy a wired full-course meal consisting of $N$ courses in a restaurant.
The $i$-th course is:

  • if $X_i=0$, an antidotal course with a tastiness of $Y_i$;
  • if $X_i=1$, a poisonous course with a tastiness of $Y_i$.

When Takahashi eats a course, his state changes as follows:

  • Initially, Takahashi has a healthy stomach.
  • When he has a healthy stomach,
    • if he eats an antidotal course, his stomach remains healthy;
    • if he eats a poisonous course, he gets an upset stomach.
  • When he has an upset stomach,
    • if he eats an antidotal course, his stomach becomes healthy;
    • if he eats a poisonous course, he dies.

The meal progresses as follows.

  • Repeat the following process for $i = 1, \ldots, N$ in this order.
    • First, the $i$-th course is served to Takahashi.
    • Next, he chooses whether to "eat" or "skip" the course.
      • If he chooses to "eat" it, he eats the $i$-th course. His state also changes depending on the course he eats.
      • If he chooses to "skip" it, he does not eat the $i$-th course. This course cannot be served later or kept somehow.
    • Finally, (if his state changes, after the change) if he is not dead,
      • if $i \neq N$, he proceeds to the next course.
      • if $i = N$, he makes it out of the restaurant alive.

An important meeting awaits him, so he must make it out of there alive.
Find the maximum possible sum of tastiness of the courses that he eats (or $0$ if he eats nothing) when he decides whether to "eat" or "skip" the courses under that condition.

Constraints

  • All input values are integers.
  • $1 \le N \le 3 \times 10^5$
  • $X_i \in \{0,1\}$
    • In other words, $X_i$ is either $0$ or $1$.
  • $-10^9 \le Y_i \le 10^9$

Input

The input is given from Standard Input in the following format:

$N$
$X_1$ $Y_1$
$X_2$ $Y_2$
$\vdots$
$X_N$ $Y_N$

Output

Print the answer as an integer.


Sample Input 1

5
1 100
1 300
0 -200
1 500
1 300

Sample Output 1

600

The following choices result in a total tastiness of the courses that he eats amounting to $600$, which is the maximum possible.

  • He skips the $1$-st course. He now has a healthy stomach.
  • He eats the $2$-nd course. He now has an upset stomach, and the total tastiness of the courses that he eats amounts to $300$.
  • He eats the $3$-rd course. He now has a healthy stomach again, and the total tastiness of the courses that he eats amounts to $100$.
  • He eats the $4$-th course. He now has an upset stomach, and the total tastiness of the courses that he eats amounts to $600$.
  • He skips the $5$-th course. He now has an upset stomach.
  • In the end, he is not dead, so he makes it out of the restaurant alive.

Sample Input 2

4
0 -1
1 -2
0 -3
1 -4

Sample Output 2

0

For this input, it is optimal to eat nothing, in which case the answer is $0$.


Sample Input 3

15
1 900000000
0 600000000
1 -300000000
0 -700000000
1 200000000
1 300000000
0 -600000000
1 -900000000
1 600000000
1 -100000000
1 -400000000
0 900000000
0 200000000
1 -500000000
1 900000000

Sample Output 3

4100000000

The answer may not fit into a $32$-bit integer type.

Input

题意翻译

## 题目描述 高桥君决定去一家诡异的饭店,享用包含 $ N $ 道菜的套餐。每道菜有两个属性 $ X, Y $,含义如下: - $ X_i = 0 $ 表示第 $ i $ 道菜**有解毒功效**; - $ X_i = 1 $ 表示第 $ i $ 道菜**有毒**; - $ Y_i $ 表示第 $ i $ 道菜的**美味程度**。 高桥君每品尝一道菜,他的身体状况就会发生如下的变化: - 起初,高桥君感到**舒适**。 - 当他感到**舒适**之时, - 如果他吃了一道**有解毒功效**的菜,他依然会感到**舒适**; - 如果他吃了一道**有毒**的菜,他就会感到**不适**。 - 当他感到**不适**之时, - 如果他吃了一道**有解毒功效**的菜,他就然会感到**舒适**; - 如果他吃了一道**有毒**的菜,他就会**当场去世**。 菜是一道一道上的,对于第 $ i $ 道菜,用餐流程如下: - 首先,上菜。 - 然后,高桥君可以选择**品尝**或选择**跳过**这道菜。 - 如果他选择**品尝**这道菜,他的身体状况就会随着前文提及的规则改变。 - 如果他选择**跳过**这道菜,这道菜不会留在餐桌上,之后也不会再上这道菜(即再也吃不到这道菜)。 - 在做出上述选择后,如果高桥君还活着, - 如果 $ i \neq N $,上第 $ i + 1 $ 道菜,继续上述流程。 - 如果 $ i = N $,高桥君就可以活着走出饭店。 高桥君还有一个重要的会议要开,所以他必须活着走出饭店。 现在,请你求出高桥君所品尝的菜肴的美味程度之和的最大值(如果他什么也不吃,我们认为答案为 $ 0 $)。 ## 说明/提示 - 保证输入的数均为整数。 - $ 1 \le N \le 3 \times 10^5 $ - $ X_i \in \{0, 1\} $ - 也就是说,$ X_i $ 要么为 $ 0 $,要么为 $ 1 $。 - $ -10^9 \le Y_i \le 10^9 $ ## 样例一解释 对于本组数据,如下的一系列选择可以使得高桥君所品尝的菜肴的美味程度之和达到最大值,即 $ 600 $。 - 他选择跳过第 $ 1 $ 道菜,此时他感到舒适。 - 他选择品尝第 $ 2 $ 道菜,此时他感到不适,他所品尝的菜肴的美味程度之和目前为 $ 300 $。 - 他选择品尝第 $ 3 $ 道菜,此时他又感到舒适,他所品尝的菜肴的美味程度之和目前为 $ 100 $。 - 他选择品尝第 $ 4 $ 道菜,此时他又感到不适,他所品尝的菜肴的美味程度之和目前为 $ 600 $。 - 他选择跳过第 $ 5 $ 道菜,此时他仍感到不适。 - 虽然他最后感到不适,但他还是活着走出了饭店。 ## 样例二解释 对于本组数据,高桥君可以选择什么也不吃,故答案为 $ 0 $。 ## 样例三解释 答案可能超出 $ 32 $ 位整数的范围。

Output

分数:$400$分

问题描述

高桥同学决定在一家餐厅享用由$N$道菜组成的有线全餐。
第$i$道菜是:

  • 如果$X_i=0$,一道口感为$Y_i$的“解毒菜”;
  • 如果$X_i=1$,一道口感为$Y_i$的“毒菜”。

当高桥同学吃一道菜时,他的状态会发生如下变化:

  • 起初,高桥同学有一个健康的胃。
  • 当他有一个健康的胃时,
    • 如果他吃一道“解毒菜”,他的胃保持健康;
    • 如果他吃一道“毒菜”,他会胃不舒服。
  • 当他胃不舒服时,
    • 如果他吃一道“解毒菜”,他的胃变得健康;
    • 如果他吃一道“毒菜”,他会死掉。

接下来是用餐过程。

  • 按照顺序重复以下过程,$i = 1, \ldots, N$。
    • 首先,将第$i$道菜呈给高桥同学。
    • 接下来,他选择是否要“吃”或“跳过”这道菜。
      • 如果他选择“吃”它,他会吃第$i$道菜。根据他吃的菜,他的状态也会改变。
      • 如果他选择“跳过”它,他不会吃第$i$道菜。这道菜不能稍后供应或以某种方式保留。
    • 最后,(如果他的状态发生变化,在变化后)如果他没有死,
      • 如果$i \neq N$,他将进入下一道菜。
      • 如果$i = N$,他活着离开餐厅。

他有一个重要的会议在等他,所以他必须活着离开那里。
当他决定在这种条件下是否要“吃”或“跳过”这些菜时,找出他吃的所有菜的口感之和的最大可能值(如果他什么都没吃,则为$0$)。

约束条件

  • 所有输入值都是整数。
  • $1 \le N \le 3 \times 10^5$
  • $X_i \in \{0,1\}$
    • 换句话说,$X_i$是$0$或$1$。
  • $-10^9 \le Y_i \le 10^9$

输入

输入从标准输入以以下格式给出:

$N$
$X_1$ $Y_1$
$X_2$ $Y_2$
$\vdots$
$X_N$ $Y_N$

输出

将答案作为整数打印。


样例输入 1

5
1 100
1 300
0 -200
1 500
1 300

样例输出 1

600

以下选择使得他吃的所有菜的口感之和为$600$,这是最大的可能值。

  • 他跳过了第$1$道菜。现在他有一个健康的胃。
  • 他吃了第$2$道菜。现在他胃不舒服,他吃的所有菜的口感之和为$300$。
  • 他吃了第$3$道菜。现在他又有一个健康的胃,他吃的所有菜的口感之和为$100$。
  • 他吃了第$4$道菜。现在他又胃不舒服,他吃的所有菜的口感之和为$600$。
  • 他跳过了第$5$道菜。现在他胃不舒服。
  • 最后,他没有死,所以他活着离开了餐厅。

样例输入 2

4
0 -1
1 -2
0 -3
1 -4

样例输出 2

0

对于这个输入,最好的选择是不吃任何东西,此时答案为$0$。


样例输入 3

15
1 900000000
0 600000000
1 -300000000
0 -700000000
1 200000000
1 300000000
0 -600000000
1 -900000000
1 600000000
1 -100000000
1 -400000000
0 900000000
0 200000000
1 -500000000
1 900000000

样例输出 3

4100000000

答案可能不适用于$32$位整数类型。

HINT

n个菜,吃完状态为0和1,0再吃0是不允许的;每道菜也可以选择不吃,求n道菜的最大价值?

加入题单

算法标签: