Problem p02562 (C++) — buggy submission, verdict: Time Limit Exceeded

#line 2 "/Users/kaage/Desktop/ProgrammingWorkspace/library/other/template.hpp"
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx2")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define all(V) V.begin(), V.end()
typedef unsigned int uint;
typedef long long lint;
typedef unsigned long long ulint;
typedef std::pair<int, int> P;
typedef std::pair<lint, lint> LP;
constexpr int INF = INT_MAX / 2;
constexpr lint LINF = LLONG_MAX / 2;
constexpr double eps = DBL_EPSILON;
constexpr double PI = 3.141592653589793238462643383279;
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
inline lint gcd(lint a, lint b) {
  while (b) {
    lint c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }
bool isprime(lint n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
template <typename T> T mypow(T a, lint b) {
  T res(1);
  while (b) {
    if (b & 1)
      res *= a;
    a *= a;
    b >>= 1;
  }
  return res;
}
lint modpow(lint a, lint b, lint m) {
  lint res(1);
  while (b) {
    if (b & 1) {
      res *= a;
      res %= m;
    }
    a *= a;
    a %= m;
    b >>= 1;
  }
  return res;
}
template <typename T> void printArray(std::vector<T> &vec) {
  rep(i, vec.size()) {
    std::cout << vec[i];
    std::cout << (i == (int)vec.size() - 1 ? "\\n" : " ");
  }
}
template <typename T> void printArray(T l, T r) {
  T rprev = std::prev(r);
  for (T i = l; i != rprev; i++) {
    std::cout << *i << " ";
  }
  std::cout << *rprev << std::endl;
}
LP extGcd(lint a, lint b) {
  if (b == 0)
    return {1, 0};
  LP s = extGcd(b, a % b);
  std::swap(s.first, s.second);
  s.second -= a / b * s.first;
  return s;
}
LP ChineseRem(const lint &b1, const lint &m1, const lint &b2, const lint &m2) {
  lint p = extGcd(m1, m2).first;
  lint tmp = (b2 - b1) * p % m2;
  lint r = (b1 + m1 * tmp + m1 * m2) % (m1 * m2);
  return std::make_pair(r, m1 * m2);
}
template <typename F> inline constexpr decltype(auto) lambda_fix(F &&f) {
  return [f = std::forward<F>(f)](auto &&...args) {
    return f(f, std::forward<decltype(args)>(args)...);
  };
}
#line 3 "/Users/kaage/Desktop/ProgrammingWorkspace/library/graph/MinCostFlow.hpp"
class MinCostFlow {
  class edge {
  public:
    int to, cap;
    lint cost;
    int rev, id;
  };
  int n, idx = 0, s, t;
  lint curres = 0;
  std::vector<std::vector<edge>> vec;
  std::vector<int> prevv, preve;
  std::vector<lint> h, dist;
  bool negative = false;
  lint BellmanFord() {
    dist.assign(n, LINF);
    dist[s] = 0;
    rep(i, n - 1) {
      rep(j, n) {
        rep(k, vec[j].size()) {
          const edge &e = vec[j][k];
          if (e.cap > 0 &&
              chmin(dist[e.to], dist[j] + e.cost + h[j] - h[e.to])) {
            prevv[e.to] = j;
            preve[e.to] = k;
          }
        }
      }
    }
    if (dist[t] == LINF) {
      std::cerr << "The demand is over maximum flow." << std::endl;
      return -1;
    }
    rep(i, n) h[i] += dist[i];
    for (int i = t; i != s; i = prevv[i]) {
      vec[prevv[i]][preve[i]].cap--;
      vec[i][vec[prevv[i]][preve[i]].rev].cap++;
    }
    return h[t];
  }

public:
  MinCostFlow(int n, int s, int t) : n(n), s(s), t(t) {
    vec.resize(n);
    h.resize(n);
    dist.resize(n);
    prevv.resize(n);
    preve.resize(n);
  }
  void add_edge(int from, int to, int cap, lint cost) {
    if (cost < 0)
      negative = true;
    vec[from].push_back({to, cap, cost, (int)vec[to].size(), -1});
    vec[to].push_back({from, 0, -cost, (int)vec[from].size() - 1, idx++});
  }
  lint add_flow(int f) {
    if (negative) {
      curres += BellmanFord();
      f--;
      negative = false;
    }
    while (f > 0) {
      dist.assign(n, LINF);
      dist[s] = 0;
      prique<LP> que;
      que.push({0, s});
      while (!que.empty()) {
        LP p = que.top();
        que.pop();
        if (dist[p.second] < p.first)
          continue;
        rep(i, vec[p.second].size()) {
          edge &e = vec[p.second][i];
          if (e.cap > 0 && chmin(dist[e.to], dist[p.second] + e.cost +
                                                 h[p.second] - h[e.to])) {
            prevv[e.to] = p.second;
            preve[e.to] = i;
            que.push({dist[e.to], e.to});
          }
        }
      }
      if (dist[t] == LINF) {
        std::cerr << "The demand is over the maximum flow." << std::endl;
        return -1;
      }
      rep(i, n) h[i] += dist[i];
      int d = f;
      for (int i = t; i != s; i = prevv[i]) {
        chmin(d, vec[prevv[i]][preve[i]].cap);
      }
      f -= d;
      curres += (lint)d * h[t];
      for (int i = t; i != s; i = prevv[i]) {
        vec[prevv[i]][preve[i]].cap -= d;
        vec[i][vec[prevv[i]][preve[i]].rev].cap += d;
      }
    }
    return curres;
  }
  std::vector<lint> restore() {
    std::vector<lint> res(idx);
    rep(i, n) {
      for (const auto &j : vec[i]) {
        if (j.id != -1)
          res[j.id] = j.cap;
      }
    }
    return res;
  }
  void reset() {
    rep(i, n) {
      for (auto &j : vec[i]) {
        if (j.id != -1) {
          vec[j.to][j.rev].cap += j.cap;
          j.cap = 0;
        }
      }
    }
  }
};
#line 3 "main.cpp"
int N, K, A[55][55];
int main() {
  std::cin >> N >> K;
  rep(i, N) { rep(j, N) std::cin >> A[i][j]; }
  int f;
  lint max = 0;
  MinCostFlow mcf(2 * N + 2, 0, 2 * N + 1);
  REP(i, N) mcf.add_edge(0, i, K, 0);
  rep(i, N) {
    rep(j, N) { mcf.add_edge(i + 1, N + 1 + j, 1, -A[i][j]); }
  }
  rep(i, N) mcf.add_edge(N + 1 + i, 2 * N + 1, K, 0);
  REP(i, K * N) {
    if (chmax(max, -mcf.add_flow(1)))
      f = i;
  }
  std::cout << max << std::endl;
  mcf.reset();
  mcf.add_flow(f);
  auto vec = mcf.restore();
  rep(i, N) {
    rep(j, N) {
      if (vec[N + i * N + j])
        std::cout << 'X';
      else
        std::cout << '.';
    }
    std::cout << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#line 2 "/Users/kaage/Desktop/ProgrammingWorkspace/library/other/template.hpp"
#define _CRT_SECURE_NO_WARNINGS
#pragma target("avx2")
#pragma optimize("O3")
#pragma optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define all(V) V.begin(), V.end()
typedef unsigned int uint;
typedef long long lint;
typedef unsigned long long ulint;
typedef std::pair<int, int> P;
typedef std::pair<lint, lint> LP;
constexpr int INF = INT_MAX / 2;
constexpr lint LINF = LLONG_MAX / 2;
constexpr double eps = DBL_EPSILON;
constexpr double PI = 3.141592653589793238462643383279;
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
inline lint gcd(lint a, lint b) {
  while (b) {
    lint c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline lint lcm(lint a, lint b) { return a / gcd(a, b) * b; }
bool isprime(lint n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
template <typename T> T mypow(T a, lint b) {
  T res(1);
  while (b) {
    if (b & 1)
      res *= a;
    a *= a;
    b >>= 1;
  }
  return res;
}
lint modpow(lint a, lint b, lint m) {
  lint res(1);
  while (b) {
    if (b & 1) {
      res *= a;
      res %= m;
    }
    a *= a;
    a %= m;
    b >>= 1;
  }
  return res;
}
template <typename T> void printArray(std::vector<T> &vec) {
  rep(i, vec.size()) {
    std::cout << vec[i];
    std::cout << (i == (int)vec.size() - 1 ? "\\n" : " ");
  }
}
template <typename T> void printArray(T l, T r) {
  T rprev = std::prev(r);
  for (T i = l; i != rprev; i++) {
    std::cout << *i << " ";
  }
  std::cout << *rprev << std::endl;
}
LP extGcd(lint a, lint b) {
  if (b == 0)
    return {1, 0};
  LP s = extGcd(b, a % b);
  std::swap(s.first, s.second);
  s.second -= a / b * s.first;
  return s;
}
LP ChineseRem(const lint &b1, const lint &m1, const lint &b2, const lint &m2) {
  lint p = extGcd(m1, m2).first;
  lint tmp = (b2 - b1) * p % m2;
  lint r = (b1 + m1 * tmp + m1 * m2) % (m1 * m2);
  return std::make_pair(r, m1 * m2);
}
template <typename F> inline constexpr decltype(auto) lambda_fix(F &&f) {
  return [f = std::forward<F>(f)](auto &&...args) {
    return f(f, std::forward<decltype(args)>(args)...);
  };
}
#line 3 "/Users/kaage/Desktop/ProgrammingWorkspace/library/graph/MinCostFlow.hpp"
class MinCostFlow {
  class edge {
  public:
    int to, cap;
    lint cost;
    int rev, id;
  };
  int n, idx = 0;
  std::vector<std::vector<edge>> vec;
  std::vector<int> prevv, preve;
  std::vector<lint> h, dist;
  bool negative = false;
  lint BellmanFord(int s, int t) {
    dist.assign(n, LINF);
    dist[s] = 0;
    rep(i, n - 1) {
      rep(j, n) {
        rep(k, vec[j].size()) {
          const edge &e = vec[j][k];
          if (e.cap > 0 &&
              chmin(dist[e.to], dist[j] + e.cost + h[j] - h[e.to])) {
            prevv[e.to] = j;
            preve[e.to] = k;
          }
        }
      }
    }
    if (dist[t] == LINF) {
      std::cerr << "The demand is over maximum flow." << std::endl;
      return -1;
    }
    rep(i, n) h[i] += dist[i];
    for (int i = t; i != s; i = prevv[i]) {
      vec[prevv[i]][preve[i]].cap--;
      vec[i][vec[prevv[i]][preve[i]].rev].cap++;
    }
    return h[t];
  }

public:
  MinCostFlow(int n) : n(n) {
    vec.resize(n);
    h.resize(n);
    dist.resize(n);
    prevv.resize(n);
    preve.resize(n);
  }
  void add_edge(int from, int to, int cap, lint cost) {
    if (cost < 0)
      negative = true;
    vec[from].push_back({to, cap, cost, (int)vec[to].size(), -1});
    vec[to].push_back({from, 0, -cost, (int)vec[from].size() - 1, idx++});
  }
  lint min_cost_flow(int s, int t, int f) {
    lint res = 0;
    h.assign(n, 0);
    if (negative) {
      res += BellmanFord(s, t);
      f--;
    }
    while (f > 0) {
      dist.assign(n, LINF);
      dist[s] = 0;
      prique<LP> que;
      que.push({0, s});
      while (!que.empty()) {
        LP p = que.top();
        que.pop();
        if (dist[p.second] < p.first)
          continue;
        rep(i, vec[p.second].size()) {
          edge &e = vec[p.second][i];
          if (e.cap > 0 && chmin(dist[e.to], dist[p.second] + e.cost +
                                                 h[p.second] - h[e.to])) {
            prevv[e.to] = p.second;
            preve[e.to] = i;
            que.push({dist[e.to], e.to});
          }
        }
      }
      if (dist[t] == LINF) {
        std::cerr << "The demand is over maximum flow." << std::endl;
        return -1;
      }
      rep(i, n) h[i] += dist[i];
      int d = f;
      for (int i = t; i != s; i = prevv[i]) {
        chmin(d, vec[prevv[i]][preve[i]].cap);
      }
      f -= d;
      res += (lint)d * h[t];
      for (int i = t; i != s; i = prevv[i]) {
        vec[prevv[i]][preve[i]].cap -= d;
        vec[i][vec[prevv[i]][preve[i]].rev].cap += d;
      }
    }
    return res;
  }
  std::vector<lint> restore() {
    std::vector<lint> res(idx);
    rep(i, n) {
      for (const auto &j : vec[i]) {
        if (j.id != -1)
          res[j.id] = j.cap;
      }
    }
    return res;
  }
};
#line 3 "main.cpp"
int N, K, A[55][55];
int main() {
  std::cin >> N >> K;
  rep(i, N) { rep(j, N) std::cin >> A[i][j]; }
  int f;
  lint max = 0;
  REP(i, K * N) {
    MinCostFlow mcf(2 * N + 2);
    REP(j, N) mcf.add_edge(0, j, K, 0);
    rep(j, N) {
      rep(k, N) { mcf.add_edge(j + 1, N + 1 + k, 1, -A[j][k]); }
    }
    rep(j, N) mcf.add_edge(N + 1 + j, 2 * N + 1, K, 0);
    if (chmax(max, -mcf.min_cost_flow(0, 2 * N + 1, i)))
      f = i;
  }
  MinCostFlow mcf(2 * N + 2);
  REP(j, N) mcf.add_edge(0, j, K, 0);
  rep(j, N) {
    rep(k, N) { mcf.add_edge(j + 1, N + 1 + k, 1, -A[j][k]); }
  }
  rep(j, N) mcf.add_edge(N + 1 + j, 2 * N + 1, K, 0);
  std::cout << -mcf.min_cost_flow(0, 2 * N + 1, f) << std::endl;
  std::vector<lint> vec = mcf.restore();
  rep(i, N) {
    rep(j, N) {
      if (vec[N + i * N + j])
        std::cout << 'X';
      else
        std::cout << '.';
    }
    std::cout << std::endl;
  }
  return 0;
}