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

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

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

  int64_t sum = 0;
  if (a >= k)
    sum += k;
  else {
    if (a < k) {
      sum += a;
      k = k - a;
    }
    if (k != 0) {
      k = k - b;
      if (k != 0)
        sum += (-1) * k;
    }
  }

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

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

  int64_t sum = 0;
  rep(i, k) {
    if (a != 0) {
      sum += 1;
      a--;
    } else if (b != 0) {
      sum += 0;
      b--;
    } else if (c != 0) {
      sum -= 1;
      c--;
    }
  }
  cout << sum << endl;

  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
  string As, Bs, Cs, Ks;
  cin >> As >> Bs >> Cs >> Ks;
  int A = stoi(As), B = stoi(Bs), C = stoi(Cs), K = stoi(Ks);

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

      cout << A << endl;
    }
    return 0;
  }

  if (A + B + C >= K) {
    int dff = K - (A + B);
    // if (A - dff > 0)
    //  {
    cout << A - dff << endl;
    // }
  }
  // cout << 0 << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
  string As, Bs, Cs, Ks;
  cin >> As >> Bs >> Cs >> Ks;
  int A = stoi(As), B = stoi(Bs), C = stoi(Cs), K = stoi(Ks);

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

      cout << A << endl;
    }
    return 0;
  }

  if (A + C >= K) {
    cout << A - C << endl;
  }

  if (A > C) {
    if (A > K) {
      int dff = K - A;
      cout << A - dff << endl;
    } else {
      cout << 0 << endl;
    }
  } else {
    cout << 0 << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long one, zero, minus_one;
  int k, amount = 0;
  cin >> one >> zero >> minus_one >> k;
  if (one >= k) {
    amount = k;
  } else if (one + zero >= k) {
    amount = one;
  } else {
    amount = one - (k - (one + zero));
  }
  cout << amount << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long one, zero, minus_one;
  int k, amount = 0;
  cin >> one >> zero >> minus_one >> k;

  for (int i = 0; i < k; i++) {
    if (one > 0) {
      one -= 1;
      amount += 1;
    } else if (zero > 0) {
      zero -= 1;
    } else {
      minus_one -= 1;
      amount -= 1;
    }
  }
  cout << amount << 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, ans = 0;
  cin >> a >> b >> c >> k;

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


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;

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

#include <iostream>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int sum = 0;
  if (a > 0 && k > 0) {
    if (a >= k) {
      sum += k;
      k = 0;
    } else if (a < k) {
      sum += a;
      k -= a;
    }
  }
  if (b > 0 && k > 0) {
    if (b >= k) {
      k = 0;
    } else if (b < k) {
      k -= b;
    }
  }
  if (c > 0 && k > 0) {
    if (c >= k) {
      sum -= k;
      k = 0;
    } else if (c < k) {
      sum -= c;
      k -= c;
    }
  }
  cout << sum;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int sum = 0;
  while (k--) {
    if (a > 0) {
      a--;
      sum++;
      continue;
    }
    if (b > 0) {
      b--;
      continue;
    }
    if (c > 0) {
      c--;
      sum--;
      continue;
    }
  }
  cout << sum;
  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 (a + b >= k) {
    if (a >= k) {
      cout << k << endl;
    } else {
      cout << a << endl;
    }
  }

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

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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
// #define fore(i,a) for(auto &i:a)
// #define all(x) (x).begin(),(x).end()
// #pragma GCC optimize ("-O3")
// using namespace std; void _main(); int main() { cin.tie(0);
// ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf =
// INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T& a,
// const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class
// T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

using namespace std;

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

  long long sum = 0;

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

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i >= b; i--)
// #define fore(i,a) for(auto &i:a)
// #define all(x) (x).begin(),(x).end()
// #pragma GCC optimize ("-O3")
// using namespace std; void _main(); int main() { cin.tie(0);
// ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf =
// INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T& a,
// const T& b) { if (a < b) { a = b; return 1; } return 0; } template<class
// T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

using namespace std;

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

  long int sum = 0;
  rep(i, 0, K) {
    if (A > 0) {
      sum += 1;
      A--;
    } else if (B > 0) {
      sum += 0;
      B--;
    } else {
      sum -= 1;
      C--;
    }
  }

  cout << sum << 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;
  cin >> b;
  cin >> c;
  cin >> k;
  long long i = 0;
  long long sum = 0;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

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

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


