Problem p02682 (C++) — buggy submission, verdict: Time Limit Exceeded

// JAI SHREE RAM
#pragma GCC optimize("O3")
typedef long long int ll;
typedef unsigned long long int ull;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FOR(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define sz(a) int((a).size())
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define loop for (int i = 0; i < n; i++)
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max3(a, max(b, c), d)
#define max5(a, b, c, d, e) max4(a, max(b, c), d, e)
#define min3(a, b, c) min(a, min(b, c))
#define min4(a, b, c, d) min3(a, min(b, c), d)
#define min5(a, b, c, d, e) min4(a, min(b, c), d, e)
#define sec second
#define fir first
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repe(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
// #define all(a) (a).begin(),(a).end()
#define vec vector<int>
#define M_PI 3.14159265358979323846 /* pi */
#define pb push_back
#define pii pair<int, int>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast int a, b, c, k, ans = 0;
  cin >> a >> b >> c >> k;

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

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

  ans -= k;

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

// JAI SHREE RAM
#pragma GCC optimize("O3")
typedef long long int ll;
typedef unsigned long long int ull;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define FOR(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define sz(a) int((a).size())
#define forn(i, n) for (int i = 0; i < (n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define loop for (int i = 0; i < n; i++)
#define max3(a, b, c) max(a, max(b, c))
#define max4(a, b, c, d) max3(a, max(b, c), d)
#define max5(a, b, c, d, e) max4(a, max(b, c), d, e)
#define min3(a, b, c) min(a, min(b, c))
#define min4(a, b, c, d) min3(a, min(b, c), d)
#define min5(a, b, c, d, e) min4(a, min(b, c), d, e)
#define sec second
#define fir first
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repe(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
// #define all(a) (a).begin(),(a).end()
#define vec vector<int>
#define M_PI 3.14159265358979323846 /* pi */
#define pb push_back
#define pii pair<int, int>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast int a, b, c, k, ans = 0;
  cin >> a >> b >> c >> k;
  while (k--) {
    if (a > 0)
      ans++, a--;
    else if (b > 0)
      b--;
    else if (c > 0)
      ans--, c--;
    // cout<<a<<b<<c<<endl;
  }
  cout << ans << endl;

  return 0;
}