forked from fineanmol/Hacktoberfest2024
-
Notifications
You must be signed in to change notification settings - Fork 30
/
bny2.cpp
46 lines (44 loc) · 895 Bytes
/
bny2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll asum(vector<ll> vec){
int n = vec.size();
map<ll, ll> m;
for(ll i=0; i<n; i++){
m[vec[i]]+=1;
}
vector<pair<ll, ll>> ans;
for(auto &val:m){
ans.push_back({val.first, val.second});
}
ll x = ans.size();
vector<ll> test(x, -1);
test[0] = ans[0].first * ans[0].second;
if(ans[1].first == ans[0].first+1){
test[1] = max(vec[0], ans[1].first * ans[1].second);
}
else{
test[1] = vec[0] + ans[1].first * ans[1].second;
}
for(ll i=2; i<x; i++){
if(ans[i].first == ans[i-1].first+1){
test[i] = max(test[i-1], test[i-2]+(ans[i].first * ans[i].second));
}
else{
test[i] = test[i-1] + (ans[i].first * ans[i].second);
}
}
return test[x-1];
}
int main(){
ll n;
cin>>n;
vector<ll> vec;
for(ll i=0; i<n; i++){
ll x;
cin>>n;
vec.push_back(x);
}
ll maxsum = asum(vec);
cout<<maxsum<<endl;
}