CF1788E.Sum Over Zero

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an array a1,a2,,ana_1, a_2, \ldots, a_n of nn integers. Consider SS as a set of segments satisfying the following conditions.

  • Each element of SS should be in form [x,y][x, y] , where xx and yy are integers between 11 and nn , inclusive, and xyx \leq y .
  • No two segments in SS intersect with each other. Two segments [a,b][a, b] and [c,d][c, d] intersect if and only if there exists an integer xx such that axba \leq x \leq b and cxdc \leq x \leq d .
  • For each [x,y][x, y] in SS , ax+ax+1++ay0a_x+a_{x+1}+ \ldots +a_y \geq 0 .

The length of the segment [x,y][x, y] is defined as yx+1y-x+1 . f(S)f(S) is defined as the sum of the lengths of every element in SS . In a formal way, f(S)=[x,y]S(yx+1)f(S) = \sum_{[x, y] \in S} (y - x + 1) . Note that if SS is empty, f(S)f(S) is 00 .

What is the maximum f(S)f(S) among all possible SS ?

输入格式

The first line contains one integer nn ( 1n21051 \leq n \leq 2 \cdot 10^5 ).

The next line is followed by nn integers a1,a2,,ana_1, a_2, \ldots, a_n ( 109ai109-10^9 \leq a_i \leq 10^9 ).

输出格式

Print a single integer, the maximum f(S)f(S) among every possible SS .

输入输出样例

  • 输入#1

    5
    3 -3 -2 5 -4

    输出#1

    4
  • 输入#2

    10
    5 -2 -4 -6 2 3 -6 5 3 -2

    输出#2

    9
  • 输入#3

    4
    -1 -2 -3 -4

    输出#3

    0

说明/提示

In the first example, S={[1,2],[4,5]}S=\{[1, 2], [4, 5]\} can be a possible SS because a1+a2=0a_1+a_2=0 and a4+a5=1a_4+a_5=1 . S={[1,4]}S=\{[1, 4]\} can also be a possible solution.

Since there does not exist any SS that satisfies f(S)>4f(S) > 4 , the answer is 44 .

In the second example, S={[1,9]}S=\{[1, 9]\} is the only set that satisfies f(S)=9f(S)=9 . Since every possible SS satisfies f(S)9f(S) \leq 9 , the answer is 99 .

In the third example, SS can only be an empty set, so the answer is 00 .

首页