#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define INF 2e9 #define MOD 1000000007 //#define MOD 998244353 #define LINF (long long)4e18 #define jck 3.141592 #define PI acos(-1.0); const double EPS = 1e-10; using ll = long long; using Pi = pair; using Pl = pair; int main(){ int N; cin >> N; deque A(2*N); rep(i,2*N) cin >> A[i]; ll ans = 0; while(!A.empty()){ int k = A.size(); int a = A[0],b = A[1],c = A[k-1],d = A[k-2]; if(a-b>c-d){ ans += a-b; A.pop_front(); A.pop_front(); } else{ ans += c-d; A.pop_back(); A.pop_back(); } } cout << ans << endl; }