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

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

  cout << sum;

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

  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;
  int sum = 0;
  vector<int> S;
  cin >> A >> B >> C >> K;

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

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

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

void Main() {
  long long A, B, C, K, cnt;
  long long minNum;
  cin >> A >> B >> C >> K;
  cnt = min(A, K);
  if (A + B < K) {
    cnt = cnt - min(C, (K - (A + B)));
  }
  cout << cnt << endl;
}
int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
}


Error: TLE

Fixed submission:

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

void Main() {
  long long A, B, C, K, cnt = 0;
  long long minNum;
  cin >> A >> B >> C >> K;
  for (long long i = 0; i < K; i++) {
    if (A > cnt && A > i) {
      cnt++;
      continue;
    }
    if (B > i - A) {
      continue;
    }
    if (C > i - (A + B)) {
      cnt--;
    }
  }
  cout << cnt << endl;
}
int main() {
  std::cout << std::fixed << std::setprecision(15);
  Main();
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  long long a, b, c, k;
  cin >> a >> b >> c >> k;
  long long i;
  long long cnt = 0;
  for (i = 1; i <= k; i++) {
    if (a > k) {
      cout << k << "\\n";
      return 0;
    } else if (a + b > k) {
      cout << a << "\\n";
      return 0;
    } else {
      cout << a - (k - a - b) << "\\n";
      return 0;
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  long long a, b, c, k;
  cin >> a >> b >> c >> k;
  long long i;
  long long cnt = 0;
  for (i = 1; i <= k; i++) {
    if (a > 0) {
      cnt++;
      a--;
    } else if (b > 0) {
      b--;
    } else if (c > 0) {
      cnt--;
      c--;
    }
  }
  cout << cnt << "\\n";
  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 a0 = a, b0 = b, c0 = c;
  for (int i = 0; i < k; i++) {
    if (a > 0)
      a--;
    else if (b > 0)
      b--;
    else if (c > 0)
      c--;
  }
  int ans = (a0 - a) * 1 + (b0 - b) * 0 + (c0 - c) * -1;
  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 a0 = a, b0 = b, c0 = c;
  for (int i = 0; i < k; i++) {
    if (a > 0)
      a--;
    else if (a == 0 && b > 0)
      b--;
    else if (a == 0 && b == 0 && c > 0)
      c--;
  }
  int ans = (a0 - a) * 1 + (b0 - b) * 0 + (c0 - c) * -1;
  cout << ans;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<int, int> P;
const int INF = 500000000;

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

Error: TLE

Fixed submission:

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

typedef pair<int, int> P;
const int INF = 500000000;

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

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

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

  while (k > 0) {
    if (a != 0) {
      if (a < k) {
        ans += a;
        k -= a;
        a = 0;
      } else {
        ans += k;
        k -= a;
        a -= k;
      }
    } else if (b != 0) {
      k -= b;
      b = 0;
    } else {
      if (c < k) {
        ans -= c;
        k -= c;
        c = 0;
      } else {
        ans -= k;
        k -= c;
        c -= k;
      }
    }
    // cout<<k;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

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

  while (k > 0) {
    if (a != 0) {
      ans++;
      a--;
      k--;
    } else if (b != 0) {
      b--;
      k--;
    } else {
      ans--;
      c--;
      k--;
    }
    // cout<<k;
  }

  cout << ans << endl;

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

#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 ans = 0;
  if (k <= a) {
    rep(i, k) ans++;
    cout << ans << endl;
    return 0;
  } else if (k > a && k <= b) {
    rep(i, a) ans++;
    cout << ans << endl;
    return 0;
  } else {
    rep(i, a) ans++;
    rep(i, k - b - a) ans--;
    cout << ans << endl;
    return 0;
  }
}

Error: 0

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 ans = 0;
  vector<ll> d(k);
  rep(i, c) { d.push_back(-1); }
  rep(i, b) { d.push_back(0); }
  rep(i, a) { d.push_back(1); }
  reverse(d.begin(), d.end());
  rep(i, k) {
    // cout << d[i] << endl;
    ans += d[i];
  }
  cout << ans << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error

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


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
  ll a, b, c, k, i, ans = 0, temp = 0;
  cin >> a >> b >> c >> k;
  ll arr[a + b + c];
  for (i = 0; i < a; i++)
    arr[i] = 1;
  for (i = a; i < a + b; i++)
    arr[i] = 0;
  for (i = a + b; i < a + b + c; i++)
    arr[i] = -1;
  if (a < 2) {
    cout << "0\\n";
  } else {
    for (i = 0; i < k; i++) {
      if (arr[i] + temp > 0)
        temp += arr[i];
      else
        temp = 0;
      ans = max(temp, ans);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

// ABC167B.cpp

#include <iostream>
using namespace std;
int main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int ans = 0;
  if (a >= k) { // 1のカードがたくさんある時
    ans = k;
    cout << ans << endl;
    return 0;
  } else { // 1のカードがあまりない時
    ans = a;
    k -= a;
  }
  if (b < k) {
    k -= b;
    ans -= k;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

// MandaB.cpp : このファイルには 'main'
// 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//

#include <iostream>
using namespace std;

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

// プログラムの実行: Ctrl + F5 または [デバッグ] > [デバッグなしで開始] メニュー
// プログラムのデバッグ: F5 または [デバッグ] > [デバッグの開始] メニュー

// 作業を開始するためのヒント:
//    1. ソリューション エクスプローラー
//    ウィンドウを使用してファイルを追加/管理します
//   2. チーム エクスプローラー ウィンドウを使用してソース管理に接続します
//   3. 出力ウィンドウを使用して、ビルド出力とその他のメッセージを表示します
//   4. エラー一覧ウィンドウを使用してエラーを表示します
//   5. [プロジェクト] > [新しい項目の追加] と移動して新しいコード
//   ファイルを作成するか、[プロジェクト] > [既存の項目の追加]
//   と移動して既存のコード ファイルをプロジェクトに追加します
//   6. 後ほどこのプロジェクトを再び開く場合、[ファイル] > [開く] >
//   [プロジェクト] と移動して .sln ファイルを選択します
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 = a + b + c;
  if (k >= 1 && k <= a)
    cout << k << endl;
  else if (k > a && k <= a + b)
    cout << a << endl;
  else
    cout << 2 * a + b - k << endl;
}

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

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

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

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
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) {
      sum += 1;
      a -= 1;
    } else if (b != 0) {
      b -= 1;
    } else {
      sum -= 1;
    }
  }
  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, d;
  cin >> a >> b >> c >> d;
  int e = 0;
  if (d >= a) {
    e += a;
    d -= a;
  } else {
    e += d;
    d = 0;
  }
  if (d >= b) {
    d -= b;
  } else {
    d = 0;
  }
  e -= d;
  cout << e << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int e = 0;
  for (int i = 0; i < d; i++) {
    if (a != 0) {
      e += 1;
      a -= 1;
    } else if (b != 0) {
      b -= 1;
    } else {
      e -= 1;
      c -= 1;
    }
  }
  cout << e << endl;
}
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 (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 <bits/stdc++.h>
using namespace std;

int main() {
  int A, B, C, K, sum = 0;
  cin >> A >> B >> C >> K;
  for (int i = 0; i < K; i++) {
    if (A > 0) {
      sum++;
      A--;
    } else if (B > 0) {
      B--;
    } else {
      sum--;
    }
  }
  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, ans;
  cin >> A >> B >> C >> K;

  ans = 0;

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

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

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

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {

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

  ans = 0;

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

  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;
  if (K <= A + B)
    if (K < A)
      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;
  bool end = false;
  cin >> A >> B >> C >> K;
  for (int i = 0; i <= C; i++) {
    for (int j = 0; j <= B; j++) {
      for (int l = 0; l <= A; l++) {
        if (i + j + l == K) {
          cout << i * (-1) + l << endl;
          end = true;
          break;
        }
        if (end)
          break;
      }
      if (end)
        break;
    }
    if (end)
      break;
  }
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error

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

  int k;
  cin >> k;

  int output = 0;
  if (k <= a) {
    output = k;
  } else if (k <= a + b) {
    output = a;
  } else {
    output = a * 1 + (k - a - b) * -1;
  }

  cout << output << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c;
  cin >> a >> b >> c;
  int d = a + b + c;
  int k;
  cin >> k;
  int al[d];
  for (int i = 0; i < a; i++) {
    al[i] = 1;
  }
  for (int i = 0; i < b; i++) {
    al[i + a] = 0;
  }
  for (int i = 0; i < c; i++) {
    al[i + a + b] = -1;
  }
  int output = 0;
  for (int i = 0; i < k; i++) {
    output += al[i];
  }
  cout << output << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Runtime Error

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

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

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

Error: WA

Fixed submission:

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

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

  int a, b, c, k, ans = 0;
  cin >> a >> b >> c >> k;
  int t = a + b + c;
  int arr[t];
  for (int i = 0; i < t; i++) {
    if (i < a)
      arr[i] = 1;
    else if (i < a + b)
      arr[i] = 0;
    else
      arr[i] = -1;
  }
  for (int i = 0; i < k; i++) {
    ans += arr[i];
  }
  cout << ans;
}
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 ll = long long;
using namespace std;

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

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

  return 0;
}


Error: TLE

Fixed submission:

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

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

  cout << sum << endl;

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

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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

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

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

typedef long long ll;

void solve(long long A, long long B, long long C, long long K) {
  int tot = 0;
  if (A > K) {
    tot += K;
    cout << tot << endl;
    return;
  } else {
    tot += A;
    K -= A;
  }

  if (B > K) {
    cout << tot << endl;
    return;
  } else {
    K -= B;
  }
  tot -= K;
  cout << tot << endl;
  return;
}
int main() {
  long long A;
  scanf("%lld", &A);
  long long B;
  scanf("%lld", &B);
  long long C;
  scanf("%lld", &C);
  long long K;
  scanf("%lld", &K);
  solve(A, B, C, K);
  return 0;
}


Error: TLE

Fixed submission:

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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

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

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

typedef long long ll;

void solve(long long A, long long B, long long C, long long K) {
  int tot = 0;
  rep(i, K) {
    if (A > 0) {
      tot += 1;
      A--;
    } else if (B > 0) {
      B--;
    } else {
      tot -= 1;
      C--;
    }
  }
  cout << tot << endl;
}

int main() {
  long long A;
  scanf("%lld", &A);
  long long B;
  scanf("%lld", &B);
  long long C;
  scanf("%lld", &C);
  long long K;
  scanf("%lld", &K);
  solve(A, B, C, K);
  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;
using ll = long long;
using p = pair<int, int>;

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

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

  cout << ans << endl;

  // for(int x=0;x<=A;x++){
  //   for(int y=0;y<=B;y++){
  //     int z = K-x-y;
  //     if(z<0 || z > C) continue;
  //     int sum = x-z;
  //     if(sum > max){
  //       max = sum;
  //     }
  //   }
  // }
  // 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;
using ll = long long;
using p = pair<int, int>;

int main() {
  int A, B, C, K;
  cin >> A >> B >> C >> K;
  int max = -K;
  for (int x = 0; x <= A; x++) {
    for (int y = 0; y <= B; y++) {
      int z = K - x - y;
      if (z < 0 || z > C)
        continue;
      int sum = x - z;
      if (sum > max) {
        max = sum;
      }
    }
  }
  cout << max << endl;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iomanip>
#include <sstream>
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vi vector<ll>
#define qu queue<ll>
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define nout(ans) cout << (ans) << "\\n"
#define sout(ans) cout << (ans) << " "
using namespace std;

/*
Decimal Places: cout << fixed << setprecision(n) << ans << endl

String to integer:
stringstream dark(s);
int x = 0; dark >> x;
*/

bool compare(const pair<int, int> &i, const pair<int, int> &j) {
  return i.second < j.second;
}

void solve() {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  if (a >= k) {
    nout(k);
  } else if (a + b >= k) {
    nout(a);
  } else {
    nout(a - (k - a - b));
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iomanip>
#include <sstream>
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vi vector<ll>
#define qu queue<ll>
#define min_3(a, b, c) min(a, min(b, c))
#define max_3(a, b, c) max(a, max(b, c))
#define nout(ans) cout << (ans) << "\\n"
#define sout(ans) cout << (ans) << " "
using namespace std;

/*
Decimal Places: cout << fixed << setprecision(n) << ans << endl

String to integer:
stringstream dark(s);
int x = 0; dark >> x;
*/

bool compare(const pair<int, int> &i, const pair<int, int> &j) {
  return i.second < j.second;
}

void solve() {
  ll a, b, c, k, s = 0;
  cin >> a >> b >> c >> k;
  while (k--) {
    if (a > 0) {
      s += 1;
      a--;
    } else if (b > 0) {
      s += 0;
      b--;
    } else if (c > 0) {
      s -= 1;
      c--;
    }
  }
  nout(s);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
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 < (n); ++i)
#define ll long long

int main(void) {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll 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;

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

int main(void) {
  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 {
      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, d, k, i, j, ans = 0;
  cin >> a >> b >> c >> k;
  if (a < k) {
    ans += a;
    k -= a;
  } else {
    ans = k;
    k = 0;
  }
  if (b < k) {
    k -= b;
  } else {
    k = 0;
  }
  ans -= k;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

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