hdu6024

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn = 3010;
const ll INF = 1e9;
struct Node
{
    int pos, cost;
}node[maxn];
//int pos[maxn], cost[maxn];
ll dp[maxn][2];


bool cmp(Node a, Node b)
{
    return a.pos < b.pos;
}


int main()
{
    int n;
    while(cin >> n)
    {
        for(int i = 1; i <= n; i++)
            dp[i][0] = dp[i][1] = 2 * maxn * INF;

        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &node[i].pos, &node[i].cost);
        }
        sort(node + 1, node + 1 + n, cmp);    //排序!不能按读入的直接处理
        dp[1][1] = node[1].cost;

        for(int i = 2; i <= n; i++)
        {
            dp[i][1] = min(dp[i - 1][0], dp[i - 1][1]) + node[i].cost;       //build i-th

            dp[i][0] = dp[i - 1][1] + node[i].pos - node[i - 1].pos;
            ll sum = node[i].pos - node[i - 1].pos;
            ll k = 1;         //原来设为int会溢出
            for(int j = i - 1; j >= 2; j--)
            {
                sum += (node[j].pos - node[j - 1].pos) * (++k);
                dp[i][0] = min(dp[i][0], dp[j - 1][1] + sum);
            }

        }
        cout << min(dp[n][0], dp[n][1]) << endl;
    }
    return 0;
}

 

posted on 2019-05-30 21:13  stagelovepig  阅读(155)  评论(0编辑  收藏  举报