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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef map<ll, ll> mm;
typedef vector<ll> mv;
typedef pair<ll, ll> mp;
typedef set<ll> ms;
typedef queue<ll> mq;
typedef deque<ll> mdq;
typedef stack<ll> mst;
typedef priority_queue<ll> mpq;
typedef priority_queue<ll, vector<ll>, greater<ll>> mmh;
typedef complex<double> cd;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define flush cout.flush();
#define all(v) v.begin(), v.end()
#define pb(v) push_back(v)
#define cn continue
#define forl(i, n) for (ll i = 0; i < n; i++)
#define forlr(i, n) for (ll i = n - 1; i >= 0; i--)
#define rev(s) reverse(s.begin(), s.end())
void swap(ll &a, ll &b) {
  ll tmp = a;
  a = b;
  b = tmp;
}
ll atoistring(string s) {
  stringstream lol(s);
  ll x;
  lol >> x;
  return x;
}
void pv(mv v) {
  forl(i, v.size()) { cout << v[i] << " "; }
  cout << "\\n";
}
void pa(ll *v, ll size) {
  forl(i, size) { cout << v[i] << " "; }
  cout << "\\n";
}
// ll nck(ll N,ll K,ll mod)
// {
// 	if(N<K)
// 	{
// 		return 0;
// 	}
// 	return (((facs[N]*facsinv[N-K])%mod)*facsinv[K])%mod;
// }
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void checkmod(ll &x, ll mod) {
  if (x >= mod) {
    x %= mod;
  }
}
ll sumofgp(ll a, ll n, ll m) {
  // n = number of terms - 1
  if (n < 0) {
    return 0;
  }
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return (1 + a) % m;
  }
  ll ans;
  if (n % 2) {
    ans = (1 + a) * sumofgp((a * a) % m, (n - 1) / 2, m);
  } else {
    ans = 1 + ((a * (1 + a)) % m) * sumofgp((a * a) % m, n / 2 - 1, m);
  }
  return ans % m;
}
ll power_of_2_not_less(ll n) {
  if (!n) {
    return 1;
  }
  n--;
  n |= n >> 1;
  n |= n >> 2;
  n |= n >> 4;
  n |= n >> 8;
  n |= n >> 16;
  n |= n >> 32;
  n++;
  return n;
}
// void checktime()
// {
// 	struct timespec ts;
// 	printf("Running Program\\n");
// 	clock_gettime(CLOCK_MONOTONIC_RAW,&ts);
// 	long double st=ts.tv_nsec/(1e9)+ts.tv_sec;
// 	// Call Funcn Here
// 	clock_gettime(CLOCK_MONOTONIC_RAW,&ts);
// 	long double en=ts.tv_nsec/(1e9)+ts.tv_sec;
// 	printf("Program ended\\nTime = %Lf\\n",en-st);
// }
mv v[20];
ll tmparr[20];
int main(void) {
  fastio ll n, m, x1;
  cin >> n >> m >> x1;
  forl(i, n) forl(j, m + 1) {
    ll x;
    cin >> x;
    v[i].pb(x);
  }
  string s = "";
  forl(i, n) s += '0';
  ll ans = 1e18;
  forl(i, pow(2, n) - 1) {
    forlr(j, n) if (s[j] == '0') {
      s[j] = '1';
      break;
    }
    else s[j] = '0';
    forl(j, m) tmparr[j] = 0;
    ll tmpsum = 0;
    forl(j, n) if (s[j] == '1') {
      forl(k, m) tmparr[k] += v[j][k + 1];
      tmpsum += v[j][0];
    }
    bool f = 1;
    forl(j, m) if (tmparr[j] < x1) f = 0;
    if (f)
      ans = min(ans, tmpsum);
  }
  cout << (ans == 1e18 ? -1 : ans) << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef map<ll, ll> mm;
typedef vector<ll> mv;
typedef pair<ll, ll> mp;
typedef set<ll> ms;
typedef queue<ll> mq;
typedef deque<ll> mdq;
typedef stack<ll> mst;
typedef priority_queue<ll> mpq;
typedef priority_queue<ll, vector<ll>, greater<ll>> mmh;
typedef complex<double> cd;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define flush cout.flush();
#define all(v) v.begin(), v.end()
#define pb(v) push_back(v)
#define cn continue
#define forl(i, n) for (ll i = 0; i < n; i++)
#define forlr(i, n) for (ll i = n - 1; i >= 0; i--)
#define rev(s) reverse(s.begin(), s.end())
void swap(ll &a, ll &b) {
  ll tmp = a;
  a = b;
  b = tmp;
}
ll atoistring(string s) {
  stringstream lol(s);
  ll x;
  lol >> x;
  return x;
}
void pv(mv v) {
  forl(i, v.size()) { cout << v[i] << " "; }
  cout << "\\n";
}
void pa(ll *v, ll size) {
  forl(i, size) { cout << v[i] << " "; }
  cout << "\\n";
}
// ll nck(ll N,ll K,ll mod)
// {
// 	if(N<K)
// 	{
// 		return 0;
// 	}
// 	return (((facs[N]*facsinv[N-K])%mod)*facsinv[K])%mod;
// }
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void checkmod(ll &x, ll mod) {
  if (x >= mod) {
    x %= mod;
  }
}
ll sumofgp(ll a, ll n, ll m) {
  // n = number of terms - 1
  if (n < 0) {
    return 0;
  }
  if (n == 0) {
    return 1;
  }
  if (n == 1) {
    return (1 + a) % m;
  }
  ll ans;
  if (n % 2) {
    ans = (1 + a) * sumofgp((a * a) % m, (n - 1) / 2, m);
  } else {
    ans = 1 + ((a * (1 + a)) % m) * sumofgp((a * a) % m, n / 2 - 1, m);
  }
  return ans % m;
}
ll power_of_2_not_less(ll n) {
  if (!n) {
    return 1;
  }
  n--;
  n |= n >> 1;
  n |= n >> 2;
  n |= n >> 4;
  n |= n >> 8;
  n |= n >> 16;
  n |= n >> 32;
  n++;
  return n;
}
// void checktime()
// {
// 	struct timespec ts;
// 	printf("Running Program\\n");
// 	clock_gettime(CLOCK_MONOTONIC_RAW,&ts);
// 	long double st=ts.tv_nsec/(1e9)+ts.tv_sec;
// 	// Call Funcn Here
// 	clock_gettime(CLOCK_MONOTONIC_RAW,&ts);
// 	long double en=ts.tv_nsec/(1e9)+ts.tv_sec;
// 	printf("Program ended\\nTime = %Lf\\n",en-st);
// }

mv arr[20];
ll tmparr[20];
bool done[20];
ll n, m, k;
ll mini = 1e18;

void getAns(ll x, ll ansd) {
  if (x == n)
    return;
  // cout<<x<<" "<<ansd<<"\\n";
  ll cnt = 0;
  forl(i, n) cnt += done[i];
  forl(i, n) {
    if (done[i])
      cn;
    done[i] = 1;
    ansd += arr[i][0];
    bool f = 1;
    for (ll j = 1; j <= m; j++) {
      tmparr[j - 1] += arr[i][j];
      if (tmparr[j - 1] < k)
        f = 0;
    }
    if (f)
      mini = min(mini, ansd);
    else
      getAns(x + 1, ansd);
    for (ll j = 1; j <= m; j++)
      tmparr[j - 1] -= arr[i][j];
    ansd -= arr[i][0];
    done[i] = 0;
  }
  return;
}

int main(void) {
  fastio cin >> n >> m >> k;
  forl(i, n) {
    forl(j, m + 1) {
      ll x;
      cin >> x;
      arr[i].pb(x);
    }
  }
  getAns(0, 0);
  cout << (mini == 1e18 ? -1 : mini) << "\\n";
  return 0;
}