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

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

int main() {
  int a, b, c, d, count = 0, x = 0;
  cin >> a >> b >> c >> d;
  if (a > d) {
    cout << d;
  } else if ((a + b) >= d) {
    cout << a;
  } else {
    cout << a - (d - (a + b));
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int a, b, c, d, count = 0, x = 0;
  cin >> a >> b >> c >> d;
  for (int i = 0; i < d; i++) {
    if (a > 0) {
      a = a - 1;
      count = count + 1;
    } else if (a <= 0 && b > 0) {
      b = b - 1;
      x = x + 1;
    } else {
      count = count - 1;
    }
  }

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

#include <iostream>
#include <vector>

using namespace std;

int main() {

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

  vector<int> X;
  if (K > A) {
    K -= A;
    if (K > B) {
      K -= B;
      if (K > C) {
        cout << A - C << endl;
      } else {
        cout << A - K << endl;
      }
    } else {
      cout << A << endl;
    }
  } else {
    cout << K << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {

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

  vector<int> X;
  for (int i = 0; i < A; ++i) {
    X.push_back(1);
  }
  for (int i = 0; i < B; ++i) {
    X.push_back(0);
  }
  for (int i = 0; i < C; ++i) {
    X.push_back(-1);
  }

  int sum = 0;
  for (int i = 0; i < K; ++i) {
    sum += X[i];
  }
  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;
  int sum = 0;
  int i;
  cin >> a >> b >> c >> k;
  for (i = 1; i <= k; i++) {
    if (i <= a) {
      sum = sum + 1;
    } else if (i <= a + b) {
    } else if (i <= a + b + c) {
      sum = sum - 1;
    };
  }
  cout << sum;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int a, b, c, k;
  int total;
  int sum = 0;
  int i, j;
  cin >> a >> b >> c >> k;
  total = a + b + c;
  for (i = 1; i <= k; i++) {
    if (i <= a) {
      sum = sum + 1;
    } else if (i <= a + b) {
    } else if (i <= a + b + c) {
      sum = sum - 1;
    };
  }
  cout << sum;

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

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

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

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

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


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
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) {
      --K;
      --A;
      ++ans;
    } else if (A == 0 && B > 0) {
      --K;
      --B;
    } else if (B == 0 && C > 0) {
      --K;
      --C;
      --ans;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long int64;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1 << 29;
const long long INFL = 1e18;
const int MOD = 1000000007;
const int MAX = 210000;
const int MAX_NUM = 999999999;
void solve() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int64 ans = 0;
  int cnt = 0;
  for (int i = 1; i <= a; ++i) {
    ans++;
    cnt++;
    if (cnt >= k) {
      cout << ans << endl;
      return;
    }
  }
  for (int i = 1; i <= b; ++i) {
    cnt++;
    if (cnt >= k) {
      cout << ans << endl;
      return;
    }
  }
  for (int i = 1; i <= c; ++i) {
    ans--;
    cnt++;
    if (cnt >= k) {
      cout << ans << endl;
      return;
    }
  }
  return;
}
int main() {
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long int64;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1 << 29;
const long long INFL = 1e18;
const int MOD = 1000000007;
const int MAX = 210000;
const int MAX_NUM = 999999999;
void solve() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int64 ans = -INFL;
  for (int i = 0; i <= a; ++i) {
    for (int j = 0; j <= b; ++j) {
      int l = k - (i + j);
      if ((i + j + l) == k) {
        int64 tmp = (1 * i) + (0 * j) + (-1 * l);
        ans = max(ans, tmp);
      }
    }
  }
  cout << ans << endl;
  return;
}
int main() {
  solve();
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  int count = 0;

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

  return 0;
}

Error: TLE

Fixed submission:

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

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

  int count = 0;
  while (k > 0) {
    if (a > 0) {
      a--;
      count++;

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

    k--;
  }

  cout << count;

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

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

    } else {
      k -= b;
      ans -= k;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

#include <bits/stdc++.h>

using namespace std;

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

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

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  if (A + B >= K)
    cout << A << endl;
  else
    exit(1);

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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using P = pair<int, int>;

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

  int ans = 0;

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

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

  ans -= k;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using P = pair<int, int>;

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

  int ans = 0;
  while (k > 0) {
    if (a) {
      ans += 1;
      a--;
    } else if (b) {
      b--;
    } else {
      ans -= 1;
      c--;
    }

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

#include <bits/stdc++.h>
const double PI = acos(-1);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
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;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
const double PI = acos(-1);
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
int main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  vector<long long> num(a + b + c);
  rep(i, a) num.at(i) = 1;
  rep(i, b) num.at(a + i) = 0;
  rep(i, c) num.at(a + b + i) = -1;
  long long ans = 0;
  rep(i, k) ans += num.at(i);
  cout << ans << 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 ans = k < a ? k : a;
  int l = k - a - b;
  if (l > 0) {
    ans -= l;
  }
  cout << ans << 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 d = 0;
  int ans = -2000000001;
  for (int i = 0; i <= a; i++) {
    for (int j = 0; j <= b; j++) {
      int l = k - (i + j);
      d = 1 * i + -1 * l;
      if (ans < d) {
        ans = d;
      }
    }
  }
  cout << ans << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

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

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

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

  /*
  while(true){
      f(a>0)
      {
          sum += k - a;
          if(a)
      }else if(a==0 && b > 0)
      {
          b--;
      }
      else if(b == 0 && c > 0)
      {
          sum--;
          c--;
      }
  }*/

  cout << sum << endl;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

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

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

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

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

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

#define ll long long

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

Error: TLE

Fixed submission:

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

#define ll long long

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
constexpr int mod = 1e9 + 1;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
constexpr int mod = 1e9 + 1;

int main() {
  ll a, b, c, k;
  ll res = 0;
  cin >> a >> b >> c >> k;
  while (k--) {
    if (a != 0) {
      res++;
      a--;
    } else if (b != 0) {
      b--;
    } else if (c != 0) {
      c--;
      res += -1;
    }
  }
  cout << res;
  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 sum = 0, i = k;

  if (a <= i) {
    sum += a;
    i -= a;
  } else {
    sum += i;
    i -= i;
  }

  if (b <= i) {
    i -= b;
  } else {
    i -= i;
  }

  if (c <= i) {
    sum -= c;
    i -= c;
  } else {
    sum -= i;
    i -= i;
  }

  cout << sum;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

  int sum = 0, i = 0;
  while (i < k) {
    if (a-- > 0)
      sum += 1;
    else if (b-- > 0)
      sum += 0;
    else if (c-- > 0)
      sum -= 1;
    else
      break;
    i++;
  }

  cout << sum;

  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;

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

    } else {
      sum--;
    }
  }

  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) {
      a--;
      sum++;
    } else if (b != 0) {
      b--;

    } else {
      sum--;
    }
  }

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

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

using namespace std;

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

  // a +1
  // b 0
  // c -1
  // total k

  if (a >= k) {
    // cout << "here1" << endl;
    cout << 1 * k << endl;
    return 0;
  } else if (a + b >= k) {
    // cout << "here2" << endl;
    cout << 1 * a + 0 * b << endl;
    return 0;
  } else {
    // a + b < k, k - (a + b) > 0
    long max = 1 * a + 0 * b + (-1 * (k - (a + b)));
    cout << max << endl;
    return 0;
  }
}

Error: 1

Fixed submission:

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

using namespace std;

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

  // a +1
  // b 0
  // c -1
  // total k
  if (a < 0 || b < 0 || c < 0) {
    return 1;
  }
  if (k < 1) {
    return 1;
  }
  if (a + b + c > 2000000000 || k < a + b + c) {
    return 1;
  }

  if (a >= k) {
    // cout << "here1" << endl;
    cout << 1 * a << endl;
    return 0;
  } else if (a + b >= k) {
    // cout << "here2" << endl;
    cout << 1 * a << endl;
    return 0;
  } else {
    // cout << "here3" << endl;
    int max = 1 * a + (-1 * (k - a - b));
    cout << max << endl;
    return 0;
  }
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main(void) {
  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;

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

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

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

#include <bits/stdc++.h>
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define sz(v) (int)(v).size()
using namespace std;
using P = pair<int, int>;
typedef long long ll;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int ans = 0;

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(x, to) for (int(x) = 0; (x) < (to); (x)++)
#define rep2(x, fr, to) for (int(x) = (fr); (x) < (to); (x)++)
#define sz(v) (int)(v).size()
using namespace std;
using P = pair<int, int>;
typedef long long ll;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int ans = 0;

  rep(i, k) {
    if (a > 0) {
      ans++;
      a--;
    } else if (b > 0) {
      b--;
    } else {
      ans--;
    }
  }
  cout << ans << '\\n';

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 100000
#define FI freopen("input.txt", "r", stdin)
#define FO freopen("output.txt", "w", stdout)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie();                                                                   \\
  cout.tie();
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d %d", &a, &b)

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 100000
#define FI freopen("input.txt", "r", stdin)
#define FO freopen("output.txt", "w", stdout)
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie();                                                                   \\
  cout.tie();
#define sc(a) scanf("%d", &a)
#define sc2(a, b) scanf("%d %d", &a, &b)

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

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


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 cnt = 0;
  while (k--) {
    if (a > 0)
      ++cnt, --a;
    else if (b > 0)
      --b;
    else if (c > 0)
      --cnt, --c;
    else
      break;
  }
  cout << cnt << 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, num = 0;
  cin >> A >> B >> C >> K;
  if (A >= K) {
    while (K != 0) {
      K--;
      num++;
    }
  } else if (A + B >= K) {
    while (A != 0) {
      A--;
      num++;
    }
  } else {
    for (int i = 0; i < K - A - B; i++) {
      num--;
    }
    while (A != 0) {
      A--;
      num++;
    }
  }
  cout << num << endl;
}


Error: TLE

Fixed submission:

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

int main() {
  int A, B, C, K, num = 0;
  cin >> A >> B >> C >> K;
  while (K != 0) {
    while (A != 0) {
      num++;
      A--;
      K--;
    }
    while (B != 0) {
      B--;
      K--;
    }
    while (C != 0) {
      num--;
      C--;
      K--;
    }
  }
  cout << num << 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;
using ll = long long;
using p = pair<int, int>;

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

  ll s = 0;
  if (a >= k) {
    s = k;
  } else if (a + b >= k) {
    s = a;
  } else {
    s = a - (k - a - b);
  }
  cout << s << 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, k;
  cin >> a >> b >> c >> k;

  ll s = 0;
  rep(i, k) {
    if (a > 0) {
      s += 1;
      a--;
    } else if (b > 0) {
      b--;
    } else {
      s -= 1;
      c--;
    }
  }
  cout << s << 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++)
using namespace std;

int main(int argc, const char *argv[]) {

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

  int A, B, C, K, max = 0;

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

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

  cout << max << 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;

int main(int argc, const char *argv[]) {

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

  int A, B, C, K, max = 0;

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

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

  cout << max << endl;

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

/**
 *  author: mahfuzz
 *  created: 10.05.2020
 **/

#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define trace(x) cout << #x << ':' << x << "\\n"
typedef long long ll;

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

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

  int cnt = 0;
  if (k >= a) {
    k -= a;
    cnt += a;
  } else {
    cnt += k;

    k -= k;
  }

  if (k >= b)
    k -= b;
  else
    k -= k;

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

    cnt -= k;
    k -= k;
  }

  cout << cnt << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

/**
 *  author: mahfuzz
 *  created: 10.05.2020
 **/

#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define trace(x) cout << #x << ':' << x << "\\n"
typedef long long ll;

int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);

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

  int cnt = 0;

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

    k--;
  }

  cout << cnt << "\\n";

  return 0;
}