题解 | #删除相邻数字的最大分数#
删除相邻数字的最大分数
https://www.nowcoder.com/practice/3bcf72c738b6494bbe1ebe0ffde56152
#include <iostream> #include<vector> #include<algorithm> using namespace std; const int val = 10000; int main() { int n; cin >> n; vector<int> a(n); for(int i = 0; i < n; i ++) cin >> a[i]; vector<long long> count(val, 0); int maxVal = 0; for(int num : a){ count[num] += num; maxVal = max(maxVal, num); } vector<long long> dp(maxVal + 1, 0); dp[1] = count[1]; for(int i = 2; i <= maxVal; i ++){ dp[i] = max(dp[i - 2] + count[i], dp[i - 1]); } cout << dp[maxVal] << endl; return 0; }