Skip to content

Instantly share code, notes, and snippets.

@fardinabir
Created March 18, 2021 11:09
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 fardinabir/e108e61bcab2f48b53ef2e4f8b58bb22 to your computer and use it in GitHub Desktop.
Save fardinabir/e108e61bcab2f48b53ef2e4f8b58bb22 to your computer and use it in GitHub Desktop.
#include <bits/stdc++.h>
#define mxs 100005
#define ll long long int
using namespace std;
int arr[100005],n,m;
ll node[100005];
pair <int,int> pi[100005];
ll query(int ind)
{
ll mx=0;
for(int i=ind;i>0;i-=(i&-i))
mx=max(mx,node[i]);
return mx;
}
void update(int ind,ll cnt)
{
for(int i=ind;i<100005;i+=(i&-i))
node[i]=max(node[i],cnt);
}
int main()
{
ll t,k,a,b,i,q,j,l,s,c,mx=-1,mn=INT_MAX,ts=0;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>k;
pi[i]={k,-i};
}
sort(pi+1,pi+n+1);
for(i=1;i<=n;i++)
{
s=query(-pi[i].second-1);
s+=pi[i].first;
update(-pi[i].second,s);
}
cout<<query(mxs-1)<<endl;
return 0;
}
/*
6
33 77 45 51 32 79
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment