BZOJ 1096
其实和锯木厂那题是差不多的
然后主要是式子列出来 斜率优化都是这种套路
然后这次知道了++h的时候一定要注意顺序 否则解就是另一个意思了
/*
if you can't see the repay
Why not just work step by step
rubbish is relaxed
to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod = (int)1e9+7;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
/*namespace sgt
{
#define mid ((l+r)>>1)
#undef mid
}*/
const int MAX_N = 1000025;
ll sum[MAX_N],x[MAX_N],p[MAX_N],c[MAX_N],b[MAX_N],dp[MAX_N];
int h = 0,t = 1,q[MAX_N];
ll getup(int j,int k)
{
return ((dp[j]+b[j]) - (dp[k]+b[k]));
}
ll getdown(int j,int k)
{
return sum[j] - sum[k];
}
int main()
{
//ios::sync_with_stdio(false);
//freopen("a.txt","r",stdin);
//freopen("b.txt","w",stdout);
int n;
scanf("%d",&n);
for(int i = 1;i<=n;++i)
{
scanf("%lld%lld%lld",&x[i],&p[i],&c[i]);
b[i] = b[i-1] + x[i]*p[i];
sum[i] = sum[i-1] + p[i];
}
for(int i = 1;i<=n;++i)
{
while(h<t&&getup(q[h+1],q[h])<=x[i]*getdown(q[h+1],q[h])) ++h;
dp[i] = dp[q[h]] + c[i] + (sum[i]-sum[q[h]])*x[i] - (b[i]-b[q[h]]);
while(h<t&&getup(i,q[t])*getdown(q[t],q[t-1])<=getup(q[t],q[t-1])*getdown(i,q[t])) --t;
q[++t] = i;
}
printf("%lld\n",dp[n]);
//fclose(stdin);
//fclose(stdout);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}