Advertisement
allia

кузнечик 31

Nov 26th, 2020
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. int poisk_grass (int N, long long *grass)
  7. {
  8.   long long *sum_grass = new long long[N+1];
  9.   sum_grass[2] = 0;
  10.   sum_grass[3] = grass[0] + grass[2];
  11.   sum_grass[4] = grass[0] + grass[3];
  12.  
  13.   for (long long k = 5; k <= N; k++)
  14.     sum_grass[k] = max(sum_grass[k-2], sum_grass[k-3]) + grass[k-1];
  15.  
  16.    return sum_grass[N];
  17. }
  18.  
  19. int main()
  20. {
  21.   int N;
  22.   cin >> N;
  23.  
  24.   long long *grass = new long long[N];
  25.  
  26.   for (long long i = 0; i<N; i++)
  27.   cin >> grass[i];
  28.  
  29.   if (N == 0)
  30.    cout << 0;
  31.    else if (N == 1 || N == 2)
  32.     cout << grass[0];
  33.    else cout << poisk_grass(N, grass);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement