D. Yet Another Yet Another Task

D. Yet Another Yet Another Tasktime limit per test1.5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

Alice and Bob are playing yet another card game. This time the rules are the following. There are nn cards lying in a row in front of them. The ii-th card has value aiai.First, Alice chooses a non-empty consecutive segment of cards [l;r][l;r] (l≤rl≤r). After that Bob removes a single card jj from that segment (l≤j≤r)(l≤j≤r). The score of the game is the total value of the remaining cards on the segment(al+al+1+⋯+aj−1+aj+1+⋯+ar−1+ar)(al+al+1+⋯+aj−1+aj+1+⋯+ar−1+ar). In particular, if Alice chooses a segment with just one element, then the score after Bob removes the only card is 00.Alice wants to make the score as big as possible. Bob takes such a card that the score is as small as possible.What segment should Alice choose so that the score is maximum possible? Output the maximum score.InputThe first line contains a single integer nn (1≤n≤1051≤n≤105) — the number of cards.The second line contains nn integers a1,a2,…,ana1,a2,…,an (−30≤ai≤30−30≤ai≤30) — the values on the cards.OutputPrint a single integer — the final score of the game.
Examples
input
5
5 -2 10 -1 4
output
6
input
8
5 2 5 3 -30 -30 6 9
output
10
input
3
-10 6 -15
output
0
NoteIn the first example Alice chooses a segment [1;5][1;5] — the entire row of cards. Bob removes card 33 with the value 1010 from the segment. Thus, the final score is 5+(−2)+(−1)+4=65+(−2)+(−1)+4=6.In the second example Alice chooses a segment [1;4][1;4], so that Bob removes either card 11 or 33 with the value 55, making the answer 5+2+3=105+2+3=10.In the third example Alice can choose any of the segments of length 11: [1;1][1;1], [2;2][2;2] or [3;3][3;3]. Bob removes the only card, so the score is 00. If Alice chooses some other segment then the answer will be less than 00.

#include <bits/stdc++.h>
using namespace std;
long long ll,tt,i,j,b[1212145],c[1111456],d;
long long x,m,n,r,s,t,k,l,y,z;
string p,q ;
long long a[1234567];
vector<int> h,hh;
int main()
{
    cin >> n;
    for (i = 0; i <n; i ++)
        cin >> a[i];
    s = 0;
    for (z=1;z<=30; z++){
        x=-100;
        m=0;
        for (i = 0; i <n; i ++){
            if (a[i]>m+a[i]){
                m=a[i];
                x=a[i];
            }
            else {
                   m += a[i];
                   x=max(x,a[i]);
                }
            if (x > z){
                m=0;
                x=0;
            }
                s=max(s,m-x);
            }
            
        }
        cout <<s<<endl;
        return 0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值