Mehulcoder

Esh001

Jun 13th, 2021 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1.  
  2. /*
  3. Author: Mehul Chaturvedi
  4. Talent is overrated
  5. */
  6.  
  7. #include <bits/stdc++.h>
  8. using namespace std;
  9.  
  10. using ll = long long;
  11. #define INF 4557430888798830399ll
  12.  
  13. vector<ll> dp;
  14. ll get(vector<ll> h, ll start, ll c) {
  15.     ll n = h.size();
  16.  
  17.     if (start == n - 1) {
  18.         return 0;
  19.     }
  20.     if (dp[start] != -1) {
  21.         return dp[start];
  22.     }
  23.  
  24.     ll ans = INF;
  25.     for (int i = start + 1; i < n; ++i) {
  26.         ans = min(ans, (h[i] - h[start]) * (h[i] - h[start]) + c + get(h, i, c));
  27.     }
  28.  
  29.     return dp[start] = ans;
  30. }
  31.  
  32. int main(int argc , char ** argv) {
  33.     ios_base::sync_with_stdio(false) ;
  34.     cin.tie(NULL) ;
  35.  
  36.     ll n, c; cin >> n >> c;
  37.     vector<ll> h(n, 0);
  38.     dp.clear();
  39.     dp.resize(n, -1);
  40.  
  41.     for (int i = 0; i < n; ++i) {
  42.         cin >> h[i];
  43.     }
  44.  
  45.     cout << get(h, 0, c);
  46.     return 0 ;
  47. }
Add Comment
Please, Sign In to add comment