alexjercan/bugnet huggingface dataset:
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;

  while (true) {

    if (K == A) {
      ANS = A * 1;
      break;
    }

    if (K < A) {
      ANS = K * 1;
      break;
    }

    if (K > A) {
      if (A != 0) {
        ANS = A * 1;
        K -= A;
      }
    }

    if (K == B) {
      break;
    }

    if (K < B) {
      break;
    }

    if (K > B) {
      K -= B;
    }

    ANS -= K;
    break;
  }

  cout << ANS << endl;
}

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 = 0; i < K; i++) {
    if (A != 0) {
      ANS += 1;
      A -= 1;
      continue;
    }

    if (B != 0) {
      ANS += 0;
      B -= 1;
      continue;
    }

    if (C != 0) {
      ANS -= 1;
      C -= 1;
      continue;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define _GLIBCXX_DEBUG
#define rep(i, c) for (int i = 0; i < (int)c; i++)
#define debug(x) cout << #x << " = " << x << endl
#define print(x) cout << x << endl;
const ll MOD = 1000000007;

int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;

  if (K < A) {
    cout << K << endl;
  }

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

  else {
    cout << A - (K - A - B) << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define _GLIBCXX_DEBUG
#define rep(i, c) for (int i = 0; i < (int)c; i++)
#define debug(x) cout << #x << " = " << x << endl
#define print(x) cout << x << endl;
const ll MOD = 1000000007;

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

  ll ans = 0;
  rep(i, K) {
    if (A > 0) {
      A--;
      ans++;
    } else if (B > 0) {
      B--;
    } else {
      C--;
      ans--;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define xrep(i, a, b) for (int i = int(a); i < int(b); ++i)
using ll = long long;
int main() {
  ll A, B, C, K;
  cin >> A >> B >> C >> K;

  ll ans = 0;

  rep(i, K) {
    if (A != 0) {
      ans += 1;
      A--;
    } else if (B != 0) {
      B--;
    } else if (C != 0) {
      ans += -1 * (K - i);
      break;
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define xrep(i, a, b) for (int i = int(a); i < int(b); ++i)
using ll = long long;
int main() {
  ll A, B, C, K;
  cin >> A >> B >> C >> K;

  ll ans = 0;

  rep(i, K) {
    if (A != 0) {
      ans += 1;
      A--;
      continue;
    }
    if (B != 0) {
      B--;
      continue;
    }
    if (C != 0) {
      ans += -1;
      C--;
      continue;
    }
  }

  cout << ans << endl;

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

#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

const int mod = 1000000007;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
#define ll long long

int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;

  int sum = 0;
  if (K - A > 0) {
    sum += A;
  } else {
    sum += K;
  }

  K -= A;

  if (K > 0) {
    K -= B;
  }

  if (K > 0) {
    if (K - C > 0) {
      sum -= C;
    } else {
      sum -= K;
    }
  }

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

const int mod = 1000000007;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))
#define ll long long

int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;

  int sum = 0;
  while (K > 0) {
    if (A > 0) {
      A--;
      sum++;
    } else if (B > 0) {
      B--;
    } else if (C > 0) {
      C--;
      sum--;
    }
    K--;
  }

  cout << sum << endl;

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


#include <iostream>
using namespace std;

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

  cin >> a >> b >> c >> k;

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

  while (1) {
    if (k == 0) {
      break;
    }

    if (a > 0) {
      ans++;
      a--;

      k--;
      continue;
    }
    if (b > 0) {
      b--;
      k--;
      continue;
    }
    if (c > 0) {
      ans--;
      c--;
      k--;
      continue;
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:


#include <iostream>
using namespace std;

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

  cin >> a >> b >> c >> k;

  while (1) {
    if (k == 0) {
      break;
    }

    if (a > 0) {
      ans++;
      a--;

      k--;
      continue;
    }
    if (b > 0) {
      b--;
      k--;
      continue;
    }
    if (c > 0) {
      ans--;
      c--;
      k--;
      continue;
    }
  }

  cout << ans << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int A, B, C, K, ans = 0;
  cin >> A >> B >> C >> K;
  if (K <= A) {
    cout << K;
  } else if (A < K && K <= A + B) {
    cout << A;
  } else if (A + B < K && K <= A + B + C) {
    cout << A + (K - A - B) * (-1);
  } else {
    cout << A - C;
  }
}


Error: WA

Fixed submission:

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

int main() {
  int A, B, C, K, ans = 0;
  cin >> A >> B >> C >> K;
  int num[A + B + C];
  for (int i = 0; i < A; i++) {
    num[i] = 1;
  }
  for (int i = 0; i < B; i++) {
    num[A + i] = 0;
  }
  for (int i = 0; i < C; i++) {
    num[A + B + i] = -1;
  }
  for (int i = 0; i < K; i++) {
    ans += num[i];
  }
  cout << ans;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;
  ll sum = 0;
  if (K >= A) {
    sum += A;
    K = K - A;
  } else {
    sum += K;
    cout << sum << endl;
    return 0;
  }
  if (K >= B) {
    K = K - B;
  } else {
    cout << sum << endl;
    return 0;
  }

  if (K >= C) {
    sum -= C;
    K = K - C;
  } else {
    sum -= K;
    cout << sum << endl;
    return 0;
  }
  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;
  ll sum = 0;
  ll card = 0;
  while (true) {
    if (card == K)
      break;
    if (A > 0) {
      sum += 1;
      card++;
      A--;
    } else if (A == 0 && B > 0) {
      card++;
      B--;
    } else if (A == 0 && B == 0 && C > 0) {
      sum--;
      card++;
      C--;
    }
  }
  cout << sum << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define PI 3.1415926535900
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = 0; (int)(n) < i; i--)
#define Int long long
using namespace std;

int main() {

  int a[4];
  cin >> a[0] >> a[1] >> a[2] >> a[3];
  int v = a[3];
  int sum = 0;

  if (v >= a[0]) {
    // 1
    sum += a[0];
    v = v - a[0];
    if (v >= a[1]) {
      // 0
      v = v - a[1];
      if (v >= a[2]) {

        //-1
        sum -= a[2];
        v = v - a[2];
        cout << sum << endl;
      } else {

        sum -= v;
        cout << sum << endl;
      }
    } else {
      cout << sum << endl;
    }
  } else {
    cout << a[3] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define PI 3.1415926535900
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = 0; (int)(n) < i; i--)
#define Int long long
using namespace std;

int main() {

  int a[4];
  cin >> a[0] >> a[1] >> a[2] >> a[3];
  int v = a[3];
  int sum = 0;
  while (a[3] > 0) {
    if (a[0] > 0) {
      a[0] -= 1;
      sum += 1;
    } else if (a[1] > 0) {
      a[1] -= 1;
    } else if (a[2] > 0) {
      a[2] -= 1;
      sum -= 1;
    }
    a[3] -= 1;
  }
  cout << sum << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <bitset>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <type_traits>
#include <utility>
#include <vector>
#define INT_MAX 2147483647
#define _LIBCPP_DEBUG 0
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int max(int a, int b) {
  int max;
  if (a > b)
    max = a;
  else
    max = b;
  return max;
}
int min(int a, int b) {
  int min;
  if (a < b)
    min = a;
  else
    min = b;
  return min;
}

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

  int sum = 0;

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

  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <type_traits>
#include <utility>
#include <vector>
#define INT_MAX 2147483647
#define _LIBCPP_DEBUG 0
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;

int max(int a, int b) {
  int max;
  if (a > b)
    max = a;
  else
    max = b;
  return max;
}
int min(int a, int b) {
  int min;
  if (a < b)
    min = a;
  else
    min = b;
  return min;
}

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

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

#include <bits/stdc++.h>
#define endl '\\n'
using namespace std;

int main() {

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

  int A, B, C, K, ans = 0;
  cin >> A >> B >> C >> K;

  if (A > K)
    cout << K << endl;
  else if (A + B > K)
    cout << A << endl;
  else
    cout << A - (K - A - B) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl '\\n'
using namespace std;

int main() {

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

  int A, B, C, K, 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 if (C > 0) {
      ans--;
      C--;
    }
  }
  cout << ans << endl;
  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 all(x) x.begin(), x.end()
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 sum = 0;
  if (a <= k) {
    sum += a;
    k -= a;
  } else {
    cout << k << endl;
    return 0;
  }
  if (b <= k)
    k -= b;
  else {
    cout << sum << endl;
    return 0;
  }
  if (c <= k)
    sum -= c;
  else
    sum -= k;
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using p = pair<int, int>;

int main() {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll sum = a + b + c;
  vector<int> v;
  ll ans = 0;

  rep(i, a) v.push_back(1);
  rep(i, b) v.push_back(0);
  rep(i, c) v.push_back(-1);
  rep(i, k) ans += v[i];

  cout << ans << endl;

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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#define arep(a, i, n) for (int i = (a); i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define cinf(x, n)                                                             \\
  for (int i = 0; i < (n); i++)                                                \\
    cin >> x[i];
#define coutf(x, n)                                                            \\
  for (int i = 0; i < (n); i++)                                                \\
    cout << x[i] << endl;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using namespace std;

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

Error: TLE

Fixed submission:

#include <iostream>

#define arep(a, i, n) for (int i = (a); i < (n); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define cinf(x, n)                                                             \\
  for (int i = 0; i < (n); i++)                                                \\
    cin >> x[i];
#define coutf(x, n)                                                            \\
  for (int i = 0; i < (n); i++)                                                \\
    cout << x[i] << endl;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

using namespace std;

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

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

int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;

  // multiset<int> bag;
  int n = 0;
  int res = 0;

  for (int i = 0; i < A; i++) {

    if (n >= K) {
      cout << res << endl;
      return 0;
      // break;
    }
    res++;
    n++;
    // cout << res << endl;
  }
  n = n + B;
  for (int i = 0; i < C; i++) {
    // cout << res << " " << n << endl;
    if (n >= K) {
      cout << res << endl;
      return 0;
      // break;
    }
    res--;
    n++;
  }

  cout << res << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;

  multiset<int> bag;

  for (int i = 0; i < A; i++) {
    bag.insert(1);
  }
  for (int i = 0; i < B; i++) {
    bag.insert(0);
  }
  for (int i = 0; i < C; i++) {
    bag.insert(-1);
  }

  auto itr = bag.rbegin();

  int i = 0;
  int res = 0;
  while (i < K) {
    res += *itr;
    ++itr;
    i++;
  }

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

// A code
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ll long long
#define vec vector<ll>
#define pb push_back
#define po pop_back
#define f(i, x, n) for (ll i = x; i < n; i++)
#define rf(i, x, n) for (ll i = x; i >= n; i--)
#define all(c) c.begin(), c.end()
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 2e3 + 10;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll sum = 0;
  if (k <= a) {
    cout << k;
  } else if (k > a && a + b >= k) {
    cout << a;
  } else {
    cout << 2 * a + b - k;
  }
}

Error: TLE

Fixed submission:

// A code
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define ll long long
#define vec vector<ll>
#define pb push_back
#define po pop_back
#define f(i, x, n) for (ll i = x; i < n; i++)
#define rf(i, x, n) for (ll i = x; i >= n; i--)
#define all(c) c.begin(), c.end()
using pii = pair<int, int>;
const int MOD = 1e9 + 7, N = 2e3 + 10;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll sum = 0;
  while (k > 0) {
    if (a != 0) {
      sum += 1;
      k--;
      a--;
    } else if (b != 0) {
      sum += 0;
      k--;
      b--;
    } else if (c != 0) {
      sum += -1;
      k--;
      c--;
    };
  }
  cout << sum << "\\n";
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  long long A, B, C, K;
  cin >> A >> B >> C >> K;
  long long ans = 0;

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

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  long long A, B, C, K;
  cin >> A >> B >> C >> K;

  int ans = 0;

  for (int i = 0; i < K; i++) {
    if (A > 0) {
      ans++;
      A--;
      continue;
    }
    if (B > 0) {
      B--;
      continue;
    }
    if (C > 0) {
      ans--;
      C--;
      continue;
    }
  }

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

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

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
vector<int> A(200001);
vector<string> s(200001);
map<int, int> sum;
int n, ans;

int main(void) {
  int a, b, c, k;
  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--;
    }
  }
  cout << ans << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int mod = 1000000007;
const double PI = 3.14159265359;
#define INF 1000000000000
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORA(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define count_1(n) __builtin_popcountll(n)
#define parity(n) __builtin_parityll(n) // Even parity 0 as output
void show(int a[], int arraysize) {
  for (int i = 0; i < arraysize; ++i) {
    cout << a[i] << " ";
  }
  cout << endl;
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void sieve(int n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
  for (int p = 2; p <= n; p++)
    if (prime[p])
      cout << p << " ";
}
int main() {
  FAST ll a, b, c, n, k = 0;
  cin >> a >> b >> c >> n;
  if (n <= a) {
    k += n;
  } else if (n > a && n <= a + b) {
    k += a;
  } else {
    k += a;
    k -= (n - (a + b));
  }

  cout << k << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const int mod = 1000000007;
const double PI = 3.14159265359;
#define INF 1000000000000
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define FOR(i, n) for (int i = 0; i < (n); i++)
#define FORA(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define FAST                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define count_1(n) __builtin_popcountll(n)
#define parity(n) __builtin_parityll(n) // Even parity 0 as output
void show(int a[], int arraysize) {
  for (int i = 0; i < arraysize; ++i) {
    cout << a[i] << " ";
  }
  cout << endl;
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
void sieve(int n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
  for (int p = 2; p <= n; p++)
    if (prime[p])
      cout << p << " ";
}
int main() {
  FAST ll a, b, c, n, k = 0;
  cin >> a >> b >> c >> n;
  while (n > 0) {
    if (a > 0) {
      k += 1;
      n--;
      a--;
    } else if (b > 0) {

      n--;
      b--;
    } else {
      k -= 1;
      n--;
      c--;
    }
  }
  cout << k << endl;
  return 0;
}
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;

  long sum = 0;

  if (k > 0 && a > 0) {
    int x = min(a, k);
    sum += x;
    k -= x;
  }

  if (k > 0 && b > 0) {

    int x = min(b, k);
    k -= x;
  }

  if (k > 0 && c > 0) {

    int x = min(c, k);
    sum -= x;
    k -= x;
  }

  cout << sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {

  long a, b, c, k;

  cin >> a >> b >> c >> k;

  long sum = 0;

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

  cout << sum << endl;

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

#include <bits/stdc++.h>
using namespace std;
int a, b, c, n, s;
int main() {
  cin >> a >> b >> c >> n;
  if (n <= a)
    cout << n;
  else if (n <= a + b)
    cout << a;
  else
    cout << a - (n - a - b);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int a, b, c, n, s;
int main() {
  cin >> a >> b >> c >> n;
  for (int i = 1; i <= n; i++) {
    if (a != 0)
      a--, s++;
    else if (b != 0)
      b--;
    else if (c != 0)
      c--, s--;
  }
  cout << s;
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

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

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

  int64_t d = k - (a + b);
  if (c < d)
    cout << a - c << endl;
  else
    cout << a - d << endl;

  return 0;
}


Error: 255

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

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

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

  int64_t d = k - (a + b);
  if (c < d)
    cout << a - c << endl;
  else
    cout << a - d << endl;

  return 0;
}