Advertisement
tsypko

E

Jul 10th, 2021
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/rope>
  3. using namespace std;
  4. using namespace __gnu_cxx;
  5. typedef long long ll;
  6. typedef unsigned long long ull;
  7. typedef unsigned int ui;
  8. typedef long double ld;
  9. typedef pair<ll, ll> ii;
  10. typedef pair<ii, ii> iii;
  11. int MOD = 1e9 + 9;
  12. const ld E = 1e-9;
  13. #define null NULL
  14. #define ms(x) memset(x, 0, sizeof(x))
  15. #ifndef LOCAL
  16. #define endl "\n"
  17. #endif
  18. #ifndef LONG_LONG_MAX
  19. #define LONG_LONG_MAX LLONG_MAX
  20. #endif
  21. #define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  22. #define _read(x) freopen(x, "r", stdin)
  23. #define _write(x) freopen(x, "w", stdout)
  24. #define files(x) _read(x ".in"); _write(x ".out")
  25. #define filesdatsol(x) _read(x ".DAT"); _write(x ".SOL")
  26. #define output _write("output.txt")
  27. #define input _read("input.txt")
  28. #define prev time_prev
  29. #ifndef M_PI
  30. #define M_PI acos(-1)
  31. #endif
  32. #define remove tipa_remove
  33. #define next tipa_next
  34. #define left tipa_left
  35. #define right tipa_right
  36. #define mod % MOD
  37. #define y1 hello_world
  38. unsigned char ccc;
  39. bool _minus = false;
  40. template<typename T>
  41. inline T sqr(T t) {
  42.     return (t * t);
  43. }
  44. inline void read(ll &n) {
  45.     n = 0;
  46.     _minus = false;
  47.     while (true) {
  48.         ccc = getchar();
  49.         if (ccc == ' ' || ccc == '\n')
  50.             break;
  51.         if (ccc == '-') {
  52.             _minus = true;
  53.             continue;
  54.         }
  55.         n = n * 10 + ccc - '0';
  56.     }
  57.     if (_minus)
  58.         n *= -1;
  59. }
  60. inline bool read(int &n) {
  61.     n = 0;
  62.     _minus = false;
  63.     while (true) {
  64.         ccc = getchar();
  65.         if (ccc == ' ' || ccc == '\n') {
  66.             if (ccc == '\n')
  67.                 return true;
  68.             break;
  69.         }
  70.         if (ccc == '-') {
  71.             _minus = true;
  72.             continue;
  73.         }
  74.         n = n * 10 + ccc - '0';
  75.     }
  76.     if (_minus)
  77.         n *= -1;
  78.     return false;
  79. }
  80. char wwww[19];
  81. int kkkk;
  82. inline void write(ll y) {
  83.     long long x = y;
  84.     kkkk = 0;
  85.     if (x < 0) {
  86.         putchar('-');
  87.         x *= -1;
  88.     }
  89.     if (!x)
  90.         ++kkkk, wwww[kkkk] = '0';
  91.     else
  92.         while (x) {
  93.             ++kkkk;
  94.             wwww[kkkk] = char(x % 10 + '0');
  95.             x /= 10;
  96.         }
  97.     for (int i = kkkk; i >= 1; --i)
  98.         putchar(wwww[i]);
  99. }
  100.  
  101. #ifdef LOCAL
  102. #define DEBUG
  103. #endif
  104.  
  105. #ifdef DEBUG
  106. #define dbg if(1)
  107. #else
  108. #define dbg if(0)
  109. #endif
  110.  
  111. int n, m;
  112.  
  113. const int MAX_N = 8e3 + 10;
  114. const int MAX_M = 8e2 + 10;
  115.  
  116. ll dp[MAX_N][MAX_M];
  117.  
  118. int c[MAX_N][MAX_M];
  119.  
  120. ll ar[MAX_N];
  121. ll sum[MAX_N];
  122.  
  123. void solve(int h, int l, int r, int tl, int tr){
  124.     if(l > r)
  125.         return;
  126.     int x = (l + r) >> 1;
  127.     c[x][h] = -1;
  128.     for(int i = tl; i <= tr && i < x; i++){
  129.         ll res = dp[i][h - 1] + (sum[x] - sum[i]) * 1LL * (x - i);
  130.         if(res < dp[x][h]){
  131.             dp[x][h] = res;
  132.             c[x][h] = i;
  133.         }
  134.     }
  135.     assert(c[x][h] != -1);
  136.     solve(h, l, x - 1, tl, c[x][h]);
  137.     solve(h, x + 1, r, c[x][h], tr);
  138. }
  139.  
  140. int main() {
  141.     sync;
  142.     srand(time(NULL));
  143.     cout.precision(10);
  144.     cout << fixed;
  145. #ifdef LOCAL
  146.     input;
  147. #else
  148.  
  149. #endif
  150.  
  151.     cin >> n >> m;
  152.     m = min(m, n);
  153.     for(int i = 0; i <= n; i++){
  154.         for(int j = 0; j <= m; j++){
  155.             dp[i][j] = 1e18;
  156.         }
  157.     }
  158.  
  159.     dp[0][0] = 0;
  160.  
  161.     for(int i = 1; i <= n; i++){
  162.         cin >> ar[i];
  163.         sum[i] = ar[i] + sum[i - 1];
  164.     }
  165.  
  166.     for(int i = 1; i <= m; i++){
  167.         solve(i, i, n, 0, n - 1);
  168.     }
  169.  
  170.     cout << dp[n][m] << endl;
  171.  
  172. }
  173.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement