[CODEFORCES724E] Goods Transportation

**Question Description**

There is *N* cities located along the one-way road. Cities is numbered from 1 to *n* in the direction of the road.

The * I *-th City had produced * p * * i * units of goods. No more Than * s * * i * units of goods can Sold in The I -th city.

For the pair of cities *I* and *J* such that 1?≤? * I*? < *j*. ≤? *n* You can no further than once transport no more than *C* units of goods by the city C14>i to the city *J*. Note that goods can is transported from a city with a lesser index to the city with a larger index. You can transport goods between cities on any order.

Determine the maximum number of produced goods that can is sold in total in all the cities after a sequence of transportat Ions.

**Input**

The first line of the input contains the 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 *p**i* (0?≤? * P**i*? ≤?109)-the number of units of goods that were produced in each city.

The third line of input contains *n* integers *s**i* (0?≤? * s**i*? ≤?109)-the number of units of goods that can is sold in each city.

**Output**

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

**Input example**

4 3 - Ten 7 4 4 7 Ten -

**Output example**

34

**Data size and conventions**

See " **Input** "

**Exercises**

This is obviously a maximum flow model, from the source point to each city with a capacity of one-way pi, from each city to the meeting point a capacity Si one-way side, and then each city to a number than it is larger than all cities with a capacity of a one-way edge of C.

However, there are up to 10,002 points, 50.015 million sides, time aside, each side needs to have a reverse arc, and each arc needs to save at least 2 int (the end of the edge and the remaining flow of the edge), for the 256MB limit is too large.

However, this problem has a better approach, due to the specificity of the map, we can be converted to the minimum cut DP, set F (i, j) to indicate the first I point J points belonging to the S set of the minimum cut is how much.

First step: First of all, from the source to node I and then to the meeting point of the flow is exhausted, so the residual network becomes this way:

If pi > Si, then a pi-si edge is connected from the source point to I;

If pi < Si, a si-pi edge is connected from I to the meeting point;

If 0 < i < J < n + 1, the edge of a C is connected from I to J.

Step two: Consider how the above DP is transferred, and whether the enumeration current point belongs to the S-set or T-set. I am very lazy, do not want to write again, transfer equation may leave the reader to think about it ...

#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <stack > #include <vector> #include <queue> #include <cstring> #include <string> #include <map > #include <set>using namespace std;const int buffersize = 1 << 16;char buffer[buffersize], *head, *TAIL;INL ine Char Getchar () {if (Head = = Tail) {int L = fread (buffer, 1, buffersize, stdin); Tail = (Head = buffer) + L; } return *head++;} int read () {int x = 0, f = 1; char c = Getchar (); while (!isdigit (c)) {if (c = = '-') f =-1; c = Getchar ();} while (IsDigit (c)) {x = x * + C-' 0 '; c = Getchar ();} return x * f;} #define MAXN 10010#define oo (1ll <<) #define LL long Longll f[2][maxn];int N, C, P[MAXN], S[maxn];int Main () {n = Read (); C = read (); for (int i = 1; I <= n; i++) P[i] = Read (), for (int i = 1; I <= n; i++) s[i] = Read (), for (int i = 0; i < 2; i++) for (int j = 0; J <= N; j + +) F[i][j] = Oo;f[0] [0] = 0; LL base = 0; BOOL cur = 1;for (int i = 1; I <= n; i++, cur ^= 1) {base + = min (S[i], p[i]); for (int j = 0; J <= N; j + +) F[cur][j] = oo;for (int j = 0; J <= I; j + +) {if (S[i] > P[i]) {if (J && f[cur^1][j-1] < oo) f[cur][j] = min (F[cur][j], F [Cur^1] [j-1] + s[i]-p[i]), if (F[cur^1][j] < oo) f[cur][j] = min (F[cur][j], F[cur^1][j] + (LL) J * C);} else {if (J && f[cur^1][j-1] < oo) f[cur][j] = min (F[cur][j], f[cur^1][j-1]), if (F[cur^1][j] < oo) F[cur][j] = Min (F[cur][j], f[cur^1][j] + p[i]-s[i] + (LL) J * C);}} LL tmp = oo;for (int i = 0; I <= n; i++) tmp = MIN (tmp, f[cur^1][i]);p rintf ("%i64d\n", base + tmp); return 0;}

[CODEFORCES724E] Goods Transportation