-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3186.cpp
More file actions
33 lines (32 loc) · 966 Bytes
/
3186.cpp
File metadata and controls
33 lines (32 loc) · 966 Bytes
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
class Solution {
public:
long long maximumTotalDamage(vector<int>& power) {
map<int, int> mp;
for (auto& p : power) mp[p]++;
int m = mp.size();
long long numbers[m];
long long counts[m];
int index = 0;
for (auto& [n, c] : mp) {
numbers[index] = n;
counts[index] = c;
index++;
}
long long dp[m];
for (int i = 0; i < m; ++i) dp[i] = 0;
long long res = 0;
for (int i = 0; i < m; ++i) {
if (i >= 1) dp[i] = max(dp[i], dp[i - 1]);
long long curr = numbers[i] * counts[i];
dp[i] = max(dp[i], curr);
for (int j = 1; j <= 3; ++j) {
if (i - j < 0) break;
if (numbers[i - j] < numbers[i] - 2) {
dp[i] = max(dp[i], curr + dp[i - j]);
}
}
res = max(res, dp[i]);
}
return res;
}
};