Pagini recente » Cod sursa (job #2080084) | Cod sursa (job #2497488) | Cod sursa (job #162920) | Profil Matwuei | Cod sursa (job #1937192)
#include <cstdio>
#include <vector>
#include <algorithm>
#define pb push_back
using namespace std;
const int DIM=100000,NMAX=200003;
char buff[DIM];
int poz=DIM-1;
void citeste(int &a)
{
a=0;
while(!('0'<=buff[poz]&&buff[poz]<='9'))
{
if(++poz==DIM) fread(buff,1,DIM,stdin),poz=0;
}
while('0'<=buff[poz]&&buff[poz]<='9')
{
a=a*10 + (buff[poz]-'0');
if(++poz==DIM) fread(buff,1,DIM,stdin),poz=0;
}
}
int v[NMAX],st[NMAX];
long long dyn[NMAX],aint[NMAX];
int prt[NMAX],x[NMAX];
int n,m;
struct str
{
int first;
int second;
bool operator <(const str &aux) const
{
if(first!=aux.first) return (first < aux.first);
return second>aux.second;
}
}evr[NMAX];
int comp(const int &a,const int &b)
{
if(v[a]==v[b]) return st[a]>st[b];
return v[a]<v[b];
}
void update(int p, long long value) {
for (aint[p += n] = value; p > 1; p >>= 1)
aint[p>>1] = max(aint[p], aint[p ^ 1]);
}
long long query(int l, int r) {
long long res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
res = max(res, aint[l ++]);
if (r & 1)
res = max(res, aint[-- r]);
}
return res;
}
inline void citire()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
st[i]=NMAX;
scanf("%d",&v[i]);
x[i]=i;
}
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&evr[i].first,&evr[i].second);
}
}
inline void build_st()
{
sort(evr+1,evr+m+1);
for(int i=1;i<=m;i++)
{
int sc=evr[i].second,fr=evr[i].first;
for(int j=sc;j>=fr;--j)
{
if(st[j]<fr-1) break;
st[j]=fr-1;
}
}
}
inline void start_dyn()
{
long long val=0;
long long maxim=0;
sort(x+1,x+n+1,comp);
for(int i=1;i<=n;i++)
{
val=query(0,st[x[i]]);
dyn[x[i]]=val+v[x[i]];
update(x[i]-1,dyn[x[i]]);
if(maxim<dyn[x[i]]) maxim=dyn[x[i]];
}
if(maxim==0) while(1);
printf("%lld\n",maxim);
}
int main()
{
freopen("hapsan.in","r",stdin);
freopen ("hapsan.out","w",stdout);
citire();
build_st();
start_dyn();
}