-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3186_Maximum_Total_Damage_With_Spell_Casting.txt
More file actions
53 lines (47 loc) · 1.58 KB
/
3186_Maximum_Total_Damage_With_Spell_Casting.txt
File metadata and controls
53 lines (47 loc) · 1.58 KB
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
47
48
49
50
51
52
53
class Solution {
public:
vector<pair<int,int>> list_powers;
unordered_map<long long, long long> memo;
void clean(vector<int>& power){
map<int, int> aparitions;
for(int i = 0; i < power.size(); i++){
aparitions[power[i]]++;
}
for(auto a: aparitions){
list_powers.push_back({a.first, a.second});
}
}
long long complete(int pos, long long value){
if(pos == list_powers.size())
return 0;
long long key = ((long long)pos << 32) ^ (value & 0xffffffff);
if(memo.find(key) != memo.end())
return memo[key];
long long take = 0, skip = 0;
if(abs(value - list_powers[pos].first) > 2){
take = list_powers[pos].first * list_powers[pos].second + complete(pos + 1, list_powers[pos].first);
}
skip = complete(pos + 1, value);
return memo[key] = max(take, skip);
}
long long maximumTotalDamage(vector<int>& power) {
map<long long, long long> freq;
for (int x : power) freq[x]++;
vector<long long> vals, cnts;
for (auto& [v,c] : freq) {
vals.push_back(v);
cnts.push_back(c);
}
int n = vals.size();
vector<long long> dp(n, 0);
for (int i = 0; i < n; i++) {
long long take = (long long)vals[i] * cnts[i];
int j = i - 1;
// buscar el último valor que cumpla la restricción |vals[i]-vals[j]|>2
while (j >= 0 && vals[i] - vals[j] <= 2) j--;
if (j >= 0) take += dp[j];
dp[i] = max(take, i>0 ? dp[i-1] : 0);
}
return dp[n-1];
}
};