#include <bits/stdc++.h>
#define st first
#define nd second
#define db double
#define re register
#define pb push_back
#define mk make_pair
#define int long long
#define ldb long double
#define pii pair<int, int>
#define ull unsigned long long
#define mst(a, b) memset(a, b, sizeof(a))
using namespace std;
const int N = 2e5 + 10, INF = 2e9;
inline int read()
{
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9') { if(ch == '-') w *= -1; ch = getchar(); }
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0', ch = getchar();
return s * w;
}
int n, c, ans, a[N];
vector<int> op;
inline int f(int x)
{
int res = a[x];
for(re int x : op) res = 2 * x - res;
return res;
}
signed main()
{
// freopen("data.in", "r", stdin);
// freopen("sol.out", "w", stdout);
n = read(), ans = INF;
for(re int i = 1; i <= n; i++) a[i] = read();
c = a[n] - a[1];
for(re int t = 1; t <= 1e3; t++){
if(t & 1){
int l = 1, r = n, mx = f(n);
int x = (mx + 1) / 2, res;
while(l <= r){
int mid = (l + r) >> 1, v = f(mid);
if(v >= x) res = v, r = mid - 1;
else l = mid + 1;
}
ans = min(ans, c + 2 * res - mx), op.pb(res);
}
else{
int l = 1, r = n, mx = f(1);
int x = (mx + 1) / 2, res;
while(l <= r){
int mid = (l + r) >> 1, v = f(mid);
if(v >= x) res = v, l = mid + 1;
else r = mid - 1;
}
ans = min(ans, c + 2 * res - mx), op.pb(res);
}
}
printf("%lld\n", ans);
return 0;
}
./Main.cpp: In function ‘long long int f(long long int)’:
./Main.cpp:28:14: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
28 | for(re int x : op) res = 2 * x - res;
| ^
./Main.cpp: In function ‘int main()’:
./Main.cpp:36:14: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
36 | for(re int i = 1; i <= n; i++) a[i] = read();
| ^
./Main.cpp:38:14: warning: ISO C++17 does not allow ‘register’ storage class specifier [-Wregister]
38 | for(re int t = 1; t <= 1e3; t++){
| ^