description: There are n reindeer at the North Pole, all battling for the highest spot on the "Top Reindeer" leaderboard on the front page of CodeNorses (a popular competitive reindeer gaming website). Interestingly, the "Top Reindeer" title is just a measure of upvotes and has nothing to do with their skill level in the reindeer games, but they still give it the utmost importance.
  
  Currently, the i-th reindeer has a score of a_i. You would like to influence the leaderboard with some operations. In an operation, you can choose a reindeer, and either increase or decrease his score by 1 unit. Negative scores are allowed.
  
  You have m requirements for the resulting scores. Each requirement is given by an ordered pair (u, v), meaning that after all operations, the score of reindeer u must be less than or equal to the score of reindeer v.
  
  Your task is to perform the minimum number of operations so that all requirements will be satisfied.
  
  Input
  
  The first line contains two integers n and m (2≤ n≤ 1000; 1≤ m≤ 1000) — the number of reindeer and requirements, respectively.
  
  The second line contains n integers a_1,…, a_n (1≤ a_i≤ 10^9), where a_i is the current score of reindeer i.
  
  The next m lines describe the requirements.
  
  The i-th of these lines contains two integers u_i and v_i (1≤ u_i, v_i≤ n; u_i≠ v_i) — the two reindeer of the i-th requirement.
  
  Output
  
  Print n integers b_1,…, b_n (-10^{15}≤ b_i≤ 10^{15}), where b_i is the score of the i-th reindeer after all operations.
  
  If there are multiple solutions achieving the minimum number of operations, you may output any.
  
  We can prove that there is always an optimal solution such that |b_i|≤ 10^{15} for all i.
  
  Examples
  
  Input
  
  
  7 6
  3 1 4 9 2 5 6
  1 2
  2 3
  3 4
  4 5
  5 6
  6 7
  
  
  Output
  
  
  1 1 4 4 4 5 6 
  
  
  Input
  
  
  4 6
  6 5 8 2
  3 1
  4 1
  3 2
  1 2
  2 3
  3 1
  
  
  Output
  
  
  6 6 6 2 
  
  
  Input
  
  
  10 18
  214 204 195 182 180 176 176 172 169 167
  1 2
  3 2
  4 2
  5 2
  6 2
  7 2
  8 2
  9 2
  10 2
  6 1
  6 2
  6 3
  6 4
  6 5
  6 7
  6 8
  6 9
  6 10
  
  
  Output
  
  
  204 204 195 182 180 167 176 172 169 167 
solution: #include <bits/stdc++.h>
  using namespace std;
  const int N = 10005, M = 100005;
  using ll = long long;
  const ll inf = LLONG_MAX >> 5;
  vector<int> curp;
  namespace FlowGraph {
  int head[N], v[M], nxt[M], ptr, t;
  ll w[M];
  inline void padd_edge(int uu, int vv, ll ww) {
    v[ptr] = vv;
    w[ptr] = ww;
    nxt[ptr] = head[uu];
    head[uu] = ptr++;
  }
  int dis[N], cur[N];
  bool bfs(int s, int t) {
    dis[s] = dis[t] = -1;
    for (auto &i : curp) dis[i] = -1;
    queue<int> q;
    q.push(s);
    dis[s] = 1;
    while (!q.empty()) {
      int nd = q.front();
      q.pop();
      for (int i = head[nd]; i != -1; i = nxt[i])
        if (w[i] && dis[v[i]] == -1) {
          dis[v[i]] = dis[nd] + 1;
          q.push(v[i]);
        }
    }
    return dis[t] != -1;
  }
  ll dfs(int pos, ll flow) {
    if (pos == t) return flow;
    ll tmp = flow;
    for (int &i = cur[pos]; i != -1; i = nxt[i])
      if (w[i] && dis[v[i]] == dis[pos] + 1) {
        ll fl = dfs(v[i], min(flow, w[i]));
        w[i] -= fl;
        w[i ^ 1] += fl;
        flow -= fl;
        if (!flow) break;
      }
    return tmp - flow;
  }
  inline void add_edge(int uu, int vv, ll ww) {
    padd_edge(uu, vv, ww);
    padd_edge(vv, uu, 0);
  }
  ll dinic(int ss, int tt) {
    t = tt;
    ll ret = 0;
    while (bfs(ss, tt)) {
      cur[ss] = head[ss];
      cur[tt] = head[tt];
      for (auto &i : curp) cur[i] = head[i];
      ret += dfs(ss, inf);
    }
    return ret;
  }
  };  // namespace FlowGraph
  ll ans[N], a[N];
  int n, m;
  vector<int> v[N];
  bool vis[N];
  void solve(ll l, ll r, vector<int> p) {
    if (p.empty()) return;
    if (l == r) {
      for (auto &i : p) ans[i] = l;
      return;
    }
    curp = p;
    ll mid = (l + r) >> 1;
    int S = 0, T = n + 1;
    FlowGraph::head[S] = FlowGraph::head[T] = -1;
    for (auto &i : p) FlowGraph::head[i] = -1, vis[i] = 1;
    FlowGraph::ptr = 0;
    for (auto &i : p)
      if (a[i] <= mid)
        FlowGraph::add_edge(S, i, 1);
      else
        FlowGraph::add_edge(i, T, 1);
    for (auto &i : p)
      for (auto &j : v[i])
        if (vis[j]) FlowGraph::add_edge(i, j, inf);
    FlowGraph::dinic(S, T);
    for (auto &i : p) vis[i] = 0;
    vector<int> lp, rp;
    for (auto &i : p)
      if (FlowGraph::dis[i] != -1)
        lp.push_back(i);
      else
        rp.push_back(i);
    solve(l, mid, lp);
    solve(mid + 1, r, rp);
  }
  int main() {
    ios::sync_with_stdio(false);
    cin >> n >> m;
    vector<int> tt;
    for (int i = 1; i <= n; i++) cin >> a[i], tt.push_back(i);
    int t1, t2;
    for (int i = 1; i <= m; i++) {
      cin >> t1 >> t2;
      v[t2].push_back(t1);
    }
    solve(0, *max_element(a + 1, a + n + 1), tt);
    for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
    return 0;
  }
  