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

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

typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll, ll> pii;
#define v(x) vector<x>
#define pb(x) push_back(x)
#define ppb() pop_back()
#define mp(x, y) make_pair(x, y)
#define mii map<ll, ll>
#define umii unordered_map<ll, ll>
#define mxheap priority_queue<ll>                  // Max Heap
#define mnheap priority_queue<ll, vi, greater<ll>> // Min Heap
#define da(type, arr, size)                                                    \\
  type *arr = new type[n] // Dynamically allocated array

#define f(i, n) for (ll i = 0; i < (n); i++)
#define fa(i, a, b) for (ll i = (a); i < (b); i++)
#define far(i, a, b) for (ll i = (a); i > (b); i--)
#define T                                                                      \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define ff first
#define ss second
#define mem(a, b) memset(a, b, sizeof(a))
#define sz size()

#define setbits(x) __builtin_popcountll(x) // Total set bits in a number
#define zerobits(x)                                                            \\
  __builtin_ctzll(x) // This will tell me number of zeroes till first set bit
#define all(c) (c).begin(), (c).end()
#define setp(x, y) fixed << setprecision(y) << x
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define INF 2e18

ll mod(ll n) { return n > 0 ? n : -1 * n; }
ld dist(ll x1, ll y1, ll x2, ll y2) {
  ld ans = round(pow(mod(x2 - x1), 2)) + round(pow(mod(y2 - y1), 2));
  ans = sqrt(ans);
  return ans;
}

ld dist(pii a, pii b) {
  ld ans = round(pow(mod(b.ff - a.ff), 2)) + round(pow(mod(b.ss - a.ss), 2));
  ans = sqrt(ans);
  return ans;
}
umii isPrime;
void SieveOfEratosthenes(ll n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (ll i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
  for (ll p = 2; p <= n; p++)
    if (prime[p])
      isPrime[p] = 1;
}
int *dp;
ll maxCompositeSummands(ll n) {
  if (n == 2 || n == 1 || n == 3) {
    return INT_MIN;
  }
  if (dp[n] != -1) {
    return dp[n];
  }
  ll mexx = INT_MIN;
  for (ll i = 4; i <= n; i++) {
    if (!isPrime[i]) {
      mexx = max(mexx, maxCompositeSummands(n - i));
    }
  }
  return dp[n] = mexx == INT_MIN ? mexx : mexx + 1;
}
int main() {
  fast ll r;
  cin >> r;
  long double pi = 3.141592653;
  long double ans = 2 * pi * r;
  cout << ans << endl;
}

Error: TLE

Fixed submission:

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

typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<ll, ll> pii;
#define v(x) vector<x>
#define pb(x) push_back(x)
#define ppb() pop_back()
#define mp(x, y) make_pair(x, y)
#define mii map<ll, ll>
#define umii unordered_map<ll, ll>
#define mxheap priority_queue<ll>                  // Max Heap
#define mnheap priority_queue<ll, vi, greater<ll>> // Min Heap
#define da(type, arr, size)                                                    \\
  type *arr = new type[n] // Dynamically allocated array

#define f(i, n) for (ll i = 0; i < (n); i++)
#define fa(i, a, b) for (ll i = (a); i < (b); i++)
#define far(i, a, b) for (ll i = (a); i > (b); i--)
#define T                                                                      \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define ff first
#define ss second
#define mem(a, b) memset(a, b, sizeof(a))
#define sz size()

#define setbits(x) __builtin_popcountll(x) // Total set bits in a number
#define zerobits(x)                                                            \\
  __builtin_ctzll(x) // This will tell me number of zeroes till first set bit
#define all(c) (c).begin(), (c).end()
#define setp(x, y) fixed << setprecision(y) << x
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define INF 2e18

ll mod(ll n) { return n > 0 ? n : -1 * n; }
ld dist(ll x1, ll y1, ll x2, ll y2) {
  ld ans = round(pow(mod(x2 - x1), 2)) + round(pow(mod(y2 - y1), 2));
  ans = sqrt(ans);
  return ans;
}

ld dist(pii a, pii b) {
  ld ans = round(pow(mod(b.ff - a.ff), 2)) + round(pow(mod(b.ss - a.ss), 2));
  ans = sqrt(ans);
  return ans;
}
umii isPrime;
void SieveOfEratosthenes(ll n) {
  bool prime[n + 1];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= n; p++) {
    if (prime[p] == true) {
      for (ll i = p * p; i <= n; i += p)
        prime[i] = false;
    }
  }
  for (ll p = 2; p <= n; p++)
    if (prime[p])
      isPrime[p] = 1;
}
int *dp;
ll maxCompositeSummands(ll n) {
  if (n == 2 || n == 1 || n == 3) {
    return INT_MIN;
  }
  if (dp[n] != -1) {
    return dp[n];
  }
  ll mexx = INT_MIN;
  for (ll i = 4; i <= n; i++) {
    if (!isPrime[i]) {
      mexx = max(mexx, maxCompositeSummands(n - i));
    }
  }
  return dp[n] = mexx == INT_MIN ? mexx : mexx + 1;
}
int main() {
  fast ll n, m;
  cin >> n >> m;
  ll sum = 0;
  for (ll i = 0; i < m; i++) {
    ll temp;
    cin >> temp;
    sum += temp;
  }
  ll ans = n - sum;
  ans < 0 ? cout << -1 << endl : cout << ans << endl;
}