fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios::sync_with_stdio(false);
  6. cin.tie(nullptr);
  7.  
  8. int n;
  9. if (!(cin >> n)) return 0;
  10.  
  11. const int MAXV = 100000;
  12. vector<long long> cnt(MAXV + 1, 0);
  13. int x;
  14. int maxv = 0;
  15. for (int i = 0; i < n; ++i) {
  16. cin >> x;
  17. if (x >= 0 && x <= MAXV) {
  18. cnt[x]++;
  19. if (x > maxv) maxv = x;
  20. }
  21. }
  22.  
  23. if (maxv == 0) {
  24. cout << 0 << '\n';
  25. return 0;
  26. }
  27.  
  28. vector<long long> dp(maxv + 1, 0);
  29. dp[0] = 0;
  30. dp[1] = cnt[1] * 1LL;
  31.  
  32. for (int v = 2; v <= maxv; ++v) {
  33. dp[v] = max(dp[v-1], dp[v-2] + cnt[v] * 1LL * v);
  34. }
  35.  
  36. cout << dp[maxv] << '\n';
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5268KB
stdin
Standard input is empty
stdout
Standard output is empty