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

#include <bits/stdc++.h>
#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}
ll mod(ll a, ll b) { return (a % b + b) % b; }
using namespace std;

ll powerm(ll base, ll exp, ll mod) {
  ll ans = 1;
  while (exp) {
    if (exp & 1)
      ans = (ans * base) % mod;
    exp >>= 1, base = (base * base) % mod;
  }
  return ans;
}

void solve() {
  ll n;
  cin >> n;
  ll a[n + 1];
  for (ll i = 1; i < n + 1; i++)
    cin >> a[i];
  ll ps[n + 1] = {0}, dp[n + 1] = {0};
  dp[0] = 0;
  dp[1] = 0;
  ps[1] = a[1];
  for (ll i = 3; i <= n; i += 2) {
    ps[i] = ps[i - 2] + a[i];
    //	cout << ps[i] << " ";
  }
  for (ll i = 2; i < n + 1; i++) {
    if (i & 1) {
      dp[i] = max(dp[i - 1], a[i] + dp[i - 2]);
    } else {
      dp[i] = max(dp[i - 2] + a[i], ps[i - 1]);
    }
  }
  cout << dp[n] << endl;
}
int main() {
  fast;
  solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define gcd(m, n) __gcd(m, n)
#define lcm(m, n) m *(n / gcd(m, n))
#define fast                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pi acos(-1.0)
#define endl '\\n'
#define MOD 1000000007
#define ull unsigned long long
#define ll long long
#define ld long double
#define pb push_back
#define dbg(x) cout << #x << "  " << x << endl;

using namespace std;

ll mod(ll a, ll b) { return (a % b + b) % b; }
ll power(ll base, ll exp) {
  ll res = 1;
  while (exp > 0) {
    if (exp % 2 == 1)
      res = (res * base);
    base = (base * base);
    exp /= 2;
  }
  return res;
}

unordered_map<ll, bool> mp;

bool chck(ll n) {
  if (mp[n] == true)
    return true;
  else
    return false;
}
void init() {
  mp.clear();
  for (ll i = 0; i <= 200000; i++) {
    mp[i] = false;
  }
}
int main() {
  init();
  fast;
  ll n, x;
  cin >> n;
  ll t = n / 2;
  vector<pair<ll, ll>> vec;
  for (ll i = 0; i < n; i++) {
    cin >> x;
    vec.pb(make_pair(x, i));
  }
  sort(vec.begin(), vec.end());
  /*	for(ll i = 0; i < n; i++)
          {
                  cout << vec[i].first <<  " " << vec[i].second << endl;
          } */
  ll sum = 0;
  sum += vec[n - 1].first;
  mp[vec[n - 1].second] = true;
  t--;
  ll i = n - 2;
  while (t--) {
    if (vec[i].second == 0 && chck(vec[i].second + 1) == true) {
      i--;
      t++;
    } else if (vec[i].second == n - 1 && chck(vec[i].second - 1) == true) {
      i--;
      t++;
    } else if (chck(vec[i].second + 1) == true ||
               chck(vec[i].second - 1) == true) {
      i--;
      t++;
    } else {
      sum += vec[i].first;
      mp[vec[i].second] = true;
      i--;
    }
  }
  cout << sum << endl;
  return 0;
}