Advertisement
Guest User

Untitled

a guest
May 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define f(a,b,c) for(int a=b;a<c;a++)
  4. typedef pair<int,int> pii;
  5. int a[500000],n,t,sum,msum,i,j;
  6. priority_queue<pair<int,int>, vector<pair<int,int> >,greater<pair<int,int> > > pq;
  7.  
  8. int main(){
  9. cin>>t;
  10. while(t--){
  11. while(!pq.empty())pq.pop();
  12. scanf("%d",&n);
  13. f(k,0,n)scanf("%d",&a[k]);
  14. msum=0;
  15. sum=0;i=0;j=0;
  16. while(i<n&&j<n){
  17. //cout<<msum<<endl;
  18. msum=max(sum,msum);
  19. if(sum<0){
  20. if(i!=pq.top().second)sum-=a[i];
  21. else{pq.pop();
  22. while(!pq.empty()&&pq.top().second<i)pq.pop();
  23. sum-=pq.top().first;
  24. }
  25. i++;
  26. }else{
  27. if(a[j]>=0)sum+=a[j];
  28. else{
  29. pq.push(make_pair(a[j],j));
  30. if(pq.top().second!=j){
  31. sum+=a[j];
  32. }
  33. }
  34. j++;
  35. }
  36. msum=max(sum,msum);
  37. }
  38. while(i<n){msum=max(sum,msum);
  39. if(i!=pq.top().second)sum-=a[i];
  40. else{pq.pop();
  41. while(!pq.empty()&&pq.top().second<i)pq.pop();
  42. sum-=pq.top().first;
  43. }
  44. i++;
  45. }cout<<msum;
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement