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

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define l(i, a, b) for (auto i = a; i < b; i++)
#define lr(i, a, b) for (auto i = a; i >= b; i--)
#define ll long long
#define lli long long int
#define ld long double
#define vec vector<int>
#define mii map<ll, ll>
#define vl vector<long long>
#define se set<long long int>
#define pi pair<long long, long long>
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define all(v) v.begin(), v.end()
#define in(x) cin >> x
#define o(x) cout << x
#define SYNC                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
int main() {
  SYNC ll a, b, c, k, sum = 0;
  in(a) >> b >> c >> k;
  if (k == a + b + c) {
    sum += (a - c);
  } else if (k <= a + b && k >= a) {
    sum += a;
  } else if (k > a + b) {
    sum += a;
    k -= (a + b);
    sum -= k;
  } else if (k < a) {
    sum += k;
  }
  o(sum);
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define l(i, a, b) for (auto i = a; i < b; i++)
#define lr(i, a, b) for (auto i = a; i >= b; i--)
#define ll long long
#define lli long long int
#define ld long double
#define vec vector<int>
#define mii map<ll, ll>
#define vl vector<long long>
#define se set<long long int>
#define pi pair<long long, long long>
#define pb push_back
#define mp make_pair
#define ft first
#define sd second
#define all(v) v.begin(), v.end()
#define in(x) cin >> x
#define o(x) cout << x
#define SYNC                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
using namespace std;
int main() {
  SYNC ll a, b, c, k, sum = 0;
  in(a) >> b >> c >> k;
  l(i, 0, k) {
    if (a > 0) {
      sum += 1;
      a--;
    } else if (b > 0) {
      sum += 0;
      b--;
    } else
      sum -= 1;
  }
  o(sum);
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  long long int A, B, C, K, M;

  M = 0;

  cin >> A >> B >> C >> K;

  if (A <= K) {
    K -= A;

    M += A;

    if (B <= K) {
      K -= B;

      if (C == K) {
        M -= C;
      } else {
        M -= K;
      }
    }
  } else {
    M += K;
  }

  cout << M << endl;
}

Error: TLE

Fixed submission:

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

int main() {

  long long int A, B, C, K, M;

  M = 0;

  cin >> A >> B >> C >> K;

  while (K > 0) {

    if (A > 0) {

      M++;

      A--;

    } else if (B > 0) {

      B--;

    } else {

      M--;

      C--;
    }

    K--;
  }

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

#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

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


Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
using P = pair<int, int>;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef long long ll;

int main() {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll ans = 0;
  rep(i, k) {
    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: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int judg() {
  int A, B, C, K, sum = 0;
  cin >> A >> B >> C >> K;
  if (K < 0)
    return sum;

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

  if (K - B > 0)
    K -= B;
  else
    return sum;

  return sum - K;
}

int main() {
  cout << judg() << endl;
  return 0;
}

Error: 1

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int end(int result) {
  cout << result << endl;
  exit(1);
}
int main() {
  int A, B, C, K, sum = 0;
  cin >> A >> B >> C >> K;
  if (K < 0)
    end(sum);

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

  if (K - B > 0)
    K -= B;
  else
    end(sum);

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

#include <iostream>
using namespace std;

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

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

  cout << sum << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int A, B, C, K;
  int sum = 0;
  int pre_sum = -2000000000;
  cin >> A >> B >> C >> K;

  for (int a = 0; a <= A; a++) {
    for (int c = 0; c <= C; c++) {
      if (a + B + c == K) {
        sum = a * 1 + c * (-1);
        if (sum >= pre_sum) {
          pre_sum = sum;
        }
      }
    }
  }

  cout << sum << endl;

  return 0;
}
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 ans = 0;
  ll count = k;
  if (a > 0) {
    if (a >= count) {
      ans += count;
      cout << ans;
      return 0;
    }
    if (a < count) {
      ans += a;
      count -= a;
    }
  }
  if (b > 0) {
    if (b >= count) {
      cout << ans;
      return 0;
    }
    if (b < count) {
      count -= b;
    }
  }
  if (c > 0) {
    ans -= count;
    cout << ans;
    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 ans = 0;
  for (int i = 0; i < k; ++i) {
    if (a > 0) {
      ++ans;
      --a;
      continue;
    }
    if (b > 0) {
      --b;
      continue;
    }
    if (c > 0) {
      --c;
      --ans;
      continue;
    }
  }
  cout << ans;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  long A, B, C, K, Ans = 0, count = 1;

  cin >> A >> B >> C >> K;

  for (int i = 1; i <= A; i++) {
    if (count > K)
      break;
    Ans++;
    count++;
  }

  count += B;

  for (int i = 1; i <= C; i++) {
    if (count > K)
      break;
    Ans--;
    count++;
  }

  cout << Ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long A, B, C, K, Ans = 0, count = 1;

  cin >> A >> B >> C >> K;

  for (int i = 1; i <= A; i++) {
    if (count > K)
      break;
    Ans++;
    count++;
  }

  for (int i = 1; i <= B; i++) {
    if (count > K)
      break;
    count++;
  }

  for (int i = 1; i <= C; i++) {
    if (count > K)
      break;
    Ans--;
    count++;
  }

  cout << Ans << endl;

  return 0;
}
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 sum = 0;
  if ((A + B) >= K && A < K) {
    sum = A;
  } else if (A >= K) {
    sum = K;
  } else {
    sum = A - (K - A - B);
  }
  cout << sum << 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;
  long long sum = 0;
  for (int i = 0; i < K; ++i) {

    if (A > 0) {
      sum++;
      A--;
    } else if (B > 0) {
      B--;
    } else if (C > 0) {
      sum--;
      C--;
    }
  }
  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++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long
#define INF 999999999
using namespace std;
int main() {
  ll a, b, c, k;
  ll 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;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ll long long
#define INF 999999999
using namespace std;
int main() {
  ll a, b, c, k;
  ll ans = 0;
  cin >> a >> b >> c >> k;
  while (k != 0) {
    k--;
    if (a) {
      ans++;
      a--;
      continue;
    }
    if (b) {
      b--;
      continue;
    }
    if (c) {
      ans--;
      c--;
      continue;
    }
  }
  cout << ans << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int a, b, c, k;      // 入力
  int i = 0, flag = 0; // 内部で使用
  int ans = 0;         // 出力

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

  for (i = 0; i < k; i++) {
    if (a > 0) {
      ans++;
      a--;
    } else if (b > 0) {
      b--;
    } else {
      flag = 1;
      break;
    }
  }
  if (i < k and flag == 1) {
    while (i < k) {
      ans--;
      c--;
      i++;
    }
  }
  cout << ans << "\\n";
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  double n, kari;
  int a, b, c, k;                 // 入力
  int i = 0, j, x, max, flag = 0; // 内部で使用
  int ans = 0;                    // 出力

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

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

  cout << ans << "\\n";
}
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 < (int)(n); i++)
int main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int count = 0;
  rep(i, k) {
    rep(j, a) {
      if (k == 0) {
        break;
      }
      count++;
      k--;
    }
    rep(j, b) {
      if (k == 0) {
        break;
      }
      k--;
    }
    rep(j, c) {
      if (k == 0) {
        break;
      }
      count--;
      k--;
    }
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int count = 0;
  rep(i, a + b + c) {
    rep(j, a) {
      if (k == 0) {
        break;
      }
      count++;
      k--;
    }
    rep(j, b) {
      if (k == 0) {
        break;
      }
      k--;
    }
    rep(j, c) {
      if (k == 0) {
        break;
      }
      count--;
      k--;
    }
  }
  cout << count << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define rep(i, cc, n) for (int i = cc; i <= n; ++i)
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: 0

Fixed submission:

#include <iostream>
#define rep(i, cc, n) for (int i = cc; i <= n; ++i)
using namespace std;

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

  int P[A + B + C];
  rep(i, 0, A + B + C - 1) {
    if (i < A)
      P[i] = 1;
    else if (i < A + B)
      P[i] = 0;
    else
      P[i] = -1;
  }

  int ans = 0;
  rep(i, 0, K - 1) { ans += P[i]; }

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

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define mod 1000000007
#define pb push_back

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll i, n, n2, m, t, j, l, pos, mpos, f = 0, k, p, n1, d, z, q, x, y, x1, y1,
                                      x2, y2;

  /*cin>>t;


 while(t--)
{


}*/
  ll a, b, c, s = 0;
  cin >> a >> b >> c >> k;

  if (a < k) {

    k -= a;
  } else {
    cout << k;
    return 0;
  }
  if (b < k) {

    k -= b;
  } else {
    cout << a;
    return 0;
  }
  cout << a - k;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define mod 1000000007
#define pb push_back

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll i, n, n2, m, t, j, l, pos, mpos, f = 0, k, p, n1, d, z, q, x, y, x1, y1,
                                      x2, y2;

  /*cin>>t;


 while(t--)
{


}*/
  ll a, b, c, s = 0;
  cin >> a >> b >> c >> k;

  while (k--) {
    if (a > 0) {
      s++;
      a--;
    } else if (b > 0) {
      b--;
    } else
      s--;
  }
  cout << s;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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


Error: TLE

Fixed submission:

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

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

  cout << sum << '\\n';
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <random>
using namespace std;

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

  unsigned int i;
  int ans = 0;

  if (A - K < 0) {
    ans = A;

    int tmp_K = K - A;
    if (B - tmp_K < 0) {
      int tmp_K2 = tmp_K - B;
      ans -= tmp_K2;
    }
  } else {
    ans = K;
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <random>
using namespace std;

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

  unsigned int i;
  int ans = 0;
  picked_A = picked_B = picked_C = 0;
  for (i = 0; i < K; i++) {
    if (picked_A < A) {
      ans += 1;
      picked_A++;
    } else if (picked_B < B) {
      picked_B++;
    } else {
      ans -= 1;
      picked_C++;
    }
  }
  cout << ans << endl;
  return 0;
}
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;

  int cnt = 0;

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

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

Error: TLE

Fixed submission:

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

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

  int cnt = 0;

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

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

int32_t main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  if (a >= k) {
    cout << k;
  } else if (a <= k && a + b >= k) {
    cout << a;
  } else {
    cout << 2 * a + b - k;
  }
}

Error: TLE

Fixed submission:

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

int32_t main() {
  ios_base::sync_with_stdio(0);
  cout.tie(0);
  cin.tie(0);
  int a, b, c, k, s = 0, p = 1;
  cin >> a >> b >> c >> k;
  for (int i = 0; i < k; i++) {
    if (p <= a) {
      s++;
      p++;
    } else if (p > a && p - a <= b) {
      p++;
    } else {
      s--;
      p++;
    }
  }
  cout << s;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;
#define rep(i, n) for (int64_t i = 0; i < n; i++)

const double PI = 3.14159265358979323846;

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

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

    } else {
      k -= b;
      if (c > k) {
        ans -= k;
      } else {
        ans -= c;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:


#include "bits/stdc++.h"

using namespace std;
#define rep(i, n) for (int64_t i = 0; i < n; i++)

const double PI = 3.14159265358979323846;

vector<string> strrr;
int64_t max_depth;

string search(int64_t a, int64_t b, int64_t c, string str, int64_t depth) {
  string temp = "", temp1 = "";
  if (a < 0 || b < 0 || c < 0) {
    return "0";
  }

  if (max_depth == depth) {
    return str;
  }

  if (strrr[depth] == "AB") {
    temp = search(a + 1, b - 1, c, str + "A", depth + 1);
    if (temp != "0") {
      return temp;
    }
    temp1 = search(a - 1, b + 1, c, str + "B", depth + 1);
  } else if (strrr[depth] == "AC") {
    temp = search(a + 1, b, c - 1, str + "A", depth + 1);
    if (temp != "0") {
      return temp;
    }
    temp1 = search(a - 1, b, c + 1, str + "C", depth + 1);
  } else {
    temp = search(a, b + 1, c - 1, str + "B", depth + 1);
    if (temp != "0") {
      return temp;
    }
    temp1 = search(a, b - 1, c + 1, str + "C", depth + 1);
  }

  if (temp1 != "0") {
    return temp1;
  } else {
    return "0";
  }
}

int main() {
  int64_t a, b, c, k;
  int64_t ans = 0;
  cin >> a >> b >> c >> k;
  for (int64_t i = 0; i < k; i++) {
    if (a > 0) {
      a--;
      ans++;
    } else if (b > 0) {
      b--;
    } else {
      c--;
      ans--;
    }
  }

  cout << ans << endl;

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

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, k, ans = 0;
  cin >> a >> b >> c >> k;
  while (k--) {
    if (a)
      a--, ans++;
    else if (b)
      b--;
    else
      ans--;
  }
  cout << ans;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

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

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
int main() {
  long long a, b, c, k;
  std::cin >> a >> b >> c >> k;

  std::vector<long long> vec;

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

  for (long long i = 0; i < b; i++) {
    vec.push_back(0);
  }

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

  int ans = 0;
  for (long long i = 0; i < k; i++)
    ans += vec[i];
  std::cout << ans << std::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;
  int maxx = -2000000000;
  if (a >= k) {
    int z = k;
    maxx = max(z, maxx);
  } else if (a + b + c < k && k <= a + b) {
    int z = a;
    maxx = max(z, maxx);
  } else {
    int d = k - a - b;
    int z = a + d * (-1);
    maxx = max(z, maxx);
  }
  cout << maxx << endl;
}

Error: TLE

Fixed submission:

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

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

#include <iostream>
using namespace std;
int main() {
  long long int a, b, c, k, s, re;
  cin >> a >> b >> c >> k;

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

    cout << s;
  }
}


Error: WA

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int main() {
  int a, b, c, k, s, re;
  cin >> a >> b >> c >> k;
  if (a < b && b < c && c < k) {
    cout << "0";
  } else if (a >= k) {
    cout << a;
  } else {
    re = k % a;
    s = a;
    if (re != 0) {
      re = re % b;
    }
    if (re != 0) {
      re = re % c;
      s = s - re;
    }
    cout << s;
  }
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define lli long long int

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  lli a = 0, b = 0, c = 0, k = 0, count = 0;
  cin >> a >> b >> c >> k;

  while (k != 0) {
    if (a == k) {
      count = a;
      break;
    }
    if (a != 0) {
      count++;
      a--;
    } else if (b != 0) {
      b--;
    } else {
      count--;
      c--;
    }
    k--;
  }
  cout << count;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define lli long long int

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  lli a = 0, b = 0, c = 0, k = 0, count = 0;
  cin >> a >> b >> c >> k;

  while (k != 0) {
    if (a != 0) {
      count++;
      a--;
    } else if (b != 0) {
      b--;
    } else {
      count--;
      c--;
    }
    k--;
  }
  cout << count;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

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

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

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

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

  cout << ans << endl;

  return 0;
}