HDU’s nn classrooms are on a line ,which can be considered as a number line. Each classroom has a coordinate. Now Little Q wants to build several candy shops in these nn classrooms.
The total cost consists of two parts. Building a candy shop at classroom ii would have some cost cici. For every classroom PP without any candy shop, then the distance between PP and the rightmost classroom with a candy shop on PP's left side would be included in the cost too. Obviously, if there is a classroom without any candy shop, there must be a candy shop on its left side.
Now Little Q wants to know how to build the candy shops with the minimal cost. Please write a program to help him.
Input
The input contains several test cases, no more than 10 test cases.
In each test case, the first line contains an integer n(1≤n≤3000)n(1≤n≤3000), denoting the number of the classrooms.
In the following nn lines, each line contains two integers xi,ci(−109≤xi,ci≤109)xi,ci(−109≤xi,ci≤109), denoting the coordinate of the ii-th classroom and the cost of building a candy shop in it.
There are no two classrooms having same coordinate.
Output
For each test case, print a single line containing an integer, denoting the minimal cost.
Sample Input
3
1 2
2 3
3 4
4
1 7
3 1
5 10
6 1
Sample Output
5
11
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
int w;
int v;
} que[3001];
int cmp(node a,node b)
{
return a.w<b.w;
}
long long dp[3001][2],res[3001][3001];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
scanf("%d%d",&que[i].w,&que[i].v);
sort(que,que+n,cmp);
memset(dp,INF,sizeof(dp));
for(int i=0; i<n; i++)
{
res[i][i]=0;
for(int j=i+1; j<n; j++)
res[i][j]=res[i][j-1]+que[j].w-que[i].w;
//假设教室i建设糖果店,求出教室i到教室j之间的总距离(此时中间的教室都没有建设糖果店)
}
dp[0][1]=que[0].v;
for(int i=1; i<n; i++)
{
dp[i][1]=min(dp[i-1][0],dp[i-1][1])+que[i].v; //教室i建设糖果店
for(int j=0; j<i; j++)
{
dp[i][0]=min(dp[i][0],dp[j][1]+res[j][i]); //教室i不建设糖果店
}
}
printf("%lld\n",min(dp[n-1][0],dp[n-1][1]));
}
return 0;
}