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

#include <bits/stdc++.h>
#include <cmath>
#include <math.h>
#include <stdio.h>
using namespace std;
#define vsort(vec) sort(vec.begin(), vec.end())
#define vreverse(vec) reverse(vec.begin(), vec.end())
using ll = long long;
using ull = unsigned long long;
const ll INF = 1001001001;

// 10億7。　　　100000007

// 最大公約数
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 * b / gcd(a, b); }

int main() {
  cout << fixed << setprecision(10);
  // cin.tie(0); ios::sync_with_stdio(false);

  ll A, B, C, K;
  cin >> A >> B >> C >> K;

  ll sum = 0;
  sum += min(K, A);
  K -= A;
  K -= B;
  if (K > 0 && C > 0) {
    sum -= min(K, C);
  }
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <math.h>
#include <stdio.h>
using namespace std;
#define vsort(vec) sort(vec.begin(), vec.end())
#define vreverse(vec) reverse(vec.begin(), vec.end())
using ll = long long;
using ull = unsigned long long;
const ll INF = 1001001001;

// 10億7。　　　100000007

// 最大公約数
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 * b / gcd(a, b); }

int main() {
  cout << fixed << setprecision(10);
  // cin.tie(0); ios::sync_with_stdio(false);

  ll A, B, C, K;
  cin >> A >> B >> C >> K;

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

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

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

int main() {
  int ap, a0, am, k;
  cin >> ap >> a0 >> am >> k;
  if (a0 == 0 && am == 0) {
    cout << k << endl;
    return 0;
  }
  long long ans = 0;
  for (int i = 0; i < k; i++) {
    if (ap > 0) {
      ap--;
      ans++;
    } else if (a0 > 0) {
      a0--;
    } else {
      am--;
      ans--;
    }
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int ap, a0, am, k;
  cin >> ap >> a0 >> am >> k;
  long long ans = 0;
  for (int i = 0; i < k; i++) {
    if (ap > 0) {
      ap--;
      ans++;
    } else if (a0 > 0) {
      a0--;
    } else {
      am--;
      ans--;
    }
  }

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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef long double ld;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // 最大公約数
ll lcm(ll a, ll b) { return a * b / gcd(a, b); };    // 最小公倍数
ll getDigit(ll num) { return log10(num) + 1; }       // 桁数
ll digitSum(ll n) {
  return (n < 10) ? n : digitSum(n / 10) + n % 10;
} // 各桁の和
#define MOD 1e9 + 7
#define pb push_back
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repn(i, n) for (ll i = 1; i <= n; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define all(x) (x).begin(), (x).end()

void Main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  if (a >= k) {
    cout << k << endl;
  } else if (a + b >= k) {
    cout << a << endl;
  } else {
    int tmp = k - a - b;
    cout << a - tmp << endl;
  }

  return;
}

signed main(void) {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef long double ld;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } // 最大公約数
ll lcm(ll a, ll b) { return a * b / gcd(a, b); };    // 最小公倍数
ll getDigit(ll num) { return log10(num) + 1; }       // 桁数
ll digitSum(ll n) {
  return (n < 10) ? n : digitSum(n / 10) + n % 10;
} // 各桁の和
#define MOD 1e9 + 7
#define pb push_back
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repn(i, n) for (ll i = 1; i <= n; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define all(x) (x).begin(), (x).end()

void Main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  vector<int> list;
  rep(i, a) list.push_back(1);
  rep(i, b) list.push_back(0);
  rep(i, c) list.push_back(-1);
  int ans = 0;
  rep(i, k) { ans += list[i]; }

  cout << ans << endl;
  return;
}

signed main(void) {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  Main();
  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++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main(void) {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll ans = 0;
  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 rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main(void) {
  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--;
    } 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>
using namespace std;

int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;
  int a = 0, b = 0, c = 0;
  if (A >= K) {
    a = K;
    cout << a << endl;
    return 0;
  }
  K -= A;
  if (B >= K) {
    a = A;
    b = K;
    cout << a << endl;
    return 0;
  }
  K -= B;
  if (C >= K) {
    a = A;
    b = B;
    c = K;
    cout << a - c << endl;
    return 0;
  }
  cout << A - C << 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 a = 0, b = 0, c = 0;
  for (int i = 0; i < K; i++) {
    if (a < A)
      a++;
    else if (b < B)
      b++;
    else if (c < C)
      c++;
  }
  cout << a - c << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; i++)

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

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; i++)

int main() {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  vector<ll> p(a + b + c);
  for (ll i = 0; i < a; i++)
    p[i] = 1;
  for (ll i = a; i < a + b; i++)
    p[i] = 0;
  for (int i = a + b; i < a + b + c; i++)
    p[i] = -1;
  ll ans = 0;
  rep(i, k) ans += p[i];
  cout << ans << "\\n";
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using ll = long long;
#define rep(i, a, b) for (ll i = a; i < ll(b); i++)
#define repr(i, a, b) for (ll i = a; i >= ll(b); i--)
#define endl "\\n"
#define ALL(x) x.begin(), x.end()
#define ALLR(x) x.rbegin(), x.rend()
using namespace std;

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

  ans += min(a, k);
  k -= min(a, k);

  if (k <= 0) {
    cout << ans << endl;
    return 0;
  }

  k -= min(b, k);

  if (k <= 0) {
    cout << ans << endl;
    return 0;
  }

  ans -= min(c, k);
  k -= min(c, k);

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using ll = long long;
#define rep(i, a, b) for (ll i = a; i < ll(b); i++)
#define repr(i, a, b) for (ll i = a; i >= ll(b); i--)
#define endl "\\n"
#define ALL(x) x.begin(), x.end()
#define ALLR(x) x.rbegin(), x.rend()
using namespace std;

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

  rep(i, 0, a) {
    if (k <= 0) {
      cout << ans << endl;
      return 0;
    }
    k--;
    ans++;
  }
  rep(i, 0, b) {
    if (k <= 0) {
      cout << ans << endl;
      return 0;
    }
    k--;
  }
  rep(i, 0, c) {
    if (k <= 0) {
      cout << ans << endl;
      return 0;
    }
    k--;
    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;
  cin >> a >> b >> c >> k;
  int ans = 0;
  int x;
  x = min(a, k);
  ans += x;
  k -= x;
  if (k <= 0) {
    cout << ans;
    return 0;
  }
  x = min(b, k);
  k -= x;
  if (k <= 0) {
    cout << ans;
    return 0;
  }
  x = min(c, k);
  ans -= x;
  cout << ans;
}

Error: TLE

Fixed submission:

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

// Bismillah hirrahmanir Rahim
#include <bits/stdc++.h>
#define corret 1
#define ll long long
#define pb push_back
#define dbug printf("I am here\\n");
#define Fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);

const int m = 10e6;
using namespace std;

int main() {

  ll a, b, c, k;

  cin >> a >> b >> c >> k;
  ll sum = 0;

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

  if (k > 0) {
    k -= b;
  }

  if (k > 0) {

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

  cout << sum << endl;

  return 0;
}


Error: 1

Fixed submission:

// Bismillah hirrahmanir Rahim
#include <bits/stdc++.h>
#define corret 1
#define ll long long
#define pb push_back
#define dbug printf("I am here\\n");
#define Fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);

const int m = 10e6;
using namespace std;

int main() {

  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll sum = 0;
  if (k > 0) {
    sum += min(a, k);
    int x = min(a, k);
    k -= x;
  }
  if (k > 0) {
    k -= b;
  }
  if (k > 0) {
    sum -= min(a, k);
    int x = min(a, k);
  }

  cout << sum << endl;

  return corret;
}
/*
4 2
10 20 30 40

*/
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() {
  ll a, b, c;
  ll k;
  cin >> a >> b >> c >> k;
  ll ans = 0;
  // rep(i,k){
  //   if(a > 0){
  //     ans++;
  //     ans++;
  //     a--;
  //   }else if(b > 0){
  //     k+b;
  //     b=0;
  //   }else if(c>0){
  //     c--;
  //     ans--;
  //   }
  // }
  int nokori;
  if (k >= a) {
    ans += a;
    nokori = k - a;
    if (abs(nokori - b) <= c && nokori > b) {
      ans = ans - (nokori - b);
    }
  } else if (k < a) {
    ans = k;
  }
  cout << ans << endl;
  return 0;
}


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

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
  int64_t a, b, c, k, sum = 0, cnt = 0;
  cin >> a >> b >> c >> k;
  while (cnt <= k) {
    if (a > k) {
      sum += k;
      break;
    } else if (a > 0 && a <= k) {
      sum += a;
      cnt += a;
      a = 0;
      continue;
    } else if (b > k) {
      break;
    } else if (b > 0 && b <= k) {
      cnt += b;
      if (cnt >= k)
        break;
      b = 0;
      continue;
    } else {
      for (; cnt < k; cnt++) {
        sum -= 1;
      }
      break;
    }
  }
  cout << sum;
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
int main() {
  int64_t a, b, c, k, sum = 0, cnt = 0;
  cin >> a >> b >> c >> k;
  while (cnt != k) {
    if (a != 0) {
      sum += 1;
      a--;
      cnt++;
      continue;
    } else if (b != 0) {
      b--;
      cnt++;
      continue;
    } else if (c != 0) {
      sum += -1;
      cnt++;
      continue;
    }
  }
  cout << sum;
}
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;
  int t = 0;
  for (int i = 1; i <= K;) {
    if (K <= A) {
      t += K;
      break;
    }
    if (A <= K) {
      t += A;
      i += A;
    }
    if (A < K && K <= A + B) {
      break;
    }
    if (A + B < K && K <= A + B + C) {
      t -= K - A - B;
      break;
    }
  }
  cout << t << 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 t = 0;
  for (int i = 1; i <= K; i++) {
    if (i <= A) {
      t++;
    }
    if (A + B < i && i <= A + B + C) {
      t--;
    }
  }
  cout << t << endl;
}
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;

  if (K <= A) {
    cout << K << endl;
    return 0;
  }
  if (K <= A + B) {
    cout << A << endl;
    return 0;
  }
  cout << A - (K - A - B);
  return 0;
}

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 ans = 0;

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

  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, ans = 0;
  cin >> a >> b >> c >> k;
  long long total = a + b + c;
  if (k >= total)
    cout << a - c << endl;
  else {
    int some = 0;
    some = k - a;
    if (some > 0) {
      ans = a;
      for (long long i = 0; i < some; i++) {
        if (b != 0)
          b--;
        else if (b == 0 && c != 0) {
          ans--;
          c--;
        }
      }
    } 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, ans = 0;
  cin >> a >> b >> c >> k;
  long long total = a + b + c;
  if (k >= total)
    cout << a - c << endl;
  else {
    for (long long i = 0; i < k; i++) {
      if (a != 0) {
        ans++;
        a--;
      } else if (a == 0 && b != 0)
        b--;
      else if (a == 0 && b == 0 && c != 0) {
        ans--;
        c--;
      } else if (a == 0 && b == 0 && c == 0)
        break;
    }
    cout << ans << endl;
  }
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll a, b, c, d;
  cin >> a >> b >> c >> d;
  int sum = 0;

  while (d > 0) {

    if (a > 0) {
      sum += min(a, d);
      d -= min(a, d);
      a = 0;
    } else {

      if (b > 0) {
        d -= min(b, d);
        b = 0;
      } else {
        sum -= min(c, d);
        d -= min(c, d);
        c = 0;
      }
    }
  }

  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int sum = 0;

  int count = 0;

  while (count < d) {

    if (a > 0) {
      a--;
      count++;
      sum++;
    } else {

      if (b > 0) {
        count++;
        b--;
      } else {
        sum--;
        count++;
      }
    }
  }

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

#include <iostream>
#include <vector>
using namespace std;
using ll = long long;

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 <iostream>
using namespace std;
using ll = long long;

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

  int ans = 0;

  if (A == K) {
    cout << A << endl;
    return 0;
  } else {
    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;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

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

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

  if (k != 0) {
    sum -= k;
    k = 0;
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int k, a, b, c, sum = 0;
  cin >> a >> b >> c >> k;
  for (int i = 0; i < k; i++) {
    if (a > 0) {
      sum++;
      a--;
    } else if (a == 0 && b > 0)
      b--;
    else if (a == 0 && b == 0 && c > 0) {
      sum--;
      c--;
    }
  }
  cout << sum << 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;
  if (k <= a)
    cout << k << endl;
  else if (k <= a + b)
    cout << a << endl;
  else
    cout << a - (k - a - b) << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int A, B, C, K;

void input() { cin >> A >> B >> C >> K; }

void how_max() {
  int max = 0;
  for (int i = 0; i < K; i++) {
    if (A > 0) {
      max++;
      A--;
    } else if (B > 0)
      B--;
    else {
      max--;
      C--;
    }

    if (i == K - 1)
      cout << max << endl;
  }
}

int main() {
  input();
  how_max();
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

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

using namespace std;
typedef long long ll;

int main(void) {
  ll a = 1, b = 0, c = -1;
  ll A, B, C, k;
  ll ans;
  cin >> A >> B >> C >> k;
  ll sa = k - A;

  if (k <= A) {
    cout << k;
  } else {
    ans = a * A;
    rep(i, sa) {
      if (B > 0) {
        ans += b;
        B--;
      } else if (C > 0) {
        ans += c;
        C--;
      }
    }
    cout << ans;
  }
  cout << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>

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

using namespace std;
typedef long long ll;

int main(void) {
  ll a = 1, b = 0, c = -1;
  ll A, B, C, k;
  ll ans;

  cin >> A >> B >> C >> k;
  vector<int> a1(A), b1(B), c1(C);

  if (k <= A) {
    cout << k;
  } else {
    ll sa = k - A;
    ans = a * A;
    rep(i, sa) {
      if (b1.size()) {
        ans += b;
        b1.pop_back();
      } else if (c1.size()) {
        ans += c;
        c1.pop_back();
      }
    }
    cout << ans;
  }
  cout << 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 < (int)(n); i++)
#define For(i, n1, n2) for (int i = (int)(n1); i < (int)(n2); i++)

int main() {
  int a, b, c, k;
  int ans = 0;
  cin >> a >> b >> c >> k;
  Rep(i, k) {
    if (a > 0) {
      ans++;
      a--;
      continue;
    }
    if (b > 0) {
      b--;
      continue;
    }
    if (c > 0) {
      ans--;
      c--;
      continue;
    }
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define For(i, n1, n2) for (int i = (int)(n1); i < (int)(n2); i++)

int main() {
  long long a, b, c, k;
  long long ans = 0;
  cin >> a >> b >> c >> k;
  Rep(i, k) {
    if (a > 0) {
      ans++;
      a--;
      continue;
    }
    if (b > 0) {
      b--;
      continue;
    }
    if (c > 0) {
      ans--;
      c--;
      continue;
    }
  }
  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;
  cin >> A >> B >> C >> K;
  int sum = 0;
  bool judge = true;
  if (K <= A) {
    sum += K;
    judge = false;
  } else {
    sum += A;
    K -= A;
  }
  if (judge) {
    if (K <= B) {
      judge = false;
    } else {
      K -= B;
      sum -= K;
    }
  }
  cout << sum << 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 sum = 0;
  for (int i = 0; i < K; i++) {
    if (A != 0) {
      sum++;
      A--;
    } else if (B != 0) {
      B--;
    } else {
      sum--;
      C--;
    }
  }
  cout << sum << endl;
}