When we are focusing on solving problems, we usually prefer to stay in front of computers rather than go out for lunch. At this time, we may call for food delivery.
Suppose there areNPeople living in a straight street that is just lies on an X-coordinate axis.ITh person's coordinate isXIMeters. And in the street there is a take-out restaurant which has coordinatesXMeters. One day at lunchtime, each person takes an order from the restaurant at the same time. As a worker in the restaurant, you need to start from the restaurant, send food toNPeople, and then come back to the restaurant. Your speed isV-1Meters per minute.
You know thatNPeople have different personal characters; therefore they have different feeling on the time their food arrives. Their feelings are measuredDispleasure Index. At the beginning,Displeasure IndexFor each person is 0. When waiting for the food,ITh person will gainBiDispleasure IndexPer minute.
If one'sDispleasure IndexGoes too high, he will not buy your food any more. So you need to keep the sum of all people'sDispleasure IndexAs low as possible in order to maximize your income. Your task is to find the minimal sumDispleasure Index.
Input
The input contains multiple test cases, separated with a blank line. Each case is started with three IntegersN(1 <=N<= 1000 ),V(V> 0 ),X(X> = 0), thenNLines followed. Each line contains two integersXI(XI> = 0 ),Bi(Bi> = 0), which are described above.
You can safely assume that all numbers in the input and output will be less than 231-1.
Please process to the end-of-file.
Output
For each test case please output a single number, which is the minimal sumDispleasure Index. One test case per line.
Sample Input
5 1 0
1 1
2 2
3 3
4
5 5
Sample output
55
For a group of customers, the customer is more likely to be less than the value due to the increase in waiting time.
DP [I] [J] [k] indicates I ~ The minimum dissatisfaction value after the take-out in the J interval. k = 0 indicates that the take-out is stopped at I, and K = 1 indicates that the take-out is stopped at J.
Then, four statuses are obtained.
DP [I] [J] [0] = min (DP [I] [J] [0], DP [I + 1] [J] [0] + (A [I + 1]. x-A [I]. x) * (TEM + A [I]. v ));
DP [I] [J] [0] = min (DP [I] [J] [0], DP [I + 1] [J] [1] + (A [J]. x-A [I]. x) * (TEM + A [I]. v ));
DP [I] [J] [1] = min (DP [I] [J] [1], DP [I] [J-1] [0] + (A [J]. x-A [I]. x) * (TEM + A [J]. v ));
DP [I] [J] [1] = min (DP [I] [J] [1], DP [I] [J-1] [1] + (A [J]. x-A [J-1]. x) * (TEM + A [J]. v ));
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define up(i,x,y) for(i=x;i<=y;i++)#define down(i,x,y) for(i=x;i>=y;i--)#define mem(a,b) memset(a,b,sizeof(a))#define w(x) while(x)const int inf = 1<<30;int dp[1005][1005][2],sum[1005];struct node{ int x,v;} a[1005];int cmp(node a,node b){ return a.x<b.x;}int set(int l,int r){ if(l>r) return 0; return sum[r]-sum[l-1];}int main(){ int n,v,x,i,j,k,pos,tem; w(~scanf("%d%d%d",&n,&v,&x)) { up(i,1,n) scanf("%d%d",&a[i].x,&a[i].v); n++; a[n].x=x,a[n].v=0; sort(a+1,a+n+1,cmp); up(i,1,n) sum[i]=sum[i-1]+a[i].v; up(i,1,n) up(j,1,n) dp[i][j][0]=dp[i][j][1]=inf; up(i,1,n) if(a[i].x==x) { pos=i; break; } dp[pos][pos][0]=dp[pos][pos][1]=sum[0]=0; down(i,pos,1) { up(j,pos,n) { tem=set(1,i-1)+set(j+1,n); if(i==j) continue; dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][0]+(a[i+1].x-a[i].x)*(tem+a[i].v)); dp[i][j][0]=min(dp[i][j][0],dp[i+1][j][1]+(a[j].x-a[i].x)*(tem+a[i].v)); dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][0]+(a[j].x-a[i].x)*(tem+a[j].v)); dp[i][j][1]=min(dp[i][j][1],dp[i][j-1][1]+(a[j].x-a[j-1].x)*(tem+a[j].v)); } } printf("%d\n",min(dp[1][n][0],dp[1][n][1])*v); } return 0;}