Problem:
Flame of Nucleus
Year 20XX — a nuclear explosion has burned the world. Half the people on the planet have died. Fearful.
One city, fortunately, was not directly damaged by the explosion. This city consists of
N
domes (numbered 1
through
N
inclusive) and
M
bidirectional transportation pipelines connecting the domes. In dome
i
,
P
i
citizens
reside currently and there is a nuclear shelter capable of protecting
K
i
citizens. Also, it takes
D
i
days to travel
from one end to the other end of pipeline
i
.
It has been turned out that this city will be polluted by nuclear radiation in
L
days after today. So each citizen
should take refuge in some shelter, possibly traveling through the pipelines. Citizens will be dead if they reach
their shelters in exactly or more than
L
days.
How many citizens can survive at most in this situation?
Input
The input consists of multiple test cases. Each test case begins with a line consisting of three integers
N
,
M
and
L
(1 ≤
N
≤ 100,
M
≥ 0, 1 ≤
L
≤ 10000). This is followed by
M
lines describing the configuration of
transportation pipelines connecting the domes. The
i
-th line contains three integers
A
i
,
B
i
and
D
i
(1 ≤
A
i
<
B
i
≤
N
, 1 ≤
D
i
≤ 10000), denoting that the
i
-th pipeline connects dome
A
i
and
B
i
. There is at most one pipeline
between any pair of domes. Finally, there come two lines, each consisting of
N
integers. The first gives
P
1
, . . .,
P
N
(0 ≤
P
i
≤ 10
6
) and the second gives
K
1
, . . .,
K
N
(0 ≤
K
i
≤ 10
6
).
The input is terminated by EOF. All integers in each line are separated by a whitespace.
Output
For each test case, print in a line the maximum number of people who can survive.
Sample Input
1 0 1
51
50
2 1 1
1 2 1
1000 0
0 1000
4 3 5
1 2 4
1 3 1
3 4 2
0 1000 1000 1000
3000 0 0 0
Output for the Sample Input
50
0
3000

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define Bankai ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

const int MX_SIZE= 1e2 + 10;
const int oo=1e9 + 1;
const int MOD=1e9+7;

int tc = 0;

void File() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

// O( v ^ 2 * E )
struct edge{
    int u;
    int v;
    int cap;
    int flow;
};

struct Dinic{
    int n;
    vector<int> dist, par, idEdge, curId;
    vector<vector<int>> adj;
    vector<edge> edges;

    Dinic(int nn) {
        n = nn;
        dist.resize(n + 1);
        adj.resize(n + 1);
        par.resize(n + 1);
        idEdge.resize(n + 1);
        curId.resize(n + 1);
    }

    void addEdge(int u, int v, int c){
        adj[u].push_back(edges.size()); edges.push_back({u, v, c, 0});
        adj[v].push_back(edges.size()); edges.push_back({v, u, 0, 0});
    }

    int flow(int src, int sink) {
        int totalFlow = 0;

        while (isPath(src, sink)) {
            for(int i = 1; i <= n; i++)
                curId[i] = 0;
            while(int newFlow = dfs(src, oo, sink))
                totalFlow += newFlow;
        }

        return totalFlow;
    }

    bool isPath(int src, int sink){
        for(int i = 1; i <= n; i++)
            dist[i] = oo;

        queue<int> q;
        q.push(src);
        dist[src] = 0;

        while(!q.empty()){
            int u = q.front();
            q.pop();

            if(u == sink)
                break;

            for(auto idx : adj[u]){
                auto [_, v, c, f] = edges[idx];
                if(f < c && dist[v] == oo){
                    dist[v] = dist[u] + 1;
                    par[v] = u;
                    idEdge[v] = idx;
                    q.push(v);
                }
            }
        }

        return dist[sink] < oo;
    }

    int dfs(int u, int flow, int sink){
        if(!flow)
            return 0;
        if(u == sink)
            return flow;
        for(; curId[u] < adj[u].size(); curId[u]++){
            int idx = adj[u][curId[u]];
            auto [_, v, c, f] = edges[idx];

            if(dist[v] != dist[u] + 1)
                continue;
            int newFlow = dfs(v, min (flow, c - f), sink);
            if(newFlow){
                edges[idx].flow += newFlow;
                edges[idx ^ 1].flow -= newFlow;
                return newFlow;
            }
        }
        return 0;
    }


};

struct floyd {
    int n;
    long long dist[MX_SIZE][MX_SIZE];

    floyd(int sz) {
        n = sz;
        init();
    }

    void init() {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                dist[i][j] = oo;
            }
        }

        for (int i = 1; i <= n; i++)
            dist[i][i] = 0;
    }

    void build() {
        for (int k = 1; k <= n; k++) {
            for (int u = 1; u <= n; u++) {
                for (int v = 1; v <= n; v++) {
                    dist[u][v] = min(dist[u][v], dist[u][k] + dist[k][v]);
                }
            }
        }
    }

};

int n, m, l;


void ROOM() {
    floyd f(n+1);
    for(int i = 1; i <= m; i++){
        long long u, v, d;
        cin >> u >> v >> d;
        f.dist[u][v] = d;
        f.dist[v][u] = d;
    }
    f.build();

    int p[n + 1], k[n + 1];
    for(int i = 1; i <= n; i++){
        cin >> p[i];
    }
    for(int i = 1; i <= n; i++){
        cin >> k[i];
    }

    int src = 2 * n + 1, sink = 2 * n + 2;
    Dinic flow(2 * n + 2);
    for(int i = 1; i <= n; i++){
        flow.addEdge(src, i, p[i]);
    }
    for(int i = 1; i <= n; i++){
        flow.addEdge(i + n, sink, k[i]);
    }

    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            if(f.dist[i][j] < l)
                flow.addEdge(i, j + n, oo);
        }
    }

    cout << flow.flow(src, sink);
}

int main() {
    Bankai
//    File();

    int t  = 3;
//    cin>>t;

    while (cin >> n >> m >> l) {
        tc++;

        ROOM();
        if (t)
            cout << "\n";

    }
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 4184
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;

//make -f ../makefile SRC=

/*
TODO
guess:
ff + max flow

WA:
21: ans=4950, 4635
25: ans=4798, 4503
29: ans=4908, 4673
33: ans=4950, 4861
37: ans=4928, 4685
41: ans=4798, 4604
45: ans=4867, 4713
49: ans=4876, 4439
53: ans=4950, 4870
57: ans=4950, 4745
88: ans=377, 327
errors=11/170

maybe do not send citizens to home shelters
and use vertex both as src and sink i.e. NN = 2*N + 2

*/


//------------------------------------------------------------------------------
bool DEBUG = false;
const int INF = 1000000000;

const int MAX_N = 100;
static int G0[MAX_N][MAX_N];
static int citizens[MAX_N];
static int capacity[MAX_N];
static int cost[MAX_N][MAX_N];

const int MAX_NN = 2*MAX_N + 2;
static int level[MAX_NN];
static int start[MAX_NN];


typedef vector<vector<int> > Graph;

//------------------------------------------------------------------------------
void floyd_warshall(int N)
{
    for (int v=0; v<N; v++) for (int w=0; w<N; w++) cost[v][w] = G0[v][w];

    for (int k=0; k<N; k++)
    {
        for (int i=0; i<N; i++)
        {
            if (cost[i][k] == INF) continue;
            for (int j=0; j<N; j++)
            {
                if (cost[k][j] == INF) continue;
                if (cost[i][j] == INF || cost[i][j] > cost[i][k] + cost[k][j])
                    cost[i][j] = cost[i][k] + cost[k][j];
            }
        }
    }
    if (DEBUG)
    {
        printf("ff:\n");
        for (int v=0; v<N-1; ++v)
            for (int w=v+1; w<N; ++w)
                if (cost[v][w] != INF)
                    printf("%d->%d: %d\n", v, w, cost[v][w]);
    }
}

//------------------------------------------------------------------------------
struct Edge
{
    int w, f, c, j;

    bool operator<(const Edge& other) const
    {
        return f < other.f;
    }

    Edge(): w(-1), f(0), c(0), j(-1) {}
    Edge(int v2, int flow, int capacity, int reverse_edge): w(v2), f(flow), c(capacity), j(reverse_edge) {}
    void debug() const { printf("(%d, %d, %d, %d)\n", w, f, c, j); }
};

typedef vector<Edge> Edges;
typedef vector<Edges> GRAPH;

//------------------------------------------------------------------------------
void add_edge(int N, GRAPH& G, int v, int w, int c)
{
    // forward edge:
    Edge e1(w, 0, c, G[w].size());

    // backward edge:
    Edge e2(v, 0, 0, G[v].size());

    G[v].push_back( e1 );
    G[w].push_back( e2 );
}

//------------------------------------------------------------------------------
bool bfs(int NN, GRAPH& GG, int src, int dest)
{
    //--------------------------------------------------------------------------
    // setup params:
    for (int v=0; v<NN; ++v) level[v] = -1;

    queue<int> Q;
    Q.push(src);
    level[src] = 0;

    //--------------------------------------------------------------------------
    // search:
    while (!Q.empty())
    {
        int v = Q.front(); Q.pop();
        for (Edge e: GG[v])
        {
            if (level[e.w] < 0 && e.f < e.c)
            {
                level[e.w] = level[v] + 1;
                Q.push(e.w);
            }
        }
    }

    //--------------------------------------------------------------------------
    // is dest reachable from src?
    return level[dest] >= 0;
}

int send_flow(int NN, GRAPH& GG, int v, int dest, int flow)
{
    if (v == dest) return flow;
    for (; start[v]<GG[v].size(); start[v]++)
    { 
        Edge& e = GG[v][start[v]];
        if (level[e.w] == level[v] + 1 && e.f < e.c)
        {
            int min_flow = min(flow, e.c-e.f);
            int new_flow = send_flow(NN, GG, e.w, dest, min_flow);
  
            if (new_flow)
            { 
                e.f += new_flow;
                GG[e.w][e.j].f -= new_flow;
                return new_flow; 
            }
        }
    }
    return 0;
}


//------------------------------------------------------------------------------
int dinic(int NN, GRAPH& GG, int src, int dest)
{
    //if (DEBUG) {for (int v=0; v<NN; ++v) for (Edge e: GG[v]) { printf("%d -> ", v); e.debug(); }}

    //--------------------------------------------------------------------------
    // base cases:
    if (dest == src) return 0;

    //--------------------------------------------------------------------------
    // init params:
    int res = 0;

    //--------------------------------------------------------------------------
    // compute:
    while (bfs(NN, GG, src, dest))
    {
        for (int v=0; v<NN; ++v) start[v] = 0;
        while (int flow = send_flow(NN, GG, src, dest, INF)) res += flow;
    }

    //--------------------------------------------------------------------------
    // report:
    return res;
}

//------------------------------------------------------------------------------
void debug(int N, Graph& G)
{
    printf("N=%d:\n", N);
    for (int v=0; v<N; ++v)
        for (int w: G[v])
            printf("%d -> %d\n", v+1, w+1);
    printf("\n");

}

void debug_flow_graph(int NN, GRAPH& GG)
{
    printf("NN=%d:\n", NN);
    for (int v=0; v<NN; ++v)
        for (auto e: GG[v])
            if (e.c > 0)
                printf("%d -> %d: %d\n", v, e.w, e.c);
}

GRAPH setup(int N)
{
    int NN = 2*N+2;
    int src = NN-2;
    int dest = NN-1;

    GRAPH GG(NN);

    for (int v=0; v<N; ++v)
        for (int w=0; w<N; ++w)
            if (cost[v][w] != INF && citizens[v] && capacity[w])
                add_edge(NN, GG, v, w+N, INF); //<----------

    for (int v=0; v<N; ++v)
        if (citizens[v])
            add_edge(NN, GG, src, v, citizens[v]); //<----------

    for (int w=0; w<N; ++w)
        if (capacity[w])
            add_edge(NN, GG, w+N, dest, capacity[w]); //<----------

    return GG;
}

int solve(int N, int K)
{
    floyd_warshall(N);
    for (int v=0; v<N; ++v)
        for (int w=0; w<N; ++w)
            if (cost[v][w] != INF && cost[v][w] >= K)
                cost[v][w] = INF;

    GRAPH GG = setup(N);
    int NN = 2*N+2;
    int src = NN-2;
    int dest = NN-1;
    if (DEBUG) debug_flow_graph(NN, GG);

    int res = dinic(NN, GG, src, dest);

    return res;
}
//------------------------------------------------------------------------------
int main()
{
    //DEBUG = true;
    //--------------------------------------------------------------------------
    int N, M, K, v, w, c, num;
    while (scanf("%d %d %d ", &N, &M, &K) != EOF)
    {
        for (int v=0; v<N; ++v) for (int w=0; w<N; ++w) G0[v][w] = INF;
        for (int v=0; v<N; ++v) G0[v][v] = 0;

        for (int m=0; m<M; ++m)
        {
            num = scanf("%d %d %d", &w, &v, &c);
            v--; w--;
            G0[v][w] = min(G0[v][w], c);
            G0[w][v] = G0[v][w];
        }
        for (int v=0; v<N; ++v) num = scanf("%d ", &citizens[v]);
        for (int v=0; v<N; ++v) num = scanf("%d ", &capacity[v]);

        int res = solve(N, K);
        printf("%d\n", res);
    }
    //--------------------------------------------------------------------------
    return 0;
}
//------------------------------------------------------------------------------
accuracy: 1
time_ms: 120
memory_kb: 3984
final_rank: 13

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)

template<class T>
using vi = vector<T>;

template<class T>
using vii = vector<vi<T>>;

template<class T>
using viii = vector<vii<T>>;

struct Dinic {
private:
    const long long inf = 1e18;
    vector<int> dist; //sからの距離
    vector<int> iter; //どこまで調べたか
public:
    struct edge {
        int to, rev; long long cap;
        edge(int t = 0, int r = 0, long long c = 0LL) : to(t), rev(r), cap(c) {}
    };

    int n;
    vector<vector<edge>> to;

    Dinic(int n_ = 1) : n(n_), to(n_), dist(n_), iter(n_) {}

    void addedge(int u, int v, long long cap) {//u->vにcapの容量
        int su = (int)to[u].size(), sv = (int)to[v].size();
        to[u].push_back({ v, sv, cap });
        to[v].push_back({ u, su, 0 });
    }

    long long dfs(int v, int t, long long f) { //t: 終点, f: flow
        if (v == t) return f;
        int sv = (int)to[v].size();
        for (int& i = iter[v]; i < sv; i++) {
            edge& nv = to[v][i];
            if (dist[nv.to] <= dist[v] || nv.cap == 0) continue; //here
            long long nf = dfs(nv.to, t, min(f, nv.cap));
            if (nf > 0) {
                nv.cap -= nf;
                to[nv.to][nv.rev].cap += nf;
                return nf;
            }
        }
        return 0;
    }

    void bfs(int s) {
        dist.assign(n, -1);
        queue<int> q;
        q.push(s);
        dist[s] = 0;
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (auto nv : to[v]) {
                if (nv.cap > 0 && dist[nv.to] < 0) {
                    dist[nv.to] = dist[v] + 1;
                    q.push(nv.to);
                }
            }
        }
    }

    long long maxflow(int s, int t) {//s:始点, t:終点
        long long res = 0, flow = 0;
        while (true) {
            bfs(s);
            if (dist[t] < 0) break;

            iter.assign(n, 0);
            while (flow = dfs(s, t, inf)) res += flow;
        };
        return res;
    }
};



int main()
{
    ll inf = 1e15;
    int n, m, l;
    vi<ll> ans;
    while (cin >> n >> m >> l) {
        Dinic dn(n * 2 + 2);
        int S = n * 2, T = S + 1;

        vii<ll> dist(n, vi<ll>(n, inf));
        rep(i, n) dist[i][i] = 0;

        rep(i, m) {
            int a, b, d;
            cin >> a >> b >> d;
            a--, b--;
            dist[a][b] = d;
            dist[b][a] = d;
        }

        rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

        rep(i, n) rep(j, n) {
            if (dist[i][j] < l) dn.addedge(i, j + n, inf);
        }

        rep(i, n) {
            int p;
            cin >> p;
            dn.addedge(S, i, p);
        }
        rep(i, n) {
            int k;
            cin >> k;
            dn.addedge(i + n, T, k);
        }
        
        ll flow = dn.maxflow(S, T);
        ans.push_back(flow);
        if (cin.eof()) break;
    }
    for (auto elem : ans) cout << elem << endl;
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3576
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

const int INF = (int)1e9 + 12345;

struct Flow {
    struct Edge {
        int u, v, capacity, flow;
    };

    int n;
    vector<vector<int>> adj;
    vector<Edge> edges;
    vector<int> ptr, layer;
    int m = 0;

    Flow(int n) : n(n), adj(n), ptr(n), layer(n) {}

    void addEdge(int u, int v, int capacity) {
        edges.push_back({ u, v, capacity, 0 });
        edges.push_back({ v, u, 0, 0 });
        adj[u].push_back(m);
        adj[v].push_back(m + 1);
        m += 2;
    }

    bool bfs(int s, int t) {
        fill(layer.begin(), layer.end(), -1);
        queue<int> q;
        q.push(s);
        layer[s] = 0;
        while (!q.empty()) {
            int u = q.front();
            q.pop();
            for (int i : adj[u]) {
                auto [_u, v, capacity, flow] = edges[i];
                if (layer[v] != -1) continue;
                if (flow == capacity) continue;
                layer[v] = layer[u] + 1;
                q.push(v);
            }
        }
        return layer[t] != -1;
    }

    int dfs(int u, int t, int cur_saturator) {
        if (u == t) {
            return cur_saturator;
        }
        for (int &ii = ptr[u]; ii < adj[u].size(); ii++) {
            int i = adj[u][ii];
            auto [_u, v, capacity, flow] = edges[i];
            if (layer[v] != layer[u] + 1) continue;
            if (flow == capacity) continue;
            int new_saturator = min(cur_saturator, capacity - flow);
            new_saturator = dfs(v, t, new_saturator);
            if (new_saturator > 0) {
                edges[i].flow += new_saturator;
                edges[i ^ 1].flow -= new_saturator;
                return new_saturator;
            }
        }
        return 0;
    }

    int maxFlow(int s, int t) {
        int flowed = 0;
        while (bfs(s, t)) {
            fill(ptr.begin(), ptr.end(), 0);
            while (int newly_flowed = dfs(s, t, INF)) {
                flowed += newly_flowed;
            }
        }
        return flowed;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m, l;
    while (cin >> n >> m >> l) {
        if (n == -1) break;

        Flow flow(n + n + 2);
        int s = 0, t = n + n + 1;

        vector<vector<int>> dist(n, vector<int>(n, INF));
        for (int u = 0; u < n; u++) {
            dist[u][u] = 0;
        }
        for (int i = 0; i < m; i++) {
            int u, v, w;
            cin >> u >> v >> w;
            u--;
            v--;
            dist[u][v] = dist[v][u] = w;
        }
        for (int k = 0; k < n; k++) {
            for (int u = 0; u < n; u++) {
                for (int v = 0; v < n; v++) {
                    dist[u][v] = min(dist[u][v], dist[u][k] + dist[k][v]);
                }
            }
        }

        for (int u = 0; u < n; u++) {
            for (int v = 0; v < n; v++) {
                if (dist[u][v] < l) {
                    flow.addEdge(u + 1, v + n + 1, INF);
                }
            }
        }

        for (int u = 0; u < n; u++) {
            int d;
            cin >> d;
            flow.addEdge(s, u + 1, d);
        }

        for (int u = 0; u < n; u++) {
            int k;
            cin >> k;
            flow.addEdge(u + n + 1, t, k);
        }

        cout << flow.maxFlow(s, t) << '\n';
    }
}
accuracy: 1
time_ms: 90
memory_kb: 4056
final_rank: 8

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

template<typename T>
struct dinic{
    struct Edge{
        int to;
        T c,f;
    };
    T eps;
    const T inf = numeric_limits<T>::max();
    int n,m = 0;
    vector<Edge> e;
    vector<vector<int>> g;
    vector<int> level,ptr;
    dinic(int n):n(n), g(n), level(n), ptr(n){
        eps = (T)1/(T)1e9;
    }
    void add_edge(int x,int y,T c){
        e.push_back({y,c,0});
        e.push_back({x,0,0});
        g[x].push_back(m++);
        g[y].push_back(m++);
    }
    bool bfs(int s,int t){
        fill(level.begin(), level.end(), -1);
        level[s] = 0;
        queue<int> q; q.push(s);
        while(q.size()){
            int s = q.front(); q.pop();
            for(int i:g[s]){
                int t = e[i].to;
                if(level[t] == -1 and (e[i].c - e[i].f) > eps){
                    level[t] = level[s] + 1;
                    q.push(t);
                }
            }
        }
        return (level[t] != -1);
    }
    T dfs(int s,int t,T psh){
        if(!(psh > eps) or s == t) return psh;
        for(int &i = ptr[s]; i < g[s].size(); i++){
            auto &eg = e[g[s][i]];
            if(level[eg.to] != level[s]+1) continue;
            if(!(eg.c - eg.f > eps)) continue;
            T f = dfs(eg.to, t, min(psh,eg.c-eg.f));
            if(f > eps){
                eg.f += f;
                e[g[s][i]^1].f -= f;
                return f;
            }
        }
        return 0;
    }

    T max_flow(int s,int t){
        T f = 0;
        while(bfs(s,t)){
            fill(ptr.begin(), ptr.end(), 0);
            while(1){
                T c = dfs(s,t,inf);
                if(c > eps) f += c;
                else break;
            }
        }
        return f;
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n,m,l;
    while(cin >> n >> m >> l){
        vector<vector<int>> d(n,vector<int>(n, l+1));
        for (int i = 0; i < n; ++i) {
            d[i][i] = 0;
        }
        for (int i = 0; i < m; i++){
            int x,y,z; cin >> x >> y >> z;
            x--; y--;
            d[x][y] = d[y][x] = min(d[x][y], z);
        }
        for (int k = 0; k < n; ++k) {
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < n; ++j) {
                    d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
                }
            }
        }
        vector<int> a(n),b(n);
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
        }
        for (int i = 0; i < n; ++i) {
            cin >> b[i];
        }
        int S = n+n, T = n+n+1;
        dinic<ll> g(n+n+2);
        for (int i = 0; i < n; ++i) {
            g.add_edge(S, i, a[i]);
            g.add_edge(i+n, T, b[i]);
        }
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if(d[i][j] < l){
                    g.add_edge(i, j+n, 1e18);
                }
            }
        }
        cout << g.max_flow(S, T) << endl;
    }
}
accuracy: 1
time_ms: 120
memory_kb: 4516
final_rank: 15

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 210

struct Info{
	Info(int arg_to,int arg_cost){
		to = arg_to;
		cost = arg_cost;
	}
	int to,cost;
};

struct Edge{
	Edge(int arg_to,int arg_capacity,int arg_rev_index){
		to = arg_to;
		capacity = arg_capacity;
		rev_index = arg_rev_index;
	}
	int to,capacity,rev_index;
};

int V,N,M,L,num_Edge;
int source = 0,sink;

vector<Edge> G[NUM];
vector<Info> first_G[NUM];
bool used[NUM];
int min_dist[NUM][NUM];
int flow_dist[NUM];
int cheked_index[NUM];
int P[NUM],K[NUM];


void add_edge(int from,int to,int capacity){
	G[from].push_back(Edge(to,capacity,G[to].size()));
	G[to].push_back(Edge(from,0,G[from].size()-1));
}

void bfs(int source){
	for(int i = 0; i < V; i++)flow_dist[i] = -1;
	queue<int> Q;
	flow_dist[source] = 0;
	Q.push(source);

	while(!Q.empty()){
		int node_id = Q.front();
		Q.pop();
		for(int i = 0; i < G[node_id].size(); i++){
			Edge &e = G[node_id][i];
			if(e.capacity > 0 && flow_dist[e.to] < 0){
				flow_dist[e.to] = flow_dist[node_id]+1;
				Q.push(e.to);
			}
		}
	}
}

int dfs(int node_id,int sink,int flow){
	if(node_id == sink)return flow;

	for(int &i = cheked_index[node_id]; i < G[node_id].size(); i++){
		Edge &e = G[node_id][i];
		if(e.capacity > 0 && flow_dist[node_id] < flow_dist[e.to]){
			int tmp_flow = dfs(e.to,sink,min(flow,e.capacity));
			if(tmp_flow > 0){
				e.capacity -= tmp_flow;
				G[e.to][e.rev_index].capacity += tmp_flow;
				return tmp_flow;
			}
		}
	}
	return 0;
}


int max_flow(int source,int sink){
	int flow = 0,add;
	while(true){
		bfs(source);
		if(flow_dist[sink] < 0)break;
		for(int i = 0; i < V; i++)cheked_index[i] = 0;
		while((add = dfs(source,sink,BIG_NUM)) > 0){
			flow += add;
		}
	}
	return flow;
}


void func(){

	V = 2*N+2;
	sink = 2*N+1;

	for(int i = 0; i <= 2*N+1; i++){
		first_G[i].clear();
		G[i].clear();
	}

	for(int i = 1; i <= N; i++){
		for(int k = 1; k <= N; k++){
			if(i != k){
				min_dist[i][k] = BIG_NUM;
			}else{
				min_dist[i][k] = 0;
			}
		}
	}

	int from,to,cost;
	for(int loop = 0; loop < M; loop++){
		scanf("%d %d %d",&from,&to,&cost);
		first_G[from].push_back(Info(to,cost));
		first_G[to].push_back(Info(from,cost));
		min_dist[from][to] = cost;
		min_dist[to][from] = cost;
	}

	for(int mid = 1; mid <= N; mid++){
		for(int start = 1; start <= N; start++){
			if(min_dist[start][mid] == BIG_NUM)continue;
			for(int end = 1; end <= N; end++){
				if(min_dist[mid][end] == BIG_NUM)continue;
				min_dist[start][end] = min(min_dist[start][end],min_dist[start][mid]+min_dist[mid][end]);
			}
		}
	}

	for(int i = 1; i <= N; i++)scanf("%d",&P[i]);
	for(int i = 1; i <= N; i++)scanf("%d",&K[i]);

	for(int i = 1; i <= N; i++){
		add_edge(source,i,P[i]);
	}

	for(int i = 1; i <= N; i++){
		add_edge(N+i,sink,K[i]);
	}

	for(int i = 1; i <= N; i++){
		for(int k = 1; k <= N; k++){
			if(min_dist[i][k] < L){
				add_edge(i,N+k,P[i]);
			}
		}
	}

	printf("%d\n",max_flow(source,sink));
}

int main(){

	while(scanf("%d %d %d",&N,&M,&L) != EOF){
		func();
	}

	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3524
final_rank: 4

code_snippet: #include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

//#define int long long
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

struct Dinic {
	using Flow = int;
	struct Edge {
		int to, rev;
		Flow cap;
		Edge() {}
		Edge(int to, int rev, Flow cap) :to(to), rev(rev), cap(cap) {}
	};
	int n;
	vector<vector<Edge>> g;
	vector<bool> used;
	vector<int> level;
	vector<int> iter;
	Dinic(int n) :n(n), g(n), used(n), level(n), iter(n) {};
	void addArc(int from, int to, Flow cap) {
		g[from].emplace_back(to, (int)g[to].size(), cap);
		g[to].emplace_back(from, (int)g[from].size() - 1, 0);
	}
	void addEdge(int a, int b, Flow cap) {
		g[a].emplace_back(b, (int)g[b].size(), cap);
		g[b].emplace_back(a, (int)g[a].size() - 1, cap);
	}
	Flow maximumFlow(int s, int t) {
		Flow total = 0;
		while (true) {
			levelize(s);
			if (level[t] < 0)return total;
			fill(iter.begin(), iter.end(), 0);
			Flow f;
			while (true) {
				f = augment(s, t, INF);
				if (f == 0)break;
				total += f;
			}
		}
	}
	Flow augment(int v, int t, Flow f) {
		if (v == t)return f;
		for (int &i = iter[v]; i < g[v].size(); i++) {
			Edge &e = g[v][i];
			if (e.cap > 0 && level[v] < level[e.to]) {
				Flow d = augment(e.to, t, min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					g[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}
	void levelize(int s) {
		fill(level.begin(), level.end(), -1);
		queue<int> q;
		level[s] = 0;
		q.push(s);
		while (q.size()) {
			int v = q.front(); q.pop();
			for (int i = 0; i < g[v].size(); i++) {
				Edge &e = g[v][i];
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					q.push(e.to);
				}
			}
		}
	}
};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	for (int N, M, L; cin >> N >> M >> L&&N;) {
		static const int V = 110;
		static int wf[V][V];
		rep(i, 0, N)rep(j, 0, N)wf[i][j] = INF;
		rep(i, 0, N)wf[i][i] = 0;

		rep(i, 0, M) {
			int s, d, w; cin >> s >> d >> w; s--, d--;
			wf[s][d] = wf[d][s] = min(wf[s][d], w);
		}

		rep(k, 0, N)rep(i, 0, N)rep(j, 0, N) {
			if (wf[i][k] != INF&&wf[k][j] != INF)
				wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j]);
		}

		vector<int> P(N); rep(i, 0, N) {
			cin >> P[i];
		}
		vector<int> K(N); rep(i, 0, N) {
			cin >> K[i];
		}

		Dinic dinic(N + N + 2);
		int s = dinic.n - 2, t = s + 1;

		rep(i, 0, N) {
			dinic.addArc(s, i, P[i]);
			dinic.addArc(i + N, t, K[i]);
		}

		rep(i, 0, N)rep(j, 0, N) {
			if (wf[i][j] < L)
				dinic.addArc(i, N + j, INF);
		}

		cout << dinic.maximumFlow(s, t) << endl;

	}
	return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3368
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

// (?????????,??????,??????)
struct edge{ int to,cap,rev; };

const int MAX_V = 202; // TODO:initialize
const int F_INF = 1010101010; // TODO:initialize
vector<edge> G[MAX_V];
int level[MAX_V]; // s??????????????¢
int iter[MAX_V]; // ???????????§??????????????£??????

void add_edge(int from, int to, int cap){
    G[from].pb({to,cap,(int)G[to].size()});
    G[to].pb({from,0,(int)G[from].size()-1});
}

void dinic_bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
        int v = que.front();
        que.pop();
        rep(i,G[v].size()){
            edge &e = G[v][i];
            if(e.cap>0 && level[e.to]<0){
                level[e.to] = level[v]+1;
                que.push(e.to);
            }
        }
    }
}

// ?¢?????????????dfs??§??¢???
int dinic_dfs(int v, int t, int f){
    if(v==t) return f;
    for(int &i=iter[v]; i<G[v].size(); ++i){
        edge &e=G[v][i];
        if(e.cap>0 && level[v]<level[e.to]){
            int d = dinic_dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

// s??????t???????????§???
int max_flow(int s, int t){
    int flow = 0;
    while(1){
        dinic_bfs(s);
        if(level[t]<0) return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dinic_dfs(s,t,F_INF))>0) flow+=f;
    }
}

const int INF = 123456789;

int main()
{
    int n,m,L;
    while(cin >>n >>m >>L)
    {
        rep(i,MAX_V) G[i].clear();

        int d[100][100];
        fill(d[0],d[100],INF);
        rep(i,m)
        {
            int a,b,cost;
            scanf(" %d %d %d", &a, &b, &cost);
            --a;
            --b;
            d[a][b] = d[b][a] = cost;
        }

        rep(i,n) d[i][i]=0;
        rep(k,n)rep(i,n)rep(j,n) d[i][j] = min(d[i][j], d[i][k]+d[k][j]);

        int S = 2*n, T = S+1;
        rep(i,n)
        {
            int p;
            scanf(" %d", &p);
            add_edge(S,i,p);
        }
        rep(i,n)
        {
            int k;
            scanf(" %d", &k);
            add_edge(n+i,T,k);
        }

        rep(i,n)rep(j,n)
        {
            if(d[i][j]<L)
            {
                add_edge(i,n+j,INF);
                add_edge(j,n+i,INF);
            }
        }

        printf("%d\n", max_flow(S,T));
    }
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3688
final_rank: 9

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) begin(v), end(v)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, s, n) for(int i = (int)(s); i < (int)(n); i++)
#define min(...) min({__VA_ARGS__})
#define max(...) max({__VA_ARGS__})

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

using pint = pair<int, int>;
using tint = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

struct Dinic {
  struct edge {
    int to, cap, rev;
    edge(){}
    edge(int to, int cap, int rev):to(to), cap(cap), rev(rev){}
  };
  vector< vector<edge> > graph;
  vector<int> level, iter;
  Dinic(int V):graph(V), level(V), iter(V){}
  void add_edge(int from, int to, int cap) {
    graph[from].emplace_back(to, cap, graph[to].size());
    graph[to].emplace_back(from, 0, graph[from].size()-1);
  }
  void bfs(int s) {
    fill(all(level), -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while(que.size()) {
      int v = que.front(); que.pop();
      for(edge& e : graph[v]) {
	if(e.cap > 0 && level[e.to] < 0) {
	  level[e.to] = level[v] + 1;
	  que.push(e.to);
	}
      }
    }
  }
  int dfs(int v, int t, int f) {
    if(v == t) return f;
    for(int &i = iter[v]; i < graph[v].size(); i++) {
      edge& e = graph[v][i];
      if(e.cap > 0 && level[v] < level[e.to]) {
	int d = dfs(e.to, t, min(e.cap, f));
	if(d > 0) {
	  e.cap -= d;
	  graph[e.to][e.rev].cap += d;
	  return d;
	}
      }
    }
    return 0;
  }
  int max_flow(int s, int t) {
    int flow = 0;
    for(;;) {
      bfs(s);
      if(level[t] < 0) return flow;
      fill(all(iter), 0);
      int f; while((f = dfs(s, t, inf)) > 0) flow += f;
    }
  }
};

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N, M, L;
  while(cin >> N >> M >> L) {
    int wf[101][101];
    fill(wf[0], wf[101], inf);
    rep(i, N) wf[i][i] = 0;
    rep(i, M) {
      int a, b, d;
      cin >> a >> b >> d;
      --a, --b;
      wf[a][b] = wf[b][a] = d;
    }

    rep(i, N) rep(j, N) rep(k, N) {
      chmin(wf[j][k], wf[j][i] + wf[i][k]);
    }

    int P[101], K[101];
    rep(i, N) cin >> P[i];
    rep(i, N) cin >> K[i];

    int s = 2*N, t = s + 1, V = t + 1;
    Dinic graph(V);
    rep(i, N) {
      graph.add_edge(s, i, P[i]);
      rep(j, N) {
	if(wf[i][j] < L) graph.add_edge(i, N+j, inf);
      }
      graph.add_edge(N+i, t, K[i]);
    }
    cout << graph.max_flow(s, t) << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 3644
final_rank: 11

code_snippet: #include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <cassert>
using namespace std;


using Weight = int;
using Capacity = int;
struct Edge {
	int src, dst; Weight weight; Capacity cap;
	Edge(int s, int d, Weight w, Capacity c) : src(s), dst(d), weight(w), cap(c) {}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

struct Dinic {
	int n, s, t;
	vector<int> level, prog, que;
	vector<vector<Capacity>> cap, flow;
	vector<vector<int>> g;
	Capacity inf;
	Dinic(const Graph &graph)
		: n(graph.size()),
		cap(n, vector<Capacity>(n)), flow(n, vector<Capacity>(n)),
		g(n, vector<int>()), inf(numeric_limits<Capacity>::max() / 8) {
		for(int i = 0; i < n; i++) {
			for(auto &e : graph[i]) {
				int u = e.src, v = e.dst;
				Capacity c = e.cap;
				cap[u][v] += c; cap[v][u] += c; flow[v][u] += c;
				g[u].push_back(v); g[v].push_back(u);
			}
		}
	}
	inline Capacity residue(int u, int v) { return cap[u][v] - flow[u][v]; }
	Capacity solve(int s_, int t_) {
		this->t = t_, this->s = s_;
		que.resize(n + 1);
		Capacity res = 0;
		while(levelize()) { prog.assign(n, 0); res += augment(s, inf); }
		return res;
	}
	bool levelize() {
		int l = 0, r = 0;
		level.assign(n, -1); level[s] = 0; que[r++] = s;
		while(l != r) {
			int v = que[l++]; if(v == t) break;
			for(const int &d : g[v]) if(level[d] == -1 && residue(v, d) != 0) {
				level[d] = level[v] + 1; que[r++] = d;
			}
		}
		return level[t] != -1;
	}
	Capacity augment(int v, Capacity lim) {
		Capacity res = 0;
		if(v == t) return lim;
		for(int &i = prog[v]; i < (int)g[v].size(); i++) {
			const int &d = g[v][i];
			if(residue(v, d) == 0 || level[v] >= level[d]) continue;
			const Capacity aug = augment(d, min(lim, residue(v, d)));
			flow[v][d] += aug; flow[d][v] -= aug;
			res += aug; lim -= aug;
			if(lim == 0) break;
		}
		return res;
	}
};


const int INF = 1 << 25;
int wf[100][100];
int A[10000];
int B[10000];
int D[10000];
int P[100];
int K[100];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N, M, L;
	while(cin >> N >> M >> L) {
		for(int i = 0; i < N; i++)
			for(int j = 0; j < N; j++)
				wf[i][j] = i == j ? 0 : INF;

		for(int i = 0; i < M; i++) {
			cin >> A[i] >> B[i] >> D[i];
			A[i]--, B[i]--;
			wf[A[i]][B[i]] = wf[B[i]][A[i]] = D[i];
		}
		for(int i = 0; i < N; i++) {
			cin >> P[i];
		}
		for(int i = 0; i < N; i++) {
			cin >> K[i];
		}

		for(int k = 0; k < N; k++)
			for(int i = 0; i < N; i++)
				for(int j = 0; j < N; j++)
					wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j]);

		Graph G(N + N + 2);
		int S = N + N, T = N + N + 1;
		for(int i = 0; i < N; i++) {
			G[i].push_back(Edge(S, i, 0, P[i]));
			G[N + i].push_back(Edge(N + i, T, 0, K[i]));
			for(int j = 0; j < N; j++) {
				if(wf[i][j] < L) {
					G[i].push_back(Edge(i, N + j, 0, P[i]));
				}
			}
		}

		Dinic dinic(G);
		cout << dinic.solve(S, T) << endl;
	}
}
accuracy: 1
time_ms: 120
memory_kb: 3524
final_rank: 7

code_snippet: #include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#include<sstream>
#include<iomanip>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
using namespace std;
//kaewasuretyuui
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int,pii> pip;
typedef vector<pip>vip;
const double PI=acos(-1);
const double EPS=1e-8;
const int inf=1e8;
struct edge{int to,cap,rev;};//ikisaki youryou gyakuhen
class MF{//max flow
	public:
	int n;
	vector<vector<edge> >G;//[MAX];
	vector<bool>used;//[MAX];
	MF(int size){
		n=size;
		G=vector<vector<edge> >(n);
	}
	void add_edge(int from, int to, int cap){
		edge q={to,cap,int(G[to].size())};
		G[from].push_back(q);
		q={from,0,int(G[from].size()-1)};
		G[to].push_back(q);
	}
	int dfs(int v,int t, int f) {
		if(v==t)return f;
		used[v]=1;
		for(int i=0;i<G[v].size();i++){
			edge &e=G[v][i];
			if(used[e.to]||e.cap<=0) continue;
			int d =dfs(e.to,t,min(f,e.cap));
			if(d>0){
				e.cap-=d;
				G[e.to][e.rev].cap+=d;
				return d;
			}
		}
		return 0;
	}
	int ff(int s,int t) {//from s to t
		int flow=0,f;
		while(1){
			used=vector<bool>(n,false);
			f=dfs(s,t,inf);
			if(f==0)return flow;
			flow+=f;
		}
	}
};
int main(){
	int n,m,l;	
	while(cin>>n>>m>>l){
		vvi cost(n,vi(n,inf));
		rep(i,n)cost[i][i]=0;
		while(m--){
			int a,b,c;
			cin>>a>>b>>c;
			a--;b--;
			cost[a][b]=cost[b][a]=c;
		}
		rep(k,n)rep(i,n)rep(j,n)
			cost[i][j]=min(cost[i][j],cost[i][k]+cost[k][j]);
		MF mf(n*2+2);
		int s=n*2,t=s+1;
		rep(i,n){
			int a;
			cin>>a;
			mf.add_edge(s,i,a);
		}
		rep(i,n){
			int a;
			cin>>a;
			mf.add_edge(i+n,t,a);
		}
		rep(i,n)rep(j,n)if(cost[i][j]<l)mf.add_edge(i,j+n,inf);
		cout<<mf.ff(s,t)<<endl;
	}
}
accuracy: 1
time_ms: 2110
memory_kb: 1532
final_rank: 19

code_snippet: #include <bits/stdc++.h>

using namespace std;

#define MAX_V 252
#define INF (1<<29)

struct Edge {
    int to, cap, rev;
    Edge(int to, int cap, int rev) :
        to(to), cap(cap), rev(rev) {}
};

vector<Edge> G[MAX_V];
int level[MAX_V], iter[MAX_V];

void add_edge(int from, int to, int cap)
{
    G[from].push_back(Edge(to, cap, G[to].size()));
    G[to].push_back(Edge(from, 0, G[from].size()-1));
}

void bfs(int s)
{
    memset(level, -1, sizeof(level));
    queue<int> Q;
    level[s] = 0;
    Q.push(s);
    while (!Q.empty()) {
	int v = Q.front(); Q.pop();
	for (int i = 0; i < (int)G[v].size(); i++) {
	    Edge &e = G[v][i];
	    if (e.cap > 0 && level[e.to] < 0) {
		level[e.to] = level[v] + 1;
		Q.push(e.to);
	    }
	}
    }
}

int dfs(int v, int t, int f)
{
    if (v == t) return f;
    for (int &i = iter[v]; i < (int)G[v].size(); i++) {
	Edge &e = G[v][i];
	if (e.cap > 0 && level[v] < level[e.to]) {
	    int d = dfs(e.to, t, min(f, e.cap));
	    if (d > 0) {
		e.cap -= d;
		G[e.to][e.rev].cap += d;
		return d;
	    }
	}
    }
    return 0;
}

int max_flow(int s, int t)
{
    int flow = 0;
    for (;;) {
	bfs(s);
	if (level[t] < 0) return flow;
	memset(iter, 0, sizeof(iter));
	int f;
	while ((f = dfs(s, t, INF)) > 0) {
	    flow += f;
	}
    }
}

void init()
{
    for (int i = 0; i < MAX_V; i++) {
        G[i].clear();
    }
}  

int main()
{
    int N, M, L;
    while (cin >> N >> M >> L) {
        int d[N][N];
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                d[i][j] = INF;
            }
            d[i][i] = 0;
        }

        init();
        
        int a, b, c;
        for (int i = 0; i < M; i++) {
            cin >> a >> b >> c; a--; b--;
            d[a][b] = d[b][a] = c;
        }

        for (int k = 0; k < N; k++) {
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < N; j++) {
                    d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
                }
            }
        }
        
        vector<int> P(N), K(N);
        for (int i = 0; i < N; i++) {
            cin >> P[i];
        }
        for (int i = 0; i < N; i++) {
            cin >> K[i];
        }
        
        int S = 2*N, T = S + 1;
        for (int i = 0; i < N; i++) {
            add_edge(S, i, P[i]);
            for (int j = 0; j < N; j++) {
                if (d[i][j] < L) {
                    add_edge(i, j + N, INF);
                }
            }
            add_edge(i + N, T, K[i]);
        }
        cout << max_flow(S, T) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 3232
final_rank: 10

code_snippet: #include <cstdio>
#include <cstring>
#include <queue>
using namespace std;

const int N = 205, M = 50000, inf = 0x3fffffff;
int n, m, l, x, y, z, e, s, t, d[N][N], hd[N], to[M], f[M], nxt[M], ch[N];

void add(int x, int y, int z) {
	to[e] = y, nxt[e] = hd[x], f[e] = z, hd[x] = e++;
	to[e] = x, nxt[e] = hd[y], f[e] = 0, hd[y] = e++;
}

bool tell() {
	memset(ch, -1, sizeof ch);
	queue<int> q;
	ch[s] = 0;
	q.push(s);
	while(!q.empty()) {
		int u = q.front(); q.pop();
		for(int i = hd[u]; ~i; i = nxt[i]) if(ch[to[i]] == -1 && f[i])
			ch[to[i]] = ch[u] + 1, q.push(to[i]);
	}
	return ch[t] != -1;
}

int zeng(int a, int b) {
	if(a == t) return b;
	int r = 0;
	for(int i = hd[a]; ~i && b > r; i = nxt[i]) if(ch[to[i]] == ch[a] + 1 && f[i]) {
		int t = zeng(to[i], min(b-r, f[i]));
		f[i] -= t, r += t, f[i^1] += t;
	}
	if(!r) ch[a] = -1;
	return r;
}

int dinic() {
	int r = 0, t;
	while(tell()) while(t = zeng(s, inf)) r += t;
	return r;
}

int main() {
	while(~scanf("%d%d%d", &n, &m, &l)) {
		e = 0, t = n*2+1;
		memset(d, 0x3f, sizeof d);
		memset(hd, -1, sizeof hd);
		for(int i = 1; i <= n; i++) d[i][i] = 0;
		while(m--) scanf("%d%d%d", &x, &y, &z), d[x][y] = d[y][x] = z;
		for(int k = 1; k <= n; k++)
		for(int i = 1; i <= n; i++)
		for(int j = 1; j <= n; j++)
			d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
		for(int i = 1; i <= n; i++) {
			scanf("%d", &x);
			add(0, i, x);
			for(int j = 1; j <= n; j++) if(d[i][j] < l) add(i, n+j, inf);
		}
		for(int i = 1; i <= n; i++) scanf("%d", &x), add(n+i, t, x);
		printf("%d\n", dinic());
	}
	return 0;
}
accuracy: 1
time_ms: 140
memory_kb: 1456
final_rank: 1

code_snippet: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;

namespace Dinic {
	const int mxn=5000, mxm=500000;
	int S, T;
	int head[mxn];
	int next[mxm], to[mxm], flow[mxm];
	inline void addedge(int idx, int a, int b, int f)
	{
		next[idx]=head[a]; head[a]=idx;
		to[idx]=b; flow[idx]=f;
	}
	int newm;
	inline void add(int a, int b, int f)
	{
		addedge((++newm)<<1, a, b, f);
		addedge(newm<<1|1, b, a, 0);
	}
	inline void init()
	{
		memset(head, 0, sizeof(head));
		newm=0;
	}
	int ch[mxn];
	inline bool tell()
	{
		memset(ch, -1, sizeof(ch));
		ch[S]=1;
		queue<int> q;
		q.push(S);
		while(!q.empty())
		{
			int u=q.front(); q.pop();
			for(int i=head[u]; i; i=next[i])
			{
				const int v=to[i];
			//printf("->%d %d %d\n", v, ch[v], flow[i]);
				if(ch[v]>0 || !flow[i]) continue;
				ch[v]=ch[u]+1;
				q.push(v);
			}
		}
		return ch[T]>0;
	}
	int expand(int a, int b)
	{
		//printf("expanding %d, %d\n", a, b);
		if(a==T) return b;
		int r=0, c;
		for(int i=head[a]; i && r<b; i=next[i])
		{
			int v=to[i];
			if(ch[v]!=ch[a]+1 || !flow[i]) continue;
			c=expand(v, min(b-r, flow[i]));
			r+=c;
			flow[i]-=c, flow[i^1]+=c;
		}
		return r;
	}
	inline int dinic(int sS, int tT)
	{
		S=sS, T=tT;
		int r=0, c;
		while(tell()) while(c=expand(S, 0x3f3f3f3f)) r+=c;
		return r;
	}
}

const int maxn=100+5;
int mx[maxn][maxn];


