#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;

#define inf 1e+9
#define endl "\n"
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep1(i, n) for (int i = 1; i < (int)(n); i++)
#define rep_rev(i, n) for (int i = n-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()

#define pb push_back
#define eb emplace_back
#define Size(c) (int)(c).size()

#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))

template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;

template <class T, class S> inline bool chmax(T &a, S b) {
    if(a < b) {a = b;return 1;} return 0;
}
template <class T, class S> inline bool chmin(T &a, S b) {
    if(a > b) {a = b;return 1;} return 0;
}
template <class T>
void line(vector<T> &x){for (auto&& xi : x) cin >> xi;}

inline void IN(void){ return;}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){ cin >> first; IN(rest...); return;}

#define INT(...) int __VA_ARGS__; IN (__VA_ARGS__)
#define LINT(...) lint __VA_ARGS__; IN (__VA_ARGS__)
#define STR(...) string __VA_ARGS__; IN(__VA_ARGS__)

template <class T> void UNIQUE(vector<T> &x) {
    sort(all(x)); x.erase(unique(all(x)), x.end());
}
template <class T> void print_vec(T first, T end){
    for (auto i = first; i < end - 1; i++) cout << *i << " ";
    cout << *(end-1) << endl;
}

template<class... T>
void debug_print(T... args){
    vector<lint> tmp = initializer_list<lint>{args...};
    print_vec(all(tmp));
}
template<class T>
void print(T a){ cout << a << endl;}

vector<string> Yes = {"No", "Yes"};
vector<string> YES = {"NO", "YES"};

int ord(char x){ return (int)(x - 'a');}
char chr(lint x){ return (char)(x + (lint)('a'));}
lint mod = 1e9+7;
// lint mod =

lint sum(vlint a){lint ret = 0; for(auto&& v:a) ret += v; return ret;}
lint vmini(vlint a, lint &index){
    lint ret = LLONG_MAX;
    rep(i, Size(a)){ if (chmin(ret, a[i])) index = i;}
    return ret;
}
lint vmaxi(vlint a, lint &index){
    lint ret = -LLONG_MAX;
    rep(i, Size(a)){ if (chmax(ret, a[i])) index = i;}
    return ret;
}

lint vmin(vlint a){
    lint ret = LLONG_MAX; for (auto && v : a) chmin(ret, v); return ret;
}
lint vmax(vlint a){
    lint ret = -LLONG_MAX; for (auto && v : a) chmax(ret, v); return ret;
}

int main(){
    INT(n, m);
    vector<vlint> ans;
    vlint check(n+1, 0); int cnt = 1; int diff = n-1;
    while (cnt < cnt + diff){
        ans.pb({cnt, cnt + diff});
        cnt++;
        diff -= 2;
        if (n % 2 == 0 && diff+2 > n/2 && diff <= n/2) diff--;
    }
    rep(i, m){
        print_vec(all(ans[i]));
    }
}

#include <bits/stdc++.h>
#define ALL(A) (A).begin(), (A).end()
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int dx[] = { 0, 1, -1, 0, 1, -1, 1, -1 };  // i<4:4way i<8:8way
int dy[] = { 1, 0, 0, -1, 1, -1, -1, 1 };

const ll mod = 1e9 + 7;
const ll INF = -1 * ((1LL << 63) + 1);
const int inf = -1 * ((1 << 31) + 1);

int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    int n,m;
    cin >> n >> m;
    if(m==1){
        cout << "1 2" << endl;
        return 0;
    }

    int i1 = 1;
    int i2 = m + 2;
    for(int diff = m;diff >= 1;diff--){
        cout << i1 << " " << i1 + diff << endl;
        i1++;
        swap(i1,i2);
    }
}


#include <iostream>
#include <vector>
#include<list>
#include <algorithm>
#include <string>
#include <math.h>
#include<map>
#include <utility>

#include<queue>
using namespace std;
#define REP(i,k,n) for(int i=k;i<n;i++)

#define INF 1844674407370955161
typedef long long unsigned int ll;
#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))
#define MOD 1000000007

template<typename T>
int vector_finder(std::vector<T> vec, T number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

template<typename T>
T gcd(T a,T b){
	if(b>0){
		return gcd(b, a % b);
	}else{
		return a;
	}
}

template<typename T>
T lcm(T a,T b){
	T g = gcd(a, b);
	return a * b / g;
}

void clear( std::queue<pair<int,int> > &q )
{
   std::queue<pair<int,int> > empty;
   std::swap( q, empty );
}
bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return false;
        }
    }

    // 素数である
    return true;
}
template<typename T>
std::vector<T> enum_div(T n)//nの約数を列挙
{
	std::vector<T> ret;
	for(T i=1 ; i*i<=n ; ++i)
	{
		if(n%i == 0)
		{
			ret.push_back(i);
			if(i!=1 && i*i!=n)
			{
				ret.push_back(n/i);
			}
		}
	}
	return ret;
}

int main()
{

  int n, m;
  long long xans=0;
  long long yans = 0;
  cin >> n >> m;
  REP(i,1,n+1){
    long long x;
    cin >> x;
    xans += (2 * i - n - 1) * x;
  }
  REP(i,1,m+1){
    long long y;
    cin >> y;
    yans += (2 * i - m - 1) * y;
  }
  xans %= MOD;
  yans %= MOD;
  ll ans = xans * yans % MOD;
  cout << ans << endl;

  return 0;
}

//thuanqvbn03
#include <bits/stdc++.h>

using namespace std;

const int MaxN = 100005;
const int Mod = 1000000007;

int n, D;
string s;
long long dp[MaxN], tmp[MaxN];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> s;
    cin >> D;
    n = s.size();
    for (int i = s[0] - '0' - 1; i >= 0; i--)
    {
        dp[i % D]++;
    }
    int Sums = (s[0] - '0') % D;
    for (int i = 1; i < n; i++)
    {
        int x = s[i] - '0';
        for (int k = 0; k < D; k++)
        {
            tmp[k] = tmp[k] + dp[k];
            tmp[k + 10] = tmp[k + 10] - dp[k] + Mod;
        }
        tmp[Sums]++;
        tmp[Sums + x]--;
        Sums = (Sums + x) % D;
        dp[0] = tmp[0];
        tmp[0] = 0;
        for (int k = 1; k < D + 10; k++)
        {
            dp[k] = (tmp[k] + dp[k - 1]) % Mod;
            tmp[k] = 0;
        }
        for (int k = D; k < D + 10; k++)
        {
            dp[k % D] = (dp[k % D] + dp[k]) % Mod;
        }
    }
    long long ans = (dp[0] + (Sums % D == 0) - 1 + Mod) % Mod;
    cout << ans;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
int main(){
    long long int k,x,a;
    cin>>k>>x;
    for(int i=1;i<=(2*k-1);i++)
 {a=x-k+i;
 cout<<a<<" ";
 }
 cout<<endl;
    return 0;

}

#include <bits/stdc++.h>
using namespace std;
#define fastIO ios::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define ft first
#define sd second
#define pb push_back
#define pob pop_back()
#define pf push_front
#define pof pop_front()
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<char,char> pcc;
typedef pair<long long,long long> pll;

int main() {
    fastIO;
    int x, k;
    cin >> k >> x;
    for(int i = x-k+1; i <= x+k-1; i++){
        if(i != x+k-1) cout << i << " ";
        else cout << i << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
struct rev_vector {
	const int magic = 123456;
	vector<int> pk;
	int l, r;
	rev_vector(){
		pk.resize(3 * magic);
		l = magic;
		r = l;
	}
	void shift_right(){
		l--;
		pk[l] = 0;
	}
	int& operator [](int pos){
		while(pos + l <= r) pk[r++] = 0;
		return pk[pos + l];
	}
	void clear(){
		l = magic;
		r = l;
	}
};
const int maxn = 100100;
int ans, k, vis[maxn];
vector<int> e[maxn];
int dfs(int u){
	int dep = 0;
	for(int v : e[u]) {
		int mv = dfs(v);
		if(mv == k - 1 && u){
			ans++;
		} else {
			dep = max(dep, mv + 1);
		}
	}
	return dep;
}
int main(){
	int n; cin >> n >> k;
	vector<int> a(n);
	for(int i = 0; i < n; i++) cin >> a[i], a[i]--;
	if(a[0] != 0){
		a[0] = 0;
		ans++;
	}
	for(int i = 0; i < n; i++){
		if(i != a[i]){
			e[a[i]].push_back(i);
		}
	}
	dfs(0);
	cout << ans << endl;
	return 0;
}

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

#define rep(i, n) for(int i = 0; i < n; i ++)
#define COUNT(i, n) for(int i = 1; i <= n; i ++)
#define ITER(c) __typeof((c).begin())
#define each(c, it) for(ITER(c) it =(c).begin(); it!=(c).end(); it++)
#define ALL(c) c.begin(), c.end()
#define mp make_pair
#define pb push_back
#define MEMSET(v, h) memset((v), h, sizeof(v))

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<string> vs;

const int INF = 1 << 24;

int main(void){
    for(int n, m, s; s = 0, cin >> n >> m, n; cout << s << endl){
        priority_queue<int> p;
        rep(i, n){
            int in; cin >> in;
            p.push(in);
        }
        COUNT(i, n){
            if(i % m) s += p.top();
            p.pop();
        }
    }

    return 0;
}


#include <iostream>
#include <utility>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <math.h>
#include <stack>
#include <set>
#include <deque>
#include <cmath>

using namespace std;
typedef long long ll;
ll mod = 1e9+7;
//ll mod = 998244353;
#define rep(i,n) for(int i = 0; i < (n); ++i)

int main() {
    string s;
    cin >> s;
    rep(i, 1<<3) {
        int num = s[0] - '0';
        string ans = "";
        ans += s[0];
        rep(j, 3) {
            if ((i>>j) & 1) {
                //cout << s[j+1] - '0' << endl;
                num += (s[j+1] - '0');
                ans += '+';
                ans += s[j+1];
            }
            else {
                //cout << s[j+1] - '0' << endl;
                num -= (s[j+1] - '0');
                ans += '-';
                ans += s[j+1];
            }
            //cout << num << endl;
            //cout << ans << endl;
        }
        if (num == 7) {
            cout << ans << "=7" << endl;
            return 0;
        }
    }
    return 0;
}

#include<bits/stdc++.h>
#define all(v) v.begin(),v.end()
using namespace std;
int main(){
	cin.tie(0);
    ios::sync_with_stdio(false);
    int n,a,b=0;
    cin>>n;
    a=n;
    while(a>0){
        b+=a%10;
        a/=10;
    }
    if(n%b==0)
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return(0);
}


#include <bits/stdc++.h>

#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define LL long long
#define ppt(x) __builtin_popcount(x)
using namespace std;
const int INF=0x3f3f3f3f,N=1e6+5,MOD=1e9+7;
const LL INF_LL=0x3f3f3f3f3f3f3f3fLL;

template<typename T>
inline int getplc(T x,int y) { return (x>>y)&1; }
template<typename T>
T square(T x) {return x*x;}

LL qpow(LL a,LL b=MOD-2,LL _MOD=MOD){
	LL res=1;
	for(;b;b>>=1,a=a*a%_MOD){
		if(b&1)res=res*a%_MOD;
	}
	return res;
}

template <typename T>
T Smax(T x) { return x; }
template<typename T, typename... Args>
T Smax(T a, Args... args) {
	return max(a, Smax(args...));
}
template <typename T>
T Smin(T x) { return x; }
template<typename T, typename... Args>
T Smin(T a, Args... args) {
	return min(a, Smin(args...));
}

template <typename T>
#define errorl(args...) {  \
	string _s = #args;   \
	replace(_s.begin(), _s.end(), ',', ' ');  \
    stringstream _ss(_s);  \
    istream_iterator<string> _it(_ss);  \
    errl(_it, args);  \
}
void errl(istream_iterator<string> it) {}
template<typename T, typename... Args>
void errl(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	errl(++it, args...);
}
#define error(args...) {  \
    string _s = #args;  \
    replace(_s.begin(), _s.end(), ',', ' ');  \
    stringstream _ss(_s);  \
    istream_iterator<string> _it(_ss);  \
    err(_it, args);  \
    cerr<<endl; \
}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}

LL a[N],b[N];

int add(LL x){
	for(int i=60;i>=0;i--){
		if(getplc(x,i)){
			if(!a[i]){
				a[i]=x;
				return 1;
			}
			else x^=a[i];
		}
	}
	return 0;
}

char s[N];

void Solve(){
	int cas;
	for(cin>>cas;cas--;){
		int n;
		cin>>n;
		rep(i,1,n){
			cin>>b[i];
		}
		cin>>s+1;

		int ans=0;
		per(i,1,n){
			if(add(b[i])&&s[i]=='1'){
				ans=1;
				break;
			}
		}

		cout<<ans<<'\n';
		for(int i=0;i<=60;i++)a[i]=0;
	}
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
//  freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	Solve();
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
using int64 = long long;

constexpr int DEBUG = 0;

class BinaryIndexedTree {
 private:
  int n;
  vector<int64> xs;
 public:
  BinaryIndexedTree(int n) : n(n), xs(n) {}

  // Adds x to the i-th element.
  void Add(int i, int64 x) {
    int cur = i;
    while (cur < n) {
      xs[cur] += x;
      cur = cur | (cur + 1);
    }
  }

  // Returns a sum of a range [l r).
  int64 SumOfRange(int l, int r) {
    if (l >= r) return 0;
    if (l > 0) {
      return SumOfRange(0, r) - SumOfRange(0, l);
    }
    int64 s = 0;
    int i = r - 1;
    while (i >= 0) {
      s += xs[i];
      i = (i & (i + 1)) - 1;
    }
    return s;
  }
};

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

  int n, q;
  cin >> n >> q;
  BinaryIndexedTree bit(n);
  vector<int64> xs(n);
  for (int i = 0; i < n; i++) {
    cin >> xs[i];
  }
  for (int i = 0; i < n; i++) {
    bit.Add(i, xs[i]);
  }
  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    if (t == 0) {
      int p, x;
      cin >> p >> x;
      bit.Add(p, x);
    } else {
      int l, r;
      cin >> l >> r;
      cout << bit.SumOfRange(l, r) << "\n";
    }
  }
}


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

int color[200000];

int n,m;
vector<vector<int>> v(200000);
/*
bool dfs(int nn, int pre, int c){
    for(int x:v[nn]){
        if(x==pre) continue;
        if(color[x]==-1) color[x]=1-c;
        //if(color[x]!=c) continue;
        if(color[x]==c) return false;

        if(!dfs(x,nn,color[x])) return false;
    }

    return true;
}*/

bool dfs(int vv,int c){
    color[vv]=c;
    for(auto x:v[vv]){
        if(color[x]==c) return false;
        if(color[x]==0 && !dfs(x,-c)) return false;
    }
    return true;
}

signed main(){
    cin >> n >> m;
    memset(color,0,sizeof(color));
    rep(i,m){
        int aa,bb;
        cin >> aa >> bb;
        v[aa-1].push_back(bb-1);
        v[bb-1].push_back(aa-1);
    }

    //color[0]=0;
    rep(i,n){
        if(color[i]==0){
            if(!dfs(i,1)){
                cout << ((n*(n-1))/2)-m << endl;
                return 0;
            }
        }
    }

    int white=0;
    int black=0;
    rep(i,n){
        if(color[i]==1) white++;
        else black++;
    }
    //cout << white << black << endl;
    cout << white*black-m << endl;

}

#include <bits/stdc++.h>

using namespace std;

class UnionFind {
 public:
  std::vector<int> parent;

  UnionFind(int n) : parent(n, -1) {}

  int root(int x) {
    if (parent[x] < 0) return x;
    return parent[x] = root(parent[x]);
  }

  bool issame(int x, int y) { return root(x) == root(y); }

  int size(int x) { return -parent[root(x)]; }

  bool unite(int x, int y) {
    int rx = root(x), ry = root(y);
    if (rx == ry) return false;
    if (parent[rx] > parent[ry]) std::swap(rx, ry);
    parent[rx] += parent[ry];
    parent[ry] = rx;
    return true;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  cin >> N >> M;
  UnionFind uf(2 * N);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    uf.unite(a, b + N);
    uf.unite(a + N, b);
  }
  if (uf.issame(0, N)) {
    cout << 1LL * N * (N - 1) / 2 - M << '\n';
  } else {
    int cnt = 0;
    for (int i = 0; i < N; i++) {
      if (uf.issame(0, i)) cnt++;
    }
    // cout << cnt << '\n';
    cout << 1LL * cnt * (N - cnt) - M << '\n';
  }
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <climits>
#include <utility>
typedef long long ll;
using namespace std;

const ll MOD = 998244353LL;
const ll MAX = 2000010LL;

ll fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

ll COM(ll n, ll k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

ll modpow(ll a, ll n) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return res;
}

int main(int argc, char *argv[]) {
  COMinit();

  ll n, m, k;

  cin >> n >> m >> k;

  ll ans = 0LL;

  for(ll i=0;i<=k;i++){
    ans += ((m*modpow(m-1LL,n-i-1LL)%MOD)*COM(n-1LL,i)) % MOD;
    ans %= MOD;
  }

  if(ans<0LL){
    ans += MOD;
  }

  cout << ans << endl;

  return 0;
}

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

#define loop(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define dump(a) cerr << #a << " = " << (a) << "(L:" << __LINE__ << ")" << endl

const long long MOD = 998244353;
const int FACT_MAX = 1000000;
long long fact[FACT_MAX];
long long inv[FACT_MAX];
long long invfact[FACT_MAX];
long long nPr(int n, int r) { return (fact[n] * invfact[n - r]) % MOD; }
long long nCr(int n, int r) {
    return (((fact[n] * invfact[r]) % MOD) * invfact[n - r]) % MOD;
}
void initialize_fact() {
    fact[0] = 1;
    inv[0] = 0;
    inv[1] = 1;
    invfact[0] = 1;
    loop(i, 1, FACT_MAX) fact[i] = (i * fact[i - 1]) % MOD;
    loop(i, 2, FACT_MAX) inv[i] = MOD - ((MOD / i) * inv[MOD % i] % MOD);
    loop(i, 1, FACT_MAX) invfact[i] = (invfact[i - 1] * inv[i]) % MOD;
}

void solve(long long N, long long M, long long K) {
    initialize_fact();

    vector<long long> pow_m(N + 1);
    pow_m[0] = 1;
    rep(i, N) pow_m[i + 1] = (pow_m[i] * (M-1)) % MOD;

    long long ans = 0;
    rep (k, K + 1) {
        long long cmb = M;
        cmb *= nCr(N-1, k);
        cmb %= MOD;
        cmb *= pow_m[N-1-k];
        cmb %= MOD;
        ans += cmb;
        ans %= MOD;
    }
    cout << ans << endl;
}

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


#include <bits/stdc++.h>

using namespace std;

#ifdef Adrian
#include "debug.h"
#else
#define debug(...) 9999
#endif

typedef long long ll;
typedef long double ld;
typedef complex<ld> point;
#define F first
#define S second
#define ii pair<ll,ll>
template <typename G>
struct triple{G F, S, T;};
#define iii triple<int>

int main()
{

#ifdef Adrian
    //freopen("test.txt", "r", stdin);
#else
    ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
#endif

    const ll mod = 998244353;
    string s;
    int h;
    cin>>s>>h;

    int n = 0;
    for(auto &i:s)
        n += (i == '1');

    vector<int> v;
    for(int i=0, j=0; i<s.size(); i=j)
    {
        for(; j<s.size() && s[j] == '1'; j++);
        v.push_back(j - i);
        ++j;
    }

    int m = v.size();
    vector<vector<ll>> cur(n + 1, vector<ll>(n + 1));
    vector<vector<ll>> next(n + 1, vector<ll>(n + 1));
    for(int i=0; i<=n; i++)
        cur[i][0] = 1;

    vector<vector<ll>> ac1(2 * n + 1, vector<ll>(n + 2));
    vector<vector<ll>> ac2(n + 1, vector<ll>(n + 2));

    for(int i=1; i<m; i++)
    {
        for(int j=0; j<=n; j++)
            for(int k=0; k<=n; k++)
            {
                ac1[j + k][j + 1] = cur[j][k];
                ac2[k][j + 1] = cur[j][k];
            }

        for(auto &j:ac1)
            for(int k=1; k<j.size(); k++)
                j[k] = (j[k] + j[k - 1]) % mod;
        for(auto &j:ac2)
            for(int k=1; k<j.size(); k++)
                j[k] = (j[k] + j[k - 1]) % mod;

        for(int j=0; j<=n; j++)
            for(int k=0; k<=n; k++)
            {
                next[j][k] = ac1[j + k][min(n, j + min(v[i], k)) + 1] - ac1[j + k][j];
                next[j][k] += ac2[k][j];

                next[j][k] %= mod;
                if(next[j][k] < 0)
                    next[j][k] += mod;
            }

        swap(next, cur);
    }

    ll ans = 0;
    for(int i=0; i<=min(h, n); i++)
        ans = (ans + cur[0][i]) % mod;

    cout<<ans<<'\n';

    return 0;
}

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

int H,W,Ans,top,stop[2010][2010];
bool g[2010][2010];

struct Point{
	int x,y;
	Point() {}
	Point(int x,int y):x(x),y(y) {}
}P[2010];

int main()
{
#ifdef h10
	freopen("F.in","r",stdin);
	freopen("F.out","w",stdout);
#endif
	int i,j;
	char c;
	scanf("%d%d",&H,&W);
	for (i=1;i<=H;i++)
	for (j=1;j<=W;j++)
	{
		c=getchar();
		while (c!='.'&&c!='#') c=getchar();
		g[i][j]=c=='#';
	}

	Ans=max(H,W);
	for (i=1;i<=H;i++)
	for (j=W;j>=2;j--)
		g[i][j]^=g[i][j-1];

	for (j=2;j<=W;j++)
	{
		int last[2];
		last[0]=last[1]=H+1;
		for (i=H;i>=1;i--)
		{
			stop[i][j]=last[g[i][j]^1];
			last[g[i][j]]=i;
		}
	}

	for (i=1;i<H;i++)
	{
		P[top=1]=Point(i,W+1);
		for (j=W;j>=2;j--)
		{
			while (P[top].x>=stop[i][j])
			{
				Ans=max(Ans,(P[top].x-i)*(P[top-1].y-j));
				top--;
			}
			P[++top]=Point(stop[i][j],j);
		}
		while (top)
			Ans=max(Ans,(P[top].x-i)*(P[top-1].y-j)),top--;
	}
	printf("%d\n",Ans);
}


#include <cstdio>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <map>
#include <set>
#include <iostream>
#include <sstream>
#include <numeric>
#include <cctype>
#include <bitset>
#include <cassert>
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define gep(i,g,j) for(int i = g.head[j]; i != -1; i = g.e[i].next)
#define each(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define rng(a) a.begin(),a.end()
#define maxs(x,y) x = max(x,y)
#define mins(x,y) x = min(x,y)
#define pb push_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcount
#define uni(x) x.erase(unique(rng(x)),x.end())
#define snuke srand((unsigned)clock()+(unsigned)time(NULL));
#define df(x) int x = in()
#define dame { puts("No"); return 0;}
#define show(x) cout<<#x<<" = "<<x<<endl;
#define PQ(T) priority_queue<T,vector<T>,greater<T> >
using namespace std;
typedef long long int ll;
typedef pair<int,int> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
inline int in() { int x; scanf("%d",&x); return x;}
inline void priv(vi a) { rep(i,sz(a)) printf("%d%c",a[i],i==sz(a)-1?'\n':' ');}
template<typename T>istream& operator>>(istream&i,vector<T>&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T>string join(vector<T>&v)
{stringstream s;rep(i,sz(v))s<<' '<<v[i];return s.str().substr(1);}
template<typename T>ostream& operator<<(ostream&o,vector<T>&v)
{if(sz(v))o<<join(v);return o;}
template<typename T1,typename T2>istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<typename T1,typename T2>ostream& operator<<(ostream&o,pair<T1,T2>&v)
{return o<<v.fi<<","<<v.se;}
const int MX = 100005, INF = 1001001001;
const ll LINF = 1e18;
const double eps = 1e-10;

// Binary Indexed Tree
struct bit {
  typedef int TT;
  int n; vector<TT> d;
  bit() {}
  bit(int mx): n(mx), d(mx) {}
  void add(int i, TT x=1) {
    for (++i;i<n;i+=i&-i) d[i] += x;
  }
  TT sum(int i) {
    TT x = 0;
    for (++i;i;i-=i&-i) x += d[i];
    return x;
  }
};
//

int f(vi& s) {
  int n = sz(s);
  bit t(n+5);
  ll res = 0;
  rep(i,n) {
    res += i-t.sum(s[i]);
    t.add(s[i]);
  }
  return res&1;
}

int main() {
  int n;
  scanf("%d",&n);
  vvi na(3, vi(n));
  rep(i,3)rep(j,n) na[i][j] = in()-1;
  vi a, b;
  rep(i,n) {
    int x = na[0][i]/3;
    a.pb(x);
    rep(j,3) if (na[j][i]/3 != x) dame;
    if (na[1][i]%3 != 1) dame;
    b.pb(na[0][i]%3/2);
  }
  rep(i,n) if ((a[i]&1) != (i&1)) dame;
  vvi s(2);
  vi cnt(2);
  rep(i,n) {
    s[i&1].pb(a[i]/2);
    cnt[i&1] += b[i];
  }
  rep(i,2) {
    cnt[i^1] += f(s[i]);
  }
  rep(i,2) if (cnt[i]&1) dame;
  puts("Yes");
  return 0;
}


#pragma GCC optimze("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pp pair<ll,ll>
#define ppp pair<ll,pp >
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
template<typename Arg1>
void __f(const char* name, Arg1&& arg1) {
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma=strchr(names+1,',');
	cout.write(names,comma-names)<<" : "<<arg1<<" | ";
	__f(comma+1,args...);
}
typedef tree<pp,null_type,less<pp>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define ld long double
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rep(i,a,b) for(ll i=a;i<b;i+=1)
#define sz(v) (ll)(v.size())
#define hell (ll)1000000007
#define slld(x) scanf("%lld",&x)
ll t=1, tt;
const ll inf=1e18;
inline ll mult(ll x,ll y){x%=hell;y%=hell;return (1LL*x*y)%hell;}
inline ll add(ll x,ll y){x+=y;return x%hell;}
inline ll powMod(ll base,ll expo){
	if(base==0)return 0;
	ll ans=1;
	while(expo){
		if(expo&1)ans=mult(ans,base);
		base=mult(base,base);
		expo>>=1;
	}
	return ans;
}
vector<ll> pre,dp;
void solve(){
	ll n;
	cin>>n;
	vector<vector<ll> > a(n,vector<ll>(n));
	rep(i,0,n)
		rep(j,0,n)
			cin>>a[i][j];
	pre.resize(1<<n);
	rep(mask,0,1LL<<n){
		rep(i,0,n){
			if((mask>>i)&1){
				rep(j,i+1,n){
					if((mask>>j)&1)pre[mask]+=a[i][j];
				}
			}
		}
	}
	dp.resize(1<<n,-inf);
	dp[0]=0;
	rep(mask,0,1LL<<n){
		vector<ll> not_taken;
		rep(i,0,n)
			if(!((mask>>i)&1))
				not_taken.pb(i);
		ll nn=sz(not_taken);
		rep(mask2,0,1LL<<nn){
			ll cur_mask=0;
			rep(j,0,sz(not_taken))
				if((mask2>>j)&1)cur_mask^=(1LL<<not_taken[j]);
			dp[mask^cur_mask]=max(dp[mask^cur_mask],dp[mask]+pre[cur_mask]);
		}
	}
	cout<<dp[(1<<n)-1];
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   	//cin>>t;
   	tt=t;
    while(t--){
        solve();
        if(t)cout<<"\n";
    }
    return 0 ;
}

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod=998244353;
#define N 321
inline int read(){
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return x*f;
}
int n,a[N],dp[N*N],sum,ans;
int main(){
	n=read();
	ans=1;
	for(int i=1;i<=n;++i){
		a[i]=read();
		ans=3LL*ans%mod;
	}
	dp[0]=1;
	for(int i=1;i<=n;++i){
		sum+=a[i];
		for(int j=sum;j>=0;--j){
			(dp[j]<<=1)%=mod;
			if(j>=a[i])dp[j]=(dp[j]+dp[j-a[i]])%mod;
		}
	}
	for(int i=sum;sum-i<=i;--i){
		ans=(ans-3LL*dp[i]%mod+mod)%mod;
	}
	if(!(sum&1)){
		memset(dp,0,sizeof(dp));
		dp[0]=1;
		for(int i=1;i<=n;++i){
			for(int j=sum;j>=a[i];--j){
				dp[j]=(dp[j]+dp[j-a[i]])%mod;
			}
		}
		ans=(ans+3LL*dp[sum>>1])%mod;
	}
	printf("%d\n",ans);
	return 0;
}


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

#define     fast()              ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define     ll                  long long int
#define     test()              ll t; cin>>t; while(t--)
#define     lp0(i,begin,end)    for(ll i=begin;i<end;i++)
#define     lp1(i,begin,end)    for(ll i=begin;i<=end;i++)
#define     rlp(i,begin,end)    for(ll i=end;i>=begin;i--)
#define     prec(n)             fixed<<setprecision(n)
#define     initial(a,i)        memset(a,i,sizeof(a))
#define     pb                  push_back
#define     PI                  3.1415926535897932384626433832795
#define     MOD                 1000000007
#define     F                   first
#define     S                   second
#define     all(a)              (a).begin(),(a).end()
#define     BPC(x)              __builtin_popcountll(x)
#define     gcd(a,b)            __gcd(a,b)

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

ll power(ll x,ll n)
{
    ll result = 1;
    while (n)
    {
        if (n & 1)
            result = result * x;
        n = n / 2;
        x = x * x;
    }
    return result;
}

// Solution Function
void solution(ll compte)
{
    // cout<<compte<<"\n";
    ll a,b,c;
    cin>>a>>b>>c;

    cout<<min((a+b),min((b+c),(c+a)));
}
    // Driver Function
int main()
{
    ll compte = 1;
    //test()
    //{
        solution(compte);
        compte++;
    //}
    return 0;
}

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int n;
long long ans,a,b,x[100009];
int main(){
	scanf("%d%lld%lld",&n,&a,&b);
	for(int i=1;i<=n;i++)scanf("%lld",&x[i]);
	for(int i=2;i<=n;i++)ans+=min(a*(x[i]-x[i-1]),b);
	printf("%lld\n",ans);
	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <unordered_map>
#include <math.h>
#include <algorithm>
#include <deque>
#include <climits>
using namespace std;
#define rep(i,l,r) for(int i=l;i<r;i++)
#define repb(i,r,l) for(int i=r;i>l;i--)
#define ll long long
#define ull unsigned long long
#define ve vector
#define umap unordered_map
#define iter iterator

int main() {
    ll x; cin>>x;
    ll n = x/11, r = x%11;
    if(r>6) cout<<n*2 + 2;
    else if(r>0) cout<<n*2 + 1;
    else cout<<n*2;
    return 0;
}

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

void solve()
{
    string s;
    cin >> s;
    rep(bit, 1 << 3)
    {
        int tmp = s[0] - '0';
        string ans;
        ans += s[0];
        rep(i, 3)
        {
            if (bit & (1 << i))
            {
                tmp += s[i + 1] - '0';
                ans += "+";
                ans += s[i + 1];
            }
            else
            {
                tmp -= s[i + 1] - '0';
                ans += "-";
                ans += s[i + 1];
            }
        }
        if (tmp == 7)
        {
            printf("%s=7\n", ans.c_str());
            return;
        }
    }
}

int main()
{
    solve();
    return 0;
}


#include <iostream>
#include <algorithm>

using namespace std;

bool Bsearch(int key, int A[], int len) {
	int l = 0, r = len - 1;
	while (l <= r) {
		int mid = l + (r - l) / 2;
		if (A[mid] > key)
			r = mid - 1;
		else if (A[mid] < key)
			l = mid + 1;
		else
			return true;
	}
	return false;
}

int main() {
	int n, *S;
	int q, *T;
	int ans = 0;
	cin >> n;
	S = new int[n];
	for (int i = 0; i < n; i++)
		cin >> S[i];
	cin >> q;
	T = new int[q];
	for (int i = 0; i < q; i++)
		cin >> T[i];

	sort(S, S + n);
	for (int i = 0; i < q; i++)
		for (int j = 0; j < n; j++)
			if (Bsearch(T[i], S, n) == true) {
				ans++;
				break;
			}

	cout << ans << endl;
	delete[] S;
	delete[] T;
	return 0;
}

#include<iostream>
using namespace std;
int main(){
  int n,a[10000],m,b,ans=0;
  cin>>n;
  for(int i=0;i<n;i++)cin>>a[i];
  cin>>m;
  while(m--){
    cin>>b;
    for(int i=0;i<n;i++)
      if(a[i]==b){ans++;break;}
  }
  cout<<ans<<endl;
  return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>

#define ll long long

using namespace std;

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

	ll n;
	cin >> n;
	vector<ll> a(n);

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

	ll sum = 0;
	for (int i = 0; i < n; i++) {
		if (i != 0) {
			if (a[i] < a[i - 1]) {
				sum += a[i - 1] - a[i];
				a[i] += a[i - 1] - a[i];

			}
		}
	}

	cout << sum;

	return 0;
}

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

int a[1010],p[1010];

int main(){
	int N,M,l,max=0,r;
	cin>>N>>M;
	for(int i=0;i<N;i++) cin>>a[i];
	for(int i=0;i<M;i++){
		cin>>l;
		for(int j=0;j<N;j++){
			if(a[j]<=l){
				p[j]++;
				break;
			}
		}
	}
	for(int i=0;i<N;i++){
		if(max<p[i]){
			max=p[i];
			r=i+1;
		}
	}
	cout<<r<<endl;
	return 0;
}


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <functional>
#include <algorithm>
#include <string>
#include <vector>
#include <algorithm>
#include <time.h>
#include <stdlib.h>
#include <math.h>

using namespace std;

int main (void)
{
	long m,min,max,p[10000],g[10000],kkk,rrr;

	for (long o = 0; o < 10000; o++)
	{
	cin >> m >> min >> max;

	if (m == 0 && min == 0)
	{
		if (max == 0)
		{
			break;
		}
	}

	kkk = 0;

	for (long i = 1; i < m + 1; i++)
	{
		cin >> p[i];
	}

	for (long j = min; j < max + 1; j++)
	{
		g[j] = p[j] - p[j + 1];

		if (kkk <= g[j])
		{
			kkk = 0;
			kkk = g[j];
			rrr = 0;
			rrr = j;
		}
	}

		cout << rrr << endl;
	}
}

#if 1
  #include <bits/stdc++.h>
#else
  #include "../../bits/stdc++.h"
#endif

#define FOR(i,a,b) for(int i = (a);i < (b);i++)
#define RFOR(i,a,b) for(int i = (b)-1;i >= (a);i--)
#define REP(i,n) FOR(i,0,(n))
#define RREP(i,n) RFOR(i,0,(n))

using namespace std;

typedef pair<int, int> P;
#define pb push_back
#define mp make_pair
#define ft first
#define sd second

int m, nin, nax;
int p[202];

bool input(){
  cin>>m>>nin>>nax;
  REP(i, m)cin>>p[i];
  return m;
}

void solve(){
  int s=0, b=0;
  FOR(i, nin-1, nax) {
    if(p[i]-p[i+1]>=s){
      b=i+1;
      s=p[i]-p[i+1];
    }
  }
  cout<<b<<endl;
}

int main(){
  cin.tie(0);

  while (input()){
      solve();
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define SZ(x) ((int)(x).size())
#define PB push_back
template <typename T>
void Puts(T x) {
    cout << x << endl;
}
struct Node {
    vector<int> parents;
    vector<int> children;
    int value;
};
int main() {
    int n, m;
    cin >> n >> m;
    auto nodes = vector<Node>(n, Node{
                                     vector<int>(0),
                                     vector<int>(0),
                                     -1});
    rep(i, n - 1 + m) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        nodes[a].children.PB(b);
        nodes[b].parents.PB(a);
    }
    auto current = 0;
    function<void(int)> dfs = [&](auto index) {
        auto &node = nodes[index];
        if (node.value != -1) return;
        for (auto &childIndex : node.children) {
            auto &child = nodes[childIndex];
            if (child.value == -1) {
                dfs(childIndex);
            }
        }
        node.value = current;
        current++;
    };
    rep(i, n) {
        dfs(i);
    };
    rep(i, n) {
        auto &node = nodes[i];
        auto &parents = node.parents;
        if (SZ(parents) == 0) {
            puts("0");
            continue;
        }
        auto minValue = INT_MAX;
        auto ans = 0;
        for (auto &parentIndex : parents) {
            auto &parent = nodes[parentIndex];
            if (parent.value < minValue) {
                ans = parentIndex + 1;
                minValue = parent.value;
            }
        }
        Puts(ans);
    }
}

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

#define FOR(a, b, c) for(int a = b; a <= c; ++a)
#define fi first
#define se second
#define pb push_back
//#define int long long

typedef pair<int, int> ii;
typedef pair<int, ii> iii;

const int N = 1e5 + 10;
const int oo = 1e18;

int n, m, root, cnt;
int deg[N], ans[N], dp[N], tpid[N], idtp[N];
bool check[N], visit[N];
vector<ii> child[N];
ii edge[N];
stack<int> st;

void dfs(int u) {
    visit[u] = 1;
    for(auto it: child[u])  if(!visit[it.fi])   {
        dfs(it.fi);
    }
    st.push(u);
}

void solve()    {
    dfs(root);
    while(!st.empty())  {
        tpid[st.top()] = ++cnt;
        idtp[cnt] = st.top();
        st.pop();
    }

    FOR(i, 1, n) dp[i] = -1;
    dp[1] = 0;
    FOR(i, 1, n)    {
        int u = idtp[i];
        for(auto it: child[u]) if(dp[tpid[it.fi]] < dp[i] + 1)
            dp[tpid[it.fi]] = dp[i] + 1;
    }
//    FOR(i, 1, n) cout << dp[tpid[i]] << ' '; cout << '\n';
    FOR(i, 1, n)    {
        for(auto it: child[i]) if(dp[tpid[it.fi]] - dp[tpid[i]] > 1)
            check[it.se] = 1;
    }
    FOR(i, 1, n - 1 + m)    if(!check[i])
        ans[edge[i].se] = edge[i].fi;
    FOR(i, 1, n) cout << ans[i] << '\n';
}

signed main()  {
//    freopen("test.inp", "r", stdin);
//    freopen("test.out", "w", stdout);
//    ios_base::sync_with_stdio(false); cout.tie(0);

    scanf("%d%d", &n, &m);
    FOR(i, 1, n - 1 + m)    {
        int u, v; scanf("%d%d", &u, &v);
        edge[i] = {u, v};
        child[u].pb({v, i});
        deg[v] ++;
    }

    FOR(i, 1, n) if(deg[i] == 0) root = i;

    return solve(), 0;
}


#include<bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep_inv(i, n, m) for(int i = (int)(n); i > (int)(m); i--)
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vc = vector<char>;
using vvl = vector<vl>;
using vvc = vector<vc>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;

int main(){
  ll N, K;
  cin >> N >> K;
  vl x(N);
  rep(i, N) cin >> x[i];

  ll ans = (ll)pow(10, 18);
  ll dist;

  rep(i, N - K + 1){
    if(x[i + K - 1] * x[i] < 0)
      dist = x[i + K - 1] - x[i] + min(abs(x[i + K - 1]), abs(x[i]));
    else
      dist = max(abs(x[i + K - 1]), abs(x[i]));

    ans = min(ans, dist);
  }

  cout << ans << "\n";

  return 0;
}

#define MOD_TYPE 1

#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
//#include <boost/multiprecision/cpp_int.hpp>
//using multiInt = boost::multiprecision::cpp_int;
/*
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
*/
using ll = long long int;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
template <typename Q_type>
using smaller_queue = priority_queue<Q_type, vector<Q_type>, greater<Q_type>>;

constexpr ll MOD = (MOD_TYPE == 1 ? (ll)(1e9 + 7) : 998244353);
constexpr int INF = (int)1e9;
constexpr ll LINF = (ll)4e18;
constexpr ld PI = acos(-1.0);
constexpr ld EPS = 1e-11;
constexpr int Dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
constexpr int Dy[] = {1, -1, 0, 0, -1, -1, 1, 1, 0};

#define REP(i, m, n) for (ll i = m; i < (ll)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define MP make_pair
#define MT make_tuple
#define YES(n) cout << ((n) ? "YES" : "NO") << "\n"
#define Yes(n) cout << ((n) ? "Yes" : "No") << "\n"
#define possible(n) cout << ((n) ? "possible" : "impossible") << "\n"
#define Possible(n) cout << ((n) ? "Possible" : "Impossible") << "\n"
#define Yay(n) cout << ((n) ? "Yay!" : ":(") << "\n"
#define all(v) v.begin(), v.end()
#define NP(v) next_permutation(all(v))
#define dbg(x) cerr << #x << ":" << x << "\n";

inline void init_main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(30) << setiosflags(ios::fixed);
}
template <typename T>
inline bool chmin(T &a, T b)
{
  if (a > b)
  {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
  if (a < b)
  {
    a = b;
    return true;
  }
  return false;
}
inline ll CEIL(ll a, ll b)
{
  return (a + b - 1) / b;
}
#pragma endregion

int main()
{
  init_main();

  int h, w;
  cin >> h >> w;
  int a[80][80], b[80][80];
  rep(i, h) rep(j, w) cin >> a[i][j];
  rep(i, h) rep(j, w) cin >> b[i][j];

  bool dp[80][80][160 * 81] = {};
  const int add = 80 * 80;
  dp[0][0][a[0][0] - b[0][0] + add] = true;
  dp[0][0][b[0][0] - a[0][0] + add] = true;
  rep(i, h) rep(j, w)
  {
    rep(k, 160 * 80)
    {
      if (!dp[i][j][k])
        continue;
      if (i + 1 != h)
      {
        int kk = k + a[i + 1][j] - b[i + 1][j];
        if (0 <= kk && kk < 160 * 80)
          dp[i + 1][j][kk] = true;
        kk = k + b[i + 1][j] - a[i + 1][j];
        if (0 <= kk && kk < 160 * 80)
          dp[i + 1][j][kk] = true;
      }
      if (j + 1 != w)
      {
        int kk = k + a[i][j + 1] - b[i][j + 1];
        if (0 <= kk && kk < 160 * 80)
          dp[i][j + 1][kk] = true;
        kk = k + b[i][j + 1] - a[i][j + 1];
        if (0 <= kk && kk < 160 * 80)
          dp[i][j + 1][kk] = true;
      }
    }
  }

  int Min = INF;
  rep(k, 160 * 80)
  {
    if (!dp[h - 1][w - 1][k])
      continue;
    int val = abs(k - add);
    chmin(Min, val);
  }
  cout << Min << endl;
  return 0;
}


#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = int64_t;
#define rep(i, j, n) for (int i = j; i < (int)n; ++i)
constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n), s(n + 1);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) s[i + 1] = s[i] + abs(a[i]);

  ll minus = INFL, minus_cnt = 0;
  rep(i, 0, n) {
    if (a[i] == 0) {
      minus = INFL;
      minus_cnt = 0;
    } else {
      minus = min(minus, abs(a[i]));
      if (a[i] < 0) ++minus_cnt;
    }
  }
  cout << s[n] - (minus_cnt % 2 == 0 ? 0 : minus * 2) << endl;
}

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
#include<utility>
#include<queue>
#include<deque>
#include<stack>
#include<set>
#include<map>
#include<bitset>
#include<string>
#include<functional>
#define rep(i,n,m) for(int i=(n);i<(int)(m);i++)
#define reps(i,n,m) for(int i=(n);i<=(int)(m);i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define SZ(x) ((int)(x).size())
#define pb push_back
#define fs first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define LB(a,x) lb(all(a), x) - a.begin()
#define UB(a,x) ub(all(a), x) - a.begin()
#define printfdouble(x) printf("%.20f\n",(x))
#define chartoint(c) (int)((c) - '0')
#define chartoll(c) (long long)((c) - '0')
#define MOD 1000000007
#define itn int
#define enld endl
#define ednl endl
#define icn cin
#define cotu cout
#define Endl endl
#define stirng string
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
using Graph = vector<vector<int>>;
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<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

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

int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    ll n; cin >> n;
    ll A[300100];
    rep(i,0,n) cin >> A[i];
    int cnt[65];
    Fill(cnt, 0);
    rep(i,0,n){
        ll tmp = A[i];
        int j = 0;
        while(tmp > 0){
            cnt[j] += (tmp % 2);
            tmp /= 2;
            j++;
        }
    }

    ll ans = 0;
    rep(i,0,60){
        ll tmp = modpow(2,i,MOD);
        ans += (tmp * (n-cnt[i]) % MOD) * cnt[i] % MOD;
    }

    ans %= MOD;
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

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

    string s;
    cin >> s;

    int64_t m = s.size();

    vector a(1, 0L);
    int64_t t = 0;
    for (auto c : s) {
        if (c == '0') {
            a.push_back(t);
        } else {
            ++t;
        }
    }
    a.push_back(t);

    int64_t k;
    cin >> k;
    k = min(k, a.back());

    int64_t n = a.size();

    constexpr auto M = 998244353L;

    vector dp(vector(k + 1, vector(a.back() + 1, 0L)));
    int64_t mk = 0;
    dp[0][0] = 1;
    for (auto i : irange(0L, n - 1)) {
        for (auto j : irange(mk, -1L, -1L)) {
            for (auto c : irange(0L, a.back() - a[i + 1] + 1)) {
                for (auto b : irange(c + 1, c + a[i + 1] - a[i] + 1)) {
                    auto w = j + b - c;
                    if (w > k) {
                        break;
                    }
                    mk = max(mk, w);
                    dp[w][c] += dp[j][b];
                    if (dp[w][c] >= M) {
                        dp[w][c] -= M;
                    }
                }
            }

            for (auto c : irange(1L, a.back() - a[i + 1] + 1)) {
                dp[j][c] += dp[j][c - 1];
                if (dp[j][c] >= M) {
                    dp[j][c] -= M;
                }
            }
        }
    }

    int64_t ans = 0;
    for (auto i : irange(0L, k + 1)) {
        ans += dp[i][0];
        if (ans >= M) {
            ans -= M;
        }
    }

    cout << ans << endl;
}

#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const ll mod = 998244353;

ll dp[310][310];

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

	string S;
	int K;
	int k = 0;
	int A[301] = {};

	cin >> S >> K;
	rep(i, S.size()) {
		if (S[i] == '0') k++;
		else {
			A[k]++;
		}
	}

	int kosuu = 0;
	dp[0][0] = 1;
	for (int i = k; i >= 0; i--) {
		for (int j = kosuu; j >= 0; j--) {
			ll tmp = 0;
			for (int k = kosuu; k >= 0; k--) {
				dp[j][k] %= mod;
				tmp += dp[j][k];
				rep1(p, A[i]) dp[j + p][k + p] += dp[j][k];
				dp[j][k] = tmp;
			}
		}
		kosuu += A[i];
	}

	ll kotae = 0;
	rep(i, min(K, kosuu) + 1) {
		kotae += dp[i][0] % mod;
	}
	co(kotae % mod);

	Would you please return 0;
}


#define _USE_MATH_DEFINES
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

void abc_160a();

void abc_161a();

void abc_162a();

void abc_163a();

int main()
{
  //abc_160a();
  //abc_161a();
  //abc_162a();
  abc_163a();

}

void abc_160a()
{
  string S;
  cin >> S;

  if (S[2] == S[3] && S[4] == S[5]) cout << "Yes\n";
  else cout << "No\n";
}

void swap(int& a, int& b)
{
  int tmp;
  tmp = a;
  a = b;
  b = tmp;
}

void abc_161a()
{
  int x, y, z;
  cin >> x >> y >> z;

  swap(x, y);
  swap(x, z);

  cout << x << " " << y << " " << z << "\n";
}

void abc_162a()
{
  string s;
  cin >> s;
  for (char c : s)
  {
    if (c == '7')
    {
      cout << "Yes\n";
      return;
    }
  }

  cout << "No\n";
}

void abc_163a()
{
  int R;
  cin >> R;

  cout << (double)(2 * R * M_PI);
}

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

 int main () {
   double a;
   cin >> a;

   cout << 2*a*3.14159265359 << endl;

 }


#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>

typedef long long ll;
using namespace std;

int main()
{
	int days[] = {31,29,31,30,31,30,31,31,30,31,30,31};
	string week[] = {"Thursday","Friday","Saturday","Sunday","Monday","Tuesday","Wednesday"};

	int a,b;
	while(~scanf("%d %d",&a,&b)){
		if(a==0 && b==0) break;

		int passed=0;
		for(int i=0;i<a - 1;i++){
			passed += days[i];
		}

		passed += b;
		cout << week[(passed - 1) % 7] << endl;
	}
}

#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
#include <cstdio>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <numeric>
#include <algorithm>
using namespace std;

typedef pair<int,int> P;
typedef long long LL;

#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
#define FOR(i,a,b) for(int i = (a); i < (int)(b); ++i)
#define FOREACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(v) ((int)v.size())
#define min_3(a,b,c) min(a,min(b,c))
#define max_3(a,b,c) max(a,max(b,c))
#define debug(x)  cerr << #x << " = " << (x) << endl;
#define PI 3.1415926535897
#define INF 1000000000
//#define INF 9

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

int main(){
	int h,w;
	int i,j;
	int minh = 0,minw = 0;;
	int taikaku;
	long long min = 999999999999999;

	while(1){

		cin >> h >> w;
		if(h == 0 &&  w == 0)break;
		min = 99999999999999999;
		taikaku = h * h + w * w;

		for(i =1;i <= 150 && i * i < taikaku;i++){
			j = i + 1;
			while(1){//925

				int  x = i * i + j * j;

				if(x == taikaku){
					if(x < min && h < i ){

						min = x;
						minh = i;
						minw = j;
						break;

					}

				}

				if(x > taikaku){
					if(i != h || j != w){
						if(min > x){
							min = x;
							minh = i;
							minw = j;

						}
					}
					break;
				}
			j++;
			}

		}

		cout << minh << " " << minw << endl;
	}

	return 0;
}


#include <iostream>
#include <algorithm>
#include <iterator>
#include <iomanip>
#include <cmath>
#include <vector>
#include <numeric>
#include <cstdio>
#include <bitset>
#include <map>
#include <string>
#include <valarray>
// #include <cctype>

using namespace std;

typedef pair<int, int> Coordinate;
typedef vector<Coordinate> Coordinates;

void output(const vector<int> &);
bool place(const vector<int> &, int pos);
void back_trace(vector<int> & queen, Coordinates & coordinates, int current, int total);
bool match(const vector<int> & queen, const Coordinates & coordinates);

int main()
{
    Coordinates coordinates;
    vector<int> queen(8);

    int n;
    cin >> n;

    coordinates.resize(n);

    int i = 0;
    while (i < n)
    {
        cin >> coordinates[i].first >> coordinates[i].second;
        i++;
    }

    back_trace(queen, coordinates, 0, 8);

}

void output(const vector<int> & queen)
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
            if (queen[i] == j)
                cout << 'Q';
            else
                cout << '.';
        cout << endl;
    }

}

bool place(const vector<int> & queen, int current)
{
    for (int i = 0; i < current; i++)
        if (queen[current]==queen[i] || abs(current-i)==abs(queen[current]-queen[i]))
            return false;
    return true;
}

void back_trace(vector<int> & queen, Coordinates & coordinates, int current, int total)
{
    if (current >= total)
    {
        if (match(queen, coordinates))
            output(queen);
    }
    else
    {
        for (int i = 0; i < total; i++)
        {
            queen[current] = i;
            if (place(queen, current))
                back_trace(queen, coordinates, current+1, total);
        }
    }

}

bool match(const vector<int> & queen, const Coordinates & coordinates)
{
    for (int i = 0; i < coordinates.size(); i++)
    {
        if (queen[coordinates[i].first] != coordinates[i].second)
            return false;
    }
    return true;
}

#include <bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll MOD = 1000000007;
const ll INF = 10000000000000000;
vector<int> x4 = {0, 1, 0, -1}, x8 = {0, 1, 1, 1, 0, -1, -1, -1};
vector<int> y4 = {1, 0, -1, 0}, y8 = {1, 1, 0, -1, -1, -1, 0, 1};
template<class T> struct edge { int from, to; T cost;};
template<class T> inline bool chmin(T& a, T b){if (a>b){a = b; return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if (a<b){a = b; return true;}return false;}
template<class T> inline T powerM(T a,T b){if (b==0) return 1;
T tmp = powerM(a,b/2); if (b%2==0) return tmp*tmp%MOD; else return tmp*tmp%MOD*a%MOD; }
template<class T> inline T power(T a,T b,T m){ if (b==0) return 1;
  T tmp = power(a,b/2,m); if (b%2==0) return tmp*tmp%m; else return tmp*tmp%m*a%m; }
template<class T> inline T gcd(T a, T b){if (b==0) return a; return gcd(b, a%b);}
template<class T> inline T lcm(T a, T b){return a / gcd(a,b) * b;}
// ax+by=gcd(a,b)を解く
template<class T> inline T extgcd(T a,T b,T &x,T &y){if (b==0){x=1; y=0; return a;} T d=extgcd(b,a%b,y,x); y -= a/b*x; return d;}
void hey(){ cout <<"hey" <<endl; }

int N;
vector<string> field(8, "........");

bool check(int x,int y){
  // 攻撃されるならtrue
  bool ng = false;
  for (int i=0; i<8; i++) if (field[i][y] == 'Q') ng = true;
  for (int j=0; j<8; j++) if (field[x][j] == 'Q') ng = true;
  int a = x, b = y;
  while (a >= 0 && b >= 0) if (field[a--][b--] == 'Q') ng = true;
  a = x, b = y;
  while (a < 8 && b < 8) if (field[a++][b++] == 'Q') ng = true;
  a = x, b = y;
  while (a >= 0 && b < 8) if (field[a--][b++] == 'Q') ng = true;
  a = x, b = y;
  while (a < 8 && b >= 0) if (field[a++][b--] == 'Q') ng = true;
  return ng;
}

void rec(int row){
  if (row == 8){
    rep(i, 8) cout <<field[i] <<endl;
    return;
  }
  // 各行について考える
  bool ok = false; rep(i, 8) ok = ok | (field.at(row).at(i) == 'Q');
  if (ok) rec(row+1);
  // Queenが置かれていなかった場合、置けるところに置く
  rep(j, 8){
    if (check(row, j)) continue;
    field[row][j] = 'Q';
    rec(row+1);
    field[row][j] = '.';
  }
}

int main() {
  cin >>N;
  rep(q, N){
    int x,y; cin >>x >>y;
    field[x][y] = 'Q';
  }
  rec(0);
}


#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
template<class S, class T> inline S max_L(S a,T b){
  return a>=b?a:b;
}
inline int my_getchar_unlocked(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread_unlocked(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar_unlocked();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar_unlocked();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite_unlocked(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar_unlocked(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite_unlocked(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar_unlocked(a);
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar_unlocked('-');
  }
  while(s--){
    my_putchar_unlocked(f[s]+'0');
  }
}
template<class S, class T> inline S chmax(S &a, T b){
  if(a<b){
    a=b;
  }
  return a;
}
int N;
int A[200000];
int main(){
  int i;
  long long res = 0;
  long long mx = -1;
  rd(N);
  {
    int Lj4PdHRW;
    for(Lj4PdHRW=(0);Lj4PdHRW<(N);Lj4PdHRW++){
      rd(A[Lj4PdHRW]);
    }
  }
  for(i=(0);i<(N);i++){
    res +=max_L(mx - A[i], 0);
    chmax(mx, A[i]);
  }
  wt_L(res);
  wt_L('\n');
  return 0;
}
// cLay varsion 20200911-1

// --- original code ---
// int N, A[2d5];
// {
//   ll res = 0, mx = -1;
//   rd(N,A(N));
//   rep(i,N){
//     res += max(mx - A[i], 0);
//     mx >?= A[i];
//   }
//   wt(res);
// }

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#define chmax(x,y) x = max(x,y)
#define chmin(x,y) x = min(x,y)

const int INF = 1e9 + 10;

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    ll ans = 0;
    for (int i = 1; i < n; ++i) {
        if (a[i-1] <= a[i]) continue;
        ans += a[i-1] - a[i];
        a[i] = a[i-1];
    }
    cout << ans << endl;
    return 0;
}


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

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pll> vp;
typedef vector<ll> vll;

const int MAX_V = 10000;

int V, E;
vi G[MAX_V];
vi rG[MAX_V];
vi vs;
bool used[MAX_V];
int cmp[MAX_V];

void addEdge(int from, int to) {
    G[from].emplace_back(to);
    rG[to].emplace_back(from);
}

void dfs(int v) {
    used[v] = true;
    rep(i, G[v].size()) {
        if (!used[G[v][i]]) dfs(G[v][i]);
    }
    vs.emplace_back(v);
}

void rdfs(int v, int k) {
    used[v] = true;
    cmp[v] = k;
    rep(i, rG[v].size()) {
        if (!used[rG[v][i]]) rdfs(rG[v][i], k);
    }
    vs.emplace_back(v);
}

int scc() {
    memset(used, 0, sizeof(used));
    vs.clear();
    rep(v, V) {
        if (!used[v]) dfs(v);
    }
    memset(used, 0, sizeof(used));
    int k = 0;
    for (int i = vs.size() - 1; i >= 0; i--) {
        if (!used[vs[i]]) rdfs(vs[i], k++);
    }
    return k;
}

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

    cin >> V >> E;
    rep(i, E) {
        int s, t;
        cin >> s >> t;
        addEdge(s, t);
    }

    scc();

    int Q;
    cin >> Q;
    rep(i, Q) {
        int u, v;
        cin >> u >> v;
        cout << (cmp[u] == cmp[v]) << endl;
    }

}

#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a);i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl

using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
gpu_hash_map<int, int> mp;*/
using ll = long long;
using ld = long double;
const ll MOD = 1000000007;
const ll root = 62;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const double PI = acos(-1);
const double eps = -1e6;
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 1001;
int board[mxN][mxN];
int dist[mxN][mxN];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int h, w;
bool inside(int x, int y) {
    return x>=0&&x<h&&y>=0&&y<w;
}
int main() {
    //freopen("file.in", "r", stdin);
    //freopen("file.out", "w", stdout);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    cin.tie(0)->sync_with_stdio(0);
    cin>>h>>w;
    string s;
    queue<ttgl> Q;
    memset(dist, INF, sizeof(dist));
    owo(i, 0, h) {
            cin>>s;
        owo(j, 0, w) {
            board[i][j] = s[j]=='#';
            if(s[j]=='#') {
                Q.push({i, j});
                dist[i][j] = 0;
            }
        }
    }
    int ans = 0;
    while(!Q.empty()) {
        auto p = Q.front();
        Q.pop();
        ans = dist[p.first][p.second];
        owo(d, 0, 4) {
            if(inside(p.first+dx[d], p.second+dy[d])) {
                if(dist[p.first+dx[d]][p.second+dy[d]]==INF) {
                    dist[p.first+dx[d]][p.second+dy[d]] = dist[p.first][p.second]+1;
                    Q.push({p.first+dx[d], p.second+dy[d]});
                }
            }
        }
    }
    cout<<ans<<"\n";
    return 0;
}


#define MYDEBUG
//
#include <bits/stdc++.h>
#ifdef MYDEBUG
#define dbp(x) cout<<#x<<": "<<x<<endl
#define dbp2(x,y) cout<<#x<<","<<#y<<": "<<x<<","<<y<<endl
#define dbp3(x,y,z) cout<<#x<<","<<#y<<","<<#z<<": "<<x<<","<<y<<","<<z<<endl
#define dbp4(w,x,y,z) cout<<#w<<","<<#x<<","<<#y<<","<<#z<<": "<<w<<","<<x<<","<<y<<","<<z<<endl
#define ifcin(x) std::ifstream cin(x)
#else
#define dbp(x)
#define dbp2(x,y)
#define dbp3(x,y,z)
#define dbp4(w,x,y,z)
#define ifcin(x)
#endif
#define ll long long
#define all(x) x.begin(), x.end()
#define rep(i, from, to) for(int i=from; i<to; ++i)
#define REP(i, from, to) for(int i=from; i<=to; ++i)
using namespace std;
template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
	out << "[";
	size_t last = v.size() - 1;
	for (size_t i = 0; i < v.size(); ++i) {
		out << v[i];
		if (i != last) {
			out << ",";
		}
	}
	out << "]";
	return out;
}
template<typename T>
ostream& operator<<(ostream& out, const vector<vector<T> >& v) {
	for (size_t i = 0; i < v.size(); ++i) {
		out << v[i] << endl;
	}
	return out;
}
template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& p) {
	out << "{" << p.first << "," << p.second << "}";
	return out;
}

static const int MAX = 200000;
static const int INF = INT_MAX;
int n, a[MAX], l[MAX / 2 + 2], r[MAX / 2 + 2];
ll inversion = 0;

void merge(int arr[], int left, int right) {
	int mid = (left + right) / 2;
	int n1 = mid - left, n2 = right - mid;
	rep(i,0,n1)
	{
		l[i] = a[left + i];
	}
	rep(j,0,n2)
	{
		r[j] = a[mid + j];
	}
	l[n1] = r[n2] = INF;
	int i = 0, j = 0;
	rep(k,0,right - left)
	{
		if (l[i] <= r[j]) {
			a[left + k] = l[i++];
		} else {
			a[left + k] = r[j++];
			//Lに残った要素の数
			//==r[j]より大きい数の個数
			//==r[j]より左にあるのに大きい(反転している)数
			//各jで反転数(n1-i)を追加
			inversion += n1 - i;
		}
	}
}

void mergeSort(int arr[], int left, int right) {
	int mid = (left + right) / 2;
	if (left + 1 < right) {
		mergeSort(arr, left, mid);
		mergeSort(arr, mid, right);
		merge(arr, left, right);
	}
}

void solve() {
	scanf("%d", &n);
	rep(i,0,n)
	{
		scanf("%d", &a[i]);
	}
	mergeSort(a, 0, n);
	printf("%lld\n", inversion);
}

void printTestCase() {
	int n = 200000;
	int ins = 0;
	printf("%d\n", n);
	rep(i,0,n)
	{
		if (ins++ > 0) {
			printf(" ");
		}
		printf("%d", n - i);
	}
}

int main() {
	solve();
//	printTestCase();
	return 0;
}

#include <iostream>
#include <algorithm>
#include <cmath>
#include <complex>
#include <numeric>
#include <array>
#include <bitset>
#include <deque>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <vector>
using namespace std;
template<typename Itr>
auto merge(Itr first1, const Itr last1, Itr first2, const Itr last2, Itr result){
  Itr cpyresult=result;
  size_t ret=0;
  for(Itr cpyfirst1=first1;; ++cpyresult){
    if(cpyfirst1==last1){
      //
      for(; first2!=last2; ++first2,++cpyresult) *cpyresult=*first2;
      break;
    }
    if(first2==last2){
      //
      for(; cpyfirst1!=last1; ++cpyfirst1,++cpyresult) *cpyresult=*cpyfirst1;
      break;
    }
    if(*cpyfirst1<=*first2){
      *cpyresult=*cpyfirst1;
      ++cpyfirst1;
    } else{
      ret+=last1-cpyfirst1; //
      *cpyresult=*first2;
      ++first2;
    }
  }
  for(; result!=cpyresult; ++first1,++result) *first1=*result;
  return ret;
}
template<typename Itr>
size_t mergeSort(Itr first, Itr last, Itr result){
  if(last-first>1){
    Itr mid=first+(last-first)/2;
    size_t ret=0;
    ret+=mergeSort(first, mid, result);
    ret+=mergeSort(mid, last, result);
    return ret+merge(first, mid, mid, last, result);
  } else return 0;
}
int32_t main(){
  size_t n; cin>>n;
  vector<uint32_t> s(n),t(n); for(auto&& e:s) cin>>e;
  const size_t c=mergeSort(begin(s), end(s), begin(t));
  cout<<c<<'\n';
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define deb(x) cout<< #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define PI 3.141592653589
#define setbits(n) __builtin_popcountll(n)
const ll mod=1e9+7;

const int N=1e5+1;
ll dp[N][101][2],d,n;
string s;

ll go(ll pos, ll k, ll mx){
    if(pos==n){
        if(k==0) return 1;
        else return 0;
    }
    if(dp[pos][k][mx]!=-1)
        return dp[pos][k][mx];
    ll ans=0,lim=s[pos]-'0';
    if(mx==0) lim=9;
    for(int i=0;i<lim;i++)
        ans=(ans+go(pos+1,(k+i)%d,0))%mod;
    if(mx==1) ans=(ans+go(pos+1,(k+lim)%d,1))%mod;
    else ans=(ans+go(pos+1,(k+lim)%d,0))%mod;
    return dp[pos][k][mx]=ans;
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll T=clock();

    cin>>s;
    cin>>d;
    n=s.size();
    memset(dp,-1,sizeof(dp));
    cout<<(go(0,0,1)-1+mod)%mod;

    cerr<<"\n\nTIME: "<<(double)(clock()-T)/CLOCKS_PER_SEC<<" sec\n";
    T = clock();
    return 0;
}


#include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define debug(x) cout << "debug" << x << endl;
using namespace std;

int main(){
    int n;
    while(cin >> n){
        range(i,1,n+1){
            int x = i;
            if(x % 3 == 0) cout << " " << x;
            else{
                while(x){
                    if(x % 10 == 3){
                         cout << " " << i;
                         break;
                    }
                    x /= 10;
                }
            }
        }
        cout << endl;
    }
}

// AOJ ITP 1_5_D
#include<cstdio>

void call( int n )
{
	int i = 1;
	do
	{
		int x = i;

		if( x % 3 == 0 )
			printf( " %d", i );
		else
		{
			do
			{
				if( x % 10 == 3 )
				{
					printf( " %d", i );
					break;
				}
				x /= 10;
			} while( x );
		}
	}while( ++i <= n );

	puts("");

	return;
}

int main()
{
	int n;
	scanf( "%d", &n );
	call( n );

	return 0;
}


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

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

  long long num, max, a, b=0;
  cin >> num >> max;
  vector<long long> list2;
  for (long long i=0; i<num; i++) {cin >> a; list2.push_back(a);}
  sort(list2.begin(),list2.end());
  reverse(list2.begin(),list2.end());
  for (long long i=0; i<max; i++) {b+=list2[i];}
  cout << b;}

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

#define ll long long int
#define pi 3.141592653589793238
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define mod 1000000007

ll ceil1(ll n,ll m){if(n%m==0){return n/m;}return n/m +1;}
/*
ll factor(ll n){
    vll ans;
	if(n==2)ans.pb(2);
	else
    {for(ll i=2;i*i<=n;i++){if(n%i==0 && n%i!=i){ans.pb(i);if(n/i!=i)ans.pb(n/i);}}ans.pb(n);}
    return ans.sz+1;
}
ll fact(ll n){ll res = 1;for (ll i = 2; i <= n; i++)res = res * i;
    return res; }

ll nCr(ll n, ll r){ return fact(n) / (fact(r) * fact(n - r)); }

void dfs(vllv &v,ll k,vll &par,vll &dis,ll p)
{
    par[k]=p;
    dis[k]=dis[p]+1;
    for(ll i=0;i<v[k].sz;i++){
        if(v[k][i]!=p){
            dfs(v,v[k][i],par,dis,k);
        }
    }
}
bool sortbysec(const pair<string,int> &a,const pair<string,int> &b){return (a.second<b.second);}

ll pow2(ll n,ll k){ll ans=1;
    while(k>0){
        if(k%2==1)ans=ans*n;n=n*n;k/=2;
    }return ans;}
bool prime(int n){
    int i,j;
    for(i=2;i<=sqrt(n);i++){
        if(n%i==0){
            return false;
        }
    }
    return true;
}
*/
void solve()
{
	ll n,k;
	cin>>n>>k;
	ll a[n];
	for(ll i=0;i<n;i++)
		cin>>a[i];
	sort(a,a+n);
	ll ans=0;
	for(ll i=n-1;i>=0;i--)
	{
		ans+=a[i];
		k--;
		if(k==0)
		{
			cout<<ans;
			return;
		}
	}
}
int main()
{
	fast;
    ll q;
    q=1;
    // cin>>q;
    while(q--)
    {
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

struct Kruskal {
    Kruskal() = default;

    explicit Kruskal(int n) { init(n); }

    struct UnionFind {
        vector<int> p, r;

        UnionFind() = default;

        UnionFind(int size) { init(size); }

        void init(int size) {
            p.resize(size);
            r.resize(size);
            for (int i = 0; i < size; ++i) {
                p[i] = i;
                r[i] = 1;
            }
        }

        int root(int x) {
            if (p[x] == x) return x;
            else return p[x] = root(p[x]);
        }

        void unite(int x, int y) {
            x = root(x);
            y = root(y);

            if (x == y) return;

            if (r[x] > r[y]) swap(x, y);
            r[x] += r[y];
            p[y] = x;
        }

        bool same(int x, int y) {
            return root(x) == root(y);
        }
    };

    struct edges {
        edges() = default;

        edges(int u, int v, long long cost) : u(u), v(v), cost(cost) {}

        bool operator<(const edges &e) const {
            return cost < e.cost;
        }

        int u, v;
        long long cost;
    };

    struct edge {
        edge() {}

        edge(int to, long long cost) : to(to), cost(cost) {}

        int to;
        long long cost;
    };

    vector<edges> E;
    vector<vector<edge>> G;

    void init(int n) {
        G.resize(n + 1);
    }

    void add(int u, int v, long long cost) {
        E.emplace_back(edges(u, v, cost));
    }

    void make_graph(edges e) {
        G[e.u].push_back(edge(e.v, e.cost));
        G[e.v].push_back(edge(e.u, e.cost));
    }

    long long kruskal(int n) {
        long long res = 0;
        UnionFind uf(n + 1);

        sort(E.begin(), E.end());
        for (auto &e : E) {
            if (!uf.same(e.u, e.v)) {
                res += e.cost;
                uf.unite(e.u, e.v);
                make_graph(e);
            }
        }

        return res;
    }

    long long kruskal() {
        return kruskal(G.size());
    }
};

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

    auto g = Kruskal(n);
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;

        g.add(a, b, c);
    }

    cout << g.kruskal() << endl;
}

#include <iostream>
#include <vector>
#include <algorithm>
#include<string>
using namespace std;

struct Edge{
	int P1;
	int P2;
	int Wei;
};

