44 lines
1.1 KiB
C++
44 lines
1.1 KiB
C++
#include <bits/stdc++.h>
|
|
|
|
typedef long long ll;
|
|
using namespace std;
|
|
|
|
int findMaxPoints(vector<int>& values, int i) {
|
|
if (i == 0) {
|
|
return values[0];
|
|
}
|
|
|
|
if (i == 1) {
|
|
return max(values[0], values[1]);
|
|
}
|
|
|
|
// findMaxPoints(values, i - 1) means you don't take values[i].
|
|
// findMaxPoints(values, i - 2) + values[i] means you take values[i].
|
|
// Then, the max points you can get is the larger one of the two cases.
|
|
return max(findMaxPoints(values, i - 1), findMaxPoints(values, i - 2) + values[i]);
|
|
}
|
|
|
|
int deleteAndEarn(vector<int>& nums) {
|
|
int maxValue = nums[0];
|
|
for (int i = 0; i < nums.size(); ++i) {
|
|
maxValue = max(maxValue, nums[i]);
|
|
}
|
|
|
|
vector<int> values(maxValue + 1, 0);
|
|
for (int i = 0; i < nums.size(); ++i) {
|
|
values[nums[i]] += nums[i];
|
|
}
|
|
|
|
return findMaxPoints(values, values.size() - 1);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
int n;
|
|
cin >> n;
|
|
vector<int> nums(n);
|
|
for (int i = 0; i < n; i++)
|
|
cin >> nums[i];
|
|
cout << deleteAndEarn(nums) << endl;
|
|
return 0;
|
|
} |