int main()
{
	int n, m, l; 
	while(scanf("%d%d%d", &n, &m, &l)==3)
	{
		memset(mx, 0x3f, sizeof(mx));
		for(int i=1; i<=n; ++i) mx[i][i]=0;
		for(int i=1; i<=m; ++i)
		{
			int x, y, l; scanf("%d%d%d", &x, &y, &l);
			mx[y][x]=mx[x][y]=min(mx[x][y], l);
		}
		for(int k=1; k<=n; ++k) for(int i=1; i<=n; ++i)
			for(int j=1; j<=n; ++j) mx[i][j]=min(mx[i][j], mx[i][k]+mx[k][j]);
		Dinic::init();
		for(int i=1; i<=n; ++i)
		{
			int x; scanf("%d", &x);
			Dinic::add(1, i+5, x);
		}
		for(int i=1; i<=n; ++i)
		{
			int x; scanf("%d", &x);
			Dinic::add(i+n+10, 2, x);
		}
		for(int i=1; i<=n; ++i) for(int j=1; j<=n; ++j)
		{
			if(mx[i][j]<l) 
			{
				Dinic::add(i+5, j+n+10, 0x3f3f3f3f);
				//printf("adding..%d %d\n", i, j);
			}
		}
		printf("%d\n", Dinic::dinic(1, 2));
	}
	
	return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 3280
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;
const int N = 1100;
const int INF = 0x3f3f3f3f;

struct Node{
	int to, cap, rev;
};

vector<Node> v[N];
bool used[N];

void add_Node(int from, int to, int cap){
	v[from].push_back((Node) { to, cap, v[to].size() });
	v[to].push_back((Node) { from, 0, v[from].size() - 1 });
}

int dfs(int s, int t, int f){
	if (s == t)
		return f;
	used[s] = true;
	for (int i = 0; i<v[s].size(); i++)	{
		Node &tmp = v[s][i];
		if (used[tmp.to] == false && tmp.cap>0){
			int d = dfs(tmp.to, t, min(f, tmp.cap));
			if (d>0){
				tmp.cap -= d;
				v[tmp.to][tmp.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}

int max_flow(int s, int t){
	int flow = 0;
	while (true) {
		memset(used, false, sizeof(used));
		int f = dfs(s, t, INF);
		if (f == 0)
			return flow;
		flow += f;
	}
}

int n, m, l;
int a[N][N], b[N], c[N];

int main(){
	while (~scanf("%d%d%d", &n, &m, &l)) {
		int s = n * 2 + 1, t = n * 2 + 2;
		memset(v, 0, sizeof(v));
		memset(a, 0x3f, sizeof(a));
		for (int i = 0; i < N; i++)
			a[i][i] = 0;
		for (int i = 0; i < N; i++)
			v[i].clear();
		for (int i = 0, x, y, d; i < m; i++) {
			scanf("%d%d%d", &x, &y, &d);
			a[x][y] = a[y][x] = min(d, a[x][y]);
		}
		for (int k = 1; k <= n; k++)
			for (int i = 1; i <= n; i++)
				for (int j = 1; j <= n; j++)
					a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
		for (int i = 1; i <= n; i++)
			scanf("%d", &b[i]);
		for (int i = 1; i <= n; i++)
			scanf("%d", &c[i]);
		for (int i = 1; i <= n; i++)
			add_Node(s, i, b[i]), add_Node(n + i, t, c[i]);
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= n; j++)
				if (a[i][j] < l)
					add_Node(i, n + j, b[i]);
		printf("%d\n", max_flow(s, t));
	}
}
accuracy: 1
time_ms: 1460
memory_kb: 28520
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
//typedef tuple<int, int, int> T;
#define FOR(i,s,x) for(int i=s;i<(int)(x);i++)
#define REP(i,x) FOR(i,0,x)
#define ALL(c) c.begin(), c.end()
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl

#define INF 1<<29

template <typename T>
struct MaxFlow {
  struct Edge {
    int to, rev; T cap;
    Edge(int to, int rev, T cap) : to(to), rev(rev), cap(cap) { }
  };

  typedef vector<Edge> Edges;
  vector<Edges> G;
  int V;
  vector<int> level, iter;

  MaxFlow(int V) : V(V) { G.resize(V); }

  void add_edge(int from, int to, T cap) {
    G[from].push_back(Edge(to, (int)G[to].size(), cap));
    G[to].push_back(Edge(from, (int)G[from].size()-1, 0));
  }

  void bfs(int source) {
    level.assign(V, -1);
    queue<int> que;
    que.push(source);
    level[source] = 0;
    while (!que.empty()) {
      int v = que.front(); que.pop();
      for (int i = 0; i < (int)G[v].size(); i++) {
	Edge &e = G[v][i];
	if (e.cap > 0 && level[e.to] < 0) {
	  level[e.to] = level[v] + 1;
	  que.push(e.to);
	}
      }
    }
  }

  T dfs(int v, int sink, T flow) {
    if (v == sink) return flow;
    for (int &i = iter[v]; i < (int)G[v].size(); i++) {
      Edge &e = G[v][i];
      if (e.cap > 0 && level[v] < level[e.to]) {
	T d = dfs(e.to, sink, min(e.cap, flow));
	if (d > 0) {
	  e.cap -= d;
	  G[e.to][e.rev].cap += d;
	  return d;
	}
      }
    }
    return 0;
  }

  T dinic(int source, int sink) {
    T flow = 0;
    while (true) {
      bfs(source);
      if (level[sink] < 0) return flow;
      iter.assign(V, 0);
      T f;
      while ((f = dfs(source, sink, INF)) > 0) {
	flow += f;
      }
    }
  }
};

int main() {

  int N;
  while (scanf("%d", &N) != EOF) {
    int M, L; scanf("%d %d", &M, &L);
    vector<vector<int>> Adj(N, vector<int>(N, INF));
    REP(i, N) Adj[i][i] = 0;
    REP(i, M) {
      int A, B, D; scanf("%d %d %d", &A, &B, &D); A--, B--;
      Adj[A][B] = Adj[B][A] = D;
    }
    REP(k, N) REP(i, N) REP(j, N) Adj[i][j] = min(Adj[i][j], Adj[i][k] + Adj[k][j]);
    MaxFlow<int> mf(2*N+2);
    REP(i, N) {
      int P; scanf("%d", &P);
      mf.add_edge(2*N, i, P);
    }
    REP(i, N) {
      int K; scanf("%d", &K);
      mf.add_edge(i+N, 2*N+1, K);
    }
    REP(i, N) REP(j, N) {
      if (Adj[i][j] < L) {
	mf.add_edge(i, j+N, INF);
      }
    }
    printf("%d\n", mf.dinic(2*N, 2*N+1));
  }
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3320
final_rank: 5

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <complex>
#include <assert.h>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;

static const double EPS = 1e-12;

static const int tx[] = {+0,+1,+0,-1};
static const int ty[] = {-1,+0,+1,+0};

class Edge{
public:
  int _to;
  int _capacity;
  int _reverse;
  Edge(int to,int capacity,int reverse) :
    _to(to), _capacity(capacity), _reverse(reverse) {}
};

class FordFulkerson {
private:
  int _n;
  bool *_used;
  vector<Edge>* _edges;
  void fill_used(){
    fill(_used,_used + _n ,false);
  }
public:
  ~FordFulkerson(){
    delete[] _edges;
    delete[] _used;
  }

  FordFulkerson(int n){
    _n = n;
    _edges = new vector<Edge>[n];
    _used = new bool[n]();
  }

  void add_edge(int from,int to,int capacity){
    _edges[from].push_back(Edge(to,capacity,_edges[to].size()));
    _edges[to].push_back(Edge(from,0,_edges[from].size()-1));
  }

  int dfs(int current,int sink,int flow){
    if(current == sink) return flow;
    _used[current] = true;
    for(int i=0;i<_edges[current].size();i++){
      Edge& e = _edges[current][i];
      int to = e._to;
      if(_used[to] || e._capacity <= 0) continue;

      int d = dfs(to,sink,min(flow,e._capacity));
      if(d <= 0) continue;
      
      e._capacity -= d;
      _edges[to][e._reverse]._capacity += d;
      return d;
    }
    
    return 0;
  }

  int compute_max_flow(int source,int sink){
    int res = 0;
    while(true){
      fill_used();
      int tmp = dfs(source,sink,INF);
      if(tmp == 0) break;
      res += tmp;
    }
    return res;
  }
};

int main(){
  int num_of_domes;
  int num_of_pipelines;
  int day_limit;

  while(~scanf("%d %d %d",&num_of_domes,&num_of_pipelines,&day_limit)){
    int dp[101][101];
    memset(dp,0x3f,sizeof(dp));

    for(int pipeline_i = 0; pipeline_i < num_of_pipelines; pipeline_i++){
      int from,to,cost;
      scanf("%d %d %d",&from,&to,&cost);
      from--; to--;
      dp[from][to] = cost;
      dp[to][from] = cost;
    }

    for(int from = 0; from < num_of_domes; from++){
      int to = from;
      dp[from][to] = 0;
    }

    for(int mid = 0; mid < num_of_domes; mid++){
      for(int from = 0; from < num_of_domes; from++){
        for(int to = 0; to < num_of_domes; to++){
          dp[from][to] = min(dp[from][to], dp[from][mid] + dp[mid][to]);
        }
      }
    }

    FordFulkerson ford_fulkerson(num_of_domes * 2 + 2);
    for(int dome_i = 0; dome_i < num_of_domes; dome_i++){
      int current_reside_citizens;
      scanf("%d",&current_reside_citizens);
      ford_fulkerson.add_edge(num_of_domes * 2,dome_i,current_reside_citizens);
    }

    for(int dome_i = 0; dome_i < num_of_domes; dome_i++){
      int capacity;
      scanf("%d",&capacity);
      ford_fulkerson.add_edge(num_of_domes + dome_i,num_of_domes * 2 + 1,capacity);
    }

    for(int from = 0; from < num_of_domes; from++){
      for(int to = 0; to < num_of_domes; to++){
        if(dp[from][to] < day_limit){
          ford_fulkerson.add_edge(from,num_of_domes + to,INF);
        }
      }
    }
    printf("%d\n",ford_fulkerson.compute_max_flow(num_of_domes * 2,num_of_domes * 2 + 1));
  }
}
accuracy: 1
time_ms: 1700
memory_kb: 1540
final_rank: 18

code_snippet: #include<bits/stdc++.h>
#define rep(i,a) for(int i=0;i<(int)(a);i++)
#define pb push_back
#define sz size()
using namespace std;
typedef vector<int> vi;

const int INF = 1e9;

struct edge{
  int from,to,cap,rev;
  edge(int a,int b,int c,int d)
    :from(a),to(b),cap(c),rev(d){}
};

struct dinic{
  int n;
  vector< vector<edge> > graph;
  vi level,iter;

  dinic(int a=0):n(a){graph.resize(a);}

  void AddEdge(int s,int g,int c){
    graph[s].pb( edge(s,g,c,graph[g].sz) );
    graph[g].pb( edge(g,s,0,graph[s].sz-1) );
  }

  void DeleteEdge(int s, int g){
    int id = -1, rev = -1;
    rep(e,graph[s].sz){
      if(graph[s][e].to == g){
	id = e; rev = graph[s][e].rev;
      }
    }
    if(id<0)return;

    swap(graph[s][id],graph[s].back());
    swap(graph[g][rev],graph[g].back());
    graph[s][id].rev = rev;
    graph[g][rev].rev = id;
    graph[s].pop_back(); graph[g].pop_back();
  }

  void bfs(int s){
    level = vi(n,-1);
    level[s] = 0;
    queue<int> q; q.push(s);
    while(q.sz){
      int v = q.front(); q.pop();
      for(edge e: graph[v]){
	if(e.cap > 0 && level[e.to] < 0){
	  level[e.to] = level[v] + 1;
	  q.push(e.to);
	}
      }
    }
  }
  
  int dfs(int v, int t, int f){
    if(v==t)return f;
    for(int &i = iter[v];i<(int)graph[v].sz;i++){
      edge &e = graph[v][i];
      if(e.cap > 0 && level[v] < level[e.to]){
	int d = dfs(e.to,t,min(f,e.cap));
	if(d > 0){
	  e.cap -= d;
	  graph[e.to][e.rev].cap += d;
	  return d;
	}
      }
    }
    return 0;
  }
  
  int max_flow(int s, int t){
    int res = 0;
    for(;;){
      bfs(s);
      if(level[t]<0)return res;
      iter = vi(n,0);
      int f;
      while((f=dfs(s,t,INF))>0)res += f;
    }
  }
};

int main(){
  int n,m,l;
  cin.tie(); ios::sync_with_stdio(0);
  while(cin >> n >> m >> l){
    vector< vector<int> > d(n, vector<int>(n,INF));
    rep(i,n)d[i][i] = 0;

    rep(i,m){
      int a,b,c;
      cin >> a >> b >> c; a--; b--;
      d[a][b] = d[b][a] = c;
    }

    rep(k,n)rep(i,n)rep(j,n)d[i][j] = min(d[i][j], d[i][k]+d[k][j]);

    int V = 2*n+2, S = V-2, T = V-1;
    dinic mf(V);
    rep(i,n){
      int p; cin >> p;
      mf.AddEdge(S,i,p);
    }
    rep(i,n){
      int k; cin >> k;
      mf.AddEdge(i+n,T,k);
    }

    rep(i,n)rep(j,n){
      if(d[i][j]<l)mf.AddEdge(i,j+n,INF);
    }

    cout << mf.max_flow(S,T) << endl;
  }
}
accuracy: 1
time_ms: 180
memory_kb: 1680
final_rank: 2

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100;
const int MAX_GN = MAX_N * 2 + 2;
const int INF = 1 << 29;

/* typedef */

typedef vector<int> vi;

/* global variables */

int n, m, l;
int ps[MAX_N], ks[MAX_N], dists[MAX_N][MAX_N];
vi rnbrs[MAX_GN];
int minfs[MAX_GN], prvs[MAX_GN];
int caps[MAX_GN][MAX_GN], flows[MAX_GN][MAX_GN];

/* subroutines */

/* main */

int main() {
  for (;;) {
    cin >> n >> m >> l;
    if (cin.eof()) break;

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) dists[i][j] = INF;
      dists[i][i] = 0;
    }
    
    for (int i = 0; i < m; i++) {
      int a, b, d;
      cin >> a >> b >> d;
      a--, b--;
      dists[a][b] = dists[b][a] = d;
    }

    for (int i = 0; i < n; i++) cin >> ps[i];
    for (int i = 0; i < n; i++) cin >> ks[i];

    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
	for (int j = 0; j < n; j++) {
	  int d0 = dists[i][k] + dists[k][j];
	  if (dists[i][j] > d0) dists[i][j] = d0;
	}
    
    int gn = 2 * n + 2;
    int st = 2 * n, gl = 2 * n + 1;

    for (int i = 0; i < gn; i++) rnbrs[i].clear();

    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
	if (dists[i][j] < l) {
	  rnbrs[i].push_back(j + n);
	  rnbrs[j + n].push_back(i);
	  caps[i][j + n] = INF;
	  caps[j + n][i] = 0;
	}
    
    for (int i = 0; i < n; i++) {
      rnbrs[st].push_back(i);
      caps[st][i] = ps[i];
      caps[i][st] = 0;
      rnbrs[i + n].push_back(gl);
      caps[i + n][gl] = ks[i];
      caps[gl][i + n] = 0;
    }

    memset(flows, 0, sizeof(flows));

    int max_flow = 0;

    for(;;) {
      for (int i = 0; i < gn; i++) prvs[i] = -1;
      prvs[st] = st;
      minfs[st] = INF;

      queue<int> q;
      q.push(st);

      while (! q.empty()) {
	int ui = q.front();
	q.pop();

	if (ui == gl) break;

	vi& rnbru = rnbrs[ui];
	for (vi::iterator vit = rnbru.begin(); vit != rnbru.end(); vit++) {
	  int vi = *vit;
	  int vc = caps[ui][vi] - flows[ui][vi];
	  if (prvs[vi] < 0 && vc > 0) {
	    prvs[vi] = ui;
	    minfs[vi] = (minfs[ui] < vc) ? minfs[ui] : vc;
	    q.push(vi);
	  }
	}
      }

      if (prvs[gl] < 0) break;

      int min_flow = minfs[gl];
      
      for (int j = gl; j != st;) {
	int i = prvs[j];
	flows[i][j] += min_flow;
	flows[j][i] -= min_flow;
	j = i;
      }

      max_flow += min_flow;
    }

    cout << max_flow << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 510
memory_kb: 1704
final_rank: 16

code_snippet: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100;
const int MAX_GN = MAX_N * 2 + 2;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

/* global variables */

int n, m, l;
vpii nbrs[MAX_N];
int ps[MAX_N], ks[MAX_N];
vi rnbrs[MAX_GN];
int dists[MAX_GN], prvs[MAX_GN];
int caps[MAX_GN][MAX_GN], flows[MAX_GN][MAX_GN];

/* subroutines */

/* main */

int main() {
  for (;;) {
    cin >> n >> m >> l;
    if (cin.eof()) break;

    for (int i = 0; i < n; i++) nbrs[i].clear();
    
    for (int i = 0; i < m; i++) {
      int a, b, d;
      cin >> a >> b >> d;
      a--, b--;
      nbrs[a].push_back(pii(b, d));
      nbrs[b].push_back(pii(a, d));
    }

    for (int i = 0; i < n; i++) cin >> ps[i];
    for (int i = 0; i < n; i++) cin >> ks[i];

    int gn = 2 * n + 2;
    for (int i = 0; i < gn; i++) rnbrs[i].clear();

    for (int st = 0; st < n; st++) {
      for (int i = 0; i < n; i++) dists[i] = INF;
      dists[st] = 0;
      rnbrs[st].push_back(st + n);
      rnbrs[st + n].push_back(st);
      caps[st][st + n] = INF;
      caps[st + n][st] = 0;
      
      priority_queue<pii,vpii,greater<pii> > q;
      q.push(pii(0, st));

      while (! q.empty()) {
	pii u = q.top();
	q.pop();

	int ud = u.first;
	int ui = u.second;
	if (ud != dists[ui]) continue;

	vpii& nbru = nbrs[ui];
	for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
	  int vi = vit->first;
	  int vd = ud + vit->second;
	  if (vd < l && dists[vi] > vd) {
	    dists[vi] = vd;
	    q.push(pii(vd, vi));
	    rnbrs[st].push_back(vi + n);
	    rnbrs[vi + n].push_back(st);
	    caps[st][vi + n] = INF;
	    caps[vi + n][st] = 0;
	  }
	}
      }
    }

    int st = 2 * n, gl = 2 * n + 1;

    for (int i = 0; i < n; i++) {
      rnbrs[st].push_back(i);
      caps[st][i] = ps[i];
      caps[i][st] = 0;
      rnbrs[i + n].push_back(gl);
      caps[i + n][gl] = ks[i];
      caps[gl][i + n] = 0;
    }
    memset(flows, 0, sizeof(flows));

    int max_flow = 0;

    for(;;) {
      for (int i = 0; i < gn; i++) dists[i] = INF, prvs[i] = -1;
      dists[st] = 0;

      priority_queue<pii,vpii,greater<pii> > q;
      q.push(pii(0, st));

      while (! q.empty()) {
	pii u = q.top();
	q.pop();

	int ud = u.first;
	int ui = u.second;
	if (ud != dists[ui]) continue;
	if (ui == gl) break;

	int vd = ud + 1;
	vi& rnbru = rnbrs[ui];
	for (vi::iterator vit = rnbru.begin(); vit != rnbru.end(); vit++) {
	  int vi = *vit;
	  int vc = caps[ui][vi] - flows[ui][vi];
	  if (vc > 0 && dists[vi] > vd) {
	    dists[vi] = vd;
	    prvs[vi] = ui;
	    q.push(pii(vd, vi));
	  }
	}
      }

      if (dists[gl] >= INF) break;

      int min_flow = INF;
      for (int i = gl; i != st; i = prvs[i]) {
	int& i0 = prvs[i];
	int flow = caps[i0][i] - flows[i0][i];
	if (min_flow > flow) min_flow = flow;
      }

      for (int i = gl; i != st; i = prvs[i]) {
	int& i0 = prvs[i];
	flows[i0][i] += min_flow;
	flows[i][i0] -= min_flow;
      }

      max_flow += min_flow;
    }

    cout << max_flow << endl;
  }

  return 0;
}
accuracy: 1
time_ms: 1010
memory_kb: 2096
final_rank: 17