1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| #include <bits/stdc++.h> using namespace std; #define IO_L 1048576 char _buf[IO_L], *S, *Ts, IO_c, IO_signum; inline char read() { if (S == Ts) { Ts = (S = _buf) + fread(_buf, 1, IO_L, stdin); if (S == Ts) return EOF; } return *S++; } inline bool read(int &x) { IO_signum = false; for (IO_c = read(); IO_c < '0' || IO_c > '9'; IO_c = read()) { if (IO_c == -1) return false; if (IO_c == '-') IO_signum = true; } x = 0; while (IO_c == '0') IO_c = read(); for (;; IO_c = read()) { if (IO_c < '0' || IO_c > '9') break; x = (x << 3) + (x << 1) + (IO_c ^ '0'); } if (IO_signum) x = -x; return true; } #define in(x, y) read(x), read(y) int n, arr[5000500]; long long ans = 9223372036854775807LL, tmp; long long sum[5000500]; long long mid; int main() { freopen("divide.in", "r", stdin); freopen("divide.out", "w", stdout); read(n); for (register int i = 1; i <= n; i++) read(arr[i]), sum[i] = sum[i - 1] + arr[i]; if (n < 100) { for (register int i = 1, range = n - 1; i < range; i++) for (register int j = i + 1; j < n; j++) tmp = max(sum[i], sum[j] - sum[i]), tmp = max(tmp, sum[n] - sum[j]), ans = min(ans, tmp); cout << ans; } else { mid = sum[n] / 3; int pos11, pos12, pos13, pos14, pos21, pos22, pos23, pos24, pos25, pos26, pos27, pos28, pos211, pos221, pos231, pos241, pos251, pos261, pos271, pos281; for (register int pos1p = 1; pos1p <= n; pos1p++) { if (sum[pos1p] > mid) { pos11 = pos1p - 1, pos12 = pos1p, pos13 = pos1p - 2, pos14 = pos1p + 1; break; } } for (register int i = pos11 + 1; i <= n; i++) { if (sum[i] - sum[pos11] > mid) { pos21 = i - 1, pos22 = i, pos211 = i - 2, pos221 = i + 1; break; } } for (register int i = pos12 + 1; i <= n; i++) { if (sum[i] - sum[pos12] > mid) { pos23 = i - 1, pos24 = i, pos231 = i - 2, pos241 = i + 1; break; } } for (register int i = pos13 + 1; i <= n; i++) { if (sum[i] - sum[pos13] > mid) { pos25 = i - 1, pos26 = i, pos251 = i - 2, pos261 = i + 1; break; } } for (register int i = pos14 + 1; i <= n; i++) { if (sum[i] - sum[pos14] > mid) { pos27 = i - 1, pos28 = i, pos271 = i - 2, pos281 = i + 1; break; } } tmp = max(max(sum[pos11], sum[pos21] - sum[pos11]), sum[n] - sum[pos21]), ans = min(ans, tmp); tmp = max(max(sum[pos11], sum[pos211] - sum[pos11]), sum[n] - sum[pos211]), ans = min(ans, tmp);
tmp = max(max(sum[pos11], sum[pos22] - sum[pos11]), sum[n] - sum[pos22]), ans = min(ans, tmp); tmp = max(max(sum[pos11], sum[pos221] - sum[pos11]), sum[n] - sum[pos221]), ans = min(ans, tmp);
tmp = max(max(sum[pos12], sum[pos23] - sum[pos12]), sum[n] - sum[pos23]), ans = min(ans, tmp); tmp = max(max(sum[pos12], sum[pos231] - sum[pos12]), sum[n] - sum[pos231]), ans = min(ans, tmp);
tmp = max(max(sum[pos12], sum[pos24] - sum[pos12]), sum[n] - sum[pos24]), ans = min(ans, tmp); tmp = max(max(sum[pos12], sum[pos241] - sum[pos12]), sum[n] - sum[pos241]), ans = min(ans, tmp);
tmp = max(max(sum[pos13], sum[pos25] - sum[pos13]), sum[n] - sum[pos25]), ans = min(ans, tmp); tmp = max(max(sum[pos13], sum[pos251] - sum[pos13]), sum[n] - sum[pos251]), ans = min(ans, tmp);
tmp = max(max(sum[pos13], sum[pos26] - sum[pos13]), sum[n] - sum[pos26]), ans = min(ans, tmp); tmp = max(max(sum[pos13], sum[pos261] - sum[pos13]), sum[n] - sum[pos261]), ans = min(ans, tmp);
tmp = max(max(sum[pos14], sum[pos27] - sum[pos14]), sum[n] - sum[pos27]), ans = min(ans, tmp); tmp = max(max(sum[pos14], sum[pos271] - sum[pos14]), sum[n] - sum[pos271]), ans = min(ans, tmp);
tmp = max(max(sum[pos14], sum[pos28] - sum[pos14]), sum[n] - sum[pos28]), ans = min(ans, tmp); tmp = max(max(sum[pos14], sum[pos281] - sum[pos14]), sum[n] - sum[pos281]), ans = min(ans, tmp); cout << ans; } return 0; }
|