Problem p02706 (C++)

Buggy submission (status: Time Limit Exceeded):
// #pragma GCC optimize("O3")
// #pragma GCC target("avx2")
//  293206GT
#include <algorithm>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

typedef long long ll;
#define int ll
using namespace std;
#define put(a) cout << (a) << '\n'
#define sqr(x) (x) * (x)
typedef pair<ll, ll> pii;
typedef long double ld;

typedef pair<ld, ld> pld;
#define rep(x, y, a) for (int x = (y); x < (int)(a); ++x)
#define all(a) a.begin(), a.end()
#define chkmax(a, b) a = max(a, (b))
#define chkmin(a, b) a = min(a, (b))
#define prev asasdasd
#define next aasdasda
#define left asdasdasdasd
#define right asnabsdkasdl
#define rank asdasdknlasd
#define move asdasdas

typedef vector<int> vi;
typedef vector<pii> vpii;
namespace IO {
template <class A, class B> ostream &operator<<(ostream &out, pair<A, B> &a) {
  out << a.first << " " << a.second;
  return out;
}

template <class A, class B>
ostream &operator<<(ostream &out, vector<pair<A, B>> &a) {
  for (pair<A, B> x : a)
    out << x.first << " " << x.second << '\n';
}

template <class A> ostream &operator<<(ostream &out, vector<A> &a) {
  for (A x : a)
    out << x << ' ';
  return out;
}

template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  in >> a.first >> a.second;
  return in;
}

template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &x : a)
    in >> x;
  return in;
}
} // namespace IO
using namespace IO;

int solve() {
  int n, m;
  cin >> n >> m;
  rep(i, 0, m) {
    int a;
    cin >> a;
    n -= a;
  }
  if (n < 0)
    put(-1);
  else
    put(n);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  //    freopen("input.txt", "r", stdin);
  int t = 1;
  //    cin >> t;
  while (t--)
    solve();
}


Fixed submission:
// #pragma GCC optimize("O3")
// #pragma GCC target("avx2")
//  293206GT
#include <algorithm>
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

typedef long long ll;
#define int ll
using namespace std;
#define put(a) cout << (a) << '\n'
#define sqr(x) (x) * (x)
typedef pair<ll, ll> pii;
typedef long double ld;

typedef pair<ld, ld> pld;
#define rep(x, y, a) for (int x = (y); x < (int)(a); ++x)
#define all(a) a.begin(), a.end()
#define chkmax(a, b) a = max(a, (b))
#define chkmin(a, b) a = min(a, (b))
#define prev asasdasd
#define next aasdasda
#define left asdasdasdasd
#define right asnabsdkasdl
#define rank asdasdknlasd
#define move asdasdas

typedef vector<int> vi;
typedef vector<pii> vpii;
namespace IO {
template <class A, class B> ostream &operator<<(ostream &out, pair<A, B> &a) {
  out << a.first << " " << a.second;
  return out;
}

template <class A, class B>
ostream &operator<<(ostream &out, vector<pair<A, B>> &a) {
  for (pair<A, B> x : a)
    out << x.first << " " << x.second << '\n';
}

template <class A> ostream &operator<<(ostream &out, vector<A> &a) {
  for (A x : a)
    out << x << ' ';
  return out;
}

template <class A, class B> istream &operator>>(istream &in, pair<A, B> &a) {
  in >> a.first >> a.second;
  return in;
}

template <class A> istream &operator>>(istream &in, vector<A> &a) {
  for (A &x : a)
    in >> x;
  return in;
}
} // namespace IO
using namespace IO;

void solve() {
  int n, m;
  cin >> n >> m;
  rep(i, 0, m) {
    int a;
    cin >> a;
    n -= a;
  }
  if (n < 0)
    put(-1);
  else
    put(n);
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  //    freopen("input.txt", "r", stdin);
  int t = 1;
  //    cin >> t;
  while (t--)
    solve();
}