fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 1000000 + 10;
  6. const long long inf = 1e9 * 1e9;
  7.  
  8. int a[N], sum[N];
  9. long long f[N];
  10. deque<int> Q;
  11. int n, m;
  12.  
  13. int x(int i) {
  14. return sum[i];
  15. }
  16.  
  17. long long y(int i) {
  18. return f[i] + 1LL * sum[i] * sum[i];
  19. }
  20.  
  21. long long k(int i) {
  22. return 2LL * sum[i];
  23. }
  24.  
  25. int main() {
  26. cin >> n >> m;
  27. for (int i=1; i<=n; i++) {
  28. cin >> a[i];
  29. sum[i] = sum[i-1] + a[i];
  30. }
  31. Q.push_back(0);
  32. for (int i=1; i<=n; i++) {
  33. while (Q.size() >= 2 && y(Q[1]) - y(Q[0]) <= k(i) * (x(Q[1]) - x(Q[0]))) Q.pop_front();
  34. int j = Q.front(), tail;
  35. f[i] = f[j] + 1LL * (sum[i] - sum[j]) * (sum[i] - sum[j]) + m;
  36. while ((tail = Q.size()) >= 2 &&
  37. (y(i) - y(Q[tail-1])) * (x(Q[tail-1]) - x(Q[tail-2])) <=
  38. (y(Q[tail-1]) - y(Q[tail-2])) * (x(i) - x(Q[tail-1])))
  39. Q.pop_back();
  40. Q.push_back(i);
  41. }
  42. cout << f[n] << endl;
  43. return 0;
  44. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
0