Advertisement
midnight_sun

Untitled

Aug 15th, 2023
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
  3. using namespace std;
  4. int arr[] = {0, 13,8,11,7,9,17,12,14,19,15,16,15,15,13,14 };
  5. int n = 15;
  6. int dp[17] = {};
  7. int trac[17] = {};
  8. int main() {
  9.     fastio;
  10.     {dp[1] = arr[1]; dp[2] = arr[2]; dp[3] = arr[3]; }
  11.     trac[1] = trac[2] = trac[3] = -1;
  12.     for(int i=4;i<=n;i++){
  13.         dp[i] = dp[i - 3];
  14.         trac[i] = i - 3;
  15.         if (dp[i] > dp[i - 2]) {
  16.             dp[i] = dp[i - 2];
  17.             trac[i] = i - 2;
  18.         }
  19.         if (dp[i] > dp[i - 1]) {
  20.             dp[i] = i - 1;
  21.             trac[i] = i - 1;
  22.         }
  23.         dp[i] += arr[i];
  24.     }
  25.     vector<int> v;
  26.     int idx = n;
  27.     while (1) {
  28.         if (trac[idx] == -1) break;
  29.         v.push_back(trac[idx]);
  30.         idx = trac[idx];
  31.     }
  32.     reverse(v.begin(), v.end());
  33.     for (int i : v) cout << i << " ";
  34.     cout << n << "\n";
  35.     cout << dp[n];
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement