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

#include <bits/stdc++.h>
// #include <boost/array.hpp>
// #include <boost/range/numeric.hpp>
// #include <boost/range/algorithm.hpp>
// #include <boost/multiprecision/cpp_int.hpp>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
// using namespace boost::multiprecision;

constexpr long long INF = LLONG_MAX;
constexpr long long MOD = 1000000007;
constexpr long double PI = M_PI;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define RFOR(i, n, r) for (ll i = (ll)(n - 1); i >= r; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(x) x.begin(), x.end()
#define CALL(x) x.cbegin(), x.cend()
#define RALL(x) x.rbegin(), x.rend()
#define CRALL(x) x.crbegin(), x.crend()

#define cout cout << fixed << setprecision(12)
#define dbglf cerr << '\\n'

#define ACCM(arr) accumulate(ALL(arr), 0LL)
#define Yes(ex) cout << ((ex) ? "Yes" : "No") << endl
#define YES(ex) cout << ((ex) ? "YES" : "NO") << endl

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using vp = vector<pair<ll, ll>>;
using vs = vector<string>;
using vc = vector<char>;
using lst = list<ll>;
using pq = priority_queue<ll>;

template <typename T> inline void veccin(vector<T> &v) {
  for (auto &&e : v) {
    cin >> e;
  }
}

template <typename T> inline void dbg(T a) { cerr << a << ' '; }

ll a, b, c, k;
ll ans = 0, sum = 0, cnt = 0;
// ld d;
bool flag = false;

/*--------------------template--------------------*/

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
// #include <boost/array.hpp>
// #include <boost/range/numeric.hpp>
// #include <boost/range/algorithm.hpp>
// #include <boost/multiprecision/cpp_int.hpp>

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
// using namespace boost::multiprecision;

constexpr long long INF = LLONG_MAX;
constexpr long long MOD = 1000000007;
constexpr long double PI = M_PI;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define RFOR(i, n, r) for (ll i = (ll)(n - 1); i >= r; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, n, 0)
#define ALL(x) x.begin(), x.end()
#define CALL(x) x.cbegin(), x.cend()
#define RALL(x) x.rbegin(), x.rend()
#define CRALL(x) x.crbegin(), x.crend()

#define cout cout << fixed << setprecision(12)
#define dbglf cerr << '\\n'

#define ACCM(arr) accumulate(ALL(arr), 0LL)
#define Yes(ex) cout << ((ex) ? "Yes" : "No") << endl
#define YES(ex) cout << ((ex) ? "YES" : "NO") << endl

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using vi = vector<ll>;
using vvi = vector<vi>;
using vp = vector<pair<ll, ll>>;
using vs = vector<string>;
using vc = vector<char>;
using lst = list<ll>;
using pq = priority_queue<ll>;

template <typename T> inline void veccin(vector<T> &v) {
  for (auto &&e : v) {
    cin >> e;
  }
}

template <typename T> inline void dbg(T a) { cerr << a << ' '; }

ll a, b, c, k;
ll ans = 0, sum = 0, cnt = 0;
// ld d;
bool flag = false;

/*--------------------template--------------------*/

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  cin >> a >> b >> c >> k;
  while (k--) {
    if (a) {
      ans++;
      a--;
    } else if (b) {
      b--;
    } else if (c) {
      ans--;
      c--;
    }
  }
  cout << ans << endl;
}