Problem p03312 (C++)

Buggy submission (status: Time Limit Exceeded):
/**********************************************************************/
/********                                                      ********/
/********                     Writer:孙~~                      ********/
/********                       Lang:C++                       ********/
/********                     Status:Unknow                    ********/
/********                                                      ********/
/**********************************************************************/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int SIZE = 200005;

int a[SIZE], n;
long long s[SIZE];
inline long long sum(int a, int b) {
  if (a == 0)
    return s[b];
  else
    return s[b] - s[a - 1];
}

inline long long calc(int id1, int id2, int id3) {
  return max(sum(0, id1), max(sum(id1 + 1, id2),
                              max(sum(id2 + 1, id3), sum(id3 + 1, n - 1)))) -
         min(sum(0, id1), min(sum(id1 + 1, id2),
                              min(sum(id2 + 1, id3), sum(id3 + 1, n - 1))));
}

int pos(int l, int r) {
  int ans;
  for (ans = l + 1; ans < r && sum(l, ans) <= sum(ans + 1, r); ans++)
    ;
  if (sum(l, ans) - sum(ans + 1, r) < sum(ans, r) - sum(l, ans - 1) && ans < r)
    return ans;
  else
    return ans - 1;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", a + i);
  s[0] = a[0];
  for (int i = 1; i < n; i++)
    s[i] = s[i - 1] + (long long)a[i];
  long long best = 1ll << 60;
  for (int i = 1; i <= n - 1; i++) {
    best = min(best, calc(pos(0, i), i, pos(i + 1, n - 1)));
  }
  printf("%lld\n", best);
  return 0;
}

Fixed submission:
/**********************************************************************/
/********                                                      ********/
/********                     Writer:孙~~                      ********/
/********                       Lang:C++                       ********/
/********                     Status:Unknow                    ********/
/********                                                      ********/
/**********************************************************************/
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
// #include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int SIZE = 200005;

int a[SIZE], n;
long long s[SIZE];
inline long long sum(int a, int b) {
  if (a == 0)
    return s[b];
  else
    return s[b] - s[a - 1];
}

inline long long calc(int id1, int id2, int id3) {
  return max(sum(0, id1), max(sum(id1 + 1, id2),
                              max(sum(id2 + 1, id3), sum(id3 + 1, n - 1)))) -
         min(sum(0, id1), min(sum(id1 + 1, id2),
                              min(sum(id2 + 1, id3), sum(id3 + 1, n - 1))));
}

int pos(int l, int r) {
  int le = l, ri = r, ans = -1;
  while (le < ri) {
    int mid = (le + ri) / 2;
    if (sum(l, mid) <= sum(mid + 1, r))
      le = mid + 1;
    else {
      ri = mid;
      ans = mid;
    }
  }
  if (ans == -1)
    return r - 1;
  else if (sum(l, ans) - sum(ans + 1, r) < sum(ans, r) - sum(l, ans - 1))
    return ans;
  else
    return ans - 1;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", a + i);
  s[0] = a[0];
  for (int i = 1; i < n; i++)
    s[i] = s[i - 1] + (long long)a[i];
  long long best = 1ll << 60;
  for (int i = 1; i <= n - 1; i++) {
    best = min(best, calc(pos(0, i), i, pos(i + 1, n - 1)));
  }
  printf("%lld\n", best);
  return 0;
}