Problem p03661 (C++)

Buggy submission (status: Runtime Error):

#pragma region Include /Defines
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;
typedef long long lli;
typedef vector<int> vi;
typedef vector<lli> vli;
typedef pair<int, int> pii;
typedef pair<lli, lli> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef long double ld;
#define all(a) std::begin(a), std::end(a)

const int INF = 0x3f3f3f3f;
const lli LINF = 0x3f3f3f3f3f3f3f3f;

template <typename T, typename T2> inline void _max(T &a, T2 b) {
  a = max((T)a, (T)b);
}
template <typename T, typename T2> inline void _min(T &a, T2 b) {
  a = min((T)a, (T)b);
}

#ifdef _DEBUG
#define epr(...) fprintf(stderr, __VA_ARGS__)
#else
#define epr(...)
#endif
#pragma endregion

const int MAX = 1e5 + 10;
int n, a[MAX];

void clear() {}

void read() {
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
}

void solve() {
  lli sum = 0;
  for (int i = 0; i < n; i++)
    sum += a[i];
  lli mind = LINF;
  lli ps = a[0];
  for (int i = 1; i < n; i++) {
    lli o = sum - ps;
    lli td = abs(o - ps);
    _min(mind, td);
    ps += a[i];
  }
  printf("%lld\n", mind);
}

int main() {
#ifdef _LOCAL_VAN
  freopen("in.txt", "r", stdin);
#endif
  while (scanf("%d", &n) == 1) {
    clear();
    read();
    solve();
#ifndef _LOCAL_VAN
    return 0;
#endif
  }
  return 0;
}

Fixed submission:

#pragma region Include /Defines
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <memory.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>

using namespace std;
typedef long long lli;
typedef vector<int> vi;
typedef vector<lli> vli;
typedef pair<int, int> pii;
typedef pair<lli, lli> pll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
typedef long double ld;
#define all(a) std::begin(a), std::end(a)

const int INF = 0x3f3f3f3f;
const lli LINF = 0x3f3f3f3f3f3f3f3f;

template <typename T, typename T2> inline void _max(T &a, T2 b) {
  a = max((T)a, (T)b);
}
template <typename T, typename T2> inline void _min(T &a, T2 b) {
  a = min((T)a, (T)b);
}

#ifdef _DEBUG
#define epr(...) fprintf(stderr, __VA_ARGS__)
#else
#define epr(...)
#endif
#pragma endregion

const int MAX = 2e5 + 10;
int n, a[MAX];

void clear() {}

void read() {
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
}

void solve() {
  lli sum = 0;
  for (int i = 0; i < n; i++)
    sum += a[i];
  lli mind = LINF;
  lli ps = a[0];
  for (int i = 1; i < n; i++) {
    lli o = sum - ps;
    lli td = abs(o - ps);
    _min(mind, td);
    ps += a[i];
  }
  printf("%lld\n", mind);
}

int main() {
#ifdef _LOCAL_VAN
  freopen("in.txt", "r", stdin);
#endif
  while (scanf("%d", &n) == 1) {
    clear();
    read();
    solve();
#ifndef _LOCAL_VAN
    return 0;
#endif
  }
  return 0;
}