alexjercan/bugnet huggingface dataset:
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int a[4];
  rep(i, 4) cin >> a[i];

  int ans = 0;

  if (a[0] <= a[3]) {
    ans += a[0];
    a[3] -= a[0];
  } else if (a[0] > a[3]) {
    ans = a[3];
    a[3] = 0;
  }
  if (a[1] <= a[3]) {
    a[3] -= a[1];
  } else {
    a[3] = 0;
  }
  if (a[2] <= a[3]) {
    ans -= a[2];
  } else {
    ans -= a[3];
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); ++i)

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  int a[4];
  rep(i, 4) cin >> a[i];

  int ans = 0;
  int n = a[3];
  rep(i, n) {
    if (a[0]) {
      ++ans;
      --a[0];
    } else if (a[1]) {
      --a[1];
    } else if (a[2]) {
      --ans;
      --a[2];
    }
  }
  cout << ans;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, K, sum;
  cin >> a >> b >> c >> K;

  sum = 0;
  int i = 0;
  for (; i < a && i < K; i++) {
    sum++;
  }

  int iii = 0;
  for (; iii < c && i + b + iii < K; iii++) {
    sum += -1;
  }

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, K, sum;
  cin >> a >> b >> c >> K;

  vector<int> vec;

  int i = 0;
  for (; i < a; i++) {
    vec.push_back(1);
  }

  int j = 0;
  for (; j < b; j++) {
    vec.push_back(0);
  }

  int k = 0;
  for (; k < c; k++) {
    vec.push_back(-1);
  }

  sort(vec.begin(), vec.end(), greater<int>());
  sum = 0;
  for (int ii = 0; ii < K; ii++) {
    sum += vec[ii];
  }

  cout << sum << endl;

  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int sum = 0;

  int xa = min(a, k);
  k = k - xa;

  int xb = min(b, k);
  k = k - xb;

  int xc = min(c, k);
  k = k - xc;

  sum = xa - xc;
  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

int main(void) {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int sum = 0;
  int count = 0;
  int aiu = 0;
  while (1) {
    for (int j = a; a > 0; a--) {
      sum += 1;
      count++;
      if (count == k) {
        aiu++;
        break;
      }
    }
    if (aiu == 1)
      break;
    for (int j = b; b > 0; b--) {
      count++;
      if (count == k) {
        aiu++;
        break;
      }
    }
    if (aiu == 1)
      break;
    for (int j = c; c > 0; c--) {
      sum -= 1;
      count++;
      if (count == k) {
        aiu++;
        break;
      }
    }
    if (aiu == 1)
      break;
  }

  cout << sum << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
const int INF = 100000000;
const int MAX_N = 100;
typedef pair<int, int> P;
typedef long long ll;

int main() {
  int A, B, C, K, ans;
  ans = 0;
  cin >> A >> B >> C >> K;
  ans += min(A, K);
  if (A < K) {
    K -= A;
  } else {
    K = 0;
  }
  if (B < K) {
    K -= B;
  } else {
    K = 0;
  }
  ans -= K;
  printf("%d\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
const int INF = 100000000;
const int MAX_N = 100;
typedef pair<int, int> P;
typedef long long ll;

int main() {
  int A, B, C, K, ans;
  ans = 0;
  cin >> A >> B >> C >> K;
  for (int i = 0; i < K; ++i) {
    if (A > 0) {
      ++ans;
      --A;
    } else if (B > 0) {
      --B;
    } else {
      --ans;
      --C;
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int ans = 0;
  if (k <= a) {
    ans = k;
  } else if (k <= a + b) {
    ans = a;
  } else {
    ans = a - (k - a - b);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

int main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int ans = 0;
  for (int i = 0; i < k; i++) {
    if (a > 0) {
      ans += 1;
      a--;
    } else if (b > 0) {
      b--;
    } else if (c > 0) {
      ans -= 1;
      c--;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error

// #define TIMER_
#include <bits/stdc++.h>
using namespace std;
#define rep(i, r) for (int i = 0; i < (r); i++)
#define pb push_back
#define ALL(a) a.begin(), a.end()
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using ll = long long;
using ull = unsigned long long;

int main() {
  fastio;
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  int res = 0;
  if (k - a <= 0) {
    cout << k << endl;
    return 0;
  } else {
    res += k;
    k -= a;
  }
  if (k - b <= 0) {
    cout << a << endl;
    return 0;
  } else {
    k -= b;
    cout << a - k << endl;
    return 0;
  }

#ifdef TIMER_
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif
}


Error: 0

Fixed submission:

// #define TIMER_
#include <bits/stdc++.h>
using namespace std;
#define rep(i, r) for (int i = 0; i < (r); i++)
#define pb push_back
#define ALL(a) a.begin(), a.end()
#define fastio                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using ll = long long;
using ull = unsigned long long;

int main() {
  fastio;
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  if (k / (a + b) > 0) {
    k -= (a);
    cout << (a + b - k) << endl;
  } else {
    cout << a << endl;
    return 0;
  }

#ifdef TIMER_
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

#define debug(x) cout << '>' << #x << ':' << x << endl;
#define loop0(i, n) for (int i = 0; i < (n); i++)
#define loop1(i, n) for (int i = 1; i <= (n); i++)
#define fori(a, b, i) for (int i = (a); i <= (b); i++)
#define ford(a, b, i) for (int i = (a); i >= (b); i--)
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL);

#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define endl "\\n"
#define fir first
#define sec second
#define pb push_back
#define mp make_pair

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MII;
typedef set<int> SI;
typedef long long int llint;

int main() {
  fast_io llint a, b, c, k;
  cin >> a >> b >> c >> k;
  VI v;

  llint sum = 0;
  // cout << sum << " " << k << endl;
  if (a <= k) {
    sum += a;
    k = k - a;
  } else {
    sum = k;
    k = 0;
  }
  // cout << sum << " " << k << endl;
  if (b <= k) {
    k = k - b;
  } else {
    k = 0;
  }
  // cout << sum << " " << k << endl;
  sum -= k;
  // cout << sum << " " << k << endl;
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>

using namespace std;

#define debug(x) cout << '>' << #x << ':' << x << endl;
#define loop0(i, n) for (int i = 0; i < (n); i++)
#define loop1(i, n) for (int i = 1; i <= (n); i++)
#define fori(a, b, i) for (int i = (a); i <= (b); i++)
#define ford(a, b, i) for (int i = (a); i >= (b); i--)
#define fast_io ios_base::sync_with_stdio(false), cin.tie(NULL);

#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define endl "\\n"
#define fir first
#define sec second
#define pb push_back
#define mp make_pair

typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MII;
typedef set<int> SI;
typedef long long int llint;

int main() {
  fast_io llint a, b, c, k;
  cin >> a >> b >> c >> k;
  VI v;
  for (int i = 0; i < a; i++)
    v.pb(1);
  for (int i = 0; i < b; i++)
    v.pb(0);
  for (int i = 0; i < c; i++)
    v.pb(-1);
  llint sum = 0;
  for (int i = 0; i < k; i++) {
    sum += v[i];
  }
  cout << sum << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  long long a, b, c, k;
  cin >> a >> b >> c >> k;

  long long ans = 0;

  if (a < k) {

    ans += a;
  } else {
    ans += k;
  }

  long long c_ = k - a - b;

  if (c_ > 0) {
    ans -= c_;
  }

  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <numeric>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int a, b, c, d;
  cin >> a >> b >> c >> d;

  int ans = 0;

  rep(i, d) {
    if (a != 0) {
      ans++;
      a--;
      continue;
    } else if (b != 0) {
      b--;
      continue;
    } else if (c != 0) {
      c--;
      ans--;
      continue;
    }
  }

  cout << ans;

  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int64_t a, b, c, k;
  int64_t sum = 0;
  cin >> a >> b >> c >> k;
  if (a >= k) {
    sum = k;
  }
  if (a < k) {
    sum += a;
    if (b < k - a) {
      sum -= (k - a - b);
    }
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int a, b, c, k;
  int sum = 0;
  cin >> a >> b >> c >> k;
  for (int i = 0; i < k; i++) {
    if (a != 0) {
      sum += 1;
      a--;
    } else {
      if (b != 0) {
        b--;
      } else {
        sum -= 1;
      }
    }
  }
  cout << sum << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long a, b, c, k;
  cin >> a >> b >> c >> k;

  if (k <= a)
    cout << k;
  else if (k > a && k <= a + b)
    cout << a;
  else
    cout << a - (k - a - b);

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  long a, b, c, k, sum = 0;
  cin >> a >> b >> c >> k;

  while (k--) {
    if (a) {
      a--;
      sum++;
    } else if (b) {
      b--;
      continue;
    } else if (c) {
      c--;
      sum--;
    }
  }

  cout << sum;

  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int ans = 0;
  if (a > k) {
    cout << k << '\\n';
  } else {
    ans = a;
    k = k - a;
    if (k <= b) {
      cout << ans << '\\n';
    } else {
      k = k - b;
      if (k <= c) {
        ans = ans - k;
        cout << ans << '\\n';
      } else {
        ans = ans - c;
        cout << ans << '\\n';
      }
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int ans = 0;
  for (int i = k; i > 0; i--) {
    if (a > 0) {
      a = a - 1;
      ans += 1;
    } else {
      if (b > 0) {
        b = b - 1;
      } else {
        c = c - 1;
        ans = ans - 1;
      }
    }
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \\
  {                                                                            \\
    for (auto i : x) {                                                         \\
      cout << i << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;
  int ans = min(A, K);
  K -= min(A, K);
  if (K > 0) {
    K -= min(B, K);
  }
  if (K > 0) {
    ans -= K;
  }
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \\
  {                                                                            \\
    for (auto i : x) {                                                         \\
      cout << i << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;
  vector<int> X;
  rep(i, A) X.push_back(1);
  rep(i, B) X.push_back(0);
  rep(i, C) X.push_back(-1);
  int ans = 0;
  rep(i, K) ans += X[i];
  cout << ans << '\\n';
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<ll> vcl;

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}
ll c2(ll n) { return n * (n - 1) / 2; }

int main() {
  ll a, b, c, k;
  ll ans = 0;
  cin >> a >> b >> c >> k;
  if (a >= k)
    ans = k;
  else if (a < k && k <= a + b)
    ans = a;
  else if (a + b < k)
    ans = a - (k - a - b);
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define mod 1000000007
#define pi 3.141592653589793
typedef vector<ll> vcl;

ll gcd(ll a, ll b) {
  if (b > a)
    swap(a, b);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
ll rep_jijo(ll n, ll x) {
  if (x == 0)
    return 1;
  if (x % 2 == 0) {
    ll t = rep_jijo(n, x / 2);
    return t * t % mod;
  }
  return n * rep_jijo(n, x - 1) % mod;
}
ll c2(ll n) { return n * (n - 1) / 2; }

int main() {
  ll a, b, c, k;
  ll ans = 0;
  cin >> a >> b >> c >> k;
  vcl s(a + b + c);
  for (ll i = 0; i < a; i++) {
    s[i] = 1;
  }
  for (ll i = a; i < a + b; i++) {
    s[i] = 0;
  }
  for (ll i = a + b; i < a + b + c; i++) {
    s[i] = -1;
  }

  rep(i, k) { ans += s[i]; }
  cout << ans << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ll A, B, C, K;
  cin >> A >> B >> C >> K;
  ll result = 0;

  if (K <= A) {
    result = K;
  } else if (K <= A + B) {
    result = A;
  } else {
    result = A - (K - A - B);
  }
  cout << result;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

int main() {
  ll A, B, C, K;
  cin >> A >> B >> C >> K;
  ll result = 0;

  for (ll i = 0; i < K; i++) {
    // cout << result << "\\n";
    if (0 < A) {
      result++;
      A--;
      continue;
    }
    if (0 < B) {
      B--;
      continue;
    }
    if (0 < C) {
      result--;
      C--;
      continue;
    }
  }
  cout << result;

  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long A, B, C, K, ans = 0;
  cin >> A >> B >> C >> K;
  if (A >= K) {
    ans = K;
  } else if (A + B >= K) {
    ans = A;
  } else {
    ans = A - (K - A - B);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  long long A, B, C, K, ans = 0;
  cin >> A >> B >> C >> K;
  for (long long i = 0; i < K; i++) {
    if (A != 0) {
      ans += 1;
      A--;
    } else if (B != 0) {
      B--;
    } else if (C != 0) {
      ans -= 1;
      C--;
    } else {
      break;
    }
  }
  cout << ans << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author emli
 */

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

class BEasyLinearProgramming {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    long long a, b, c, k;
    cin >> a >> b >> c >> k;

    if (a >= k) {
      cout << k << endl;
    } else {
      cout << a - (k - (a + b)) << endl;
    }
  }
};

int main() {
  BEasyLinearProgramming solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}


Error: 0

Fixed submission:

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author emli
 */

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

class BEasyLinearProgramming {
public:
  void solve(std::istream &cin, std::ostream &cout) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    long long a, b, c, k;
    cin >> a >> b >> c >> k;

    if (a >= k || a + b >= k) {
      cout << a << endl;
    } else {
      long long d = abs((a + b) - k) * -1;
      if (d <= c) {
        cout << a + d << endl;
        cout << 1 / 0 << endl;
      }
    }
  }
};

int main() {
  BEasyLinearProgramming solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using VI = vector<int>;

int main() {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll ans = 0;

  if (b == 0 && c == 0) {
    cout << a << endl;
    return 0;
  }
  /*else {
      if (a + b >= k) {
          cout << a << endl;
          return 0;
      }
      else {
          cout << a - (k - c) << endl;
          return 0;
      }
  }*/

  for (int i = 0; i < k; i++) {
    if (a != 0) {
      ans++;
      a--;
    } else if (b != 0) {
      b--;
    } else if (c != 0) {
      // ans--;
      // c--;
      ans -= k - i;
      break;
    }

    if (a == 0) {
      if (b + i >= k) {
        break;
      } else {
        i += b;
        b = 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
using VI = vector<int>;

int main() {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll ans = 0;

  if (b == 0 && c == 0) {
    cout << a << endl;
    return 0;
  }

  for (int i = 0; i < k; i++) {
    if (a != 0) {
      ans++;
      a--;
    } else if (b != 0) {
      b--;
    } else if (c != 0) {
      ans--;
      c--;
    }

    if (a == 0) {
      if (b + i >= k) {
        break;
      } else {
        i += b;
        b = 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(void) {
  int A, B, C, K;
  cin >> A >> B >> C >> K;
  int sum = 0;
  if (A < K) {
    sum += A;
    K -= A;
    if (B < K) {
      K -= B;
      sum -= K;
    }
  } else {
    sum += K;
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(void) {
  int A, B, C, K;
  cin >> A >> B >> C >> K;
  int sum = 0;
  rep(i, K) {
    if (A != 0) {
      sum += 1;
      A--;
    } else if (B != 0) {
      B--;
    } else {
      sum -= 1;
      C--;
    }
  }
  cout << sum << endl;
  return 0;
}