Problem p02706 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define vi vector<int>
#define vli vector<ll>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define M_PI 3.14159265358979323846
#define mp make_pair
#define sz(x) (int)x.size()
#define matrix vector<vector<ll>>
#define forn(i, a, b) for (int i = a; i < b; i++)
#define eps 0.0000000001
#define all(a) (a).begin(), (a).end()
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

const long long INF = (long long)1e11 + 1;
const long long mod = (long long)100000000;
const long long hell = (long long)1000000007;

ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y % 2)
      res = (x * res);
    x = (x * x);
    y = y / 2;
  }
  return res;
}

ll modinv(ll x, ll m) { return power(x, m - 2); }

void fastIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll gcd(ll a, ll b) {
  while (b != 0) {
    ll t = b;
    b = a % t;
    a = t;
  }
  return a;
}

void solve() {
  int n, m;
  cin >> n >> m;

  ll a[m];
  forn(i, 0, m) cin >> a[i];
  ll sum = 0;

  forn(i, 0, m) sum += a[i];
  if (sum > n) {
    cout << -1;
  } else
    cout << n - sum;
}

int main() {
  fastIO();
  int test;
  // cin>>test;
  test = 1;

  while (test--)
    solve();
}

/// Before submit=>
///     *check for integer overflow,array bounds
///     *check for n=1

/*
  For freq or prime factors

for(auto pri:prime){
  while(k%pri==0){
    pcnt[pri]++;
    k/=pri;
  }
}*/

// gcd of strings
/*
string getgcd(string s1,string s2){

    while(true){
        if(s1.length()<s2.length()){
            swap(s1,s2);
            continue;
        }
        if(s2.empty())
        return s1;

        if(s1.substr(0,s2.length())!=s2)
        return "";

        s1=s1.substr(s2.length());
    }
}
*/
/*
// for user defined DS hashmap and set

struct hash_pair{
      size_t operator()(const pair<int,pair<int,int>>& p) const{
          auto hash1=hash<int>()(p.F) ;
          auto hash2=hash<int>()(p.S.F) ;
          auto hash3=hash<int>()(p.S.S) ;
          return (hash1^hash2)^hash3;
      }
};
struct pair_hash
{
        template <class T1, class T2>
        std::size_t operator () (std::pair<T1, T2> const &pair) const
        {
                std::size_t h1 = std::hash<T1>()(pair.first);
                std::size_t h2 = std::hash<T2>()(pair.second);

                return h1 ^ h2;
        }
};
*/


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long int
#define ull unsigned long long int
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define vi vector<int>
#define vli vector<ll>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define M_PI 3.14159265358979323846
#define mp make_pair
#define sz(x) (int)x.size()
#define matrix vector<vector<ll>>
#define forn(i, a, b) for (int i = a; i < b; i++)
#define eps 0.0000000001
#define all(a) (a).begin(), (a).end()
#define ordered_set                                                            \\
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

const long long INF = (long long)1e11 + 1;
const long long mod = (long long)100000000;
const long long hell = (long long)1000000007;

ll power(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y % 2)
      res = (x * res);
    x = (x * x);
    y = y / 2;
  }
  return res;
}

ll modinv(ll x, ll m) { return power(x, m - 2); }

void fastIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll gcd(ll a, ll b) {
  while (b != 0) {
    ll t = b;
    b = a % t;
    a = t;
  }
  return a;
}

void solve() {
  int n, m;
  cin >> n >> m;
  ll a[m];
  forn(i, 0, m) cin >> a[i];
  ll sum = 0;
  forn(i, 0, n) sum += a[i];
  if (sum > n) {
    cout << -1;
  } else
    cout << n - sum;
}

int main() {
  fastIO();
  int test;
  // cin>>test;
  test = 1;

  while (test--)
    solve();
}

/// Before submit=>
///     *check for integer overflow,array bounds
///     *check for n=1

/*
  For freq or prime factors

for(auto pri:prime){
  while(k%pri==0){
    pcnt[pri]++;
    k/=pri;
  }
}*/

// gcd of strings
/*
string getgcd(string s1,string s2){

    while(true){
        if(s1.length()<s2.length()){
            swap(s1,s2);
            continue;
        }
        if(s2.empty())
        return s1;

        if(s1.substr(0,s2.length())!=s2)
        return "";

        s1=s1.substr(s2.length());
    }
}
*/
/*
// for user defined DS hashmap and set

struct hash_pair{
      size_t operator()(const pair<int,pair<int,int>>& p) const{
          auto hash1=hash<int>()(p.F) ;
          auto hash2=hash<int>()(p.S.F) ;
          auto hash3=hash<int>()(p.S.S) ;
          return (hash1^hash2)^hash3;
      }
};
struct pair_hash
{
        template <class T1, class T2>
        std::size_t operator () (std::pair<T1, T2> const &pair) const
        {
                std::size_t h1 = std::hash<T1>()(pair.first);
                std::size_t h2 = std::hash<T2>()(pair.second);

                return h1 ^ h2;
        }
};
*/