CF1788E.Sum Over Zero
提高+/省选-
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an array a1,a2,…,an of n integers. Consider S as a set of segments satisfying the following conditions.
- Each element of S should be in form [x,y], where x and y are integers between 1 and n, inclusive, and x≤y.
- No two segments in S intersect with each other. Two segments [a,b] and [c,d] intersect if and only if there exists an integer x such that a≤x≤b and c≤x≤d.
- For each [x,y] in S, ax+ax+1+…+ay≥0.
The length of the segment [x,y] is defined as y−x+1. f(S) is defined as the sum of the lengths of every element in S. In a formal way, f(S)=∑[x,y]∈S(y−x+1). Note that if S is empty, f(S) is 0.
What is the maximum f(S) among all possible S?
给你一个包含 n 个整数的数组 a1,a2,…,an。考虑集合 S,它由满足以下条件的若干区间构成:
- S 中的每个元素均为形如 [x,y] 的区间,其中 x 和 y 是介于 1 到 n(含端点)之间的整数,且满足 x≤y;
- S 中任意两个区间互不相交。两个区间 [a,b] 与 [c,d] 相交,当且仅当存在某个整数 x,使得 a≤x≤b 且 c≤x≤d;
- 对于 S 中的每个区间 [x,y],均有 ax+ax+1+…+ay≥0。
区间 [x,y] 的长度定义为 y−x+1。定义 f(S) 为 S 中所有区间的长度之和,即形式化地有:
f(S)=[x,y]∈S∑(y−x+1).
注意:若 S 为空集,则 f(S)=0。
在所有可能的 S 中,f(S) 的最大值是多少?
输入格式
The first line contains one integer n (1≤n≤2⋅105).
The next line is followed by n integers a1,a2,…,an (−109≤ai≤109).
第一行包含一个整数 n(1≤n≤2⋅105)。
下一行包含 n 个整数 a1,a2,…,an(−109≤ai≤109)。
输出格式
Print a single integer, the maximum f(S) among every possible S.
输出一个整数,即所有可能的 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] can be a possible S because a1+a2=0 and a4+a5=1. S=[1,4] can also be a possible solution.
Since there does not exist any S that satisfies f(S)>4, the answer is 4.
In the second example, S=[1,9] is the only set that satisfies f(S)=9. Since every possible S satisfies f(S)≤9, the answer is 9.
In the third example, S can only be an empty set, so the answer is 0.
在第一个例子中,S=[1,2],[4,5] 是一个可能的 S,因为 a1+a2=0 且 a4+a5=1;S=[1,4] 同样是一个可能的解。
由于不存在满足 f(S)>4 的 S,因此答案为 4。
在第二个例子中,唯一满足 f(S)=9 的集合是 S=[1,9]。由于所有可能的 S 均满足 f(S)≤9,因此答案为 9。
在第三个例子中,S 只能是空集,因此答案为 0。
输入解题思路,AI测评打分。不知道怎么写?