Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //author @ Ayush Aggarwal
- // Let's Do this shit.
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- map <ll,ll> cnt;
- ll tot[1000010];
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(NULL);
- ll t,n;
- cin>>t;
- while(t--)
- {
- cin>>n;
- memset(tot,0,sizeof(tot));
- ll sum=0,final=0,ans=0;
- ll c[n],w[n];
- for(ll i=0;i<n;i++)
- cin>>c[i];
- for(ll i=0;i<n;i++)
- {
- cin>>w[i];
- sum +=w[i];
- tot[i] = sum;
- }
- ll l=0,r=0;;
- set <ll> s;
- s.clear();
- while(l<n)
- {
- r--;
- while(r<n)
- {
- r++;
- s.insert(c[r]);
- cnt[c[r]]++;
- if(s.size()==r+1-l)
- continue;
- else
- break;
- }
- //cout<<l<<" "<<(r-1)<<endl;
- if(l>0)
- ans = tot[r-1]-tot[l-1];
- else
- ans = tot[r-1];
- //cout<<ans<<endl;
- final = max(ans,final);
- if(cnt[c[l]]==1)
- s.erase(c[l]);
- cnt[c[l]]--;
- l++;
- }
- cout<<final<<endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment