/*input
4
4 2 3 1
2 3 2 4

*/
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("unroll-loops,no-stack-protector")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld double
#define pii pair<int,int>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
const ll maxn=1e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const ll MOD=1e9+7;
const ld PI=3.14159265358979323846;
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define MP make_pair
ll mult(ll a,ll b){
    return ((a%MOD)*(b%MOD))%MOD;
}
ll mypow(ll a,ll b){
    ll res=1LL;
    while(b){
        if(b&1) res=mult(res,a);
        a=mult(a,a);
        b>>=1;
    }
    return res;
}
int a[maxn],b[maxn];
int main(){
    ios::sync_with_stdio(false),cin.tie(0);
    int n;
    cin>>n;
    REP(i,n){
        cin>>a[i];
    }
    REP(i,n) cin>>b[i];
    vector<int> v[2];
    ll sum=0;
    REP(i,n){
        sum+=a[i];
        v[i%2].pb(b[i]-a[i]);
    }
    sort(ALL(v[0]));
    sort(ALL(v[1]));
    reverse(ALL(v[0]));
    reverse(ALL(v[1]));
    ll ans=sum;
    REP(i,n/2){
        sum+=v[0][i]+v[1][i];
        MXTO(ans,sum);

    }
    cout<<ans;
}

#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define ll long long int
#define sd(x) scanf("%lld",&x)
#define sdi(x) scanf("%d",&x)
#define sdc(c) scanf("%c",&c)
#define inf 1000000000000000000ll
#define pll pair<ll,ll>
#define pii pair<int,int>
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define bits(x) __builtin_popcountll(x)
#define ld long double
#define test() ll test; cin>>test; while(test--)
#define fi first
#define se second
#define all(x) x.begin(),x.end()

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

clock_t time_p = clock();
void time_taken()
{
    time_p = clock() - time_p;
    cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}

inline ll GCD(ll x, ll y) {
    if(x<y) swap(x,y);
    if(x==0) return y;
    if(y==0) return x;
    return GCD(x%y,y);
}

ll phi(ll n) {
    ll result = n;
    for (ll i = 2; i * i <= n; i++) {
        if(n % i == 0) {
            while(n % i == 0)
                n /= i;
            result -= result / i;
        }
    }
    if(n > 1)
        result -= result / n;
    return result;
}

ll power(ll x, ll n, ll mod) {
    ll res = 1;
    x %= mod;
    while(n) {
        if(n&1) {
            res = ((res*x)%mod+mod)%mod;
        }
        x = ((x*x)%mod+mod)%mod;
        n>>=1;
    }

    return res;
}

const int MOD = 1e9+7;

inline ll add(ll x, ll y, ll MOD) {
    x %= MOD;
    y %= MOD;
    ll ans = (x+y)%MOD;
    return ans;
}

inline ll mul(ll x,ll y, ll MOD) {
    x %= MOD;
    y %= MOD;
    ll ans = ((x*y)%MOD+MOD)%MOD;
    return ans;
}

int check(int x, vector<vector<int> > &person) {
    set<int> s;
    for(int i=0;i<person.size();i++) {
        int bit = 0;
        for(int j=0;j<5;j++) {
            bit <<= 1;
            bit |= (person[i][j]>=x);
        }
        s.insert(bit);
    }

    vector<int> v;
    while(!s.empty()) {
        v.pb(*s.begin());
        s.erase(s.begin());
    }

    for(int i=0;i<v.size();i++) {
        for(int j=0;j<v.size();j++) {
            for(int k=0;k<v.size();k++) {
                if((v[i]|v[j]|v[k]) == 31) return 1;
            }
        }
    }

    return 0;
}

int main() {
    fastio;
    int N;
    cin>>N;
    vector<vector<int> > person(N,vector<int>(5));

    int a;
    for(int i=0;i<N;i++) {
        for(int j=0;j<5;j++) {
            cin>>a;
            person[i][j] = a;
        }
    }

    int ans = 0;
    int l = 1, h = 1000000000;

    while(l<=h) {
        int mid = (l+h)/2;
        if(check(mid,person)) {
            ans = mid;
            l = mid+1;
        } else {
            h = mid-1;
        }
    }

    cout<<ans<<endl;
    time_taken();
}


#include <algorithm>
#include <bitset>
#include <complex>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using TU = tuple<int, int, int>;

const int INF = 1000000000;
const int MOD = 1000000007;

int main() {
	ll A, B, K;
	cin >> A >> B >> K;
	vector<vector<ll>> dp(31, vector<ll>(31, 1));
	for (ll i = 1;i <= 30;i++) {
		dp.at(i).at(1) = i + 1;
		dp.at(1).at(i) = i + 1;
	}
	for (ll i = 2;i <= 30;i++) {
		for (ll j = 2;j <= 30;j++) {
			dp.at(i).at(j) = dp.at(i - 1).at(j) + dp.at(i).at(j - 1);
		}
	}
	ll Arest = A, Brest = B;
	ll cnt = 0;
	string ans;
	vector<ll> vec;
	while (ans.size()<A+B) {
		ll buf = 0;
		for (ll i = 0;i <= Arest;i++) {
			if (Brest == 0) {
				for (ll k = 0;k < Arest;k++) {
					ans.push_back('a');
				}
				break;
			}
			buf += dp.at(i).at(Brest - 1);
			if (buf + cnt >= K) {
				for (ll j = 0;j < Arest - i;j++) {
					ans.push_back('a');
				}
				ans.push_back('b');
				cnt += buf - dp.at(i).at(Brest - 1);
				Arest = i;
				Brest--;
			}
		}
	}
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x;}
ll mulmod(ll a, ll b) {ll res = 0; a %= mod; assert(b >= 0); for (; b; b >>= 1) {if (b & 1)res = (res + a) % mod; a = 2 * a % mod;} return res;}
ll powmod(ll a, ll b) {ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) {if (b & 1)res = res * a % mod; a = a * a % mod;} return res;}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a;}
//head

//editorial：https://atcoder.jp/contests/abc202/editorial/1893

const int N = 30;
ll dp[N + 1][N + 1];

string find(int a, int b, ll k) {
    if (a == 0) return string(b, 'b'); //一个string,b个 'b'
    if (b == 0) return string(a, 'a');

    //当前层dp是a个'a',b个'b'，多一个b就要少一个a，当前字符串排序大于k就加'a'，反之加'b'
    if (k <= dp[a - 1][b]) return string("a") + find(a - 1, b, k);
    else return string("b") + find(a, b - 1, k - dp[a - 1][b]);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int a, b;
    ll k;
    cin >> a >> b >> k;

    //组合数dp[i][j]意思是从长i宽j的方格，走到(i,j)的走法数量
    //本质是：C(i+j,j) = C(i+j-1,j) + C(i+j-1, j-1)
    //dp[i][j] -> C(i + j, j)
    dp[0][0] = 1;

    for (int i = 0; i <= a; i++)
        for (int j = 0; j <= b; j++) {
            if (i > 0) dp[i][j] += dp[i - 1][j];
            if (j > 0) dp[i][j] += dp[i][j - 1];
        }
    cout << find(a, b, k) << endl;

    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
#define int ll
#define pii pair<int,int>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define rrep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const double pi=acos(-1.0);
const double eps=1e-8;
const int INF=0x3f3f3f3f;
const int MAXN=1e5+10;
const int mod=1e9+7;
signed main()
{
    std::ios::sync_with_stdio(false);
    int a,b,w;
    cin>>a>>b>>w;
    w*=1000;
    int minn=w/b;
    int maxx=w/a;
    int flag=1;
    int tmp=minn*b;
    if(w-tmp>minn*(b-a))
        flag=0;
    tmp=maxx*a;
    if(w-tmp>minn*(b-a))
        flag=0;
    if(flag)
    {
        if(minn*b!=w) minn++;
        cout<<minn<<" "<<maxx;
    }
    else
    {
        cout<<"UNSATISFIABLE";
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i, a, b) for(int i = (int)(a); i <= (int)(b); ++i)
#define FORR(i, a, b) for(int i = (int)(a); i >= (int)(b); --i)
#define ALL(c) (c).begin(), (c).end()

using ll = long long;
using VI = vector<int>;
using VL = vector<ll>;
using VD = vector<double>;
using VII = vector<VI>;
using VLL = vector<VL>;
using VDD = vector<VD>;
using P = pair<int, int>;
using PL = pair<ll, ll>;

template <typename T>
bool chmax(T& a, const T& b) {
  if(a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T& a, const T& b) {
  if(a > b) {
    a = b;
    return true;
  }
  return false;
}

ll get_count(ll N, ll x, VL& sum_two) {
  ll count = 0;
  FOR(i, 1, N) {
    if(i >= x) break;
    count += sum_two[x - i];
  }
  return count;
}

void solve(long long N, long long K) {
  VL count_two(3 * N + 1, 0);
  VL sum_two(3 * N + 2, 0);
  FOR(i, 2, 3 * N) {
    count_two[i] = max(0ll, min(N, ll(i - 1)) - max(1ll, ll(i - N)) + 1ll);
  };
  FOR(i, 2, 3 * N) {
    sum_two[i + 1] += sum_two[i] + count_two[i];
  }
  ll ok = 0;
  ll ng = 3 * N + 1;
  while(abs(ok - ng) > 1) {
    ll mid = (ok + ng) / 2;
    if(get_count(N, mid, sum_two) < K) {
      ok = mid;
    } else {
      ng = mid;
    }
  }
  K -= get_count(N, ok, sum_two);
  ll x = 1, y = 1, z = 1;
  ll tmp_count = 0;
  FOR(i, 1, N) {
    if(tmp_count + count_two[ok - i] >= K) {
      K -= tmp_count;
      x = i;
      break;
    }
    tmp_count += count_two[ok - i];
  }
  tmp_count = 0;
  FOR(i, 1, N) {
    if(ok - x - i >= 1 && ok - x - i <= N) tmp_count++;
    if(tmp_count == K) {
      y = i;
      break;
    }
  }
  z = ok - x - y;
  cout << x << " " << y << " " << z << endl;
}

int main() {
  long long N;
  scanf("%lld", &N);
  long long K;
  scanf("%lld", &K);
  solve(N, K);
  return 0;
}


#include <bits/stdc++.h>

template <typename T> inline void rd(T& x) {
	int si = 1; char c = getchar(); x = 0;
	while(!isdigit(c)) si = c == '-' ? -1 : si, c = getchar();
	while(isdigit(c)) x = x * 10 + c - 48, c = getchar();
	x *= si;
}
template <typename T, typename... Args>
inline void rd(T& x, Args&... args) { rd(x); rd(args...); }
#define fi first
#define se second
#define mkp std::make_pair
typedef long long ll;
typedef double ff;
typedef std::pair <int, int> pii;
const int N = 1e5 + 5, B = 14, Inf = 0x3f3f3f3f, Mod = 998244353;
const ll InfLL = 0x3f3f3f3f3f3f3f3fLL;

int QPow(int a, int b) {
	int ret = 1, bas = a;
	for(; b; b >>= 1, bas = 1LL * bas * bas % Mod)
		if(b & 1) ret = 1LL * ret * bas % Mod;
	return ret;
}

int n, m, f[B][N], fac[N], fac_inv[N];
void Init() {
	fac[0] = 1;
	for(int i = 1; i <= n; ++i) fac[i] = 1LL * fac[i - 1] * i % Mod;
	fac_inv[n] = QPow(fac[n], Mod - 2);
	for(int i = n - 1; ~i; --i) fac_inv[i] = 1LL * fac_inv[i + 1] * (i + 1) % Mod;
}
int C(int x, int y) {
	return 1LL * fac[x] * fac_inv[x - y] % Mod * fac_inv[y] % Mod;
}
void Calc() {
	f[0][0] = 1;
	for(int i = 1; i < B; ++i) {
		for(int j = 0; j <= m; ++j) {
			for(int k = 0; k * (1 << (i - 1)) <= j && k <= n; k += 2)
				f[i][j] = (1LL * f[i - 1][j - k * (1 << (i - 1))] * C(n, k) + f[i][j]) % Mod;
		}
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("a.in", "r", stdin);
	freopen("a.out", "w", stdout);
#endif
	int test_case_cnt = 1; //rd(test_case_cnt);
	while(test_case_cnt--) {
		rd(n, m); Init(); Calc();
		printf("%d\n", f[B - 1][m]);
	} return 0;
}

# include <bits/stdc++.h>

using namespace std;

int a[10][10];

int main() {

#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	int n, k;
	cin >> n >> k;
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < n; ++j) {
			cin >> a[i][j];
		}
	}
	vector<int> v;
	for (int i = 1; i < n; ++i) {
		v.push_back(i);
	}
	int cnt = 0;
	do {
		int total = 0 ;
		total += a[0][v[0]];
		for (int i = 1; i < v.size(); ++i) {
			total += a[v[i - 1]][v[i]];
		}
		total += a[0][v[v.size() - 1]];
		if (total == k) {
			cnt += 1;
		}
	} while (next_permutation(v.begin(), v.begin() + v.size()));
	cout << cnt << endl;
}


#include <bits/stdc++.h>
using namespace std;

#define ll                          long long
#define ull                         unsigned long long
#define ld                          long double
#define sd                          second
#define ft                          first
#define is                          insert
#define endl                        '\n'
#define all(x)                      x.begin(),x.end()
#define PB                          push_back
#define MP                          make_pair

const long double Pi=(ld)acos((ld)-1);

ll int power(ll int x,ll int y){ll int temp;if(y==0)return 1;temp=power(x,y/2);if(y%2==0)return temp*temp;else return x*temp*temp;}

ll int gcd(ll  int a,ll int b){if(b==0){return a;}else{return gcd(b,a%b);}}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    // ll int t;
    // cin>>t;
    // while(t--)
    // {
        ll int n,k,c=0;
        cin>>n>>k;
        vector<pair<ll int,ll int>>vc;
        ll int a=k;
        for(ll int i=0;i<n;i++)
        {
            ll int i1,i2;
            cin>>i1>>i2;
            vc.PB(MP(i1,i2));
        }
        sort(vc.begin(),vc.end());

        for(ll int i=0;i<n;i++)
        {

            if(k>=(vc[i].ft))
            {
                k+=vc[i].sd;
                // k-=vc[i].ft;
                a+=vc[i].sd;
            }
            else
            {
                break;
            }
            // cout<<k<<" || ";
        }
        cout<<a;
    // }
}

#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, k, n) for (int i = k; i < (int)(n); i++)
#define repd(i, n) for (int i = n-1; i >= 0; i--)
#define rrepd(i, k, n) for (int i = n-1; i >= (int)(k); i--)
#define all(x) (x).begin(),(x).end()
#define chmax(x,y) x=max(x,y)
#define chmin(x,y) x=min(x,y)
#define F first //pairの一つ目の要素  //遅延セグ木使うときは注意
#define S second //pairの二つ目の要素  //遅延セグ木使うときは注意
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
//V,Pは大文字i,l,bは小文字
using ll = long long;
using Vi = vector<int>;
using VVi = vector<Vi>;
using Vl = vector<ll>;
using VVl = vector<Vl>;
using Vb = vector<bool>;
using VVb = vector<Vb>;
using P = pair<int,int>;
using Pl = pair<ll, ll>;
using Vs = vector<string>;
const ll mod = 1000000007;
const ll inf = 1000000000000000000;//10の18乗
#define yn {puts("Yes");}else{puts("No");}
#define dame { puts("-1"); return 0;}

int main() {
    ll n,k;
    cin >> n >> k;
    map<ll,ll> ma;
    rep(i,n){
        ll x,y;
        cin >> x >> y;
        ma[x]+=y;
    }
    ll now=k;
    ll ans=0;
    for(auto p:ma){
        ll x=p.F,y=p.S;
        if(ans+now<x){
            cout << ans+now << endl;
            return 0;
        }
        else{
            now-=(x-ans);
            ans=x;
            now+=y;
        }
    }
    cout << now+ans << endl;

}


#include <bits/stdc++.h>
using namespace std;

int r1, c1, r2, c2;

bool is_in(int x, int y) {
    if (r1 + c1 == x + y || c1 - r1 == y - x || abs(r1 - x) + abs(c1 - y) <= 3) return true;
    return false;
}

int main() {
    int ans;
    scanf("%d%d%d%d", &r1, &c1, &r2, &c2);
    if (r1 == r2 && c1 == c2) ans = 0;
    else if (is_in(r2, c2)) ans = 1;
    else {
        bool flag = false;
        for (int i = r2 - 2; i <= r2 + 2; ++i)
            for (int j = c2 - 2; j <= c2 + 2; ++j)
                if (is_in(i, j)) flag = true;
        if (is_in(r2 - 3, c2)) flag = true;
        if (is_in(r2 + 3, c2)) flag = true;
        if (is_in(r2, c2 - 3)) flag = true;
        if (is_in(r2, c2 + 3)) flag = true;
        if (((r1 + c1) & 1) == ((r2 + c2) & 1)) flag = true;
        if (flag) ans = 2;
        else ans = 3;
    }
    printf("%d", ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define ff first
#define ss second

#define pb push_back
#define mp make_pair
#define all(x) (x).begin() ,(x).end()
#define prec(x) cout<<fixed<<setprecision(x)

const ll mod = 1e9 + 7;

ll binpow(ll a, ll b , ll mod) {
	long long res = 1;
	while (b > 0) {
		if (b & 1)
			res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res % mod;
}

const int dx[] = {1 , -1 , 0 , 0};
const int dy[] = {0 , 0 , -1 , 1};

const int N = 3e5 + 5;
const ll inf = 1e16;

void solve() {

	int n;
	cin >> n;

	string s;
	cin >> s;

	string p = "ATGC";
	int fr[n][4];

	memset(fr , 0 , sizeof(fr));

	for (int i = 0 ; i < n ; i++) {
		for (int j = 0 ; j < 4 ; j++) {
			fr[i][j] = fr[i - 1][j] * ( i != 0 ) + (p[j] == s[i]);
		}
	}

	int ans = 0;

	for (int i = 0 ; i < n ; i++) {
		for (int j = i ; j < n ; j++) {
			int A = 0 , T = 0 , C = 0 , G = 0;

			T = fr[j][1] , A = fr[j][0] , G = fr[j][2] , C = fr[j][3];
			if (i > 0)
				T -= fr[i - 1][1] , A -= fr[i - 1][0] , G -= fr[i - 1][2] , C -= fr[i - 1][3];
			//cout << T << " " << A << " " << G << " " << C << "\n";
			if ((T > 0 or C > 0) and T == A and C == G)
				ans++;
		}
	}

	cout << ans << "\n";
}

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int t = 1;
	//cin >> t;

	while (t--)
		solve();

	return 0;
}

/*
Author: rafa45
Date: 28 Sep 2020
*/
#include<bits/stdc++.h>
#define ll long long
#define endl "\n"
#define mod 1000000007
using namespace std;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int i=0, j=0, n=0;
    int c1=0, c2=0, cnt=0;
    string s, sub;

    cin >> n >> s;

    for(i=0; i<n-1; i++){
        c1=0; c2=0;
        for(j=i; j<n; j++){
            if(s[j]=='A') c1++;
            else if(s[j]=='T') c1--;
            else if(s[j]=='C') c2++;
            else c2--;
            if(c1==0 && c2==0) cnt++;
        }
    }

    cout << cnt << endl;

    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define lb long long double
#define mp make_pair
#define MOD 1000000007
#define pb push_back
#define ppi pair<ll,pair<ll,ll>>
#define F(i,n) for(ll i=0;i<n;i++)
#define w(t) ll t;cin >> t;while(t--)
using namespace std;

vector<ll> divisor;
void getDivisors(ll n)
{
    for (ll i=1; i<=sqrt(n); i++)
    {
        if (n%i == 0)
        {
            // If divisors are equal, take only one
            if (n/i == i)
                divisor.push_back(i);

            else // Otherwise take both
            {
                divisor.push_back(i);
                divisor.push_back(n/i);
            }
        }
    }
}

int main()
{

    //w(t)
    {

        //cout << "Codeforces Higher rating" << endl;
        ll n;
        cin >> n;
        vector<ll> a(n),b(n);
        for(int i = 0 ; i < n ; i = i + 1){

            cin >> a[i] >> b[i];
        }
        if(n == 1)
        {
            cout << a[0] + b[0] << endl;
            return 0;
        }

        int min1ind = min_element(a.begin(),a.end()) - a.begin();
        int min2ind = min_element(b.begin(),b.end()) - b.begin();

        int x = (a[min1ind] + b[min2ind]);
        if(min1ind != min2ind) cout << max(a[min1ind],b[min2ind]) << endl;
        else
        {
            sort(a.begin(),a.end());
            sort(b.begin(),b.end());

            cout << min(x,(int)min(max(a[0],b[1]),max(a[1],b[0]))) <<endl;
        }

    }

}

#include "bits/stdc++.h"
using namespace std;

int main()
{
    int N;
    cin >> N;

    vector<int> A(N), B(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i] >> B[i];
    }

    int min_A, min_B, min_A2, min_B2;
    int min_AB = min(A[0] + B[0], A[1] + B[1]);
    int idx_A = -1;
    int idx_B = -1;
    if (A[0] < A[1]) {
        min_A = A[0];
        min_A2 = A[1];
        idx_A = 0;
    }
    else {
        min_A = A[1];
        min_A2 = A[0];
        idx_A = 1;
    }
    if (B[0] < B[1]) {
        min_B = B[0];
        min_B2 = B[1];
        idx_B = 0;
    }
    else {
        min_B = B[1];
        min_B2 = B[0];
        idx_B = 1;
    }
    for (int i = 2; i < N; i++) {
        if (A[i] < min_A) {
            min_A2 = min_A;
            min_A = A[i];
            idx_A = i;
        }
        if (A[i] < min_A2) {
            min_A2 = A[i];
        }
        if (B[i] < min_B) {
            min_B2 = min_B;
            min_B = B[i];
            idx_B = i;
        }
        if (B[i] < min_B2) {
            min_B2 = B[i];
        }
        if (A[i] + B[i] < min_AB) {
            min_AB = A[i] + B[i];
        }
    }

    int time = 0;

    if (idx_A == idx_B) {
        int max1 = max(min_A, min_B2);
        int max2 = max(min_A2, min_B);
        time = min(max1, max2);
    }
    else {
        time = max(min_A, min_B);
    }

    time = min(time, min_AB);

    cout << time << endl;
}


// #pragma GCC target("avx")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
// #define BEGIN_STACK_EXTEND(size) void * stack_extend_memory_ = malloc(size);void * stack_extend_origin_memory_;char * stack_extend_dummy_memory_ = (char*)alloca((1+(int)(((long long)stack_extend_memory_)&127))*16);*stack_extend_dummy_memory_ = 0;asm volatile("mov %%rsp, %%rbx\nmov %%rax, %%rsp":"=b"(stack_extend_origin_memory_):"a"((char*)stack_extend_memory_+(size)-1024));
// #define END_STACK_EXTEND asm volatile("mov %%rax, %%rsp"::"a"(stack_extend_origin_memory_));free(stack_extend_memory_);

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<deque>
#include<stack>
#include<string>
#include<string.h>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
#include<numeric>
#include<unordered_set>
#include<unordered_map>
#include<complex>
using namespace std;
const long long mod=998244353;
const long long inf=mod*mod;
const long long d2=(mod+1)/2;
const double EPS=1e-11;
const double INF=1e+10;
const double PI=acos(-1.0);
const int C_SIZE = 11100000;
const int UF_SIZE = 3100000;
namespace{
	long long fact[C_SIZE];
	long long finv[C_SIZE];
	long long inv[C_SIZE];
	inline long long Comb(int a,int b){
	 	if(a<b||b<0)return 0;
	 	return fact[a]*finv[b]%mod*finv[a-b]%mod;
	}
	void init_C(int n){
		fact[0]=finv[0]=inv[1]=1;
		for(int i=2;i<n;i++){
			inv[i]=(mod-(mod/i)*inv[mod%i]%mod)%mod;
		}
		for(int i=1;i<n;i++){
			fact[i]=fact[i-1]*i%mod;
			finv[i]=finv[i-1]*inv[i]%mod;
		}
	}
	long long pw(long long a,long long b){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%mod;
			a=a*a%mod;
			b/=2;
		}
		return ret;
	}
	long long pw_mod(long long a,long long b,long long M){
		if(a<0LL)return 0;
		if(b<0LL)return 0;
		long long ret=1;
		while(b){
			if(b%2)ret=ret*a%M;
			a=a*a%M;
			b/=2;
		}
		return ret;
	}
	int pw_mod_int(int a,int b,int M){
		if(a<0)return 0;
		if(b<0)return 0;
		int ret=1;
		while(b){
			if(b%2)ret=(long long)ret*a%M;
			a=(long long)a*a%M;
			b/=2;
		}
		return ret;
	}
	int ABS(int a){return max(a,-a);}
	long long ABS(long long a){return max(a,-a);}
	double ABS(double a){return max(a,-a);}
	int sig(double r) { return (r < -EPS) ? -1 : (r > +EPS) ? +1 : 0; }
	int UF[UF_SIZE];
	void init_UF(int n){
		for(int i=0;i<n;i++)UF[i]=-1;
	}
	int FIND(int a){
		if(UF[a]<0)return a;
		return UF[a]=FIND(UF[a]);
	}
	void UNION(int a,int b){
		a=FIND(a);b=FIND(b);if(a==b)return;
		if(UF[a]>UF[b])swap(a,b);
		UF[a]+=UF[b];UF[b]=a;
	}
}
// ここから編集しろ
int p[210000];
int q[210000];
int cnt[410000];
int main(){
	int a;scanf("%d",&a);
	for(int i=0;i<a;i++){
		scanf("%d%d",p+i,q+i);
		p[i]--;q[i]--;
	}
	init_UF(410000);
	for(int i=0;i<a;i++){
		UNION(p[i],q[i]);
	}
	for(int i=0;i<a;i++){
		cnt[FIND(p[i])]++;
	}
	int ret=0;
	for(int i=0;i<410000;i++){
		if(FIND(i)!=i)continue;
		ret+=min(-UF[FIND(i)],cnt[i]);
	}
	printf("%d\n",ret);
}

#include <iostream>
#include <vector>
#include <utility>

typedef long long ll;

int N,T;
double L,R;

using namespace std;
double eps = 1e-4;

int main() {
    cin >> N;
    vector<pair<double,double>> intervals;
    for(int i = 0; i < N; i++) {
        cin >> T >> L >> R;
        if(T == 1) {
            intervals.emplace_back(L,R);
        }
        else if(T == 2) {
            intervals.emplace_back(L,R-eps);
        }
        else if (T == 3) {
            intervals.emplace_back(L+eps,R);
        }
        else if (T == 4) {
            intervals.emplace_back(L+eps,R-eps);
        }
    }
    int ans = 0;
    for(int i = 0; i < N; i++) {
        for(int j = i+1; j < N; j++) {
            auto interval1 = intervals[i];
            auto interval2 = intervals[j];

//            cout << max(interval1.first, interval2.first) << " " << min(interval1.second, interval2.second) << endl;

            if (max(interval1.first, interval2.first) <= min(interval1.second, interval2.second)) {
                ans ++;
            }

//            if (! (interval1.second < interval2.first || interval1.first > interval2.second)) {
//                //cout << interval1.first << "," << interval1.second << " intersects " << interval2.first << "," <<
//                //interval2.second << endl;
//                ans++;
//            } else {
//            }
        }
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, q;
    cin >> n >> q;
    long long arr[n];
    long long diff[n];
    cin >> arr[0];
    diff[0] = arr[0] - 1;
    for (int i = 1; i < n; ++i) {
        cin >> arr[i];
        diff[i] = diff[i-1] + arr[i] - arr[i-1] - 1;
    }

    while (q--) {
        long long x;
        cin >> x;

        if (x <= diff[0]) cout << x << "\n";
        else if (x > diff[n-1]) cout << arr[n-1] + x - diff[n-1] << "\n";
        else {
            bool done = false;
            int l = 0, r = n, mid;

            while (l <= r) {
                mid = (l + r) / 2;

                if (x == diff[mid]) {
                    while (diff[mid - 1] == diff[mid]) mid--;
                    cout << arr[mid] - 1 + (x - x) << "\n";
                    done = true;
                    break;
                } else if (x < diff[mid]) {
                    r = mid - 1;
                } else {
                    l = mid + 1;
                }
            }

            if (!done) {
                while (diff[mid] < x) mid++;
                cout << arr[mid] - 1 + (x - diff[mid]) << "\n";
            }
        }
    }
}

#include <bits/stdc++.h>

typedef long long ll;
typedef long double ld;
#define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define PRECISION cout << fixed << setprecision(20);

using namespace std;

template <typename SegNode>
struct SegTree {
    vector<SegNode> tree, arr;
    int n;

    SegTree(int n): n(n) {
        tree.assign(4 * n, 0);
    }

    SegTree(int n, vector<SegNode> arr): n(n) {
        tree.assign(4 * n, 0);
        arr = arr;
        build(0, n - 1, 0);
    }

    SegNode merge(SegNode l, SegNode r) {
        return l + r;
    }

    void build(int ss, int se, int si) {
        if (ss == se) {
            tree[si] = arr[ss];
        } else {
            int mid = ss + se >> 1;
            build(ss, mid, 2 * si + 1);
            build(mid + 1, se, 2 * si + 2);
            tree[si] = merge(tree[2 * si + 1], tree[2 * si + 2]);
        }
    }

    SegNode query(int qs, int qe) {
        return query(qs, qe, 0, n - 1, 0);
    }

    SegNode query(int qs, int qe, int ss, int se, int si) {
        if (ss >= qs && se <= qe) {
            return tree[si];
        }

        int mid = ss + se >> 1;
        if (qe <= mid) {
            return query(qs, qe, ss, mid, 2 * si + 1);
        } else if (qs >= mid + 1) {
            return query(qs, qe, mid + 1, se, 2 * si + 2);
        } else {
            return merge(query(qs, qe, ss, mid, 2 * si + 1), query(qs, qe, mid + 1, se, 2 * si + 2));
        }
    }

    void update(int qi, SegNode val) {
        update(qi, val, 0, n - 1, 0);
    }

    void update(int qi, SegNode val, int ss, int se, int si) {
        if (ss == se) {
            tree[si] += val;
        } else {
            int mid = ss + se >> 1;
            if (qi <= mid)
                update(qi, val, ss, mid, 2 * si + 1);
            else
                update(qi, val, mid + 1, se, 2 * si + 2);
            tree[si] = merge(tree[2 * si + 1], tree[2 * si + 2]);
        }
    }
};

void solve() {
    int n, m, q;
    cin >> n >> m >> q;

    vector<ll> a(n + 1), b(m + 1);
    vector<array<ll, 3>> queries(q);

    vector<ll> store = {-1, 0};

    for (auto &qu: queries) {
        cin >> qu[0] >> qu[1] >> qu[2];
        store.push_back(qu[2]);
    }

    sort(store.begin(), store.end());
    store.resize(unique(store.begin(), store.end()) - store.begin());

    int len = store.size(); // 0 to len - 1
    map<ll, int> ind;
    for (int i = 0; i < len; i++) ind[store[i]] = i;

    ll suma = 0, sumb = 0, ans = 0;

    SegTree<ll> tsuma(len), tsumb(len), tcnta(len), tcntb(len);

    tcntb.update(ind[0], m);
    tcnta.update(ind[0], n);

    for (auto &qu: queries) {
        ll t = qu[0], x = qu[1], y = qu[2];

        if (t == 1) {
            // change in a
            ll org = sumb - tsumb.query(0, ind[a[x]]) + tcntb.query(0, ind[a[x]]) * a[x];
            ans -= org;
            tsuma.update(ind[a[x]], -a[x]);
            tcnta.update(ind[a[x]], -1);
            suma -= a[x];

            a[x] = y;

            tsuma.update(ind[a[x]], a[x]);
            tcnta.update(ind[a[x]], 1);
            suma += a[x];
            ll act = sumb - tsumb.query(0, ind[a[x]]) + tcntb.query(0, ind[a[x]]) * a[x];
            ans += act;
        } else {
            ll org = suma - tsuma.query(0, ind[b[x]]) + tcnta.query(0, ind[b[x]]) * b[x];
            ans -= org;
            tsumb.update(ind[b[x]], -b[x]);
            tcntb.update(ind[b[x]], -1);
            sumb -= b[x];

            b[x] = y;

            tsumb.update(ind[b[x]], b[x]);
            tcntb.update(ind[b[x]], 1);
            sumb += b[x];
            ll act = suma - tsuma.query(0, ind[b[x]]) + tcnta.query(0, ind[b[x]]) * b[x];
            ans += act;
        }

        cout << ans << "\n";
    }
}

int main() {
    FASTIO;
    PRECISION;

    int t = 1;
    // cin >> t;

    for (int i = 0; i < t; i++) {
        solve();
    }
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define endl "\n"
#define mod 1000000007
#define vec vector <ll>
#define vecp vector <pair<ll,ll>>
#define mp make_pair
#define A first
#define B second
#define PattSee ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define all(v) v.begin(),v.end()
#define mpl map<ll,ll>
#define umpl unordered_map<ll,ll>
#define rep(i,a,b) for(i=a; i<b; i++)
using namespace std;

void solve()
{
    ll i,j,flag=0;
    // Write your code here
    ll n;
    cin>>n;
    vec v(n+1,2);
    cout<<"1 ";
    for(i=2; i<=n; i++)
    {
    	for(j=2; i*j<=n; j++)
    		v[i*j]=v[i]+1;
    	cout<<v[i]<<" ";
    }
}

int main()
{
    PattSee
    ll t=1;
    //cin>>t;
    while(t--)
    {
    	solve();
    }
    return 0;
}

/*
if lcm of all is not last then +1
els +(no. of non prime)

3 + no. of non-prime divisors

5*3*7 = 21*5 = 105

105
33 35 37
17 19 21 23 25
11 13 15 17 19 21 23

48
23 25
9 11 13 15
*/

#include<bits/stdc++.h>
using namespace std;

int main() {
    int n;
    cin >> n;

    int dp[n + 1];
    dp[1] = 1;
    for(int i = 2;i <= n;++i) {
        int max_div = 1;
        for(int d = 2;d <= sqrt(i);++d) {
            if(i % d == 0) {
                max_div = max(max_div, d);
                max_div = max(max_div, i / d);
            }
        }
        dp[i] = dp[max_div] + 1;
    }

    for(int i = 1;i <= n;++i) {
        cout << dp[i] << " ";
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std ;
typedef long long ll ;

const int mod=1000000007;
const int INF=1e9 ;
const int N=2e5 ;

 ll AbsDiff(ll a[], int n)
    {
      if ( n < 2 ) return 0;
      sort(a,a+n);
      ll sum = 0;
      int i;
      for(i=n-1;i>=0;i--)
      {
        sum += ((ll)a[i]*((ll)i) - (ll)a[i]*((ll)n-i-1));
      }
      return sum;
    }

void solve(){

    int n ;
    cin>>n ;
    ll a[n] ;

    rep(i,n){
        cin>>a[i] ;
    }

    cout<<AbsDiff(a,n) ;

}

int main(){

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin) ;
        freopen("output.txt","w",stdout) ;
    #endif

        cin.tie(0);
        cout.tie(0);
        ios_base::sync_with_stdio(false);

        int y ;

        y=1 ;
        //cin>>y ;

        while(y--){

            solve() ;

        }

        return 0 ;

}

#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;

int read() {
    register int x = 0, f = 0, ch = getchar();
    for (; !isdigit(ch); f = (ch == 45), ch = getchar()) ;
    for (; isdigit(ch); x = x * 10 + ch - 48, ch = getchar()) ;
    return f ? -x : x;
}

void write(int x) {
    if (x < 0) putchar(45), x = -x;
    static int sta[35];
    int top = 0;
    do {
        sta[top++] = x % 10, x /= 10;
    } while (x);
    while (top) putchar(sta[--top] + 48);
}

const int maxn = 2e5 + 9, maxm = 2e5 + 9, inf = 0x3f3f3f3f;

int n, m;
ll a[maxn], b[maxn], v1[maxn], v2[maxn];

std::vector<int> vec[maxn];

ll c[maxn];

#define lowbit(x) ((x) & (-x))

void add(ll x, ll k) {
    for (; x <= n; x += lowbit(x)) c[x] += k;
}

ll query(ll x) {
    ll ret = 0;
    for (; x; x -= lowbit(x)) ret += c[x];
    return ret;
}

int main() {
    // freopen(".in", "r", stdin);
    // freopen(".out", "w", stdout);
    n = read();
    for (int i = 1; i <= n; ++i) v1[i] = (a[i] = read() + i);
    for (int i = 1; i <= n; ++i) v2[i] = (b[i] = read() + i);
    std::sort(v1 + 1, v1 + n + 1); std::sort(v2 + 1, v2 + n + 1);
    bool flag = 0;
    for (int i = 1; i <= n; ++i) if (v1[i] != v2[i]) {
        flag = 1;
        break;
    }
    if (flag) {
        puts("-1");
        return 0;
    }
    int tot = std::unique(v2 + 1, v2 + n + 1) - v2 - 1;
    for (int i = 1; i <= n; ++i) a[i] = std::lower_bound(v2 + 1, v2 + tot + 1, a[i]) - v2;
    for (int i = 1; i <= n; ++i) b[i] = std::lower_bound(v2 + 1, v2 + tot + 1, b[i]) - v2;
    for (int i = n; i >= 1; --i) vec[b[i]].push_back(i);
    for (int i = 1; i <= n; ++i) {
        int x = a[i];
        a[i] = vec[x].back();
        vec[x].pop_back();
    }
    ll ans = 0;
    for (int i = n; i >= 1; --i) {
        ans += query(a[i] - 1);
        add(a[i], 1);
    }
    printf("%lld\n", ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
//const ll INF = numeric_limits<ll>::max() / 4;
//const int INF = numeric_limits<int>::max() / 4;

int main() {
	// ll N;
	ll A, B, C, D;
	cin >> A >> B >> C >> D;
	ll b = A;
	ll r = 0;
	ll c = 0;
	if(C * D <= B){
		cout << -1 << endl;
		return 0;
	}
	while(true){
		if(b <= r * D){
			cout << c << endl;
			return 0;
		}
		b += B;
		r += C;
		c++;
	}
	return(0);
}

#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <tuple>
#include <utility>
#include <functional>
#include <set>
#include <map>
#include <bitset>
#include <list>
#include<iomanip>

using namespace std;
using ll = long long;
using ULL = unsigned long long;
using pll = std::pair<ll, ll>;
using vi = std::vector<int>;
using vl = std::vector<ll>;
using vpl = std::vector<pll>;
using vb = std::vector<bool>;
using db = double;
using vdb = std::vector<db>;
using qlg= std::priority_queue<ll, vl, std::greater<ll> > ; //ascending
using qll= std::priority_queue<ll, vl, std::less<ll> > ; // descending
using qdg= std::priority_queue<db, vdb, std::greater<db> > ; //ascending
using qdl= std::priority_queue<db, vdb, std::less<db> > ; // descending
template<class T>
using vv = std::vector<std::vector<T> >;
using std::cout; using std::string;
using std::endl; using std::cin;
using std::transform; using std::toupper;

#define REPL(i, n) for (ll i = 0; i < (ll)(n); i++)
#define FORL(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FORL(i, 0, n)

#define MREP(i, n) for (ll i= (ll)(n)-1; i>=0; i-- )
#define MFOR(i, a, b) for (ll i = (ll)(b)-1; i >= (ll)(a); i--)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rreps(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
#define INF pow(10, 10)
void chmin(ll& a, ll b){ if(a > b) a = b; }
void chmax(ll& a, ll b){ if(a < b) a = b; }

int main(void)
{

    ll a, b, c, d;
    cin >> a >> b >> c >> d;

    ll pro = d * c - b;
    if(pro <= 0)
    {
        cout << -1 << endl;
    }
    else
    {
        ll Time;
        if(a%pro ==0) Time = a/pro;
        else Time = a/pro + 1;
        cout << Time << endl;
    }

    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <set>
#include <sstream>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define reps(i, s, n) for (int i = s; i < n; i++)
#define debug(s, param) std::cerr << s << param << std::endl;

using namespace std;
using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

const int INF = 1e9;
const ll INFL = 1e18;
const ll MOD = 1000000007;

int f(const vector<vector<int>>& board, vector<vector<int>>& cache, int row, int col) {
  int h = board.size(), w = board[0].size();
  if (row == h - 1 && col == w - 1) return 0;
  if (cache[row][col] != -1) return cache[row][col];
  bool takahashi = (row + col) % 2 == 0 ? true : false;
  int res = takahashi ? -INF : INF;
  if (takahashi) {
    if (row < h - 1) res = max(res, f(board, cache, row + 1, col) + board[row+1][col]);
    if (col < w - 1) res = max(res, f(board, cache, row, col + 1) + board[row][col+1]);
  } else {
    if (row < h - 1) res = min(res, f(board, cache, row + 1, col) - board[row+1][col]);
    if (col < w - 1) res = min(res, f(board, cache, row, col + 1) - board[row][col+1]);
  }
  cache[row][col] = res;
  return res;
}

int main() {
  int h, w; cin >> h >> w;
  vector<vector<int>> board(h, vector<int>(w));
  rep (row, h) {
    string s; cin >> s;
    rep (col, w) {
      if (s[col] == '+') board[row][col] = 1;
      else board[row][col] = -1;
    }
  }
  vector<vector<int>> cache(h, vector<int>(w, -1));
  int ans = f(board, cache, 0, 0);
  if (ans > 0) cout << "Takahashi" << endl;
  else if (ans < 0) cout << "Aoki" << endl;
  else cout << "Draw" << endl;
  return 0;
}

#include <iostream>

void print_mat(int h, int w, int* mat){
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            std::cout << mat[i*w + j] << " ";
        }
        std::cout << std::endl;
    }
}

int main(){
    int h, w;
    std::cin >> h >> w;

    bool* map = new bool[h*w];
    int* dp = new int[h*w];

    for(int i=0; i<h*w; ++i){
        char c;
        std::cin >> c;
        map[i] = (c=='+');
    }
    if(map[h*w -1]){
        dp[h*w -1] = -1;
    }else {
        dp[h*w -1] = 1;
    }

    for(int sum=h+w-3; 0<=sum; --sum){
        for(int i=std::max(0, sum-w+1); i<std::min(h, sum+1); ++i){
            int j = sum - i;
            int index = w*i + j;
            int val;
            if(i==h-1){
                val = dp[index+1];
            }else if(j==w-1){
                val = dp[index+w];
            }else{
                val = std::min(dp[index+1], dp[index + w]);
            }
            if(map[index]){
                val = -1 - val;
            }else{
                val = 1 -val;
            }
            dp[index] = val;
        }
    }
    int res = dp[0];
    if(map[0]){
        res += 1;
    }else{
        res -= 1;
    }

    if(res > 0){
        std::cout << "Takahashi\n";
    }else if(res==0){
        std::cout << "Draw\n";
    }else{
        std::cout << "Aoki\n";
    }
    // print_mat(h, w, dp);
}


#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define all(x) begin(x), end(x)
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int n0 = 5003, mod = 998244353;
int h, w, k, dp[n0][n0], p[n0][n0], pw[n0 * n0];
char g[n0][n0];

void add(int & x, int y) {
	x += y;
	if (x >= mod) x -= mod;
}

int U(int x, int y) {
	if ((g[x][y] == 'D') || (g[x][y] == 'X')) return 1;
	return (g[x][y] == '?') * 2;
}

int L(int x, int y) {
	if ((g[x][y] == 'R') || (g[x][y] == 'X')) return 1;
	return (g[x][y] == '?') * 2;
}

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	pw[0] = 1;
	for (int i = 1; i < n0 * n0; i++)
		pw[i] = (ll)pw[i - 1] * 3 % mod;
	cin >> h >> w >> k;
	for (int i = 1; i <= h; i++)
		for (int j = 1; j <= w; j++)
			g[i][j] = '?';
	for (int i = 0; i < k; i++) {
		int a, b;
		char c; cin >> a >> b >> c;
		g[a][b] = c;
	}
	dp[1][1] = 1;
	for (int i = 1; i <= h; i++) {
		for (int j = 1; j <= w; j++) {
			if (i == 1 && j == 1) continue;
			p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1] + (g[i][j] == '?');
			int row = p[i][j - 1] - p[i - 1][j - 1];
			int col = p[i - 1][j] - p[i - 1][j - 1];
			int & res = dp[i][j];
			add(res, (ll)dp[i - 1][j] * U(i - 1, j) * pw[row] % mod);
			add(res, (ll)dp[i][j - 1] * L(i, j - 1) * pw[col] % mod);
		}
	}
	int ans = dp[h][w];
	if (g[h][w] == '?') ans = (ll)ans * 3 % mod;
	cout << ans;
}

  /*
    Auther: ghoshashis545 Ashis Ghosh 😎
    College: Jalpaiguri Govt Enggineering College
*/
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
#include<algorithm>
#include<string>
using namespace std;
#define ll int
#define int long long
#define ld long double
#define ff first
#define ss string
#define se second
#define sp(x) cout << fixed << setprecision(x)
#define endl "\n"
#define ub upper_bound
#define lb lower_bound
#define vi vector<int>
#define ipair pair<int,int>
#define vpi vector<ipair>
#define clr(a,x) memset(a,x,sizeof(a))
#define alt(v) v.begin(),v.end()
#define ralt(v) v.rbegin(),v.rend()
#define pb emplace_back
#define mp make_pair
#define PI 3.1415926535897932384626
#define cntb(x) __builtin_popcount(x)
#define fab(i,a,b) for(int i=(a);i<(b);i++)
#define fba(i,a,b) for(int i=(b);i>=(a);i--)

bool ispoweroftwo(int n){return n&(!(n&(n-1)));}
int mod=1000000007;
// int mod=998244353;
int dx[] = {1,0,-1,0};
int dy[] = {0,-1,0,1};

bool test  = 0;

const int inf = 1e18;
const int N = 2e5+5;

int n;
int a[N],ans[60],dist[N];
vector<array<int,2>>adj[N];

void dfs(int u,int p,int c){
    dist[u] = c;
    for(auto it : adj[u]){
        int v = it[0];
        int w = it[1];
        if(v == p)
            continue;
        dfs(v,u,c^w);
    }
}

void solve(int tc = 0)
{
    cin >> n;
    for(int i = 1,x,y,z; i < n; ++i){
        cin >> x >>y >> z;
        adj[x].push_back({y,z});
        adj[y].push_back({x,z});
    }

    dfs(1,-1,0);
    int res = 0;
    for(int i = 0; i < 60; ++i){
        vector<int>cnt(2,0);
        int cur = (1LL<<i)%mod;
        for(int j = 1; j <= n; ++j){
            int bit = dist[j]>>i&1;
            res += (cnt[bit^1]*cur)%mod;
            res %= mod;
            cnt[bit]++;
        }
    }
    cout<<res<<"\n";
}

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    int t=1;
    if(test)
    cin>>t;
    for(int i = 1; i <= t; ++i){
       // cout<<"Case #"<<i<<": ";
        solve(i);
    }
    return 0;
}


#include<bits/stdc++.h>
template <typename _Tp>void read(_Tp &x){
	char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
	if(f)x=-x;
}
template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);}
const int N=300005;
typedef long long ll;
int a[N];
ll s[N];
std::map<ll,int> mp;
int main(){
	int n;read(n);
	for(int i=1;i<=n;++i)read(a[i]);
	ll ans=0;
	++mp[0];
	for(int i=1;i<=n;++i){
		s[i]=s[i-1]+(i%2?a[i]:-a[i]);
		ans+=mp[s[i]]++;
	}
	printf("%lld\n",ans);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int f[11]={2,3,5,7,11,13,17,19,23,29,31};
    int m[11]={0};
    int n;
    cin>>n;
    long long daan=1;
    for(int i=2;i<=n;i++)
    {
        int ans=i;
        for(int c=0;f[c]<=n;c++)
            {
                if(ans%f[c]==0)
                {
                    int flag=0;
                    while(ans%f[c]==0)
                    {
                        ans/=f[c];
                        flag++;
                    }
                    if(flag>m[c])
                        m[c]=flag;
                }
            }
    }
    for(int i=0;i<=10;i++)
    {
        if(m[i]!=0)
            daan*=pow(f[i],m[i]);
    }
    cout<<daan+1;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
  return '"' + s + '"';
}

string to_string(const char* s) {
  return to_string((string) s);
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}

template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto &x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define fi first
#define se second
#define pb push_back
#define mod(n,k) ( ( ((n) % (k)) + (k) ) % (k))
#define forn(i,a,b) for(int i = a; i < b; i++)
#define forr(i,a,b) for(int i = a; i >= b; i--)
#define all(x) (x).begin(), (x).end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

const ll mod = 1e9+7;
int sum(int a, int b){return (a+b) % mod;}
int sub(int a, int b){return (a + mod - b) % mod;}
int mul(int a, int b){return (1ll * a * b) % mod;}
int power(int a,int b){
	int res = 1;
	while(b){
		if(b&1)res = mul(res,a);
		b >>= 1;
		a = mul(a,a);
	}
	return res;
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	ld A,B,W; cin >> A >> B >> W;
	W *= 1000;
	ll mini = (ll)1e18;
	ll maxi = -1;
	forn(i,1,W+1){
		if(A*i <= W && W <= B*i){
			mini = min(mini,ll(i));
			maxi = max(maxi,ll(i));
		}
	}
	if(maxi == -1){
		cout << "UNSATISFIABLE\n";
		return 0;
	}
	cout << mini << ' ' << maxi << '\n';
	return 0;
}
/*
__builtin_mul_overflow(x,y,&x)
-fsplit-stack
*/

#include <iostream>
using namespace std;
using ll = long long;

int main() {
  int A, B, W;
  cin >> A >> B >> W;
  int W_A, W_B;
  W_A = W * 1000 / A;
  W_B = W * 1000 / B;
  if (W_A == W_B && W * 1000 % A != 0 && W * 1000 % B != 0) {
    cout << "UNSATISFIABLE" << endl;
  } else {
    cout << (W*1000 + (B-1)) / B << " " << W_A << endl;
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int f[1100];
int main()
{
	memset(f,0,sizeof(f));
	int k;cin>>k;int sum;bool flag=0;
	for(int i=1;i<=k;i++)
	{
		cin>>sum;
		if(sum<1||sum>k) {flag=1;continue;}
		if(f[sum]==0) f[sum]++;
		else flag=1;
	}
	if(flag==1) cout<<"No";
	else cout<<"Yes";
	return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define x first
#define y second
#define pi pair
#define K 1000000007
using namespace std;

ll a[2000000];
int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll test_case=1;//cin>>test_case;
	while(test_case--)
	{
        string n;cin>>n;
        ll i=0,j=n.size()-1;
        while(i<j)swap(n[i++],n[j--]);
        string s;
         i=0;
        while(i<n.size()&&n[i]=='0')i++;
        while(i<n.size())s.push_back(n[i++]);
        ll flg=0,nt=s.size();
        for(ll i=0;i<s.size();i++)
        if(s[i]!=s[nt-1-i]){flg=1; break;}

        if(flg==0)cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
  {int N, X, i{}, V, P, s{};
  for (cin >> N >> X; ++i, cin >> V >> P;) if ((s += V * P) > 100 * X) cout << i, exit(0);
  puts("-1");}

// Premature optimization is the root of all evil in programming - Knuth.

#include <bits/stdc++.h>

using namespace std;

typedef long long int ll;

#define mod (int) (1e9+7)

void solve(){

	ll n,x; cin>>n>>x;

	x=x*100; ll curr=0,ans=-1;

	for(int i=1; i<=n; i++){
		ll a,b; cin>>a>>b;
		a=a*b; curr+=a;
		if(curr>x && ans==-1){
			ans=i;
		}
	}

	cout<<ans<<"\n";

   return ;

}

int main(){
    ios_base::sync_with_stdio(0);

    cin.tie(0);

    int t=1;//cin>>t;

    // clock_t begin, end;

    // double time_spent;

    // begin = clock(); // Time taken till now.

    while(t--){

	  solve();

    }

   //end= clock(); // Total  Time taken till now.

   //time_spent= (end- begin) / CLOCKS_PER_SEC;

   //cout<<time_spent<<"\n";

	return 0;
}


#pragma GCC optimize("Ofast")
#if 1
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"

using namespace std;

using ll = int64_t;
using db = double;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vpii = vector<pii>;
using vpll = vector<pll>;

constexpr char newl = '\n';
constexpr double eps = 1e-10;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define F0R(i,b) FOR(i,0,b)
#define RFO(i,a,b) for (int i = ((b)-1); i >=(a); i--)
#define RF0(i,b) RFO(i,0,b)
#define show(x) cerr << #x << " = " << (x) << '\n';
#define rng(a) a.begin(),a.end()
#define rrng(a) a.rbegin(),a.rend()
#define sz(x) (int)(x).size()
#define YesNo(x) cout<<((x)?"Yes":"No")<<newl;
#define YESNO(x) cout<<((x)?"YES":"NO")<<newl;
#define v(T) vector<T>
#define vv(T) vector<vector<T>>

template<class A, class B> using umap = unordered_map<A, B>;
template<class A> using uset = unordered_set<A>;

template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
template<class T> bool lcmp(const pair<T, T>& l, const pair<T, T>& r) {
  return l.first < r.first;
}
template<class T> istream& operator>>(istream& i, v(T)& v) {
  F0R(j, sz(v)) i >> v[j];
  return i;
}
template<class A, class B> istream& operator>>(istream& i, pair<A, B>& p) {
  return i >> p.first >> p.second;
}
template<class A, class B, class C> istream& operator>>(istream& i, tuple<A, B, C>& t) {
  return i >> get<0>(t) >> get<1>(t) >> get<2>(t);
}
template<class T> ostream& operator<<(ostream& o, const pair<T, T>& v) {
  o << "(" << v.first << "," << v.second << ")";
  return o;
}
template<class T> ostream& operator<<(ostream& o, const vector<T>& v) {
  F0R(i, v.size()) {
    o << v[i] << ' ';
  }
  o << newl;
  return o;
}
template<class T> ostream& operator<<(ostream& o, const set<T>& v) {
  for (auto e : v) {
    o << e << ' ';
  }
  o << newl;
  return o;
}
template<class T1, class T2> ostream& operator<<(ostream& o, const map<T1, T2>& m) {
  for (auto& p : m) {
    o << p.first << ": " << p.second << newl;
  }
  o << newl;
  return o;
}

const string target = "atcoder";
int inf = 1e9;

// 文字列のt文字目を、t文字以降とのswapのみであるchar以上にする。
// その後swap回数を返す
int F(string& S, int t, char c) {
  int u = -1;
  FOR(i, t, sz(S)) {
    if (S[i] >= c) {
      u = i;
      break;
    }
  }
  if (u < 0) return inf;
  int ans = u - t;
  while (u > t) {
    swap(S[u], S[u - 1]);
    u--;
  }
  return ans;
}

ll solve() {
  string S; cin >> S;
  ll ans = inf;
  ll base = 0; // 既にSに加えた処理回数
  F0R(i, min(sz(target), sz(S))) {
    string s(S);
    chmin(ans, base + F(s, i, target[i] + 1));
    base += F(S, i, target[i]);
  }
  if (sz(S) > sz(target)) {
    chmin(ans, base);
  }
  return ans;
}

// INSERT ABOVE HERE
signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int T; cin >> T;
  F0R(t, T) {
    ll ans = solve();
    if (ans >= inf) ans = -1;
    cout << ans << newl;
  }
}
#endif

#include <bits/stdc++.h>
using namespace std;
string mu = "atcoder", s;
int wei = 0, anss = 0;
int Find(char a) {
	for(int i = 0; i < s.size(); ++i) {
		if(s[i] > a) return wei;
	}
	return 0x3f3f3f3f;
}

int main() {
	int t; cin >> t;
	while(t--) {
		anss = 0x3f3f3f3f;
		cin >> s;
		if(s > mu) anss = 0;
		if(s.size() == 1) {
			if(anss == 0x3f3f3f3f) cout << -1 << endl;
			else cout << anss << endl;
			continue;
		}
		if(s[1] > 'a') anss = min(anss, 1);
//		if(s[1] == 'a') {
		if(s[0] == 'a') for(int i = 1; i < s.size(); ++i) {
			if(s[i] > 't') anss = min(i - 1, anss);
		}
		for(int i = 0; i < s.size(); ++i) {
			if(s[i] > 'a') anss = min(i, anss);
		}
//		}
		if(anss == 0x3f3f3f3f) cout << -1 << endl;
		else cout << anss << endl;
	}
//	cout << "atcodeer" > "atcoder" << endl;
}


// Created by ...Rahul Chandra
#include <bits/stdc++.h>

#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)

using namespace std;
using ll = long long;

ll n;

inline void read() {
    cin >> n;
}

inline void solve() {

}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    long long a,b,c,d,e,f,g,rahul=0;
    string s;
    cin>>s;
    int i=0;
    string k;
    for(auto x:s){
        if (x=='.')
        {
            break;
                        /* code */
        }
        k+=x;
    }
    cout<<k<<endl;
    return 0;
}

#include<iostream>
using namespace std;
int main(){
    string s,ans="";
    cin>>s;
    for (int i=0;i<s.length();i++){
        if (s[i]!='.') ans+=s[i];
        else break;
    }
    cout<<ans<<endl;
}


#include <bits/stdc++.h>
#include <math.h>
using namespace std;
//マクロ
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, n) for (int i = 1; i <= (int)(n); i++)
#define BIT(i, n) for (int i = 0; i < (1 << n); i++)

typedef long long ll;
//const ll INF = 10000000010;

int main() {
  int N;
  int ans = 0;
  cin >> N;
  vector<int> a(N), b(N), c(N);
  rep(i, N) {
    cin >> a.at(i);
  }
   rep(i, N) {
    cin >> b.at(i);
  }
   rep(i, N) {
    c.at(i) = a.at(i) * b.at(i);
     ans += c.at(i);
  }
  if(ans == 0) cout << "Yes" << endl;
  else cout << "No" << endl;
}

#include<bits/stdc++.h>
using namespace std;

int main() {
    int N;
    cin >> N;

    std::vector<int> A(N);
    std::vector<int> B(N);

    for (int i = 0; i < N; ++i){
        cin >> A[i];
    }

    for (int i = 0; i < N; ++i){
        cin >> B[i];
    }

    int result = std::inner_product(
        A.begin(), A.end(), // ひとつ目のベクトル
        B.begin(),          // ふたつ目のベクトル(v以上の次元数を持つこと)
        0);                 // 内積の初期値(この値に対して加算が行われる)

    string ans = "No";
    if (result == 0) {
        ans = "Yes";
    }
    cout << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
int32_t main(){
    ios_base::sync_with_stdio(NULL);
    cin.tie(NULL);
    cout.tie(NULL);
    int a,b,c;
    cin>>a>>b>>c;
    int d=min(c,min(a,b));
    cout<<a+b+c-d;
    return 0;
}

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<vector>
typedef long long ll;
using namespace std;

int a,b;

template<typename int_t>
void readx(int_t &x)
{
	x=0; int_t k=1; char ch=0;
	while (ch<'0' || ch>'9') { ch=getchar(); if (ch=='-') k=-1; }
	while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); }
	x*=k;
}

int main()
{
	readx(a); readx(b);
	printf("%d %d\n",(a+b)/2,(a-b)/2);

}


#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)

int main()
{
	char num[10];
	rep(i, 10)
	{
		cin >> num[i];
	}
	int ans = 0;
	rep(i, 10000)
	{
		int tmp = i;
		bool b[10] = { false };
		rep(j, 4)
		{
			int k = tmp % 10;
			b[k] = true;
			tmp /= 10;
		}
		bool flag = true;
		rep(j, 10)
		{
			if (b[j] && num[j] == 'x')
			{
				flag = false;
				break;
			}
			if (!b[j] && num[j] == 'o')
			{
				flag = false;
				break;
			}
		}
		if (flag) ans++;

	}
	cout << ans;
	return 0;
}

#include <algorithm>
#include <iostream>

using namespace std;

const int A = 200;

int kk[A + A + 1];

int main() {
	int n; cin >> n;
	long long ans = 0;
	while (n--) {
		int a; cin >> a, a += A;
		for (int b = 0; b <= A + A; b++)
			ans += (long long) ((a - A) - (b - A)) * ((a - A) - (b - A)) * kk[b];
		kk[a]++;
	}
	cout << ans << '\n';
	return 0;
}


#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << "\n"
#define COUTF(x) cout << setprecision(15) << (x) << "\n"
#define ENDL cout << "\n"
#define DF(x) x.erase(x.begin())
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort(x.rbegin(), x.rend())
#define REVERSE(x) reverse(ALL(x))
#define MAX(x) *max_element(ALL(x))
#define MAXI(x) max_element(ALL(x)) - x.begin()
#define SUM(x) accumulate(ALL(x), 0ll)
#define COUNT(x, y) count(ALL(x), y);
#define ANS cout << ans << "\n"
#define YES cout << "YES\n";
#define NO cout << "NO\n";
#define Yes cout << "Yes\n";
#define No cout << "No\n";
#define init() \
  cin.tie(0);  \
  ios::sync_with_stdio(false)
#define LINE cerr << "[debug] line: " << __LINE__ << endl;
#define debug(x) cerr << "[debug] " << #x << ": " << x << endl;
#define debugV(v)                       \
  cerr << "[debugV] " << #v << ":";     \
  rep(z, v.size()) cerr << " " << v[z]; \
  cerr << endl;
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using mll = map<ll, ll>;
using qll = queue<ll>;
using P = pair<ll, ll>;
using vp = vector<P>;
using vs = vector<string>;

template <typename T>
inline istream& operator>>(istream& i, vector<T>& v) {
  rep(j, v.size()) i >> v[j];
  return i;
}

template <typename T1, typename T2>
inline istream& operator>>(istream& i, pair<T1, T2>& v) {
  return i >> v.first >> v.second;
}

constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr ld PI = 3.141592653589793238462643383279;
ll get_digit(ll x) {
  return to_string(x).size();
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

ll lcm(ll a, ll b) {
  return a / gcd(a, b) * b;
}

template <class T>
bool chmax(T& a, const T& b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T>
bool chmin(T& a, const T& b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}

signed main() {
  init();
  ll N, M;
  cin >> N >> M;
  vll H(N), W(M);
  cin >> H >> W;
  SORT(H);

  vp d(N);
  rep(i, N) {
    if (i == 0) continue;
    d[i] = d[i - 1];
    if (i % 2 == 1) {
      d[i].first += abs(H[i - 1] - H[i]);
    } else {
      d[i].second += abs(H[i - 1] - H[i]);
    }
  }

  ll ans = INF;
  rep(i, M) {
    ll lb = lower_bound(ALL(H), W[i]) - H.begin();
    if (lb % 2 == 1) lb--;
    ll a = abs(H[lb] - W[i]);
    if (lb != 0) a += d[lb - 1].first;
    if (lb != N - 1) a += (d[N - 1].second - d[lb].second);
    chmin(ans, a);
  }
  ANS;
  return 0;
}

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <cstdio>
#include <numeric>
#include <functional>

using namespace std;
typedef long long ll;

ll N, M;
vector<ll> H, W;

const ll INF = 1e17;
ll fpsum[200005];
ll bpsum[200005];

int main()
{
    ll ans = INF;

    cin >> N >> M;
    H.resize(N);
    W.resize(M);
    for (ll i = 0; i < N; i++)
    {
        cin >> H[i];
    }
    for (ll i = 0; i < M; i++)
    {
        cin >> W[i];
    }
    sort(H.begin(), H.end());
    for (ll i = 0; i < N - 2;i += 2)
    {
        fpsum[i/2 + 1] = H[i + 1] - H[i] + fpsum[i/2];
        bpsum[i/2 + 1] = H[i + 2] - H[i + 1] + bpsum[i/2];
    }
    /*
    for (ll i = 0; i < N - 2; i++)
    {
        cout << fpsum[i] << " ";
    }
    cout << endl;
    for (ll i = 0; i < N - 2; i++)
    {
        cout << bpsum[i] << " ";
    }
    cout << endl;
*/
    for (ll i = 0; i < M;i++)
    {
        auto it = upper_bound(H.begin(), H.end(), W[i]);
        ll idx = it - H.begin();
        ll tans;
        if (idx % 2 == 1)
        {
            tans = fpsum[idx / 2] + abs(W[i] - H[idx - 1]) + bpsum[N / 2] - bpsum[idx / 2];
        }
        else
        {
            tans = fpsum[idx / 2] + abs(W[i] - H[idx]) + bpsum[N / 2] - bpsum[idx / 2];
        }
        ans = min(ans, tans);
    }
    cout << ans << endl;
    return 0;
}


/**
 *    author:  longvu
 *    created: 06/24/21 10:08:58
**/
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()

const int INF = numeric_limits<int>::max();
const int nax = (int)(1000001);
const int mod = 1e9 + 7;

int dp[nax];
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int l, r;
    cin >> l >> r;
    for (int i = r; i >= 2; --i) {
        dp[i] = (r / i - (l - 1) / i) * (r / i - (l - 1) / i);
        for (int j = 2 * i; j <= r; j += i) {
            dp[i] -= dp[j];
        }
    }
    int ans = 0;
    for (int i = 2; i <= r; ++i) {
        if (i >= l) {
            dp[i] -= 2 * (r / i) - 1;
        }
        ans += dp[i];
    }
    cout << ans;
    return 0;
}

// Date: 2020-10-03

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef long double LD;
typedef vector<int> VI;
typedef pair<LL, LL> pll;
typedef pair<int, int> pii;
#define IO freopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout)
#define FIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(i, a, b) for(int i = int(a); i <= int(b); ++i)
#define per(i, b, a) for(int i = int(b); i >= int(a); --i)
#define D(x) cout << #x << " = " << x << endl;
#define mem(x, y) memset(x, y, sizeof(x))
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)x.size())
#define mk make_pair
#define pb push_back
#define fi first
#define se second
const LL INF = 1e18;
const LL mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
LL qpow(LL x, LL y, LL MOD) {LL a=1; while(y){ if(y&1) a=a*x%MOD; x=x*x%MOD; y>>=1; } return a;}

int main() {
    int a, b;
    cin >> a >> b;
    cout << (a+b)/2 << " " << (a-b)/2;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);}
template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);}
template<class T> void print(T& v){ for(auto it : v) cout << it << " "; cout << endl; }
template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); }
template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n);  for(auto& a : v) init(a, x, m...); }

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

    int x, y;
    cin >> x >> y;
    if (x == y)
        cout << x << endl;
    else
        cout << 3-x-y << endl;

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}

/*
 * @Author: tusikalanse
 * @Date: 2021-06-26 19:48:54
 * @LastEditTime: 2021-06-26 20:04:29
 * @LastEditors: tusikalanse
 * @Description:
 * @FilePath: /atcoder/abc/207/b.cpp
 */
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
LL gcd(LL a, LL b) {
    return b == 0 ? a : gcd(b, a % b);
}
const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
//-------------------end head--------------

int main() {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b >= d * c) {
        cout << -1 << endl;
        return 0;
    }
    cout << 1 + (a - 1) / (d * c - b) << endl;

    return 0;
}


#include <bits/stdc++.h>
#define SZ(v) ((int)(v).size())
using namespace std;

using ll = long long;

template<typename... Args>
void read(Args&... args)
{
	((cin >> args), ...);
}

template<typename... Args>
void write(Args... args)
{
	((cout << args << " "), ...);
}

template<typename... Args>
void writeln(Args... args)
{
	((cout << args << " "), ...); cout << '\n';
}

using T = double;
struct Line
{
	T a, b;

	T eval(pair<T, T> pt) {return a * pt.first + b;}

	Line compose(Line other) const
	{
		return {a * other.a, a * other.b + b};
	}
};

int main(void)
{
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	int nbPoints, valMax, nbBloques;
	read(nbPoints, valMax, nbBloques);
	vector<bool> blocked(nbPoints+1);
	while (nbBloques--)
	{
		int i;
		read(i);
		blocked[i] = true;
	}
	vector<Line> dp(nbPoints+1, {0, 0});
	dp[0] = {1, 0};
	dp[nbPoints] = {0,0};
	Line sum = {0, 0};
	for (int i(nbPoints-1); i > 0; --i)
	{
		int furthest = min(nbPoints, i + valMax+1);
		sum.a -= dp[furthest].a;
		sum.b -= dp[furthest].b;
		sum.a += dp[i+1].a;
		sum.b += dp[i+1].b;
		if (blocked[i]) dp[i] = {1, 0};
		else
		{
			dp[i].a += sum.a / valMax;
			dp[i].b += sum.b / valMax;
			dp[i].b++;
		}
	}
	Line eq({0, 0});
	for (int j(1); j <= valMax; ++j)
	{
		int nxt = min(nbPoints, j);
		eq = {eq.a + dp[nxt].a, eq.b + dp[nxt].b};
	}
	eq.a /= valMax;
	eq.b /= valMax;
	eq.b++;
	eq.a -= 1;
	if (abs(eq.a) < 1e-9)
		writeln(-1);
	else
		cout << setprecision(10) << fixed << -eq.b/eq.a << endl;
}

#include <bits/stdc++.h>
using namespace std;

#ifdef LOCAL
#include "/debug.h"
#else
#define db(...)
#endif

#define all(v) v.begin(), v.end()
#define pb push_back
using ll = long long;
const int NAX = 2e5 + 5, MOD = 1000000007;

vector<int> solveCase(int n, vector<int64_t> &a)
{
    // TODO: edit here
    db(n, a);
    vector<int> res;
    int idx = 1;
    for (size_t i = n; i >= 1;)
    {
        if (i == 3)
        {
            if (a[1] == 1 && a[2] == 2 && a[3] == 3)
                break;
            int id = -1;
            for (int j = 1; j + 1 <= i; j++)
                if ((j & 1) == (idx & 1))
                {
                    id = j;
                    break;
                }
            db(a, idx, id);
            swap(a[id], a[id + 1]);
            res.pb(id);
            db(idx, id, a);
            idx = (idx + 1) % 2;
            continue;
        }
        if (a[i] == i)
        {
            --i;
            continue;
        }
        int id = -1;
        for (int j = 1; j + 1 <= i; j++)
            if ((j & 1) == (idx & 1))
                if ((a[j] == i))
                {
                    id = j;
                    break;
                }
        db(i, id);
        if (id == -1)
        {
            for (int j = 1; j + 1 <= i; j++)
                if ((j & 1) == (idx & 1))
                {
                    id = j;
                    break;
                }
            if (id == -1)
            {
                cout << "WA\n";
                exit(0);
                assert(id != -1);
            }
        }
        swap(a[id], a[id + 1]);
        res.pb(id);
        db(idx, id, a);
        idx = (idx + 1) % 2;
    }
    db(a);
    db(res);
    return res;
}

// generated by oj-template v4.7.2 (https://github.com/online-judge-tools/template-generator)
int main()
{
#ifndef LOCAL
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
#endif
    constexpr char endl = '\n';
    // failed to analyze input format
    // TODO: edit here
    int t;
    cin >> t;
    for (size_t i = 0; i < t; i++)
    {
        int n;
        cin >> n;
        vector<int64_t> a(n + 1);
        for (int i = 1; i <= n; ++i)
        {
            cin >> a[i];
        }
        auto ans = solveCase(n, a);
        int M = ans.size();
        cout << M << endl;
        for (int i = 0; i < M; ++i)
        {
            cout << ans[i] << ' ';
        }
        cout << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
template<class T, class U> void umin(T& x, const U& y){ x = min(x, (T)y);}
template<class T, class U> void umax(T& x, const U& y){ x = max(x, (T)y);}
template<class T, class U> void init(vector<T> &v, U x, size_t n) { v=vector<T>(n, (T)x); }
template<class T, class U, typename... W> void init(vector<T> &v, U x, size_t n, W... m) { v=vector<T>(n);  for(auto& a : v) init(a, x, m...); }

int main(int argc, char** argv) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) freopen("d:\\code\\cpp\\contests\\stdin", "r", stdin);

    int n;
    cin >> n;
    vector<int> a(n), b(n);
    FOR(i, n) cin >> a[i] >> b[i];

    int sol = 1e9;
    FOR(i, n){
        FOR(j, n){
            if (i == j)
                umin(sol, a[i] + b[j]);
            else
                umin(sol, max(a[i], b[j]));
        }
    }
    cout << sol << endl;

    if (argc == 2 && atoi(argv[1]) == 123456789) cout << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}

#include <iostream>
#include <string>
#include <vector>
#include <utility>
using ll=long long;
using namespace std;

int main(){
    int N;
    cin>>N;
    vector<int> A(N);
    vector<int> B(N);
    for(int i=0;i<N;i++){
        cin>>A[i]>>B[i];
    }
    int MX=1e6;
    int minA=MX;
    int minB=MX;
    for(int i=0;i<N;i++){
        minA=min(minA,A[i]);
        minB=min(minB,B[i]);
    }
    //cout<<minA<<" "<<minB<<endl;

    int cnta=0;
    int cntb=0;
    int cntab=0;
    for(int i=0;i<N;i++){
        if(A[i]==minA){
            cnta++;
        }
        if(B[i]==minB){
            cntb++;
        }
        if(A[i]==minA && B[i]==minB){
            cntab++;
        }
    }

    int ans=0;
    if(cnta==1 && cntb==1 && cntab==1){
        ans=minA+minB;
        int mmA=MX;
        int mmB=MX;
        for(int i=0;i<N;i++){
            if(A[i]!=minA){
                mmA=min(mmA,A[i]);
                mmB=min(mmB,B[i]);
            }
        }
        ans=min(ans,min(max(mmA,minB),max(minA,mmB)));
    }else{
        ans=max(minA,minB);
    }
    cout<<ans<<endl;

    return 0;
}


#include <fstream>
#include <iostream>

#include <algorithm>
#include <array>
#include <bitset>
#include <complex>
#include <functional>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define all(container) (container).begin(), (container).end()
#define sz(container) static_cast<int>((container).size())

using namespace std;

class Solver {
public:
  void solve(istream& in, ostream& out) {
    out.precision(20);

    int tests = 1;
    for (int test = 1; test <= tests; test++) {
      solveOne(in, out);
    }
  }

private:
  void solveOne(istream& in, ostream& out) {
    int N;
    in >> N;
    vector<int64_t> A(N), B(N);
    for (auto& a : A) {
      in >> a;
    }
    for (auto& b : B) {
      in >> b;
    }
    vector<int> oddA, evenA, oddB, evenB;
    for (int i = 0; i < N; i++) {
      if (A[i] >= B[i]) {
        if (i % 2 == 0) evenA.push_back(i);
        else oddA.push_back(i);
      } else {
        if (i % 2 == 0) evenB.push_back(i);
        else oddB.push_back(i);
      }
    }
    auto byA = [&](int lhs, int rhs) {
      return (A[lhs] - B[lhs]) < (A[rhs] - B[rhs]);
    };
    auto byB = [&](int lhs, int rhs) {
      return (B[lhs] - A[lhs]) < (B[rhs] - A[rhs]);
    };
    sort(all(oddA), byA);
    sort(all(evenA), byA);
    sort(all(oddB), byB);
    sort(all(evenB), byB);
    int64_t answer = 0;
    while (!oddA.empty() && !evenA.empty()) {
      answer += A[oddA.back()];
      oddA.pop_back();
      answer += A[evenA.back()];
      evenA.pop_back();
    }
    while (!oddB.empty() && !evenB.empty()) {
      answer += B[oddB.back()];
      oddB.pop_back();
      answer += B[evenB.back()];
      evenB.pop_back();
    }
    auto lastMatch = [&](vector<int> aPos, vector<int> bPos) {
      assert(sz(aPos) == sz(bPos));
      reverse(all(bPos));
      int n = sz(aPos);
      for (int i = 0; i < n; i++) {
        answer += max(A[aPos[i]] + A[bPos[i]], B[aPos[i]] + B[bPos[i]]);
      }
    };
    lastMatch(oddA, evenB);
    lastMatch(evenA, oddB);
    out << answer << endl;
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  Solver solver;
  solver.solve(cin, cout);

  return 0;
}

#include"bits/stdc++.h"
using namespace std;
using ll = long long;
const ll mod = 1000000007;
#define rep(i,m,n) for(ll i=m;i<n;i++)

int main() {
	ll n;
	string s;
	cin >> n >> s;
	ll ans = n;
	vector<bool>f(n);
	rep(i, 0, n-2) {
		if (f[i] == 0&&f[i+1]==0&&f[i+2]==0) {
			if (s[i] == 'f' && s[i + 1] == 'o' && s[i + 2] == 'x') {
				f[i] = 1;
				f[i + 1] = 1;
				f[i + 2] = 1;
				ans -= 3;
				ll first = i - 2;
				ll second = i - 1;
				ll third = i + 3;
				ll fourth = i + 4;
			retry:
				while (1) {
					if (second <= 0)break;
					if (f[second] == 1)second--;
					else break;
				}
				first = second - 1;
				while (1) {
					if (first < 0)break;
					if (f[first] == 1)first--;
					else break;
				}
				while (1) {
					if (third >= n - 1)break;
					if (f[third] == 1) {
						third++;
					}
					else break;
				}
				fourth = third + 1;
				while (1) {
					if (fourth > n - 1)break;
					if (f[fourth] == 1)fourth++;
					else break;
				}
				while (1) {
					bool flag = 0;
					if (first >= 0 && third <= n - 1) {
						if (s[first] == 'f' && s[second] == 'o' && s[third] == 'x') {
							ans -= 3;
							//cout << front - 1 << " " << front << " " << back << endl;
							f[first] = 1;
							f[second] = 1;
							f[third] = 1;
							goto retry;
							flag = 1;
						}
					}
					if (second >= 0 && fourth <= n - 1) {
						if (s[second] == 'f' && s[third] == 'o' && s[fourth] == 'x') {
							ans -= 3;
							//cout << front << " " << back << " " << back + 1 << endl;
							f[second] = 1;
							f[third] = 1;
							f[fourth] = 1;
							goto retry;
							flag = 1;
						}
					}
					if (flag == 0)break;
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}


#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_ll.hpp>

#define rep(i,a,b) for(ll i=a;i<b;i++)
#define rrep(i,b,a) for(ll i=b;i>=a;i--)
#define fori(a) for(auto i : a )
#define all(a) begin(a), end(a)
#define set(a,b) memset(a,b,sizeof(a))
#define sz(a) a.size()
double pi=acos(-1);
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front //deque
// #define mp make_pair
#define pq priority_queue
const ll mod=1000000007;
#define f first
#define s second
#define pii pair< ll, ll >
#define vi vector<ll>
#define vpii vector<pii>
#define debug(v) for(auto i:v) cout<<i<<" ";
#define tc ll t; cin >> t; while(t--)

// using namespace boost::multiprecision;
using namespace std;
void optimizeIO(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const ll N=1000005;
ll fact[N],invfact[N];
ll power(ll x,ll y){
  if(y<=0) return 1LL;
  ll z=power(x,y/2);
  if(y%2) return (((z*z)%mod)*x)%mod;
  return (z*z)%mod;
}

void pre(){
  fact[0]=invfact[0]=invfact[1]=fact[1]=1;
  rep(i,2,N) fact[i]=(i*fact[i-1])%mod;
  rep(i,2,N) invfact[i]=(invfact[i-1]*power(i,mod-2))%mod;
}
ll nCr(ll n,ll k){ if (k < 0 || k > n) return 0; return (((fact[n]*invfact[n-k])%mod)*invfact[k])%mod; }

const ll N1=1e6+1;
vector<ll> isprime(N1,1),prime;
void seive(){
  rep(i,2,sqrt(N1)+1){
      if(isprime[i]){
        for(ll j=i*i;j<N1;j+=i) isprime[j]=0;
        prime.pb(i);
      }
  }
  rep(i,sqrt(N1)+1,N1) if(isprime[i]) prime.pb(i);
}

struct dsu {
    vector<ll> par, rank;
    dsu(ll n): par(n+1), rank(n+1) {
        for (ll i = 0; i <= n; i++) {
            par[i] = i;
            rank[i]= 1;
        }
    }
    ll root(ll a) {
        if (a == par[a]) return a;
        return par[a] = root(par[a]);
    }
    void merge(ll a, ll b) {
        a = root(a);
        b = root(b);
        if (a == b) return;
        if (rank[a] > rank[b]) swap(a, b);
        par[b] = a;
    }
    set<ll> parent(ll n){
        set<ll> s;
        for(ll i=1;i<=n;i++){
            s.insert(root(i));
        }
        return s;
    }
};
void solve(){
  ll n,m;
  cin>>n>>m;
  vector<int>h(n),w(m);
  rep(i,0,n) cin>>h[i];
  rep(i,0,m) cin>>w[i];
  sort(all(h));
  vector<ll> dp(n+1,0),suf(n+1,0);
  for(ll i=0;i+1<n;i+=2){
    dp[i]=(i==0?0:dp[i-1])+abs(h[i+1]-h[i]);
    dp[i+1]=dp[i];
  }
  for(ll i=n-1;i-1>=0;i-=2){
    suf[i]=(i==n-1?0:suf[i+1])+abs(h[i]-h[i-1]);
    suf[i-1]=suf[i];
  }
  ll ans=LLONG_MAX;
  rep(i,0,m){
    auto id=lower_bound(all(h),w[i]);
    if(id==h.end()) ans=min(ans,dp[n-2]+abs(w[i]-h[n-1]));
    else if(id==h.begin()) ans=min(ans,suf[1]+abs(w[i]-h[0]));
    else{
      ll idx=id-h.begin();
      if(idx%2==0) ans=min(ans,dp[idx-1]+abs(w[i]-h[idx])+suf[idx+1]);
      else ans=min(ans,(idx-2>=0?dp[idx-2]:0)+abs(w[i]-h[idx-1])+suf[idx]);
    }
  }
  cout<<ans<<endl;
}
int32_t main(){
    optimizeIO();
    {solve();}
}

#pragma GCC diagnostic warning "-Wextra"
#pragma GCC diagnostic warning "-Wshadow"
#include <bits/stdc++.h>
using namespace std;

template <class A, class B> bool cmin(A& a, B b) { return a > b && (a = b, true); }
template <class A, class B> bool cmax(A& a, B b) { return a < b && (a = b, true); }

signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);

  long N, M;
  cin >> N >> M;
  vector<long> H(N);
  for (long i = 0; i < N; i++) cin >> H.at(i);
  vector<long> W(M);
  for (long i = 0; i < M; i++) cin >> W.at(i);

  sort(H.begin(), H.end());

  vector<long> L, R;
  for (long i = 0; i + 1 < N; i += 2) {
    L.push_back(H.at(i + 1) - H.at(i));
  }
  for (long i = 1; i + 1 < N; i += 2) {
    R.push_back(H.at(i + 1) - H.at(i));
  }

  auto cs = [](auto vec) {
    vector<long> ret((long)vec.size() + 1);
    partial_sum(vec.begin(), vec.end(), ret.begin() + 1);
    return ret;
  };

  L = cs(L);
  R = cs(R);

  long mi = LONG_MAX;
  for (const auto& a : W) {
    // long opp = lower_bound(H.begin(), H.end(), a) - H.begin();
    // if (opp & 1) opp--;
    // cmin(mi, L.at(opp / 2) + abs(H.at(opp) - a) + (R.back() - R.at(opp / 2)));
    long opp = upper_bound(H.begin(), H.end(), a) - H.begin();
    if (opp & 1) opp--;
    cmin(mi, L.at(opp / 2) + abs(H.at(opp) - a) + (R.back() - R.at(opp / 2)));
  }
  cout << mi << '\n';
}


#include<bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define drep(i,n) for(int i = (n-1); i >= 0; i--)
#define all(v) (v).begin(),(v).end()
#define maxs(x,y) (x = max(x,y))
#define mins(x,y) (x = min(x,y))
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a/gcd(a,b)*b; }
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
const ll MOD = 1e9+7;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<int> a(m), b(m);
    vector<int> e(n);

    rep(i,m) {
        cin >> a[i] >> b[i];
        a[i]--; b[i]--;
        e[a[i]] |= 1 << b[i];
        e[b[i]] |= 1 << a[i];
    }
    vector<int> dp(1 << n, 99999);
    dp[0] = 0;
    for (int i = 1; i < (1<<n); i++) {
        rep(j, n) {
            if ((i & (1<<j))==0) continue;
            int target = i - (1<<j);
            if(dp[target] <= 1 && (e[j] & target) == target) {
                dp[i] = 1;
            }
            // 1つ調べれば十分なのでbreak
            break;
        }
    }
    rep(i, (1<<n)) {
        for (int j = i; j > 0; j = (j-1) & i) {
            // iをjとkの2つのグループに分ける。このi,jのループはO(3^n)
            int k = i-j;
            // 適切な分け方を探して最小値を更新する
            dp[i] = min(dp[i], dp[j] + dp[k]);
        }
    }
    cout << dp[(1 << n) - 1] << endl;
	return 0;
}

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<n;++i)
#define repn(i,n) for(int i=1;i<=n;++i)
#define LL long long
#define pii pair <int,int>
#define pb push_back
#define fi first
#define se second
#define mpr make_pair

using namespace std;

const LL MOD=1e9+7;

LL t,N;

int main()
{
  cin>>t>>N;
  LL n=0;
  n+=N/t*100;
  N%=t;
  LL cur=0;
  while(N>0)
  {
    if(cur+t>=100) --N;
    cur=(cur+t)%100;
    ++n;
  }
  cout<<(LL)(n+floor((double)t/100.0*(double)n)-1)<<endl;
  return 0;
}


#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("inline")
// #include<bits/stdc++.h>
#include<iostream>
#include<string>
using namespace std;

string dfs(int n,string s){
    if(n == 1)return s;
    while(n%3!=1){
        string t;
        for(int i=1;i<n;i++){
            if(s[i]==s[i-1])t+=s[i];
            else {
                int x = 'R'+'W'+'B';
                x -= s[i] + s[i-1];
                t += (char)x;
            }
        }
        swap(t,s);
        n--;
    }
    string t;
    for(int i=0;i<n;i+=3)t+=s[i];
    return dfs((int)t.size(),t);
}
int main(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    cout << dfs(n,s) << endl;
}

#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(n1,n) for(int i=n1;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define ss(s) scanf("%s",s)
#define pi(x) printf("%d\n",x)
#define pl(x) printf("%lld\n",x)
#define ps(s) printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int>  pii;
typedef pair<ll, ll>  pl;
typedef vector<int>   vi;
typedef vector<ll>    vl;
typedef vector<pii>   vpii;
typedef vector<pl>    vpl;
typedef vector<vi>    vvi;
typedef vector<vl>    vvl;
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) {
    uniform_int_distribution<int> uid(0, lim - 1);
    return uid(rang);
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int t = 1;
    while (t--) {
        ll n; cin >> n;
        vl v(n + 1);
        ll sum = 0;
        fo(1, n + 1) {
            cin >> v[i];
            sum += v[i];
        }
        ll dp[n + 1][sum + 1];
        memset(dp, 0, sizeof(dp));
        fo(0, n + 1)
        dp[i][0] = 1;
        for (ll i = 1; i <= n; i++) {
            for (ll j = 1; j <= sum; j++) {
                if (j - v[i] >= 0) {
                    dp[i][j] = dp[i - 1][j] || dp[i - 1][j - v[i]];
                }
                else {
                    dp[i][j] = dp[i - 1][j];
                }
            }
        }
        ll max1 = INT_MAX;
        for (ll i = 1; i <= sum; i++) {
            if (dp[n][i] == 1) {
                //  cout << i << " ";
                ll a = sum - i;
                //cout << a << endl;
                max1 = min(max1, max(a, i));
            }
        }
        cout << max1 << endl;

    }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define range(a) a.begin(), a.end()
#define endl "\n"
#define Yes() cout << "Yes" << endl
#define No() cout << "No" << endl
#define MP make_pair
using P = pair<int, int>;
const unsigned long long mod = 1e9 + 7;
const long long INF = 1LL<<60;
void chmin(long long &a, long long b) { if (a > b) a = b; }
void chmax(long long &a, long long b) { if (a < b) a = b; }

int main(void){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int T;
	cin >> T;
	while(T--){
		ll N;
		cin >> N;
		if(N % 2 == 1){
			cout << "Odd" << endl;
		}else{
			N /= 2;
			if(N % 2 == 1){
				cout << "Same" << endl;
			}else{
				cout << "Even" << endl;
			}
		}
	}

	return 0;
}

/*
    author:  Maksim1744
    created: 13.02.2021 15:00:56
*/

#include "bits/stdc++.h"

using namespace std;

#define ll   long long
#define ld   long double

#define mp   make_pair
#define pb   push_back
#define eb   emplace_back

#define sum(a)     ( accumulate ((a).begin(), (a).end(), 0ll))
#define mine(a)    (*min_element((a).begin(), (a).end()))
#define maxe(a)    (*max_element((a).begin(), (a).end()))
#define mini(a)    ( min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a)    ( max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) ( lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) ( upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

template<typename T>             vector<T>& operator--            (vector<T>& v){for (auto& i : v) --i;            return  v;}
template<typename T>             vector<T>& operator++            (vector<T>& v){for (auto& i : v) ++i;            return  v;}
template<typename T>             istream& operator>>(istream& is,  vector<T>& v){for (auto& i : v) is >> i;        return is;}
template<typename T>             ostream& operator<<(ostream& os,  vector<T>& v){for (auto& i : v) os << i << ' '; return os;}
template<typename T, typename U> pair<T,U>& operator--           (pair<T, U> &p){--p.first; --p.second;            return  p;}
template<typename T, typename U> pair<T,U>& operator++           (pair<T, U> &p){++p.first; ++p.second;            return  p;}
template<typename T, typename U> istream& operator>>(istream& is, pair<T, U>& p){is >> p.first >> p.second;        return is;}
template<typename T, typename U> ostream& operator<<(ostream& os, pair<T, U>& p){os << p.first << ' ' << p.second; return os;}
template<typename T, typename U> pair<T,U> operator-(pair<T,U> a, pair<T,U> b){return mp(a.first-b.first, a.second-b.second);}
template<typename T, typename U> pair<T,U> operator+(pair<T,U> a, pair<T,U> b){return mp(a.first+b.first, a.second+b.second);}
template<typename T, typename U> void umin(T& a, U b){if (a > b) a = b;}
template<typename T, typename U> void umax(T& a, U b){if (a < b) a = b;}

#ifdef HOME
#define SHOW_COLORS
#include "C:/C++ libs/print.cpp"
#else
#define show(...)     42
#define mclock        42
#define shows         42
#define debug if (false)
#endif

void test_case(int test) {
    // a >= l * 2
    // a = l * 2 + k
    // b = l..l+k, c=l+k..l
    // sum from k=0 to r-l*2 of (k+1)
    // sum from k=1 to r-l*2+1 of k
    ll l, r;
    cin >> l >> r;
    if (l * 2 > r) {
        cout << 0 << '\n';
    } else {
        ll n = r - l * 2 + 1;
        cout << n * (n + 1) / 2 << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    int T;
    cin >> T;
    for (int test = 1; test <= T; ++test) {
        test_case(test);
    }

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <vector>
// #include <bits/stdc++.h>
#include <queue>
#include <algorithm>
#include <string>
#include <iomanip>
#include <cmath>
#include <map>

using namespace std;
typedef long long ll;

#define rep(i,k,n) for(ll i=k; i<(ll)(n); i++)

int main(){
    ll n; cin >> n;
    vector<ll> a(n);
    // vector<ll,vector<ll>> v(100);
    map<ll,ll> m;
    rep(i,0,n) {
        cin >> a[i];
    }
    rep(i,0,n) {
        ll index = a[i]%200;
        m[index]++;
    }

    ll ans = 0;
    // rep(i,0,200) {
    //     cout << i << " " << m[i] << endl;
    // }

    rep(i,0,200) {
        ans+=m[i]*(m[i]-1)/2;
    }

    cout << ans << endl;

    return 0;
}

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))

template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	int n;
	cin >> n;
	vector<ll> a(n);
	rep(i, n) cin >> a[i];
	vi p;
	for(int i = 2; i <= 50; i++){
		int f = 1;
		for(int j = 2; (j * j) <= i; j++){
			if(i % j == 0) f = 0;
		}
		if(f) p.pb(i);
	}
	ll mn = 1e18;
	rep(i, 1 << (int)(p.size())){
		int f = 1;
		ll num = 1LL;
		rep(j, p.size()) if(i & (1 << j)) num *= (ll)(p[j]);
		rep(j, n) if(gcd(a[j], num) == 1) f = 0;
		if(f) mn = min(mn, num);
	}
	cout << mn << '\n';
	return 0;
}
/*
Things to look out for:
	- Integer overflows
	- Make sure that max is large enough (2e9, 4e18)
	- Special cases
Be careful!
*/


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=2e3+5;
ll dp[maxn][maxn],sum[3][maxn*2],belong[maxn][maxn];
char buff[maxn][maxn];
const int mod=1e9+7;
int main() {
    int n,m;cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>(buff[i]+1);
    for(int i=1;i<=m;i++) belong[1][i]=i;
    for(int i=2;i<=n;i++) belong[i][1]=m+i-1;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            if(belong[i][j]) continue;
            belong[i][j]=belong[i-1][j-1];
        }
    }
    dp[1][1]=1;
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            if(buff[i][j]=='#') {
                sum[0][belong[i][j]]=sum[1][i]=sum[2][j]=0;
            }else {
                if(i!=1||j!=1) dp[i][j]=(sum[0][belong[i][j]]+sum[1][i]+sum[2][j])%mod;
                (sum[0][belong[i][j]]+=dp[i][j])%=mod;
                (sum[1][i]+=dp[i][j])%=mod;
                (sum[2][j]+=dp[i][j])%=mod;
            }
        }
    }
    cout<<dp[n][m]%mod<<endl;
    return 0;
}

#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <map>
#include <iostream>
#include <vector>
#include <queue>
#include <unordered_map>
#include <random>
#include <stack>
#include <set>
#include <list>
#include <unordered_set>

#define bug(x) cout<<"zdongdebug1: "<<x<<endl;
#define bug2(x, y) cout<<"zdongdebug2: "<<x<<" "<<y<<endl;
#define bug3(x, y, z) cout<<"zdongdebug3: "<<x<<" "<<y<<" "<<z<<endl;
using namespace std;
typedef long long ll;
const int maxn = 2005;
const int mod = 1000000007;

int d[maxn][maxn];
string s[maxn];
vector<pair<int,int>>a[26];

int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

int main() {
#ifdef suiyuan2009
  freopen("/Users/suiyuan2009/CLionProjects/icpc/input.txt", "r", stdin);
  //freopen("/Users/suiyuan2009/CLionProjects/icpc/output.txt", "w", stdout);
#endif
  int n,m;
  cin>>n>>m;
  int sx,sy,tx,ty;
  for(int i=0;i<n;i++){
    cin>>s[i];
    for(int j=0;j<m;j++){
      auto tt = s[i][j];
      if(tt>='a'&&tt<='z')a[tt-'a'].push_back(make_pair(i,j));
      else if(tt=='S')sx=i,sy=j;
      else if(tt=='G')tx=i,ty=j;
    }
  }
  int mask=0;
  queue<int>qx;
  queue<int>qy;
  memset(d,-1,sizeof(d));
  d[sx][sy] = 0;
  qx.push(sx);
  qy.push(sy);
  while(!qx.empty()){
    int x = qx.front();
    qx.pop();
    int y = qy.front();
    qy.pop();
    auto tt = s[x][y];
    if(tt>='a'&&tt<='z'&&!(mask&(1<<(tt-'a')))){
      mask|=(1<<(tt-'a'));
      auto tmp = d[x][y];
      for(auto pr: a[tt-'a']){
        if(d[pr.first][pr.second]==-1){
          d[pr.first][pr.second]=tmp+1;
          qx.push(pr.first);
          qy.push(pr.second);
        }
      }
    }
    for(int i=0;i<4;i++){
      int nx = x+dir[i][0];
      int ny = y+dir[i][1];
      if(nx<0||nx>=n||ny<0||ny>=m||s[nx][ny]=='#'||d[nx][ny]!=-1)continue;
      d[nx][ny] = d[x][y] + 1;
      qx.push(nx);
      qy.push(ny);
    }
  }
  cout<<d[tx][ty]<<endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N ;
  double X=0;
  for(double i = 1; i < N; i++){
    X +=  N/i;
  }
  cout << std::fixed << std::setprecision(12) << X << endl;
}

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6;
typedef long long ll;
template<class T> T read()
{
	T f = 1, k = 0;
	char c = getchar();
	while (c < '0' || c > '9')
	{
		if (c == '-')
			f = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9')
	{
		k = k * 10 + c - '0';
		c = getchar();
	}
	return f * k;
}

double n, ans;
vector<int> v[maxn];
int main()
{
	n = read<double>();
	for (double i = 1; i < n; i++)
	{
		ans += 1.000000 / i;
	}
	ans *= n;
	printf("%lf\n", ans);
}