Goods transportation
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Output

Print the maximum total number of produced goods that can be sold in all cities after a sequence of transportations.

Examples
input
3 0
1 2 3
3 2 1
output
4
input
5 1
7 4 2 1 0
1 2 3 4 5
output
12
input
4 3
13 10 7 4
4 7 10 13
output
34
分析:考虑最大流等于最小割,从小到大dp;
   dp[i][j]表示前i个点有j个点在最小割点集里,
   则dp[i][j]=min(dp[i-1][j-1]+s[i],dp[i-1][j]+j*c+p[i]);
   dp[i-1][j-1]+s[i]表示i留在s-割的代价,dp[i-1][j]+j*c+p[i]表示i留在t-割,除了要花费p[i]外,还要花费j*c的代价;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<ll,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t;
ll p[maxn],s[maxn],ans[maxn],c;
int main()
{
int i,j;
scanf("%d%lld",&n,&c);
rep(i,,n)scanf("%lld",&p[i]);
rep(i,,n)scanf("%lld",&s[i]);
rep(i,,n)
{
ans[i]=1e18;
for(j=i;j>=;j--)
{
ans[j]=min(ans[j]+j*c+p[i],ans[j-]+s[i]);
}
ans[]+=p[i];
}
ll ret=1e18;
rep(i,,n)ret=min(ret,ans[i]);
printf("%lld\n",ret);
//system("Pause");
return ;
}

Goods transportation的更多相关文章

  1. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation 动态规划

    E. Goods transportation 题目连接: http://codeforces.com/contest/724/problem/E Description There are n ci ...

  2. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E - Goods transportation 最大流转最小割转dp

    E - Goods transportation 思路:这个最大流-> 最小割->dp好巧妙哦. #include<bits/stdc++.h> #define LL long ...

  3. [codeforces724E]Goods transportation

    [codeforces724E]Goods transportation 试题描述 There are n cities located along the one-way road. Cities ...

  4. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) E. Goods transportation (非官方贪心解法)

    题目链接:http://codeforces.com/contest/724/problem/E 题目大意: 有n个城市,每个城市有pi件商品,最多能出售si件商品,对于任意一队城市i,j,其中i&l ...

  5. Codeforces 724 E Goods transportation

    Description 有 \(n\) 个点,每个点有一个入流和出流,每个点与编号比它大的点连边,容量为 \(c\) ,求最大流. Sol DP. 这种特殊的图可以DP,把最大流转化成最小割. 最小割 ...

  6. CF724E Goods transportation 最小割 DP

    照惯例CF的题不放原题链接... 题意:一个序列上有n个点,每个点有权值pi和si.表示这个点一开始有pi个物品,最多可以卖出si个物品,每个点都可以把物品向编号更大的点运输,但是对于i < j ...

  7. CF724E Goods transportation

    最大流既视感 然后 TLEMLE既视感 然后 最大流=最小割 然后 dp[i][j]前i个点j个点在S集合,最小割 然后 dp[i][j]=min(dp[i-1][j]+p[i]+j*c,dp[i-1 ...

  8. Codeforces 724E Goods transportation(最小割转DP)

    [题目链接] http://codeforces.com/problemset/problem/724/E [题目大意] 每个城市有pi的物品可以运出去卖,si个物品可以买, 编号小的城市可以往编号大 ...

  9. CodeForces E. Goods transportation【最大流+dp最小割】

    妙啊 首先暴力建图跑最大流非常简单,s向每个i连流量为p[i]的边,每个i向t连流量为s[i]的边,每个i向j连流量为c的边(i<j),但是会又T又M 考虑最大流=最小割 然后dp求最小割,设f ...

随机推荐

  1. 戏说HTML5(转)

    如果有非技术人员问你,HTML5是什么,你会怎么回答? 新的HTML规范... 给浏览器提供了牛逼能力,干以前不能干的事...(确切地说应该是给浏览器规定了许多新的接口标准,要求浏览器实现牛逼的功能. ...

  2. iOS-Runtime机制详解

    一.简介 runtime是一套底层的纯c语言的API,我们写的代码在程序运行过程中都会被转化成runtime的C代码执行,例如[target doSomething];会被转化成objc_msgSen ...

  3. Ubuntu shortcuts

    Ubuntu shortcuts 打开系统管理器 gnome-system-monitor

  4. JVM基础(6)-常用参数总结

    参考文章: 并发编程网:http://ifeve.com/useful-jvm-flags-part-4-heap-tuning/ 一.参数分类 HotSpot JVM 提供了三类参数. 第一类包括了 ...

  5. VS2010编译错误 LNK 2019 unresolved external symbol错误解决办法

    Link错误有很多种,主要是没有在连接中加入lib文件路径,或者lib配置正确,传参错误 一个solution里面多个project之间引用其他project函数会出现这个错误,由于包含了头文件而没处 ...

  6. TreeSize Free 查看文件夹大小 v2.3.3 汉化版

    <b>软件名称: <a href="http://www.bkill.com/download/30740.html"><font color=&qu ...

  7. java 图片高保真缩放

    public class NarrowImage { /**     * @param im     *            原始图像     * @param resizeTimes     *  ...

  8. Codeforces Round 371 Div2 B.Passwords

    原题: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  9. Mybatis插件原理和PageHelper结合实战分页插件(七)

    今天和大家分享下mybatis的一个分页插件PageHelper,在讲解PageHelper之前我们需要先了解下mybatis的插件原理.PageHelper 的官方网站:https://github ...

  10. C语言_愤怒的小鸟

    // ConsoleApplication4.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<stdio.h> ...