Little_hobbit

329 Лесенка 2 - Динамика

Jul 4th, 2020
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. // Условие: https://acmp.ru/index.asp?main=task&id_task=329
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. //int d[1001], way[1000];
  9.  
  10. int main()
  11. {
  12.     int n;
  13.     cin >> n;
  14.     vector<int> d(n + 1, 0), f(n+1, 0), way(n, 0);
  15.     for (int i = 1; i <= n; ++i)
  16.     {
  17.         cin >> d[i];
  18.     }
  19.  
  20.     for (int i = 1; i <= n; ++i)
  21.     {
  22.         f[i] = (i != 1)? max(f[i - 1], f[i - 2]) + d[i] : d[i];
  23.     }
  24.  
  25.     int k = n, i = 0;
  26.     while (k > 0)
  27.     {
  28.         way[i++] = k;
  29.         if (f[k] == f[k - 1] + d[k])
  30.             k -= 1;
  31.         else
  32.             k-=2;
  33.     }
  34.  
  35.     cout << f[n] << endl;
  36.     for (int j = i - 1; j >= 0; --j)
  37.     {
  38.         cout << way[j] << ' ';
  39.     }
  40.  
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment