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

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <functional>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <type_traits>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n, s) for (int i = s; i < (int)(n); i++)

ll calcNumOfCombination(ll n, ll r) {
  ll num = 1;
  for (ll i = 1; i <= r; i++) {
    num = num * (n - i + 1) / i;
  }
  return num;
}

int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

int findSumDigits(int s) {
  int num = 0;
  int temp = 0;
  while (s) {
    temp = s % 10;
    num += temp;
    s = s / 10;
  }
  return num;
}

int countDivisorNum(int n) {
  int count = 0;
  for (int i = 1; i <= n; i++) {
    if (n % i == 0) {
      count++;
    }
  }
  return count;
}

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

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

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

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdio>
#include <functional>
#include <iostream>
#include <limits>
#include <numeric>
#include <queue>
#include <type_traits>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n, s) for (int i = s; i < (int)(n); i++)

ll calcNumOfCombination(ll n, ll r) {
  ll num = 1;
  for (ll i = 1; i <= r; i++) {
    num = num * (n - i + 1) / i;
  }
  return num;
}

int gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; }

int findSumDigits(int s) {
  int num = 0;
  int temp = 0;
  while (s) {
    temp = s % 10;
    num += temp;
    s = s / 10;
  }
  return num;
}

int countDivisorNum(int n) {
  int count = 0;
  for (int i = 1; i <= n; i++) {
    if (n % i == 0) {
      count++;
    }
  }
  return count;
}

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

  for (ll i = k; i != 0; i--) {
    if (a != 0) {
      ans++;
      a--;
    } else if (a == 0 && b != 0) {
      b--;
    } else if (a == 0 && b == 0 && c != 0) {
      ans--;
      c--;
    }
  }

  cout << ans << endl;

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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
/****************************/
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define qi queue<int>
#define mii map<int, int>
#define msi map<string, int>
#define vs vector<string>
#define vc vector<char>
/****************************/
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep3(i, a, n) for (ll i = (a); i < (n); i++)
#define rep4(i, a, n) for (ll i = (a)-1; i >= n; i--)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define pi 3.1415926535897932384626433832795
#define zero(n) memset(n, 0, sizeof(n));
#define SZ(n) n.size()
#define sn st.nd
ll dx[4] = {0, -1, 0, 1};
ll dy[4] = {1, 0, -1, 0};
/****************************/
int main() {
  ios::sync_with_stdio(0);
  ll n, a, b, c;
  cin >> a >> b >> c >> n;
  if (n <= a) {
    cout << n;
    return 0;
  }
  if (n <= a + b) {
    cout << a;
    return 0;
  }
  cout << a - (n - a - b);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
/****************************/
#define ll long long
#define vi vector<int>
#define pii pair<int, int>
#define vpii vector<pair<int, int>>
#define qi queue<int>
#define mii map<int, int>
#define msi map<string, int>
#define vs vector<string>
#define vc vector<char>
/****************************/
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep3(i, a, n) for (ll i = (a); i < (n); i++)
#define rep4(i, a, n) for (ll i = (a)-1; i >= n; i--)
#define mp make_pair
#define pb push_back
#define st first
#define nd second
#define pi 3.1415926535897932384626433832795
#define zero(n) memset(n, 0, sizeof(n));
#define SZ(n) n.size()
#define sn st.nd
ll dx[4] = {0, -1, 0, 1};
ll dy[4] = {1, 0, -1, 0};
/****************************/
int main() {
  ios::sync_with_stdio(0);
  ll n, a, b, c;
  cin >> a >> b >> c >> n;
  ll ans = 0;
  rep(i, n) {
    if (a) {
      ans++;
      a--;
    } else {
      if (b) {
        b--;
      } else {
        ans--;
        c--;
      }
    }
  }
  cout << ans;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
int main(void) {
  int a, b, c, k;
  cin >> a >> b >> c >> k;

  int a_prime = min(k, a);
  k -= a_prime;
  int b_prime = min(k, b);
  k -= b_prime;
  int c_prime = k;

  int ans = a_prime - c_prime;

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

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

  int Result = 0;

  std::vector<int> num;

  // 数字を順番に配列に入れる
  int i;
  for (i = 1; i <= a; i++) {
    num.push_back(1);
  }

  for (i = 1; i <= b; i++) {
    num.push_back(0);
  }

  for (i = 1; i <= c; i++) {
    num.push_back(-1);
  }

  // 読み出し
  for (i = 1; i <= k; i++) {
    Result += num.at(i - 1);
  }

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

#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
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() {
  int A, B, C;
  cin >> A >> B >> C;
  int K;
  cin >> K;
  int ans = 0;
  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 <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)n; i++) // n回繰り返す
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, n) for (int i = 1; i <= (int)n; i++) // i=1からi=nまで
#define RREP(i, n) for (int i = n; i >= 1; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using graph = vector<vector<int>>;
const ll mod = pow(10, 9) + 7;
const int INF = 1e9;
const ll LINF = 1e18;
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() {
  int A, B, C;
  cin >> A >> B >> C;
  int K;
  cin >> K;
  int ans = 0;
  while (K > 0) {
    if (A != 0) {
      ans++;
      A--;
    } else if (B != 0) {
      B--;
    } else {
      ans--;
      C--;
    }
    K--;
  }
  cout << ans << endl;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define li long long int
int main() {
  int a, b, c, k, sum = 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: TLE

Fixed submission:

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

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  li a, b, c, k, i, j, sum = 0;
  vector<li> v;
  cin >> a >> b >> c >> k;
  for (i = 0; i < a; i++)
    v.emplace_back(1);
  for (i = 0; i < b; i++)
    v.emplace_back(0);
  for (i = 0; i < c; i++)
    v.emplace_back(-1);
  for (i = 0; i < k; i++)
    sum += v[i];
  cout << sum << endl;
  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;
  int ans = 0;

  while (a--) {
    if (k == 0)
      break;
    ans++;
    k--;
  }
  while (b--) {
    if (k == 0)
      break;
    k--;
  }
  while (c--) {
    if (k == 0)
      break;
    ans--;
    k--;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  int ans = 0;
  int v[a + b + c];
  int i = 0;
  while (a > 0) {
    v[i] = 1;
    a--;
    i++;
  }
  while (b > 0) {
    v[i] = 0;
    b--;
    i++;
  }
  while (c > 0) {
    v[i] = -1;
    c--;
    i++;
  }

  for (int i = 0; i < k; i++)
    ans += v[i];
  cout << ans << endl;

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

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

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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

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

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

  int sum = 0;
  /*
      while (K > 0)
      {
          if(A > 0 ){
              sum ++;
              A --;
              K --;

          }else if(B > 0){

              B --;
              K --;

          }else{
              C --;
              K --;
              sum --;
          }
      }
      */
  while (A > 0 && K > 0) {
    sum++;
    A--;
    K--;
  }
  while (B > 0 && K > 0) {
    B--;
    K--;
  }
  while (C > 0 && K > 0) {
    sum--;
    C--;
    K--;
  }

  cout << sum << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

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

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

  int sum = 0;

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

  cout << sum << endl;

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

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

int main(void) {

  int a, b, c, k;

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

  if (a >= k)
    cout << k;
  else if (a + b >= k)
    cout << a;
  else
    cout << a - (k - (a + b));

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

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

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

    cout << x;
    return 0;
  }

  cout << x;

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  long long a, b, c, d, x = 0;
  cin >> a >> b >> c >> d;

  for (int i = 0; i < d; i++) {
    if (a > 0) {
      x++;
      a--;
      continue;
    }

    if (b > 0) {
      b--;
      continue;
    }
    if (c > 0) {
      x--;
      c--;
    }
  }
  cout << x;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int64_t A, B, C, K;
  cin >> A >> B >> C >> K;
  int64_t sum = 0;
  if (K > A) {
    K = K - A;
    sum = A;
    if (K > B) {
      K = K - B;
      while (K > 0) {
        sum -= 1;
        C = C - 1;
        K -= 1;
      }
    } else {
    }
  } else {
    sum = K;
  }

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <math.h>
using namespace std;
int main() {
  int64_t A, B, C, K;
  cin >> A >> B >> C >> K;
  int64_t sum = 0;
  while (K > 0) {
    if (A > 0) {
      sum += 1;
      A = A - 1;
    } else if (B > 0) {
      sum += 0;
      B = B - 1;
    } else {
      sum -= 1;
      C = C - 1;
    }
    K -= 1;
  }

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int a, b, c, k;
  cin >> a >> b >> c >> k;
  if (a >= k) {
    cout << k << endl;
  } else {
    k = k - a;
    if (b >= k) {
      cout << a << endl;
    } else {
      cout << a - (k - b) << endl;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int a, b, c, k, count = 0;
  cin >> a >> b >> c >> k;
  for (int i = 0; i < k; i++) {
    if (a != 0) {
      count++;
      a--;
    } else if (b != 0) {
      b--;
    } else {
      count--;
      c--;
    }
  }
  cout << count << endl;

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

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

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

  ll ans = 0;
  for (size_t i = 1; i <= K; i++) {
    if (A <= 0 && B <= 0 && C <= 0) {
      break;
    }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
int main() {
  int t, n, i, j, c, a, b, k, ss = 0, l, m;
  cin >> a >> b >> c >> k;
  if ((k - a) < 0) {
    ss += (k * 1);
    cout << ss << endl;
    return 0;
  } else {
    if ((k - a) >= 0)
      ss += (a * 1);
    k -= a;
    k -= b;
    if (k > 0)
      ss += k * (-1);
    cout << ss << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
int main() {
  int t, n, i, j, c, a, b, k, ss = 0, l, m;
  cin >> a >> b >> c >> k;
  for (i = 1; i <= k && i <= a; i++) {

    ss += 1;
  }

  for (j = 1; i <= k && j <= b; i++, j++) {

    ss += 0;
  }

  for (m = 1; i <= k && m <= c; i++, m++) {

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, c) for (int i = 0; i < c; ++i)
#define swap(a, b, type)                                                       \\
  do {                                                                         \\
    type temp;                                                                 \\
    temp = a;                                                                  \\
    a = b;                                                                     \\
    b = temp;                                                                  \\
  } while (0);
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int e = min(a, d);
  d -= e;
  int f = min(b, d);
  d -= f;
  int g = d;
  int ans = e - g;
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, c) for (int i = 0; i < c; ++i)
#define swap(a, b, type)                                                       \\
  do {                                                                         \\
    type temp;                                                                 \\
    temp = a;                                                                  \\
    a = b;                                                                     \\
    b = temp;                                                                  \\
  } while (0);
int main() {
  long long int a, b, c, d;
  long long int e = 0;
  cin >> a >> b >> c >> d;
  for (int i = d; i > 0; i--) {
    if (a > 0) {
      e++;
      a--;
    } else if (a == 0 && b > 0) {
      b--;
    } else {
      e--;
      c--;
    }
  }
  cout << e << endl;
  return 0;
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, a, b)                                                           \\
  for (int i = a; a < b; i++)                                                  \\
    ;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
#define pb push_back
#define debug(x) cerr << #x << ": " << x << endl
#define debug_vec(v)                                                           \\
  cerr << #v << ":";                                                           \\
  rep(i, v.size()) cerr << " " << v[i];                                        \\
  cerr << endl
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;
}
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

void solve() {
  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;
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}


Error: TLE

Fixed submission:

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

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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define _PI (3.141592653589793)
int main() {
  int A, B, C, K;
  std::cin >> A >> B >> C >> K;
  int remain = K;
  int sum = 0;
  if (A >= remain) {
    sum += remain;
    remain = 0;
  } else {
    sum += A;
    remain -= A;
  }
  if (B >= remain) {
    remain = 0;
  } else {
    remain -= B;
  }
  sum -= remain;
  std::cout << sum << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int A, B, C, K;
  int out = 0;
  std::vector<int> data;

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

  for (int i = 0; i < A; i++) {
    data.push_back(1);
  }

  for (int i = 0; i < B; i++) {
    data.push_back(0);
  }

  for (int i = 0; i < C; i++) {
    data.push_back(-1);
  }
  if (A == K) {
    std::cout << A << std::endl;
  } else {
    std::sort(data.begin(), data.end(), std::greater<int>()); // 降順ソート

    for (int i = 0; i < K; i++) {
      out += data[i];
    }
    std::cout << out << std::endl;
  }
}
Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

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

bool isPrime(int x) {
  if (x == 1)
    return 0;
  if (x == 2)
    return 1;
  if (x % 2 == 0)
    return 0;
  for (int i = 3; i * i <= x; i++)
    if (x % i == 0)
      return 0;
  return 1;
}
int main() {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  int ans = 0;
  if (a - k < 0) {
    k -= a;
    ans += a;
    if (b - k < 0) {
      k -= b;
      if (c - k < 0) {
        ans -= c;
        cout << ans << endl;
      } else {
        ans -= k;
        cout << ans << endl;
      }
    } else {
      cout << ans << endl;
      return 0;
    }
  } else {
    cout << k << endl;
    return 0;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

bool isPrime(int x) {
  if (x == 1)
    return 0;
  if (x == 2)
    return 1;
  if (x % 2 == 0)
    return 0;
  for (int i = 3; i * i <= x; i++)
    if (x % i == 0)
      return 0;
  return 1;
}
int main() {
  ll a, b, c, k;
  cin >> a >> b >> c >> k;
  ll ans = 0;
  while (k != 0) {
    if (a != 0) {
      a--;
      ans++;
    } else if (b != 0) {
      b--;
    } else if (c != 0) {
      c--;
      ans -= 1;
    }
    k--;
  }
  cout << ans << endl;
  return 0;
}