Description
There are n cities located along the one-way road. Cities are numbered from 1 to n in the direction of the road.
The i-th city had produced pi units of goods. No more than si units of goods can be sold in the i-th city.
For each pair of cities i and j such that 1 ≤ i < j ≤ n you can no more than once transport no more than c units of goods from the city i to the city j. Note that goods can only be transported from a city with a lesser index to the city with a larger index. You can transport goods between cities in any order.
Determine the maximum number of produced goods that can be sold in total in all the cities after a sequence of transportations.
Input
The first line of the input contains two integers n and c (1 ≤ n ≤ 10 000, 0 ≤ c ≤ 109) — the number of cities and the maximum amount of goods for a single transportation.
The second line contains n integers pi (0 ≤ pi ≤ 109) — the number of units of goods that were produced in each city.
The third line of input contains n integers si (0 ≤ si ≤ 109) — the number of units of goods that can be sold in each city.
Solution
翻译:
有n个城市,城市i生产p[i]个产品,最多能卖s[i]个产品。对于一对(i,j)我们最多能将c个产品从i运到j,且一对城市仅此一次
想到了网络流的做法,然鹅光是建图就GG了。弃疗
考虑求最小割。我们设f[i,j]表示前i个城市有j个在S集的最小割,讨论一下第i个分在哪里就行了
Code
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define fill(x,t) memset(x,t,sizeof(x))
typedef __int64 LL;
const LL INF=1e15;
const int N=10005;
LL f[2][N],s[N],p[N],c;
int main(void) {
int n; scanf("%d%I64d",&n,&c);
rep(i,1,n) scanf("%I64d",&p[i]);
rep(i,1,n) scanf("%I64d",&s[i]);
f[0][0]=0;
rep(i,1,n) {
rep(j,0,i) f[i&1][j]=INF;
f[i&1][0]=f[(i-1)&1][0]+p[i];
rep(j,1,i-1) f[i&1][j]=std:: min(f[(i-1)&1][j-1]+s[i],f[(i-1)&1][j]+p[i]+j*c);
f[i&1][i]=f[(i-1)&1][i-1]+s[i];
}
LL ans=INF;
rep(i,0,n) ans=std:: min(f[n&1][i],ans);
printf("%I64d\n", ans);
return 0;
}