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

#include <bits/stdc++.h>
#define endl "\\n"
#define cnt count
#define in insert
#define er erase
#define ll long long
#define pb push_back
#define fi first
#define se second
#define vi vector<ll>
#define vc vector<char>
#define vpi vector<pair<ll, ll>>
#define si set<ll>
#define sc set<char>
#define msi multiset<ll>
#define msc multiset<char>
#define lb lower_bound
#define ub upper_bound
#define mll map<ll, ll>
#define gcd __gcd
#define mp(i, j) pb(make_pair(i, j))
#define all(v) v.begin(), v.end()
#define forl(i, n) for (ll i = 0; i < n; i++)
#define fore(i, n) for (ll i = 1; i <= n; i++)
#define forn(i, n) for (ll i = n - 1; i >= 0; i--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define wq                                                                     \\
  ll q;                                                                        \\
  cin >> q;                                                                    \\
  while (q--)
#define pattern                                                                \\
  ll n;                                                                        \\
  cin >> n;
#define pattern1                                                               \\
  ll n;                                                                        \\
  cin >> n;                                                                    \\
  ll ar[n];                                                                    \\
  forl(i, n) { cin >> ar[i]; }
#define pattern2                                                               \\
  ll n, k;                                                                     \\
  cin >> n;                                                                    \\
  cin >> k;                                                                    \\
  ll ar[n];                                                                    \\
  forl(i, n) { cin >> ar[i]; }
#define pattern3                                                               \\
  ll n;                                                                        \\
  cin >> n;                                                                    \\
  char ar[n];                                                                  \\
  forl(i, n) { cin >> ar[i]; }
#define pattern4                                                               \\
  ll n, k;                                                                     \\
  cin >> n;                                                                    \\
  cin >> k;                                                                    \\
  char ar[n];                                                                  \\
  forl(i, n) { cin >> ar[i]; }
using namespace std;
ll bpow(ll a, ll b, ll mod) {
  a %= mod;
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = res * a % mod;
    }
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
bool dio(ll a, ll b, ll c) {
  ll g = gcd(a, b);
  if (g % c == 0 || c % g == 0) {
    return true;
  } else {
    return false;
  }
}
bool pdrome(string s) {
  ll n = s.length();
  forl(i, n / 2) {
    if (s[i] != s[n - i - 1]) {
      return false;
    }
  }
  return true;
}
ll onf(ll n) {
  for (ll i = 2; i < n; i++) {
    if (n % i == 0) {
      return i;
    }
  }
  return 1;
}
ll bis(ll arr[], ll l, ll r, ll x) {
  while (l <= r) {
    ll m = l + (r - l) / 2;
    if (arr[m] == x) {
      return m;
    }
    if (arr[m] < x) {
      l = m + 1;
    } else {
      r = m - 1;
    }
  }
  return -1;
}
ll isprime(ll x) {
  ll k;
  for (ll i = 2; i <= x; i++) {
    if (x % i == 0) {
      k = i;
      break;
    }
  }
  if (k == x) {
    return 1;
  } else {
    return 0;
  }
}
ll mindig(ll x) {
  ll ans = 9;
  ll t = x;
  while (x) {
    ans = min(ans, x % 10);
    x /= 10;
  }
  x = t;
  return ans;
}
ll maxdig(ll x) {
  ll ans = 0;
  ll t = x;
  while (x) {
    ans = max(ans, x % 10);
    x /= 10;
  }
  x = t;
  return ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio;
  ll a, b, c, k, ans = 0;

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define endl "\\n"
#define cnt count
#define in insert
#define er erase
#define ll long long
#define pb push_back
#define fi first
#define se second
#define vi vector<ll>
#define vc vector<char>
#define vpi vector<pair<ll, ll>>
#define si set<ll>
#define sc set<char>
#define msi multiset<ll>
#define msc multiset<char>
#define lb lower_bound
#define ub upper_bound
#define mll map<ll, ll>
#define gcd __gcd
#define mp(i, j) pb(make_pair(i, j))
#define all(v) v.begin(), v.end()
#define forl(i, n) for (ll i = 0; i < n; i++)
#define fore(i, n) for (ll i = 1; i <= n; i++)
#define forn(i, n) for (ll i = n - 1; i >= 0; i--)
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define wq                                                                     \\
  ll q;                                                                        \\
  cin >> q;                                                                    \\
  while (q--)
#define pattern                                                                \\
  ll n;                                                                        \\
  cin >> n;
#define pattern1                                                               \\
  ll n;                                                                        \\
  cin >> n;                                                                    \\
  ll ar[n];                                                                    \\
  forl(i, n) { cin >> ar[i]; }
#define pattern2                                                               \\
  ll n, k;                                                                     \\
  cin >> n;                                                                    \\
  cin >> k;                                                                    \\
  ll ar[n];                                                                    \\
  forl(i, n) { cin >> ar[i]; }
#define pattern3                                                               \\
  ll n;                                                                        \\
  cin >> n;                                                                    \\
  char ar[n];                                                                  \\
  forl(i, n) { cin >> ar[i]; }
#define pattern4                                                               \\
  ll n, k;                                                                     \\
  cin >> n;                                                                    \\
  cin >> k;                                                                    \\
  char ar[n];                                                                  \\
  forl(i, n) { cin >> ar[i]; }
using namespace std;
ll bpow(ll a, ll b, ll mod) {
  a %= mod;
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = res * a % mod;
    }
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
bool dio(ll a, ll b, ll c) {
  ll g = gcd(a, b);
  if (g % c == 0 || c % g == 0) {
    return true;
  } else {
    return false;
  }
}
bool pdrome(string s) {
  ll n = s.length();
  forl(i, n / 2) {
    if (s[i] != s[n - i - 1]) {
      return false;
    }
  }
  return true;
}
ll onf(ll n) {
  for (ll i = 2; i < n; i++) {
    if (n % i == 0) {
      return i;
    }
  }
  return 1;
}
ll bis(ll arr[], ll l, ll r, ll x) {
  while (l <= r) {
    ll m = l + (r - l) / 2;
    if (arr[m] == x) {
      return m;
    }
    if (arr[m] < x) {
      l = m + 1;
    } else {
      r = m - 1;
    }
  }
  return -1;
}
ll isprime(ll x) {
  ll k;
  for (ll i = 2; i <= x; i++) {
    if (x % i == 0) {
      k = i;
      break;
    }
  }
  if (k == x) {
    return 1;
  } else {
    return 0;
  }
}
ll mindig(ll x) {
  ll ans = 9;
  ll t = x;
  while (x) {
    ans = min(ans, x % 10);
    x /= 10;
  }
  x = t;
  return ans;
}
ll maxdig(ll x) {
  ll ans = 0;
  ll t = x;
  while (x) {
    ans = max(ans, x % 10);
    x /= 10;
  }
  x = t;
  return ans;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio;
  ll a, b, c, k, ans = 0;
  vi v;
  cin >> a >> b >> c >> k;
  forl(i, a) { v.pb(1); }
  forl(i, b) { v.pb(0); }
  forl(i, c) { v.pb(-1); }
  sort(all(v));
  reverse(all(v));
  forl(i, k) { ans += v[i]; }
  cout << ans;
}