fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. using i64 = long long;
  5. using i128 = __int128_t;
  6.  
  7. struct Line {
  8. i64 k, m;
  9. };
  10.  
  11. i128 f(const Line& L, i64 x) { return (i128)L.k * x + (i128)L.m; }
  12.  
  13. bool bad(const Line& a, const Line& b, const Line& c) {
  14. return (i128)(c.m - a.m) * (a.k - b.k) <= (i128)(b.m - a.m) * (a.k - c.k);
  15. }
  16.  
  17. int main() {
  18. ios::sync_with_stdio(false);
  19. cin.tie(nullptr);
  20. int N;
  21. i64 A, B, C;
  22. if (!(cin >> N >> A >> B >> C)) return 0;
  23. deque<Line> dq;
  24. dq.push_back({0, 0});
  25. i64 P = 0, dp = 0;
  26. for (int i = 1; i <= N; ++i) {
  27. i64 x; cin >> x;
  28. P += x;
  29. while (dq.size() >= 2 && f(dq[0], P) <= f(dq[1], P)) dq.pop_front();
  30. i128 best = f(dq[0], P);
  31. i128 cur = (i128)A * P * P + (i128)B * P + (i128)C + best;
  32. dp = (i64)cur;
  33. Line nl = { -2 * A * P, dp + A * P * P - B * P };
  34. while (dq.size() >= 2 && bad(dq[dq.size()-2], dq.back(), nl)) dq.pop_back();
  35. dq.push_back(nl);
  36. }
  37. cout << dp << '\n';
  38. return 0;
  39. }
Success #stdin #stdout 0.01s 5312KB
stdin
4
-1 10 -20
2 2 3 4
stdout
9