CF1788E.Sum Over Zero

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

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 x≤yx \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 a≤x≤ba \leq x \leq b and c≤x≤dc \leq x \leq d.
  • For each [x,y][x, y] in SS, ax+ax+1+…+ay≥0a_x+a_{x+1}+ \ldots +a_y \geq 0.

The length of the segment [x,y][x, y] is defined as y−x+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(y−x+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?

给你一个包含 nn 个整数的数组 a1,a2,…,ana_1, a_2, \ldots, a_n。考虑集合 SS,它由满足以下条件的若干区间构成:

  • SS 中的每个元素均为形如 [x,y][x, y] 的区间,其中 xx 和 yy 是介于 11 到 nn(含端点)之间的整数,且满足 x≤yx \leq y;
  • SS 中任意两个区间互不相交。两个区间 [a,b][a, b] 与 [c,d][c, d] 相交,当且仅当存在某个整数 xx,使得 a≤x≤ba \leq x \leq b 且 c≤x≤dc \leq x \leq d;
  • 对于 SS 中的每个区间 [x,y][x, y],均有 ax+ax+1+…+ay≥0a_x + a_{x+1} + \ldots + a_y \geq 0。

区间 [x,y][x, y] 的长度定义为 y−x+1y - x + 1。定义 f(S)f(S) 为 SS 中所有区间的长度之和,即形式化地有:

f(S)=∑[x,y]∈S(y−x+1).f(S) = \sum_{[x, y] \in S} (y - x + 1).

注意:若 SS 为空集,则 f(S)=0f(S) = 0。

在所有可能的 SS 中,f(S)f(S) 的最大值是多少?

输入格式

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

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

第一行包含一个整数 nn(1≤n≤2⋅1051 \leq n \leq 2 \cdot 10^5)。

下一行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(−109≤ai≤109-10^9 \leq a_i \leq 10^9)。

输出格式

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

输出一个整数,即所有可能的 SS 中 f(S)f(S) 的最大值。

输入输出样例

  • 输入#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) \gt 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.

在第一个例子中,S=[1,2],[4,5]S={[1, 2], [4, 5]} 是一个可能的 SS,因为 a1+a2=0a_1+a_2=0 且 a4+a5=1a_4+a_5=1;S=[1,4]S={[1, 4]} 同样是一个可能的解。

由于不存在满足 f(S)>4f(S) \gt 4 的 SS,因此答案为 44。

在第二个例子中,唯一满足 f(S)=9f(S)=9 的集合是 S=[1,9]S={[1, 9]}。由于所有可能的 SS 均满足 f(S)≤9f(S) \leq 9,因此答案为 99。

在第三个例子中,SS 只能是空集,因此答案为 00。

输入解题思路,AI测评打分。不知道怎么写?

首页