Language: C++

// D.

#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <vector>

using namespace std;

typedef long long LL;

vector<LL> v, sum;

LL check(LL a, LL b, LL c) {
	LL n = v.size();
	if (a >= b || a >= c || b >= c || a < 1 || c > n-1) {
		return 1LL << 60;
	}
	LL p = sum[a], q = sum[b] - sum[a], r = sum[c] - sum[b], s = sum[n] - sum[c];
	LL mn = min({ p, q, r, s });
	LL mx = max({ p, q, r, s });
	return mx - mn;
}

int main(int argc, char *argv[]) {
	LL n;
	cin >> n;
	v.resize(n);
	sum.resize(n + 1);
	for (LL i = 0; i < n; ++i) {
		cin >> v[i];
		sum[i + 1] = sum[i] + v[i];
	}
	LL pos[3];
	pos[1] = min(lower_bound(sum.begin(), sum.end(), sum[n] / 2) - sum.begin(), n - 2);
	pos[0] = lower_bound(sum.begin(), sum.end(), sum[pos[1]] / 2) - sum.begin();
	pos[2] = min(max(lower_bound(sum.begin(), sum.end(), (sum[pos[1]] + sum[n]) / 2) - sum.begin(), pos[1] + 1), n - 1);
	LL ans = check(pos[0], pos[1], pos[2]);
	for (LL t = 0; t < 1000; ++t) {
		for (LL da = -2; da <= 2; ++da) {
			LL a = pos[0] + da;
			for (LL db = -2; db <= 2; ++db) {
				LL b = pos[1] + db;
				for (LL dc = -2; dc <= 2; ++dc) {
					LL c = pos[2] + dc;
					LL x = check(a, b, c);
					if (x < ans) {
						ans = x; pos[0] = a; pos[1] = b; pos[2] = c;
					}
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}


Compiler output:
a.cc: In function 'int main(int, char**)':
a.cc:36:21: error: no matching function for call to 'min(__gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >::difference_type, LL)'
   36 |         pos[1] = min(lower_bound(sum.begin(), sum.end(), sum[n] / 2) - sum.begin(), n - 2);
      |                  ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:36:21: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'LL' {aka 'long long int'})
   36 |         pos[1] = min(lower_bound(sum.begin(), sum.end(), sum[n] / 2) - sum.begin(), n - 2);
      |                  ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:5:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:36:21: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   36 |         pos[1] = min(lower_bound(sum.begin(), sum.end(), sum[n] / 2) - sum.begin(), n - 2);
      |                  ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:38:25: error: no matching function for call to 'max(__gnu_cxx::__normal_iterator<long long int*, std::vector<long long int> >::difference_type, LL)'
   38 |         pos[2] = min(max(lower_bound(sum.begin(), sum.end(), (sum[pos[1]] + sum[n]) / 2) - sum.begin(), pos[1] + 1), n - 1);
      |                      ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:38:25: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'LL' {aka 'long long int'})
   38 |         pos[2] = min(max(lower_bound(sum.begin(), sum.end(), (sum[pos[1]] + sum[n]) / 2) - sum.begin(), pos[1] + 1), n - 1);
      |                      ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:38:25: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   38 |         pos[2] = min(max(lower_bound(sum.begin(), sum.end(), (sum[pos[1]] + sum[n]) / 2) - sum.begin(), pos[1] + 1), n - 1);
      |                      ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~