Skip to content

Instantly share code, notes, and snippets.

@mishal23
Last active May 21, 2018 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mishal23/79930b304c8af1abc14858c0481eba60 to your computer and use it in GitHub Desktop.
Save mishal23/79930b304c8af1abc14858c0481eba60 to your computer and use it in GitHub Desktop.
/*
Problem:- Save Gotham!
Link:- https://practice.geeksforgeeks.org/problems/save-gotham/0
Author:- Mishal Shah
*/
#include "bits/stdc++.h"
#define MOD 1000000001
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int i,j,n;
long long sum=0;
cin>>n;
int a[n];
stack<int> b;
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=0;i<n;i++)
{
if(i==0)
{
b.push(a[i]);
}
else
{
if(a[i]>b.top())
{
while(!b.empty() && a[i]>b.top())
{
//cout<<sum<<" ";
sum = (sum%MOD + a[i]%MOD)%MOD;
b.pop();
}
}
b.push(a[i]);
}
}
cout<<sum<<endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment