Problem p00523 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"

int N;
bool check(const int from, const vector<long long int> &sums,
           const long long int norma) {
  long long int base = *(sums.begin() + from);
  auto it = lower_bound(sums.begin() + from, sums.begin() + from + N + 1,
                        base + norma);
  if (it == sums.begin() + from + N + 1)
    return false;
  base = *it;
  auto jt = lower_bound(it, sums.begin() + from + N + 1, base + norma);
  if (jt == sums.begin() + from + N + 1)
    return false;
  base = *jt;
  if (*(sums.begin() + from + N) - base >= norma)
    return true;
  else
    return false;
}
int main() {
  cin >> N;
  vector<long long int> nums;
  for (int i = 0; i < N; ++i) {
    long long int a;
    cin >> a;
    nums.push_back(a);
  }
  long long int sum = 0;
  vector<long long int> sums;
  sums.push_back(sum);
  for (int t = 0; t < 2; ++t) {

    for (int i = 0; i < N; ++i) {
      sum += nums[i];
      sums.push_back(sum);
    }
  }
  long long int ans = 0;
  for (int i = 0; i < N; ++i) {
    long long int amin = 0;
    long long int amax = 1e18;
    while (amin + 1 != amax) {
      long long int amid = (amin + amax) / 2;
      if (check(i, sums, amid)) {
        amin = amid;
      } else {
        amax = amid;
      }
    }
    ans = max(ans, amin);
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"

int N;
bool check(const int from, const vector<long long int> &sums,
           const long long int norma) {
  vector<long long int> nowsums(sums.begin() + from,
                                sums.begin() + from + N + 1);
  long long int base = *nowsums.begin();
  auto it = lower_bound(nowsums.begin(), nowsums.end(), base + norma);
  if (it == nowsums.end())
    return false;
  base = *it;
  auto jt = lower_bound(it, nowsums.end(), base + norma);
  if (jt == nowsums.end())
    return false;
  base = *jt;
  if (nowsums.back() - base >= norma)
    return true;
  else
    return false;
}
int main() {
  cin >> N;
  vector<long long int> nums;
  for (int i = 0; i < N; ++i) {
    long long int a;
    cin >> a;
    nums.push_back(a);
  }
  long long int sum = 0;
  vector<long long int> sums;
  sums.push_back(sum);
  for (int t = 0; t < 2; ++t) {

    for (int i = 0; i < N; ++i) {
      sum += nums[i];
      sums.push_back(sum);
    }
  }
  long long int ans = 0;
  for (int i = 0; i < N; ++i) {
    long long int amin = 0;
    long long int amax = 1e18;
    while (amin + 1 != amax) {
      long long int amid = (amin + amax) / 2;
      if (check(i, sums, amid)) {
        amin = amid;
      } else {
        amax = amid;
      }
    }
    ans = max(ans, amin);
  }
  cout << ans << endl;
  return 0;
}