Since there may be spells with the same damage, and if multiple spells with the same damage are selected, the total damage should add the damage multiplied by the count of that damage spell. Therefore, we can first save the count of spells for each damage value, and then deduplicate the power. After deduplicating and sorting the power, let f(i) represent the maximum total damage when selecting from the 0th to the ith type of spell and finally selecting the ith type of spell. The state transition equation can be listed as:
classSolution { public: longlongmaximumTotalDamage(vector<int> &power) { // Let f(i) represent the maximum total damage when selecting from the 0th to the ith type of spell and finally selecting the ith type of spell. // f(i) = max(f(j), j< i && power[j] < power[i]-2) + power[i] * mp[power[i]] int i, j, n; longlong max = 0, ans = 0; unordered_map<long, long> count;