提交时间:2021-12-13 13:35:08

运行 ID: 35087

#include <bits/stdc++.h> #define ll long long using namespace std; const int N = 1000000 + 10; int n,q[N]; ll a[N],p[N],c[N],dp[N],sum[N],sum2[N],K[N]; inline ll getx(int x){ return sum[x]; } inline ll gety(int x){ return dp[x] + sum2[x]; } inline double slope(int x,int y){ return 1.0 * (gety(x) - gety(y)) / (getx(x) - getx(y)); } int main() { cin >> n; for (int i = 1;i <= n;i++) { cin >> a[i] >> p[i]>> c[i]; sum[i] = sum[i - 1] + p[i]; sum2[i] = sum2[i - 1] + (long long)a[i] * p[i]; K[i] = a[i]; } int head = 1,tail = 1; for (int i = 1;i <= n;i++) { while (head < tail && slope(q[head],q[head + 1]) <= K[i]) head++; dp[i] = dp[q[head]] + (long long)(a[i] * (sum[i] - sum[q[head]])) - (long long)(sum2[i] - sum2[q[head]]) + c[i]; while (head < tail && slope(q[tail - 1],q[tail]) >= slope(q[tail],i)) tail--; q[++tail] = i; } cout << dp[n]; return 0; }