fork download
  1. // ~~ icebear love attttttt ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define int long long
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<int, ii> iii;
  8.  
  9. template<class T>
  10. bool minimize(T &a, const T &b) {
  11. if (a > b) return a = b, true;
  12. return false;
  13. }
  14.  
  15. template<class T>
  16. bool maximize(T &a, const T &b) {
  17. if (a < b) return a = b, true;
  18. return false;
  19. }
  20.  
  21. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  22. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  23. #define REP(i, n) for(int i=0; i<(n); ++i)
  24. #define RED(i, n) for(int i=(n)-1; i>=0; --i)
  25. #define MASK(i) (1LL << (i))
  26. #define mp make_pair
  27. #define pb push_back
  28. #define fi first
  29. #define se second
  30. #define all(x) x.begin(), x.end()
  31. #define task "pizza"
  32.  
  33. const int MOD = 1e9 + 7;
  34. const int inf = 1e9 + 27092008;
  35. const ll INF = 1e18 + 27092008;
  36. const int N = 2e5 + 5;
  37. int n, B;
  38. ll dp[N], total[N], prefB[N];
  39. array<int, 3> a[N];
  40.  
  41. class CHT {
  42. private:
  43. struct Line{
  44. ll a, b;
  45. Line(ll _a=0, ll _b=0): a(_a), b(_b) {}
  46. friend long double intersect(Line A, Line B){
  47. return 1.0 * (A.b - B.b) / (B.a - A.a);
  48. }
  49. ll calc(int x) {return 1LL * a * x + b;}
  50. };
  51. bool useless(const Line &A, const Line &B, const Line &C) { // find max
  52. return intersect(A, C) >= intersect(B, C);
  53. }
  54. deque<Line> hull;
  55.  
  56. public:
  57. void make_best(int x){
  58. while(hull.size() >= 2 && hull[0].calc(x) <= hull[1].calc(x))
  59. hull.pop_front();
  60. }
  61.  
  62. void add(ll a, ll b){
  63. Line A = Line(a, b);
  64. while(hull.size() >= 2 && useless(hull[hull.size()-2], hull.back(), A))
  65. hull.pop_back();
  66. hull.pb(A);
  67. }
  68.  
  69. ll query(int x) {return (hull.front()).calc(x);}
  70. } cht;
  71.  
  72. void init(void) {
  73. cin >> n >> B;
  74. FOR(i, 1, n) cin >> a[i][0] >> a[i][1] >> a[i][2];
  75. sort(a + 1, a + n + 1);
  76. FOR(i, 1, n) {
  77. total[i] = total[i - 1] + a[i][1] + a[i][0] * a[i][2];
  78. prefB[i] = prefB[i - 1] + a[i][2];
  79. }
  80. }
  81.  
  82. void process(void) {
  83. FOR(i, 1, n) dp[i] = -INF;
  84. // dp[i] = max(dp[j - 1] - total[j - 1] + a[i][0] * prefB[j - 1])
  85. // + total[i] - a[i][0] * prefB[i] - B
  86. cht.add(0, 0);
  87. FOR(i, 1, n) {
  88. cht.make_best(a[i][0]);
  89. dp[i] = cht.query(a[i][0]) + total[i] - a[i][0] * prefB[i] - B;
  90. cht.add(prefB[i], dp[i] - total[i]);
  91. }
  92. cout << dp[n];
  93. }
  94.  
  95. signed main() {
  96. ios_base::sync_with_stdio(0);
  97. cin.tie(0); cout.tie(0);
  98. if (fopen(task".inp", "r")) {
  99. freopen(task".inp", "r", stdin);
  100. freopen(task".out", "w", stdout);
  101. }
  102. int tc = 1;
  103. // cin >> tc;
  104. while(tc--) {
  105. init();
  106. process();
  107. }
  108. return 0;
  109. }
  110.  
  111.  
Success #stdin #stdout 0s 5248KB
stdin
Standard input is empty
stdout
Standard output is empty