Problem p03850 (C++)

Buggy submission (status: Time Limit Exceeded):
// 0bd35dc5e428754fa574049b6be8efe0

#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

const int MAXN = (int)1e5;

int n;

int A[MAXN + 5];
char type[MAXN + 5];

inline char safe_getchar() {
  register char ch(getchar());
  while (!isgraph(ch))
    ch = getchar();
  return ch;
}

inline void input() {
  n = read<int>();
  type[1] = '+';
  for (int i = 1; i <= n; ++i) {
    if (i > 1)
      type[i] = safe_getchar();
    A[i] = read<int>();
  }
}

inline void solve() {
  LL suf = 0;
  for (int i = 1; i <= n; ++i)
    suf += A[i];

  LL ans = LLONG_MIN, pre = 0;
  for (int i = 1, j; i <= n; i = j) {
    LL res = A[i];
    suf -= A[i];
    for (j = i + 1; type[j] == '+'; ++j)
      res += A[j], suf -= A[j];

    if (i > 1) {
      chkmax(ans, pre - res + suf);
      pre += res - A[i] - A[i];
    } else
      pre += res;
  }
  chkmax(ans, pre);

  printf("%lld\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}


Fixed submission:
// 0bd35dc5e428754fa574049b6be8efe0

#include <bits/stdc++.h>

using namespace std;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int)(u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status() {
  ifstream t("/proc/self/status");
  cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>())
       << endl;
}

template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T> inline T read() {
  register T sum(0), fg(1);
  register char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      fg = -1;
  for (; isdigit(ch); ch = getchar())
    sum = sum * 10 - '0' + ch;
  return sum * fg;
}

typedef long long LL;
typedef pair<int, int> pii;

const int MAXN = (int)1e5;

int n;

int A[MAXN + 5];
char type[MAXN + 5];

inline char safe_getchar() {
  register char ch(getchar());
  while (!isgraph(ch))
    ch = getchar();
  return ch;
}

inline void input() {
  n = read<int>();
  type[1] = '+';
  for (int i = 1; i <= n; ++i) {
    if (i > 1)
      type[i] = safe_getchar();
    A[i] = read<int>();
  }
}

inline void solve() {
  LL suf = 0;
  for (int i = 1; i <= n; ++i)
    suf += A[i];

  LL ans = LLONG_MIN, pre = 0;
  for (int i = 1, j; i <= n; i = j) {
    LL res = A[i];
    suf -= A[i];
    for (j = i + 1; type[j] == '+'; ++j)
      res += A[j], suf -= A[j];

    if (i > 1) {
      chkmax(ans, pre - res + suf);
      pre += res - A[i] - A[i];
    } else
      pre += res;
  }
  chkmax(ans, pre);

  printf("%lld\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
//	freopen("E.in", "r", stdin);
//	freopen("E.out", "w", stdout);
#endif

  input();
  solve();

  return 0;
}