Problem p03062 (C++)

Buggy submission (status: Runtime Error):
/**
 *   author:  prakhar17252
 *   created: 27.04.2019 18:27:22
 **/
#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;

using ll = long long;
using ld = long double;
#define int ll

#define pb emplace_back
#define push emplace
#define mp make_pair
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define len(a) (int)a.size()
#define reset(a, val) memset(a, val, sizeof(a))
#define endl '\n'
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.first << ' ' << v.second;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <class Arg1, class... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *sep = strchr(names + 1, ',');
  cerr.write(names, sep - names) << " : " << arg1 << "  ";
  __f(sep + 1, args...);
}
#else
#define trace(...) 0
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

template <class T, class cmp = less<T>>
using ordered_set =
    tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;

const ll mod = (int)1e9 + 7;
const ll inf = (ll)2e18;
const ll ninf = (ll)-2e18;

ll power(ll x, ll y, ll mod = inf) {
  if (y == 0)
    return 1LL;
  if (y % 2)
    return ((x % mod) * power(x, y - 1, mod)) % mod;
  return power((x * x) % mod, y / 2, mod);
}

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

  time_t t1, t2;
  t1 = clock();

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif

  int n;
  cin >> n;
  vector<int> a(n), neg, pos;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0) {
      neg.emplace_back(a[i]);
    } else {
      pos.emplace_back(a[i]);
    }
  }

  int ans = 0;
  if (neg.size() % 2 == 0) {
    for (int i = 0; i < n; i++) {
      ans += abs(a[i]);
    }
  } else {
    sort(a.begin(), a.end(),
         [&](int i, int j) -> bool { return abs(i) < abs(j); });
    for (int i = 1; i < n; i++) {
      ans += abs(a[i]);
    }
    ans -= abs(a[0]);
  }

  cout << ans << '\n';

  t2 = clock();
#ifndef ONLINE_JUDGE
  cerr << endl << "time taken: " << (ld)(t2 - t1) / CLOCKS_PER_SEC << endl;
#endif // ONLINE_JUDGE

  return 0;
}

Fixed submission:
/**
 *   author:  prakhar17252
 *   created: 27.04.2019 18:27:22
 **/
#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;

using ll = long long;
using ld = long double;
#define int ll

#define pb emplace_back
#define push emplace
#define mp make_pair
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define len(a) (int)a.size()
#define reset(a, val) memset(a, val, sizeof(a))
#define endl '\n'
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  for (T i : v)
    os << i << ' ';
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const pair<T, S> &v) {
  os << v.first << ' ' << v.second;
  return os;
}
template <class T, class S>
ostream &operator<<(ostream &os, const map<T, S> &v) {
  for (auto i : v)
    os << '(' << i.first << "=>" << i.second << ')' << ' ';
  return os;
}

#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <class Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << endl;
}
template <class Arg1, class... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *sep = strchr(names + 1, ',');
  cerr.write(names, sep - names) << " : " << arg1 << "  ";
  __f(sep + 1, args...);
}
#else
#define trace(...) 0
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target(                                                            \
    "avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define _CRT_SECURE_NO_WARNINGS
#endif // ifndef ONLINE_JUDGE

template <class T, class cmp = less<T>>
using ordered_set =
    tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;

const ll mod = (int)1e9 + 7;
const ll inf = (ll)2e18;
const ll ninf = (ll)-2e18;

ll power(ll x, ll y, ll mod = inf) {
  if (y == 0)
    return 1LL;
  if (y % 2)
    return ((x % mod) * power(x, y - 1, mod)) % mod;
  return power((x * x) % mod, y / 2, mod);
}

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

  time_t t1, t2;
  t1 = clock();

  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  //     freopen("error.txt", "w", stderr);
  // #endif

  int n;
  cin >> n;
  vector<int> a(n), neg, pos;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] < 0) {
      neg.emplace_back(a[i]);
    } else {
      pos.emplace_back(a[i]);
    }
  }

  int ans = 0;
  if (neg.size() % 2 == 0) {
    for (int i = 0; i < n; i++) {
      ans += abs(a[i]);
    }
  } else {
    sort(a.begin(), a.end(),
         [&](int i, int j) -> bool { return abs(i) < abs(j); });
    for (int i = 1; i < n; i++) {
      ans += abs(a[i]);
    }
    ans -= abs(a[0]);
  }

  cout << ans << '\n';

  t2 = clock();
#ifndef ONLINE_JUDGE
  cerr << endl << "time taken: " << (ld)(t2 - t1) / CLOCKS_PER_SEC << endl;
#endif // ONLINE_JUDGE

  return 0;
}