Beingamanforever

Boredom, nice solution, good dp state

Dec 10th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. /**
  2.  *    author:  compounding
  3.  *    created: 2024-12-10 15:46:47
  4.  **/
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
  8. #define NeedForSpeed                  \
  9.     ios_base::sync_with_stdio(false); \
  10.     cin.tie(NULL);                    \
  11.     cout.tie(NULL);
  12. #define int long long
  13. #define all(x) (x).begin(), (x).end()
  14. typedef vector<int> vi;
  15. typedef vector<bool> vb;
  16. typedef vector<vi> vvi;
  17. typedef vector<pair<int, int>> vpi;
  18. #define f first
  19. #define s second
  20. #define yes cout << "YES" << endl
  21. #define no cout << "NO" << endl
  22. #define endl "\n"
  23. const int mod = 1000000007;
  24. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  25. const int N = (int)1e5 + 5;
  26. int a[N], cnt[N];
  27. void solve()
  28. {
  29.     int n;
  30.     cin >> n;
  31.     for (int i = 1; i <= n; i++)
  32.     {
  33.         cin >> a[i];
  34.         cnt[a[i]]++;
  35.     }
  36.     // dp[i] = maximum sum of elements we can get such that we allowed to delete elements <= i
  37.     vi dp(N, 0);
  38.     dp[1] = cnt[1]; // if we delete 1, then we get cnt[1]
  39.     for (int i = 2; i < N; i++)
  40.     {
  41.         // if we delete i, then we get cnt[i] * i & allowed to delete elements <= i - 2 = f(i - 2)
  42.         // if we delete i - 1, then we get f(i - 1)
  43.         dp[i] = max(dp[i - 1], dp[i - 2] + cnt[i] * i);
  44.     }
  45.     cout << dp[N - 1] << endl;
  46.     return;
  47. }
  48.  
  49. signed main()
  50. {
  51.     NeedForSpeed;
  52.     int t = 1;
  53.     // cin >> t;
  54.     while (t--)
  55.     {
  56.         solve();
  57.     }
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment