Advertisement
Dang_Quan_10_Tin

POLE

Feb 21st, 2020
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define task ""
  3. #define pb push_back
  4. #define ld long double
  5. #define ll long long
  6. #define ull unsigned long long
  7.  
  8. using namespace std;
  9.  
  10. int typetest;
  11.  
  12. inline void fastIOfileinput(){
  13.     ios_base:: sync_with_stdio(0);
  14.     cin.tie(0);
  15.     cout.tie(0);
  16.     if(fopen(task".inp", "r")){
  17.         freopen(task".inp", "r", stdin);
  18.         freopen(task".out", "w", stdout);
  19.     }
  20.     if(fopen(task".in", "r")){
  21.         freopen(task".in", "r", stdin);
  22.         freopen(task".out", "w", stdout);
  23.     }
  24.     typetest = 0;
  25. }
  26.  
  27. ll l[10009][1009], h[10009][1009];
  28. int n;
  29. ll a[10009], m;
  30. ll f[10009][1009];
  31. ll c;
  32.  
  33. inline void Enter(){
  34.     cin >> n >> c;
  35.     for(int i = 1; i <= n; ++i){
  36.         cin >> a[i];
  37.         m = max(m, a[i]);
  38.     }
  39.     memset(f, 0, sizeof(f));
  40.     for(int i = 1; i <= n; ++i){
  41.         h[i][m + 1] = INT_MAX;
  42.         l[i][0] = INT_MAX;
  43.     }
  44. }
  45. inline void solve(){
  46.     for(int i = 1; i <= n; ++i){
  47.         for(ll j = 1; j <= m; ++j){
  48.             if(j >= a[i]){
  49.                 f[i][j] = (ll) (i != 1) * min(l[i - 1][j] + c * j, h[i - 1][j] - c * j) + (j - a[i]) * (j - a[i]);
  50.                 l[i][j] = min(l[i][j - 1], f[i][j] - c * j);
  51.             }
  52.             else{
  53.                 f[i][j] = l[i][j] = INT_MAX;
  54.             }
  55.             if(m - j + 1 >= a[i]){
  56.             f[i][m - j + 1] = (ll)(i != 1) * min(l[i - 1][m - j + 1] + c * (m - j + 1),
  57.                                   h[i - 1][m - j + 1] - c * (m - j + 1)) + (m - j + 1LL - a[i]) * (m - j + 1LL - a[i]);
  58.             h[i][m - j + 1] = min(h[i][m - j + 2], f[i][m - j + 1] + c * (m - j + 1));
  59.             }
  60.             else{
  61.                 h[i][m - j + 1] = h[i][m - j + 2];
  62.             }
  63.         }
  64.     }
  65.     ll ans = 1e18;
  66.     for(int i = 1; i <= m; ++i){
  67.         ans = min(ans, f[n][i]);
  68.     }
  69.     cout << ans;
  70. }
  71.  
  72. int main(){
  73.     fastIOfileinput();
  74.     if(typetest){
  75.         int t;
  76.         cin >> t;
  77.         while(t--){
  78.             Enter();
  79.             solve();
  80.         }
  81.     }
  82.     else{
  83.         Enter();
  84.         solve();
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement