Zxr960115 is owner of a large farm. He feeds m cute cats and employsp feeders. There's a straight road across the farm andn hills along the road, numbered from 1 ton from left to right. The distance between hilli and(i - 1) isdi meters. The feeders live in hill 1.
One day, the cats went out to play. Cat i went on a trip to hillhi, finished its trip at timeti, and then waited at hillhi for a feeder. The feeders must take all the cats. Each feeder goes straightly from hill 1 ton without waiting at a hill and takes all thewaiting cats at each hill away. Feeders walk at a speed of 1 meter per unit time and are strong enough to take as many cats as they want.
For example, suppose we have two hills (d2 = 1) and one cat that finished its trip at time 3 at hill 2(h1 = 2). Then if the feeder leaves hill 1 at time 2 or at time 3, he can take this cat, but if he leaves hill 1 at time 1 he can't take it. If the feeder leaves hill 1 at time 2, the cat waits him for 0 time units, if the feeder leaves hill 1 at time 3, the cat waits him for 1 time units.
Your task is to schedule the time leaving from hill 1 for each feeder so that the sum of the waiting time of all cats is minimized.
The first line of the input contains three integers n, m, p(2 ≤ n ≤ 105, 1 ≤ m ≤ 105, 1 ≤ p ≤ 100).
The second line contains n - 1 positive integersd2, d3, ..., dn(1 ≤ di < 104).
Each of the next m lines contains two integershi andti(1 ≤ hi ≤ n, 0 ≤ ti ≤ 109).
Output an integer, the minimum sum of waiting time of all cats.
Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use thecin,cout streams or the%I64d specifier.
4 6 2 1 3 5 1 0 2 1 4 9 1 10 2 10 3 12
3
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 100100
#define M 111
using namespace std;
long long s[N],a[N],q[N],dp[M][N];
int h,t,n,m,p,i,j,d[N];
long long x;
double getdp(int k)
{
return dp[i-1][k]+a[j]*(j-k)-s[j]+s[k];
}
long long Y(int k)
{
return dp[i-1][k]+s[k];
}
long long X(int x)
{
return x;
}
bool judge(int l,int k,int j) //如果满足条件,则k比l更优
{
return (Y(k)-Y(l))<=(X(k)-X(l))*a[j];
}
bool maintain(int k,int i,int j)
{
return (Y(k)-Y(i))*(X(j)-X(k))>=(Y(j)-Y(k))*(X(k)-X(i)); //取三个点维护单调性
}
int main()
{
scanf("%d%d%d",&n,&m,&p);
if (p>=m)
{
puts("0\n");
return 0;
}
for(int i=2;i<=n;i++)
{
scanf("%d",&d[i]);
d[i]+=d[i-1];
}
for(int i=1;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
a[i]=(long long)y-d[x];
}
sort(a+1,a+1+m);
for(int i=1;i<=m;i++)
s[i]=s[i-1]+a[i];
for(int i=1;i<=m;i++) dp[0][i]=(long long)1e18;
for(i=1;i<=p;i++)
{
h=t=0; q[0]=0;
for(j=0;j<=m;j++)
{
while (h<t && judge(q[h],q[h+1],j)) h++; //如果h+1比h更优,则继续往后找更优的
dp[i][j]=getdp(q[h]);
while (h<t && maintain(q[t],q[t-1],j)) t--; //维护单调性
q[++t]=j;
}
}
printf("%I64d\n",dp[p][m]);
return 0;
}