p326.ans1
======================
YES

=================
p326.ans2
======================
NO

=================
p326.cpp
======================
#include <bits/stdc++.h>
// #include <coding_library/graph/maxflow.hpp>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

template<class T>
class MaxFlow {
  private:
    struct Edge {
        T flow, cap;
        int idx, rev, to;
        Edge(int _to, int _rev, T _flow, T _cap, int _idx)
            : to(_to), rev(_rev), flow(_flow), cap(_cap), idx(_idx) {}
    };

    vector<int> dist, po;
    int n;

    bool bfs(int s, int t) {
        fill(dist.begin(), dist.end(), -1);
        fill(po.begin(), po.end(), 0);

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

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

            for(Edge e: adj[u]) {
                if(dist[e.to] == -1 && e.flow < e.cap) {
                    dist[e.to] = dist[u] + 1;
                    q.push(e.to);
                }
            }
        }
        return dist[t] != -1;
    }

    T dfs(int u, int t, T fl = INF) {
        if(u == t) {
            return fl;
        }

        for(; po[u] < (int)adj[u].size(); po[u]++) {
            auto& e = adj[u][po[u]];
            if(dist[e.to] == dist[u] + 1 && e.flow < e.cap) {
                T f = dfs(e.to, t, min(fl, e.cap - e.flow));
                e.flow += f;
                adj[e.to][e.rev].flow -= f;
                if(f > 0) {
                    return f;
                }
            }
        }

        return 0;
    }

  public:
    const static T INF = numeric_limits<T>::max();

    MaxFlow(int n = 0) { init(n); }

    vector<vector<Edge>> adj;

    void init(int _n) {
        n = _n;
        adj.assign(n + 1, {});
        dist.resize(n + 1);
        po.resize(n + 1);
    }

    void add_edge(int u, int v, T w, int idx = -1) {
        adj[u].push_back(Edge(v, adj[v].size(), 0, w, idx));
        adj[v].push_back(Edge(u, adj[u].size() - 1, 0, 0, -1));
    }

    T flow(int s, int t) {
        assert(s != t);

        T ret = 0, to_add;
        while(bfs(s, t)) {
            while((to_add = dfs(s, t))) {
                ret += to_add;
            }
        }

        return ret;
    }
};

int n;
vector<int> w, r;
vector<vector<int>> cnt;

void read() {
    cin >> n;
    w.resize(n);
    r.resize(n);
    cnt.assign(n, vector<int>(n, 0));

    cin >> w >> r;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            cin >> cnt[i][j];
        }
    }
}

void solve() {
    // Classic baseball-elimination via max flow. Team 1 can finish first only
    // if it wins all of its remaining games, giving it cnt_win_a = w[0]+r[0]
    // wins. Every other game between teams i and j (i,j != 1) must be assigned
    // to one of the two, and no other team i may exceed cnt_win_a wins.
    //
    // Build a flow network: source -> a node per remaining game pair (i,j)
    // with capacity equal to the number of remaining games between them; that
    // pair node -> team i and -> team j (capacity = those games, so the wins
    // can go either way); team i -> sink with capacity cnt_win_a - w[i], the
    // headroom team i has before tying team 1. If any team already has more
    // wins than cnt_win_a the answer is immediately NO.
    //
    // The answer is YES iff every remaining game can be routed, i.e. the max
    // flow equals the total number of games to distribute.

    int cnt_win_a = w[0] + r[0];

    int pairs = n * n;
    MaxFlow<int> mf(n + pairs + 2);
    int s = n + pairs, t = n + pairs + 1;

    for(int i = 1; i < n; i++) {
        if(w[i] > cnt_win_a) {
            cout << "NO" << endl;
            return;
        }

        int max_wins = cnt_win_a - w[i];
        mf.add_edge(i, t, max_wins);
    }

    int need_flow = 0;
    for(int i = 1; i < n; i++) {
        for(int j = i + 1; j < n; j++) {
            if(!cnt[i][j]) {
                continue;
            }

            need_flow += cnt[i][j];
            mf.add_edge(s, n + (i * n + j), cnt[i][j]);
            mf.add_edge(n + (i * n + j), i, cnt[i][j]);
            mf.add_edge(n + (i * n + j), j, cnt[i][j]);
        }
    }

    if(mf.flow(s, t) < need_flow) {
        cout << "NO" << endl;
    } else {
        cout << "YES" << endl;
    }
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
p326.in1
======================
3
1 2 2
1 1 1
0 0 0
0 0 0
0 0 0

=================
p326.in2
======================
3
1 2 2
1 1 1
0 0 0
0 0 1
0 1 0

=================
statement.txt
======================
326. Perspective
Time limit per test: 0.25 second(s)
Memory limit: 65536 kilobytes
input: standard
output: standard



Breaking news! A Russian billionaire has bought a yet undisclosed NBA team. He's planning to invest huge effort and money into making that team the best. And in fact he's been very specific about the expected result: the first place.

Being his advisor, you need to determine whether it's possible for your team to finish first in its division or not.

More formally, the NBA regular season is organized as follows: all teams play some games, in each game one team wins and one team loses. Teams are grouped into divisions, some games are between the teams in the same division, and some are between the teams in different divisions.

Given the current score and the total number of remaining games for each team of your division, and the number of remaining games between each pair of teams in your division, determine if it's possible for your team to score at least as much wins as any other team in your division.

Input
The first line of input contains N (2 ≤ N ≤ 20) — the number of teams in your division. They are numbered from 1 to N, your team has number 1.

The second line of input contains N integers w1, w2,..., wN, where wi is the total number of games that ith team has won to the moment.

The third line of input contains N integers r1, r2,..., rN, where ri is the total number of remaining games for the ith team (including the games inside the division).

The next N lines contain N integers each. The jth integer in the ith line of those contains aij — the number of games remaining between teams i and j. It is always true that aij=aji and aii=0, for all i ai1 + ai2 +... + aiN ≤ ri.

All the numbers in input are non-negative and don't exceed 10\,000.

Output
On the only line of output, print "YES" (without quotes) if it's possible for the team 1 to score at least as much wins as any other team of its division, and "NO" (without quotes) otherwise.

Example(s)
sample input
sample output
3
1 2 2
1 1 1
0 0 0
0 0 0
0 0 0
YES

sample input
sample output
3
1 2 2
1 1 1
0 0 0
0 0 1
0 1 0
NO

=================