#include <bits/stdc++.h>
using namespace std;
int main() {
  long int cards[3] = {0}, sum = 0, k, turns = 0, adder, break_flag = 0;
  cin >> cards[0] >> cards[1] >> cards[2];
  cin >> k;
  for (int i = 0; i < 3; i++) {
    if (i == 0)
      adder = 1;
    else if (i == 1)
      adder = 0;
    else
      adder = -1;
    for (int j = 0; j < cards[i]; j++) {
      if (turns == k) {
        break;
        break_flag = 1;
      } else {
        sum += adder;
        turns++;
      }
    }
    if (break_flag == 1)
      break;
  }
  cout << sum;
  return 0;
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
int main() {
  long int cards[3] = {0}, sum, k, turns = 0, adder, break_flag = 0;
  cin >> cards[0] >> cards[1] >> cards[2];
  cin >> k;
  for (int i = 0; i <= 3; i++) {
    if (i == 0)
      adder = 1;
    else if (i == 1)
      adder = 0;
    else
      adder = -1;
    for (int j = 0; j < cards[i]; j++) {
      if (turns == k) {
        break;
        break_flag = 1;
      } else {
        sum += adder;
        turns++;
      }
    }
    if (break_flag == 1)
      break;
  }
  cout << sum;
  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 <= K) {
    sum += A;
    K -= A;
  } else {
    cout << K << endl;
    return 0;
  }

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

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using vi = vector<int>;
using vii = vector<vi>;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using vi = vector<int>;
using vii = vector<vi>;

int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;
  int max = -1 * C;
  for (int i = 0; i <= A; i++) {
    for (int j = K - i - B; j <= C; j++) {
      if (max < i - j) {
        max = i - j;
      }
    }
  }
  cout << max << endl;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

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

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

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

    if (k == 0)
      break;
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  if (a + b <= k) {
    cout << a + (k - (a + b)) * (-1) << endl;
    return 0;
  } else {
    cout << k << 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;
  long long ans = 0;
  long long s[a + b + c + 100];
  for (long long i = 0; i < a; i++) {
    s[i] = 1;
  }
  for (long long j = 0; j < b; j++) {
    s[a + j] = 0;
  }
  for (long long l = 0; l < c; l++) {
    s[a + b + l] = -1;
  }

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

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

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


Error: TLE

Fixed submission:

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

int main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  pq v;
  map<int, int> mp;
  mp[1] = a;
  rep(i, a) v.push(1);
  mp[0] = b;
  rep(i, b) v.push(0);
  mp[-1] = c;
  rep(i, c) v.push(-1);
  ll ans = 0;

  rep(i, k) {
    ans += v.top();
    v.pop();
  }
  cout << ans << endl;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
using namespace std;
int main(void) {
  // Your code here!
  long long int a, b, c, k, point = 0;
  scanf("%lld %lld %lld %lld", &a, &b, &c, &k);
  for (int i = 0; i < k; i++) {
    if (a > 0) {
      point++;
      a--;
    } else if (b > 0) {
      b--;
    } else {
      point--;
    }
  }
  printf("%lld", point);
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
int main(void) {
  // Your code here!
  long long int a, b, c, k, point = 0;
  scanf("%lld %lld %lld %lld", &a, &b, &c, &k);
  for (int i = 0; i < k; i++) {
    if (a > 0) {
      point++;
      a--;
    } else if (b > 0) {
      b--;
    } else {
      point--;
      c--;
    }
  }
  printf("%lld", point);
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int sum = 0;
  while (a != 0 && k != 0) {
    k--;
    a--;
    sum++;
  }
  while (b != 0 && k != 0) {
    k--;
    b--;
  }
  while (k != 0 && c != 0) {
    k--;
    c--;
    sum--;
  }

  cout << sum << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int sum = 0;
  while (k != 0) {
    k--;
    if (a != 0) {
      sum++;
      a--;
    } else if (b != 0) {
      b--;
    } else {
      sum--;
      c--;
    }
  }

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

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

int main() {
  int A;       // １の枚数
  int B;       // ０の枚数
  int C;       //-１の枚数
  int K;       // 取得枚数
  int max = 0; // 最大値
  cin >> A >> B >> C >> K;
  if (A >= K) {
    max = K;
  } else {
    if (A + B >= K) {
      max = A;
    } else {
      max = 2 * A - K + B;
    }
  }
  cout << max;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int A;       // １の枚数
  int B;       // ０の枚数
  int C;       //-１の枚数
  int K;       // 取得枚数
  int max = 0; // 最大値
  cin >> A >> B >> C >> K;
  for (int i = 0; i < K; i++) {
    if (A != 0) {
      max = max + 1;
      A = A - 1;
    } else {
      if (B != 0) {
        B = B - 1;
      } else {
        max = max - 1;
      }
    }
  }
  cout << max;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

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

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

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

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

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

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

  ll ans = 0;
  int i;
  for (i = 1; i <= K; i++) {
    if (A != 0) {
      A -= 1;
      ans += 1;
    } else if (B != 0) {
      B -= 1;
    } else if (C != 0) {
      C -= 1;
      ans -= 1;
    }
  }
  cout << ans << endl;

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

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

int main(int argc, char const *argv[]) {
  int a, b, c, k;
  scanf("%d%d%d%d", &a, &b, &c, &k);
  printf("%d\\n", min(a, k) - max(0, k - a - b));
  return 0;
}


Error: TLE

Fixed submission:

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

int main(int argc, char const *argv[]) {
  ll a, b, c, k;
  scanf("%lld%lld%lld%lld", &a, &b, &c, &k);
  vector<ll> v;
  while (a--)
    v.push_back(1);
  while (b--)
    v.push_back(0);
  while (c--)
    v.push_back(-1);
  ll ans = 0;
  for (int i = 0; i < k; ++i) {
    ans += v[i];
  }
  printf("%lld\\n", ans);
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

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

  long int sum = 0LL;

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

  cout << sum;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  long 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;
}
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 main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int d = 0;
  while (k) {
    if (a) {
      d++;
      k--;
      a--;
    } else if (b) {
      k--;
      b--;
    } else if (c) {
      d--;
      k--;
      c--;
    }
  }
  cout << d;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

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

  std::cin >> a >> b >> c >> k;

  int sum = 0;
  if (k <= a) {
    std::cout << k;
  } else if ((a < k) && (k <= a + b)) {
    std::cout << a;
  } else {
    std::cout << a * 2 - k + b;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

int main() {
  std::vector<int> vec(0);
  int a = 0;
  int b = 0;
  int c = 0;
  int k = 0;

  std::cin >> a >> b >> c >> k;
  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 sum = 0;
  for (int i = 0; i < k; i++) {
    sum += vec[i];
  }

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

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

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

  cin >> A >> B >> C >> K;
  while (1) {
    if (A == 0 || K == 0) {
      break;
    }
    sum++;
    A--;
    K--;
  }
  while (1) {
    if (B == 0 || K == 0) {
      break;
    }
    B--;
    K--;
  }
  while (1) {
    if (K == 0) {
      break;
    }
    sum--;
    K--;
  }
  cout << sum << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

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

  cin >> A >> B >> C >> K;
  while (1) {
    if (A == 0 || K == 0) {
      break;
    }
    sum++;
    A--;
    K--;
  }
  while (1) {
    if (B == 0 || K == 0) {
      break;
    }
    B--;
    K--;
  }
  while (1) {
    if (K == 0) {
      break;
    }
    sum--;
  }
  cout << sum << endl;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int 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 ans = a;
    k -= (a + b);
    while (k > 0) {
      ans--;
      k--;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// time:
#include <bits/stdc++.h>
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++;
      a--;
    } else if (b != 0) {
      b--;
    } else {
      ans--;
      c--;
    }
  }

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

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#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;
  long long N = A + B + C;

  long long KA = K - A;
  long long KAB = K - A - B;
  if (KA < 0)
    KA = 0;
  if (KAB < 0)
    KAB = 0;

  ans = 1 * min(A, K) + 0 * min(B, KA) + -1 * min(C, KAB);

  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cctype>
#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;
  long long N = A + B + C;
  vector<long long> vec(N);

  for (long long i = 0; i < N; i++) {
    if (i < A) {
      vec.at(i) = 1;
    } else if (i >= A && i < A + B) {
      vec.at(i) = 0;
    } else if (i >= A + B && i < N) {
      vec.at(i) = -1;
    }
  }

  for (long long i = 0; i < K; i++) {
    ans += vec.at(i);
  }

  cout << ans << endl;

  return 0;
}