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

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

typedef long long ll;
const long double PI = (acos(-1));
const long long MOD = 1000000007;
long long modpow(long long a, long long n, long long m) {
  long long ans = 1;
  while (n) {
    if (n & 1) {
      ans = (ans * a) % m;
    }
    a = (a * a) % m;
    n >>= 1;
  }
  return ans;
}
long long combi(long long n, long long a) {
  long long ans = 1, ans1 = 1;
  for (long long i = n - a + 1; i <= n; i++) {
    ans *= i % MOD;
    ans %= MOD;
  }

  for (long long i = 2; i <= a; i++)
    ans1 = (ans1 * i) % MOD;
  ans1 = modpow(ans1, MOD - 2, MOD);
  return ((ans % MOD) * ans1) % MOD;
}

template <typename T>
bool next_combination(const T first, const T last, int k) {
  const T subset = first + k;
  // empty container | k = 0 | k == n
  if (first == last || first == subset || last == subset) {
    return false;
  }
  T src = subset;
  while (first != src) {
    src--;
    if (*src < *(last - 1)) {
      T dest = subset;
      while (*src >= *dest) {
        dest++;
      }
      iter_swap(src, dest);
      rotate(src + 1, dest + 1, last);
      rotate(subset, subset + (last - dest) - 1, last);
      return true;
    }
  }
  // restore
  rotate(first, subset, last);
  return false;
}

void dfs(vector<ll> s, ll mx, ll N, vector<vector<ll>> &arr) {
  if (s.size() == (size_t)N) {
    // cout << s.c_str() << endl;
    arr.push_back(s);
  } else {
    for (ll c = s.size() > 0 ? 0 : 0; c <= mx; c++) {
      s.push_back(c);
      dfs(s, mx, N, arr);
      s.pop_back();
    }
  }

  return;
}

int bfs(int a, int b, int h, int w, char tmp[][101]) {
  int ans[101][101] = {0};
  char c[101][101] = {0};
  queue<pair<int, int>> s;

  for (int i = 0; i <= h; i++) {
    for (int j = 0; j <= w; j++) {
      c[i][j] = tmp[i][j];
    }
  }

  s.push(make_pair(a, b));

  while (s.size() > 0) {

    pair<int, int> tmp = s.front();
    s.pop();
    c[tmp.first][tmp.second] = '#';
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (tmp.first + i < 1 || tmp.first + i > h) {
          continue;
        }
        if (tmp.second + j < 1 || tmp.second + j > w) {
          continue;
        }
        if (i != 0 && j != 0) {
          continue;
        }
        if (i == 0 && j == 0) {
          continue;
        }
        if (c[tmp.first + i][tmp.second + j] == '#') {
          continue;
        }
        c[tmp.first + i][tmp.second + j] = '#';
        if (ans[tmp.first + i][tmp.second + j] == 0) {
          ans[tmp.first + i][tmp.second + j] = ans[tmp.first][tmp.second] + 1;
        } else {
          ans[tmp.first + i][tmp.second + j] =
              min(ans[tmp.first + i][tmp.second + j],
                  ans[tmp.first][tmp.second] + 1);
        }
        s.push(make_pair(tmp.first + i, tmp.second + j));
      }
    }
  }

  int asd = 0;

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      asd = max(a, ans[i][j]);
    }
  }

  return asd;
}

ll modfactorial(ll a) {
  if (a == 1)
    return 1;

  return (a % MOD) * (modfactorial(a - 1) % MOD);
}

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

map<ll, ll> prime_factor(ll n) {
  map<ll, ll> ret;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

int main() {
  ll n, m, x;
  cin >> n >> m >> x;

  vector<ll> c(n);
  vector<vector<ll>> a;
  for (int i = 0; i < n; i++) {

    cin >> c[i];
    vector<ll> tmp(m);
    ll total = 0;
    for (int j = 0; j < m; j++) {
      cin >> tmp[j];
    }

    a.push_back(tmp);
  }

  vector<ll> s;
  vector<ll> ss(m, 0);
  vector<vector<ll>> arr;
  dfs(s, 1, n, arr);

  ll ans = 10000000000;
  for (int i = 0; i < arr.size(); i++) {
    vector<ll> tmp = arr[i];
    ll total = 0;
    for (int j = 0; j < n; j++) {
      if (tmp[j]) {
        total += c[j];

        for (int k = 0; k < m; k++) {
          ss[k] += a[j][k];
        }

        bool f = true;
        for (int k = 0; k < m; k++) {
          if (ss[k] < x) {
            f = false;
            break;
          }
        }

        if (f) {
          ans = min(ans, total);
        }
      }
    }
    for (int k = 0; k < m; k++) {
      ss[k] = 0;
    }
  }

  if (ans == 10000000000) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}
// cout << std::fixed << std::setprecision(15)


Error: TLE

Fixed submission:

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

typedef long long ll;
const long double PI = (acos(-1));
const long long MOD = 1000000007;
long long modpow(long long a, long long n, long long m) {
  long long ans = 1;
  while (n) {
    if (n & 1) {
      ans = (ans * a) % m;
    }
    a = (a * a) % m;
    n >>= 1;
  }
  return ans;
}
long long combi(long long n, long long a) {
  long long ans = 1, ans1 = 1;
  for (long long i = n - a + 1; i <= n; i++) {
    ans *= i % MOD;
    ans %= MOD;
  }

  for (long long i = 2; i <= a; i++)
    ans1 = (ans1 * i) % MOD;
  ans1 = modpow(ans1, MOD - 2, MOD);
  return ((ans % MOD) * ans1) % MOD;
}

template <typename T>
bool next_combination(const T first, const T last, int k) {
  const T subset = first + k;
  // empty container | k = 0 | k == n
  if (first == last || first == subset || last == subset) {
    return false;
  }
  T src = subset;
  while (first != src) {
    src--;
    if (*src < *(last - 1)) {
      T dest = subset;
      while (*src >= *dest) {
        dest++;
      }
      iter_swap(src, dest);
      rotate(src + 1, dest + 1, last);
      rotate(subset, subset + (last - dest) - 1, last);
      return true;
    }
  }
  // restore
  rotate(first, subset, last);
  return false;
}

void dfs(vector<ll> s, ll mx, ll N, vector<vector<ll>> &arr) {
  if (s.size() == (size_t)N) {
    // cout << s.c_str() << endl;
    arr.push_back(s);
  } else {
    for (ll c = s.size() > 0 ? 1 : 1; c <= mx; c++) {
      s.push_back(c);
      dfs(s, mx, N, arr);
      s.pop_back();
    }
  }

  return;
}

int bfs(int a, int b, int h, int w, char tmp[][101]) {
  int ans[101][101] = {0};
  char c[101][101] = {0};
  queue<pair<int, int>> s;

  for (int i = 0; i <= h; i++) {
    for (int j = 0; j <= w; j++) {
      c[i][j] = tmp[i][j];
    }
  }

  s.push(make_pair(a, b));

  while (s.size() > 0) {

    pair<int, int> tmp = s.front();
    s.pop();
    c[tmp.first][tmp.second] = '#';
    for (int i = -1; i <= 1; i++) {
      for (int j = -1; j <= 1; j++) {
        if (tmp.first + i < 1 || tmp.first + i > h) {
          continue;
        }
        if (tmp.second + j < 1 || tmp.second + j > w) {
          continue;
        }
        if (i != 0 && j != 0) {
          continue;
        }
        if (i == 0 && j == 0) {
          continue;
        }
        if (c[tmp.first + i][tmp.second + j] == '#') {
          continue;
        }
        c[tmp.first + i][tmp.second + j] = '#';
        if (ans[tmp.first + i][tmp.second + j] == 0) {
          ans[tmp.first + i][tmp.second + j] = ans[tmp.first][tmp.second] + 1;
        } else {
          ans[tmp.first + i][tmp.second + j] =
              min(ans[tmp.first + i][tmp.second + j],
                  ans[tmp.first][tmp.second] + 1);
        }
        s.push(make_pair(tmp.first + i, tmp.second + j));
      }
    }
  }

  int asd = 0;

  for (int i = 1; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      asd = max(a, ans[i][j]);
    }
  }

  return asd;
}

ll modfactorial(ll a) {
  if (a == 1)
    return 1;

  return (a % MOD) * (modfactorial(a - 1) % MOD);
}

ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

map<ll, ll> prime_factor(ll n) {
  map<ll, ll> ret;
  for (ll i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if (n != 1)
    ret[n] = 1;
  return ret;
}

int main() {
  ll n, m, x;
  cin >> n >> m >> x;

  vector<ll> c(n);
  vector<vector<ll>> a;
  for (int i = 0; i < n; i++) {

    cin >> c[i];
    vector<ll> tmp(m);
    ll total = 0;
    for (int j = 0; j < m; j++) {
      cin >> tmp[j];
      total += tmp[j];
    }

    a.push_back(tmp);
  }

  vector<ll> v(n);
  for (int i = 0; i < n; i++) {
    v[i] = i;
  }

  ll ans = 1000000000;
  do {
    vector<ll> aa(m, 0);
    ll tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp += c[v[i]];
      for (int j = 0; j < m; j++) {
        aa[j] += a[v[i]][j];
      }

      bool f = true;
      for (int j = 0; j < m; j++) {
        if (aa[j] < x) {
          f = false;
          break;
        }
      }

      if (f) {
        ans = min(ans, tmp);
        break;
      }
    }

  } while (next_permutation(v.begin(), v.end()));

  if (ans == 1000000000) {
    cout << -1 << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}
// cout << std::fixed << std::setprecision(15)