Mirbek

Покупка билетов

Dec 28th, 2021
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 5005;
  6.  
  7. int n;
  8. long long a[N], b[N], c[N], dp[N];
  9.  
  10. int main(){
  11.     cin >> n;
  12.  
  13.     for (int i = 1; i <= n; i++) {
  14.         cin >> a[i] >> b[i] >> c[i];
  15.     }
  16.  
  17.     dp[1] = a[1];
  18.     dp[2] = min(a[1] + a[2], b[1]);
  19.  
  20.     for (int i = 3; i <= n; i++) {
  21.         long long first_value = a[i] + dp[i - 1];
  22.         long long second_value = b[i - 1] + dp[i - 2];
  23.         long long third_value = c[i - 2] + dp[i - 3];
  24.         dp[i] = min(first_value, min(second_value, third_value));
  25.     }
  26.  
  27.     cout << dp[n] << endl;
  28. }
Advertisement
Add Comment
Please, Sign In to add comment