int main(void){

	vector<Edge> EdgeCol;
	int V, E;
	cin >> V >> E;
        if (V == 1){cout << 0<<endl;return 0;}
	vector<int>Select(V,0);
	for (int i = 0; i < E; ++i){
		int s, t, w;
		cin >> s >> t >> w;
		EdgeCol.push_back(Edge{ s, t, w });
	}
	sort(EdgeCol.begin(), EdgeCol.end(), [](Edge E1, Edge E2){return E1.Wei > E2.Wei; });
	int WeiSum = 0;
	int a=1;
	while (1){
		int aP1 = Select[EdgeCol.back().P1];
		int aP2 = Select[EdgeCol.back().P2];
		if (aP1 && aP2){
			if (aP1 != aP2){
				replace(Select.begin(), Select.end(), aP2, aP1);
				WeiSum += EdgeCol.back().Wei;
			}
		}
		else if (!aP1 && !aP2){
			Select[EdgeCol.back().P1] = a;
			Select[EdgeCol.back().P2] = a;
			a++;
			WeiSum += EdgeCol.back().Wei;
		}
		else{
			Select[EdgeCol.back().P1] = max(aP1, aP2);
			Select[EdgeCol.back().P2] = max(aP1, aP2);
			WeiSum += EdgeCol.back().Wei;
		}
		EdgeCol.pop_back();
		if (all_of(Select.cbegin(), Select.cend(), [=](int s){return s == *Select.cbegin();})){
			cout << WeiSum << endl;
			return 0;
		}
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;
constexpr int INF = 2e9;
int main() {
  int n, q;
  string s;
  cin >> n >> s >> q;
  while (q--) {
    int k;
    cin >> k;
    ll a = 0, b = 0, c = 0;
    ll res = 0;
    for (int i = 0; i < n; i++) {
      if (i >= k) {
        if (s[i - k] == 'D') a--, c -= b;
        if (s[i - k] == 'M') b--;
      }
      if (s[i] == 'D') a++;
      if (s[i] == 'M') b++, c += a;
      if (s[i] == 'C') res += c;
    }
    cout << res << endl;
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep2(i, m, n) for(int i=int(m); i<int(n); ++i)
#define rep(i, n) rep2(i, 0, n)
using ll = long long;
using V = vector<int>;
using Vll = vector<ll>;
using VV = vector<V>;
using VVll = vector<Vll>;

char s[1000000];
ll t[3][1000001];

int main() {
  int n, q;
  scanf("%d%s%d", &n, s, &q);

  V D, C;
  rep(i, n) {
    rep(j, 3) t[j][i+1] = t[j][i];
    switch(s[i]){
      case 'D':
        ++t[0][i+1];
        D.push_back(i);
        break;
      case 'M':
        ++t[1][i+1];
        break;
      case 'C':
        ++t[2][i+1];
        C.push_back(i);
        break;
    }
  }

  rep(_, q) {
    int k;
    scanf("%d", &k);
    ll ans = 0;
    for (auto &c : C) ans += t[1][c] * (t[0][c] - t[0][max(c-k+1, 0)]);
    for (auto &a : D) ans -= t[1][a+1] * (t[2][min(a+k, n)] - t[2][a+1]);
    printf("%lld\n", ans);
  }
  return 0;
}


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

#define rep(i, n) for(long long i=0;i<(long long)(n);i++)
#define rep2(i, s, n) for(long long i=(s);i<(long long)(n);i++)
#define repi(i, n) for(int i=0;i<(int)(n);i++)
#define rep2i(i, s, n) for(int i=(s);i<(int)(n);i++)
#define all(v) v.begin(), v.end()

#define deg2rad(deg) (((deg)/360)*2*M_PI)
#define rad2deg(rad) (((rad)/2/M_PI)*360)

using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;

const ll INF = (1LL<<60);
const int INFi = (1<<29);

/*素数判定*/
bool is_prime(ll n){
    if(n==1) return false;
    for(ll i=2;i*i<=n;i++){
        if(n%i==0) return false;
    }
    return true;
}

/*約数列挙*/
vll enum_divisors(ll n){
    vll l;
    for(ll i=1;i*i<=n;i++){
        if(n%i==0){
            l.push_back(i);
            if(n/i != i) l.push_back(n/i);
        }
    }
   sort(all(l));
   return l;
}

/*素因数分解*/
vector<P> prime_factorize(ll n){
    vector<P> l;
    for(ll i=2;i*i<=n;i++){
        if(n%i!=0) continue;
        ll e = 0;
        while(n%i==0){
            e++;
            n /= i;
        }
        l.push_back({i, e});
    }
    if(n!=1) l.push_back({n, 1});
    return l;
}

/*最小公倍数*/
ll lcm(ll a, ll b){
    return a*b/__gcd(a,b);
}

/*最大公約数*/
ll gcd(ll a, ll b){
    return __gcd(a,b);
}

/*組み合わせ(Combination)*/
const ll CMAX = 1010000;
const ll CMOD = 1e9+7;
ll fac[CMAX], finv[CMAX], inv[CMAX];
// テーブルを作る前処理
void combinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (ll i = 2; i < CMAX; i++) {
		fac[i] = fac[i - 1] * i % CMOD;
		inv[i] = CMOD - inv[CMOD%i] * (CMOD / i) % CMOD;
		finv[i] = finv[i - 1] * inv[i] % CMOD;
	}
}
// 二項係数計算
ll comb(ll n, ll k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % CMOD) % CMOD;
}

int main(){

    ll n, q; cin >> n >> q;
    string s; cin >> s;
    vll l(q), r(q); rep(i, q) cin >> l[i] >> r[i];

    vll a(n, 0);
    a[0] = 0;

    bool b = false;
    if(s[0]=='A') b = true;

    rep2(i, 1, n){
        a[i] = a[i-1];
        if(b && s[i]=='C'){
            a[i]++;
        }

        if(s[i]=='A'){
            b = true;
        }
        else{
            b = false;
        }
    }

    rep(i, q){
        cout << a[r[i]-1]-a[l[i]-1] << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
const int inf = 1001001001;

int main(){

    ll n, q;
    string s;
    cin >> n >> q >> s;
    vector<ll> dp(n, 0);
    for(ll i = 0; i < n-1; i++){
        if(s[i] == 'A' && s[i+1] == 'C'){
            dp[i+1] = (dp[i] + 1);
        }
        else{
            dp[i+1] = dp[i];
        }
    }

    while(q){
        ll l,r;
        cin >> l >> r;
        cout << dp[r-1] - dp[l-1] << endl;
        q--;
    }

}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int main()
{
	int i = 0;
	char a[20];

	scanf("%s", a);

	while (1) {
		i++;
		if (a[i] == '\0') break;
	}
	i--;
	while (i != -1) {
		printf("%c", a[i]);
		i--;
	}
	printf("\n");

		return (0);

}

#include <algorithm>
#include <iostream>
#include <string>

int main() {
  std::string input;
  std::getline(std::cin, input);
  std::reverse(input.begin(), input.end());
  std::cout << input + '\n';
  return 0;
}


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

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

    vector<int> divisor_list(0);
    for (int i = 1; i * i <= M; i++){
        if (M % i == 0){
            divisor_list.push_back(i);
            divisor_list.push_back(M / i);
        }
    }
    sort(all(divisor_list));
    int len = divisor_list.size();
    int ans = 1;
    rep(i,len){
        if (M / divisor_list[i] >= N){
            ans = divisor_list[i];
        }
    }
    cout << ans << endl;
}

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

int main() {

  int a,b;
  cin >> a >> b;

  if(a%b == 0) cout << 0 << endl;
  else cout << 1 << endl;

}


#include <algorithm>
#include <iostream>
#include <limits.h>
#include <string>
#include <vector>

#define el endl
#define fd fixed

using namespace std;

class Point
{
private:
  int m_x;
  int m_y;
public:
  void setX(int x);
  void setY(int y);
  int getX();
  int getY();

  Point(int x, int y) {
    setX(x);
    setY(y);
  }
};

void Point::setX(int x) {
  m_x = x;
}

void Point::setY(int y) {
  m_y = y;
}

int Point::getX() {
  return m_x;
}

int Point::getY() {
  return m_y;
}

int main() {
  int N, x, y, M, l, rx, ry;
  string d;
  while (1) {
    cin >> N;
    vector<Point> gems;
    if (N == 0) break;
    for (int i = 0; i < N; i++) {
      cin >> x >> y;
      gems.push_back(Point(x, y));
    }
    cin >> M;
    rx = 10;
    ry = 10;
    while (M--) {
      cin >> d >> l;
      while (l--) {
        if (d == "N") ry++;
        if (d == "E") rx++;
        if (d == "S") ry--;
        if (d == "W") rx--;
        for (int i = 0; i < gems.size(); i++) {
          if (rx == gems[i].getX() && ry == gems[i].getY()) {
            gems.erase(gems.begin()+i);
            break;
          }
        }
      }
    }
    if (gems.empty()) cout << "Yes" << el;
    else cout << "No" << el;
  }
}


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

int a[100005],b[100005],c[100005];
vector<int>d[100006];

void dfs(int v,int s) {
	a[v]=a[s]+1;
	b[v]=1;
	c[v]=s;
	for(auto x:d[v]) {
		if(x==s) continue;
		dfs(x,v);
		b[v]=max(b[v],b[x]+1);
	}

}
int main(){
    int n,u,v;
    cin>>n>>u>>v;
	for(int i=1;i<n;i++) {
		int x,y;
		cin>>x>>y;
		d[x].push_back(y);
		d[y].push_back(x);
	}
	dfs(v,0);
	int ans=-1;
	int dis=a[u]-a[v];

	dis=(dis-1)/2;
//	cout<<dis<<endl;
	while(dis--){
	    u=c[u];
	}
	if(d[u].size()==1)
	     ans=a[u]-2;
	else
	    ans=b[u]+a[u]-3;

	cout<<ans<<endl;

}

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair <ll,ll> P;
#define REP(i, n) for(ll i = 0LL; i < (ll)(n); i++)
#define REPR(i, n) for(ll i = n-1; i >= 0LL; i--)
#define FOR(i, n, m) for(ll i = n; i < (ll)(m); i++)
#define PRINT(x) cout << x << endl
#define ALL(v) v.begin(), v.end()
#define INF (ll)1e18
#define INFD 1e18
#define PI 3.14159265358979
#define MOD 1000000007
// #define MOD 998244353
#define VL vector<ll>

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

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

template< typename T >
struct edge {
	int src, to;
	T cost;

	edge(int to, T cost) : src(-1), to(to), cost(cost) {}

	edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

	edge &operator=(const int &x) {
		to = x;
		return *this;
	}

	operator int() const { return to; }
};

template< typename T >
using Edges = vector< edge< T > >;
/*
WeightedGraph<ll> g(V);
g[a].emplace_back(b, c);
*/
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;

template< typename T >
void dijkstra(WeightedGraph< T > &g, vector< T > &dist, vector< T > &prev, vector< T > &num, int s) {
	T N = g.size();
	dist.resize(N, INF);
	prev.resize(N, -1); // 初期化
	num.resize(N, 0);
	using Pi = pair< T, int >;
	priority_queue< Pi, vector< Pi >, greater< Pi > > que;
	dist[s] = 0;
	que.emplace(dist[s], s);
	while(!que.empty()) {
	T cost;
	int idx;
	tie(cost, idx) = que.top();
	que.pop();
	if(dist[idx] < cost) continue;
	for(auto &e : g[idx]) {
		auto next_cost = cost + e.cost;
		if(dist[e.to] < next_cost) continue;
		if (dist[e.to] == next_cost) {
			num[e.to] += num[idx];
			num[e.to] %= MOD;
			continue;
		}
		dist[e.to] = next_cost;
		prev[e.to] = idx; // 頂点 v を通って e.to にたどり着いた
		num[e.to] = num[idx];
		que.emplace(dist[e.to], e.to);
	}
	}
	return;
}

/* get_path(prev, t)
	入力：dijkstra で得た prev, ゴール t
	出力： t への最短路のパス
*/
template< typename T >
vector<T> get_path(const vector<T> &prev, T t) {
	vector<T> path;
	for (T cur = t; cur != -1; cur = prev[cur]) {
		path.push_back(cur);
	}
	reverse(path.begin(), path.end()); // 逆順なのでひっくり返す
	return path;
}

int main()
{
	ll N, u, v;
	cin >> N >> u >> v;
	u--; v--;
	if (N == 2) {
		PRINT(0);
		return 0;
	}
	WeightedGraph<ll> G(N);
	REP(i, N-1) {
		ll a, b;
		cin >> a >> b;
		a--; b--;
		G[a].emplace_back(b,1);
		G[b].emplace_back(a,1);
	}
	vector<ll> dist1, prev1, num1;
	dijkstra(G,dist1,prev1,num1,u);
	vector<ll> dist2, prev2, num2;
	dijkstra(G,dist2,prev2,num2,v);
	// REP(i, N) cout << dist1[i] << " ";
	// cout << endl;
	// REP(i, N) cout << dist2[i] << " ";
	// cout << endl;
	ll ma = 0;
	REP(i, N) {
		if (dist1[i] < dist2[i]) {
			ma = max(ma, dist2[i]);
		}
	}
	PRINT(ma-1);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define int long long
#define fr(i,a,b) for(int i = a ; i <= b ; ++i)
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()
#define pb push_back
#define fst first
#define snd second
using namespace __gnu_pbds;
using ordered_set =
 tree<int, null_type,less<int>,
 rb_tree_tag,tree_order_statistics_node_update>;
//find_by_order(k):returns iterator to kth element starting from 0
//order_of_key(k):returns count of elements strictly smaller than k
typedef long long ll;typedef pair<int, int> pii;
typedef vector<int> vi;typedef long double ld;
template<class T>
using min_heap = priority_queue<T,vector<T>,greater<T>>;
template<typename T> T gcd(T a, T b){return(b?__gcd(a,b):a);}
template<typename T> T lcm(T a, T b){return(a*(b/gcd(a,b)));}
template<typename T>
void remdup(vector<T>& v) {
	sort(all(v)); v.erase(unique(all(v)), v.end());
}
template<typename T> void re(vector<T> &v) {trav(i,v) cin >> i;}
void unsyncIO() { ios_base::sync_with_stdio(0); cin.tie(0); }
#ifdef np
#include "C:\Users\navodit\Desktop\mycodes\pr.h"
#else
#define trace(...)
#endif

#define MOD 1000000007

void solve(){
	int n ; cin >> n ;
	int mx = 0 ;
	for(int  i = 1 ; i*i <= n ;++i ){
		mx = max(mx,i*i);
	}
	cout << mx << endl;
}
signed main() {
	unsyncIO();
	int tt =1 ;
	// cin >> tt;
	rep(i,0,tt) solve();

#ifdef np
	cout <<endl<<endl<< "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;

#define REP(i,e,s) for(register int i=e; i<=s; i++)
#define DREP(i,e,s) for(register int i=e; i>=s; i--)
#define ll long long
#define DE(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG(a) DE("DEBUG: %d\n",a)

#define int long long

int read() {
	int x=0,f=1,ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
const int MOD=1e9+7,MAXN=200000+10;

int fac[MAXN],inv[MAXN];

int qpow(int a,int b) {
	int ans=1;
	while(b) {
		if(b&1) ans=(ans*a)%MOD;
		a=(a*a)%MOD;
		b>>=1;
	}
	return ans;
}

int c(int m,int n) {
	if(n==m||!m||!n) return 1;
	return ((fac[m]*inv[n]%MOD)*inv[m-n])%MOD;
}

int calc(int x_1,int y_1,int x_2,int y_2) {
	return c(abs(x_2-x_1)+abs(y_2-y_1),abs(x_2-x_1));
}

signed main() {
	int h=read(),w=read(),a=read(),b=read();
	fac[0]=1;
	REP(i,1,h+w) fac[i]=(fac[i-1]*i)%MOD;

	REP(i,1,h+w) inv[i]=qpow(fac[i],MOD-2);
	int ans=0;
	REP(i,1,h-a) {
		ans=(ans+calc(1,1,b,i)*(calc(b+1,i,w,h)))%MOD;
	}

	printf("%lld\n",ans%MOD);
	return 0;
}


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

int main() {
  int N, K;
  cin >> N >> K;
  vector<double> p(N+1, 0);
  int x;
  double val;

  for(int i=1; i<=N; i++){
    cin >> x;
    val = x*(x+1) * 0.5 / x;
    p.at(i) = p.at(i-1) + val;
  }

  double maxi=0;
  for(int i=0; i<N-K+1; i++){
    maxi = max(maxi, p.at(K+i) - p.at(i));
  }
  printf("%.6f\n", maxi);
  return 0;
}

//...Bismillahir Rahmanir Rahim...
// Code by    Asad Bin Saber

#include <bits/stdc++.h>

using namespace std;

// typedefs...
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;

// constants...
const double PI = acos(-1);
const int Mod = 1e9+7; // 998244353;
const int MXS = 2e5+5;
const int MXI = 1e9+5;
const ll MXL = 1e15+5;
const int INF = 1e5+5;

// defines...
#define MP make_pair
#define PB push_back
#define fi first
#define se second
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)

// chess moves...
// int dx[] = {+0, +0, -1, +1}; ///Up-down, Left-Right
// int dy[] = {+1, -1, +0, +0};
// int dx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's Move
// int dy[] = {-1,+1,+0,+0,+1,+1,-1,-1};
// int dx[] = {-2, -2, -1, -1,  1,  1,  2,  2}; ///Knight's Move
// int dy[] = {-1,  1, -2,  2, -2,  2, -1,  1};

// structs...
struct hash_pair {
    template <class T1, class T2>
    size_t operator()(const pair<T1, T2>& p) const
    {
        auto hash1 = hash<T1>{}(p.first);
        auto hash2 = hash<T2>{}(p.second);
        return hash1 ^ hash2;
    }
};

// functions...
ll gcd(ll a, ll b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}
ll lcm(ll a, ll b)
{
    return (a/gcd(a, b)*b);
}
ll comb(ll a, ll b, ll M)
{
    ll x = max(a-b, b), ans=1;

    for(ll K=a, L=1; K>=x+1; K--, L++){
        ans *= K;
        ans /= L;
    }

    return ans % M;
}

void task()
{
    //code here...

    int n, ans, mn = MXI;

    cin >> n;
    int ara[n];

    for(int K=0; K<n; K++) cin >> ara[K];
    ans = n;

    for(int K=0; K<n; K++){
		mn = min(mn, ara[K]);

		if(mn<ara[K]) ans--;
	}

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

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

    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);

    task();

    return 0;
}


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

int main() {

	int n; cin >> n;
	if (n == 2) {
		int o, e; cin >> o >> e;
		if (o == e) cout << 1 << endl;
		else cout << 0 << endl;
		return 0;
	}
	const int p = n / 2;
	const int z = 100001;
	vector<int> o_num(z, 0), e_num(z, 0);
	for (int i = 0; i < n; i++) {
		int v; cin >> v;
		if (i % 2 == 0) o_num[v]++;
		else e_num[v]++;
	}
	int o_firstCnt = 0;
	int o_secondCnt = 0;
	int o_firstCntValue = 0;
	int e_firstCntValue = 0;
	int e_firstCnt = 0;
	int	e_secondCnt = 0;
	for (int i = 0; i < z; i++) {
		if (o_firstCnt <= o_num[i]) {
			o_firstCntValue = i;
			o_firstCnt = o_num[i];
		}
		if (e_firstCnt <= e_num[i]) {
			e_firstCntValue = i;
			e_firstCnt = e_num[i];
		}
	}
	sort(o_num.rbegin(), o_num.rend());
	sort(e_num.rbegin(), e_num.rend());
	o_firstCnt = o_num[0];
	o_secondCnt = o_num[1];
	e_firstCnt = e_num[0];
	e_secondCnt = e_num[1];
	int ans;
	if (o_firstCntValue != e_firstCntValue) {
		ans = n - o_firstCnt - e_firstCnt;
	}
	else { // o_firstCntValue == e_firstCntValue
		ans = min(n - o_secondCnt - e_firstCnt, n - o_firstCnt - e_secondCnt);
	}
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, v;
  cin >> n;
  int* ohash = (int*) calloc(100000, sizeof(int));
  int* ehash = (int*) calloc(100000, sizeof(int));
  for (int i = 0; i < n/2; i++) {
    cin >> v;
    ohash[v - 1]++;
    cin >> v;
    ehash[v - 1]++;
  }
  int omax = 0, emax = 0;
  for (int i = 0; i < 100000; i++) {
    if (ohash[i] > ohash[omax]) omax = i;
    if (ehash[i] > ehash[emax]) emax = i;
  }
  if (omax != emax) {
    cout << n - ohash[omax] - ehash[emax];
  }
  else {
    int omax2 = 0, emax2 = 0;
    for (int i = 0; i < 100000; i++) {
      if (ohash[i] > ohash[omax2] && i != omax) omax2 = i;
      if (ehash[i] > ehash[emax2] && i != emax) emax2 = i;
    }
    if (ohash[omax] == n/2 && ehash[emax] == n/2) {
      cout << n/2;
    }
    else if (ohash[omax] == n/2) {
      cout << n/2 - ehash[emax2];
    }
    else if (ehash[emax] == n/2) {
      cout << n/2 - ohash[omax2];
    }
    else {
      cout << min(n - ohash[omax] - ehash[emax2], n - ohash[omax2] - ehash[emax]);
    }
  }
  free(ohash);
  free(ehash);
  return 0;
}


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

void solve(){
    ll n;
    cin>>n;
    ll rem, num = 26;
    string str="";

        while(n != 0){
            // cout<<1<<endl;
            rem = n%26;
            if(rem == 0){
                str += 'z';
                n = n/26;
                n--;
            }
            else{
                rem--;
                n = n/26;
                char ch = 'a';
                ch += rem;
                str += ch;
            }

            // n;
        }
    reverse(str.begin(),str.end());
    cout<<str<<endl;

}

int main(){
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int q;
    q=1;
    // cin>>q;
    while(q--) solve();

    return 0;
}

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

int main() {
  int n;
  cin >> n;
  ll ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (i % 3 && i % 5) ans += i;
  }
  cout << ans << endl;
  return 0;
}


using namespace std;
#include <bits/stdc++.h>
int main(){
    int a,b;cin>>a>>b;
    if(a<=8&&b<=8)std::cout << "Yay!" << std::endl;
    else
    std::cout << ":(" << std::endl;
}

// Sakhiya07 - Yagnik Sakhiya

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

#define ll long long int
#define ld long double

#define pll pair<ll,ll>
#define ff first
#define ss second

#define pb push_back
#define all(x) x.begin(),x.end()

#define MOD  1000000007
const int N = 100005;

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);srand(time(NULL));

void solve()
{
    ll l,r;
    cin >> l >> r;
    ll dif = (r - l + 1);
    if(dif>=2020)
    {
        cout<<"0\n";
    }
    else
    {
        ll ans = 1e18;
        for(ll i=l;i<=r;i++)
        {
            for(ll j=i+1;j<=r;j++)
            {
                ll temp = i*j;
                temp %= 2019;
                ans = min(ans,temp);
            }
        }
        cout<<ans;
    }
}

int main()
{
    FAST;
    int t = 1;
 // cin >> t;
    for(int i=1;i<=t;i++)
    {
        solve();
    }
}


#include <iostream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <list>
#include <set>
#include <unordered_set>
#include <vector>
#include <utility>
#include <algorithm>
#include <queue>
#include <cstdint>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

constexpr ll inf = INT64_MAX / 4;
constexpr double pi = asin(1) * 2;
constexpr ll mod = 1000000007;

pair<ll, ll> extgcd(ll a, ll b) {
    if (b == 0)return make_pair(a, 0);
    auto t = extgcd(b, a % b);
    return make_pair(t.second, t.first - t.second * (a / b));
}

ll invmod(ll n) {
    return extgcd(n, mod).first;
}

class modint {
public:
    ll n;

    modint() = default;

    explicit modint(ll number) {
        n = ((number % mod) + mod) % mod;
    }

    bool operator==(modint e) {
        return n == e.n;
    }

    modint operator+(modint e) {
        return modint(n + e.n);
    }

    modint operator-(modint e) {
        return modint(n - e.n);
    }

    modint operator-(ll e) {
        return modint(n - e);
    }

    modint operator*(modint e) {
        return modint(n * e.n);
    }

    modint operator/(modint e) {
        return modint(n * invmod(e.n));
    }

};

template<class T>
T f1(T n, T m) {
    return m * m * (n * n * (n + T(1)) - (n * (n + T(1)) * (T(2) * n + T(1))) / T(3));
}

template<class T>
T f2(T n, T m) {
    return (f1(n, m) + f1(m, n)) / T(2);
}

template<class T>
T comb(T n, T k) {
    return k == T(0) ? T(1) : comb(n - T(1), k - T(1)) * n / k;
}

int main() {
    modint n, m, k;
    cin >> n.n >> m.n >> k.n;
    cout << (f2(n, m) * comb(n * m - 2, k - 2)).n << endl;
}

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

using ll = long long;

// 組合せを素数で割った値を求める
const int MAX = 510000;
const int MOD = 1e9 + 7;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++){
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

// 二項係数計算
long long COM(int n, int k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

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

  ll n, m, k;
  cin >> n >> m >> k;

  COMinit();

  ll ans = 0;
  for(int i = 1; i <= m - 1; i++){
    ll keep = (((i * n * n) % MOD) * (m - i)) % MOD;
    keep *= COM(n * m - 2, k - 2);
    keep %= MOD;
    ans += keep;
    ans %= MOD;
  }
  for(int i = 1; i <= n - 1; i++){
    ll keep = (((i * m * m) % MOD) * (n - i)) % MOD;
    keep *= COM(n * m - 2, k - 2);
    keep %= MOD;
    ans += keep;
    ans %= MOD;
  }

  cout << ans << '\n';
}


#include <cstdio>
#include <cstdlib>
#include <numeric>

using namespace std;

int main() {
  char *line = NULL;
  size_t buffer_size = 0;
  while (getline(&line, &buffer_size, stdin) > 0) {
    int ls[10];
    int v1, v2;
    sscanf(line, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
           ls, ls+1, ls+2, ls+3, ls+4, ls+5, ls+6, ls+7, ls+8, ls+9,
           &v1, &v2);
    int total_len = accumulate(ls, ls + 10, 0);
    double x1 = v1 * static_cast<double>(total_len) / (v1 + v2);
    for (int i = 0, acc = 0; i < 10; ++i) {
      acc += ls[i];
      if (x1 <= acc) {
        printf("%d\n", i + 1);
        break;
      }
    }
  }
  free(line);

  return 0;
}

#include<stdio.h>
int main(void){
	int l[10],v[2];
	int sv,sl,i;
	double n;
	while(~scanf("%d",l)){
		sv=0;sl=l[0];
		for(i=1;i<=9;i++){
			scanf(",%d",l+i);
			sl+=l[i];
		}
		for(i=0;i<2;i++){
			scanf(",%d",v+i);
			sv+=v[i];
		}
		n=sl/(double)sv;
		n*=v[0];
		for(i=0;n>0;i++)n-=l[i];
		printf("%d\n",i);
	}
	return 0;
}


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

#define ll long long int

#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define xx first
#define yy second

#define pii pair <int, int >
#define pll pair < ll, ll >
#define xx first
#define yy second
#define vi vector < int >
#define vl vector < ll >
#define vp vector < pii >

#define inf  100000000

#define f(i, c, n) for(int i = c; i < n; i++)
#define fe(i, c, n) for(int i = c; i <= n; i++)
#define fr(i, n, c) for(int i = n - 1; i >= c; i--)

#define be(a) a.begin(), a.end()
#define vsort(a) sort(a.begin(), a.end())
#define mem(a) memset(a, 0, sizeof(a))
#define memn(a) memset(a, -1, sizeof(a))
#define srti(a, n) sort(a, a + n)
#define srtd(a, n) sort(a, a + n, greater<int>())
#define LB(a, x) (lower_bound(vbe(a), x) - a.begin())
#define UB(a, x) (upper_bound(vbe(a), x) - a.begin())
#define S " "
#define N "\n"

//cout << "Case " << ++cs << ": " <<
#define fast ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define FILE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

void solve() {
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	int mn = min(a, b);
	mn += min(c, d);
	cout << mn << N;
}

int main() {
    // FILE;
    fast;
    // int t; cin >> t; while(t--)
    solve();
}

#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
#define ref goto refrain

class dice{
	public:
	int o,u,s,e,n,w;//over,under,south,east,north,west
	void change(char a);
};
void dice::change(char a){
	int tem=o;
	if(a=='S'){
		o=n;	n=u;	u=s;	s=tem;
	}
	else if(a=='E'){
		o=w;	w=u;	u=e;	e=tem;
	}
	else if(a=='N'){
		o=s;	s=u;	u=n;	n=tem;
	}
	else if(a=='W'){
		o=e;	e=u;	u=w;	w=tem;
	}
	else{
		cout<<"Error!"<<endl;
		exit(0);
	}
}

int main(){
	dice a;
	cin>>a.o>>a.s>>a.e>>a.w>>a.n>>a.u;
	char com[100];
	cin>>com;
	int m=strlen(com);
	for(int i=0;i<m;i++){
		a.change(com[i]);
	}
	cout<<a.o<<endl;
	return 0;
}