0% found this document useful (0 votes)
24 views1 page

Sodapdf Converted 1 Mu7ktt

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views1 page

Sodapdf Converted 1 Mu7ktt

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

class Solution {

public:
#define ll long long int
long long maximumTotalDamage(vector<int>& a) {
ll ans=0;

sort(a.begin(),a.end());
vector<pair<ll,ll>> v;
map<ll,ll> m;
for(auto it:a){
m[it]++;
}
for(auto it:m){
v.push_back({it.first,it.second});
}
vector<ll> dp(v.size()+3,0);

dp[0]=v[0].first*v[0].second;
int n=v.size();

for(int i=1;i<n;i++){
ll take, nottake;
nottake=dp[i-1];
take=v[i].first*v[i].second;
// le rahe hai to iske pehle ke nahi le sakte
for(int j=i-1;j>=0;j--){
if(abs(v[i].first-v[j].first)>2){
take+=dp[j];
break;
}
}
dp[i]=max(take,nottake);
}
ans=dp[n-1];
return ans;

}
};

You might also like