#include<bits/stdc++.h>
using namespace std;
using lli = long long;
#define rep(i,n) for(int i=0;i<n;i++)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class T>
ostream &operator<<(ostream &os, const vector<T> &x){
    os << "{";
    for(size_t i = 0; i < x.size(); i++){
        if(i < x.size()-1) os << x[i] << ", ";
        else os << x[i];
    }
    os << "}";
    return os;
}

int main(void){
    int n;
    cin >> n;
    vector<int> t(n);
    rep(i, n) cin >> t[i];
    vector<unordered_map<int, int>> dp(n+1);
    dp[0][0] = 1;
    const int INF = 1e5;

    rep(i, n){
        for(int j = -INF; j <= INF; j++){
            if(dp[i][j] == 0) continue;
            if(j-t[i] >= -INF) chmax(dp[i+1][j-t[i]], dp[i][j]);
            if(j+t[i] <= INF) chmax(dp[i+1][j+t[i]], dp[i][j]);
        }
    }
    int ans = INF;
    for(int j = -INF; j <= INF; j++){
        if(dp[n][j] == 1) chmin(ans, abs(j));
    }
    int sum = 0;
    rep(i, n) sum+=t[i];
    ans = (ans+sum)/2;
    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>

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;
}

std::vector<std::string> split(const std::string& source, const char delim);

int main(){
    std::string buffer;
    std::getline(std::cin, buffer);
    const int NUM_DISH = std::stoi(buffer);

    std::getline(std::cin, buffer);
    auto splitCostTime = split(buffer, ' ');
    std::vector<int> costTimeList;
    for(const auto& costTime : splitCostTime){
        costTimeList.emplace_back(std::stoi(costTime));
    }

    std::array<std::unique_ptr<int[]>, 101> dp;
    for(int i = 0; i < NUM_DISH + 1; ++i){
        dp[i] = std::make_unique<int[]>(101010);
        for(int t = 0; t < 101010; ++t){
            dp[i][t] = INT_MAX;
        }
    }
    dp[0][0] = 0;
    for(int i = 0; i < NUM_DISH; ++i){
        for(int t = 0; t < 101010; ++t){
            if(dp[i][t] != INT_MAX){
                chmin(dp[i + 1][t + costTimeList[i]], dp[i][t]);
                chmin(dp[i + 1][t], dp[i][t] + costTimeList[i]);
            }
        }
    }

    int ans = INT_MAX;
    for(int t = 0; t < 101010; ++t){
        chmin(ans, std::max(t, dp[NUM_DISH][t]));
    }
    std::cout << ans << std::endl;

    return 0;
}

std::vector<std::string> split(const std::string& source, const char delim){
    std::vector<std::string> splitData;
    std::stringstream sstream(source);
    std::string itemBuffer;

    while(std::getline(sstream, itemBuffer, delim)){
        splitData.emplace_back(itemBuffer);
    }

    return splitData;
}


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

ll n;
vector<ll> x, y, z;
ll dp[1ll<<18][20];
const ll INF = 1ll<<60;
ll rec(ll s, ll v){
    if(dp[s][v] != 0) return dp[s][v];
    if(s == (1<<n) - 1 && v==0) return 0;

    ll ret = INF;
    for(ll nv = 0; nv < n; ++nv){
        if(s & (1<<nv)) continue;

        ret = min(ret, rec((s|(1<<nv)), nv) + abs(x[v]-x[nv]) + abs(y[v]-y[nv]) + max(0ll, z[nv]-z[v]));
    }

    return dp[s][v] = ret;
}

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

    cin >> n;
    x.resize(n);
    y.resize(n);
    z.resize(n);
    rep(i, n) cin >> x[i] >> y[i] >> z[i];

    cout << rec(0, 0) << endl;

    return 0;
}


#include<bits/stdc++.h>

#define ll long long
#define sameer ios_base::sync_with_stdio(false);cin.tie(NULL);

using namespace std;

int main() {
     sameer;
     ll n; cin>>n;
     ll a[n],ans=0;
     for(int i=0; i<n; i++) cin>>a[i];
     map<int, int> m;
     for(int i=0; i<n; i++) {
          ans= ans + m[a[i]-(a[i]/200)*200]++;
     }
     cout<<ans;
     return 0;
}

#include<iostream>
using namespace std;
int main()
{
	long long n,k;
	cin>>n>>k;
	while(k--)
	{
		(n%200==0)?n/=200:n=n*1000+200;
	}
	cout<<n;
	return 0;
}


#include <iostream>
#include <cmath>
#include <climits>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <stack>
#include <bitset>
#include <functional>
#include <random>
#include <cassert>

typedef long long ll;
using namespace std;

constexpr ll mod = 1000000007;
constexpr ll mod998 = 998244353;

int N, F[200000], T[200000], cnt;

int par(int x) {
	if (T[x] == x)return x;
	else return T[x] = par(T[x]);
}

void uni(int x, int y) {
	x = par(x);
	y = par(y);
	T[x] = y;
}

int main() {
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> F[i];
		F[i]--;
		T[i] = i;
	}
	for (int i = 0; i < N; i++) {
		uni(i, F[i]);
	}
	for (int i = 0; i < N; i++) {
		if (i == T[i])cnt++;
	}
	ll res = 1;
	for (int i = 0; i < cnt; i++) {
		res *= 2;
		res %= mod998;
	}
	cout << (res - 1 + mod998) % mod998 << endl;
}

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
constexpr int inf=1e9+7;
constexpr ll longinf=1LL<<60 ;
constexpr ll mod=998244353 ;

struct UnionFind{
   vector<int> par;
   UnionFind(int n):par(n,-1){}
   int find(int x){
       if(par[x]<0)return x;
       return par[x]=find(par[x]);
   }
   bool unite(int x,int y){
       x=find(x);
       y=find(y);
       if(x==y)return false;
       if(par[x]>par[y]){
           par[y]+=par[x];
           par[x]=y;
       }
       else{
           par[x]+=par[y];
           par[y]=x;
       }
       return true;
   }

   bool same(int x,int y){
       return find(x)==find(y);
   }
   int size(int x){
       return -par[find(x)];
   }
};
int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    UnionFind uf(n);
    rep(i,n){
        int x;
        cin>>x;
        uf.unite(i,x-1);
    }
    ll ans = 1;
    rep(i,n){
        if(uf.find(i)==i)ans=ans*2%mod;
    }
    cout<<ans-1<<endl;
    return 0;
}


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

int main() {
  cin.tie(nullptr); // Do not flush "cout" when processing "cin".

  string x;
  ll m;
  cin >> x >> m;

  // コーナーケース
  if ((int)x.size() == 1) {
    if (stoi(x) <= m) {
      cout << 1 << endl;
    } else {
      cout << 0 << endl;
    }
    return 0;
  }

  // x に含まれる最も大きい数字 d
  int d = 0;
  for (char c : x) {
    d = max(d, c - '0');
  }

  // 二分探索
  ll ac = d;     // 下限
  ll wa = m + 1; // 上限

  while (wa - ac > 1) {
    // wj = waiting judge
    ll wj = (ac + wa) / 2;

    ll v = 0;
    for (char c : x) {
      // v * wj > m を超えてはいけない
      // 両辺に wj を掛ける
      //
      //   v > (m / wj)
      if (v > (m / wj)) {
        // (v > m) を満たすように、v に適当な値を代入する。
        v = m + 1;
      } else {
        // 文字列 x を reverse する必要がない。
        //
        //   1桁目 --> 0 * wj + (c - '0')
        //   2桁目 --> (c - '0') * wj + (c - '0')
        //   3桁目 --> (c - '0') * wj^2 + (c - '0') * wj^1 + (c - '0')
        v = v * wj + (c - '0');
      }
    }
    if (v <= m) {
      ac = wj;
    } else {
      wa = wj;
    }
  }

  cout << ac - d << endl;
  return 0;
}

#include"bits/stdc++.h"
using namespace std;
using ll=long long;
template<class T=ll>inline T in(istream&is=cin){T ret;is>>ret;return ret;}
template<class T,class Size=typename vector<T>::size_type>inline auto vector_2d(Size h,Size w,T v){return vector<vector<T>>(h,vector<T>(w,v));}
template<class P>struct comp_pair_by_second{bool operator()(const P&p1,const P&p2){return p1.second<p2.second||(p1.second==p2.second&&p1.first<p2.first);}};

int main()
{
    vector<ll>x_v;
    for(auto x:in<string>())x_v.push_back(x-'0');

    const ll m=in();

    if(x_v.size()==1){
        cout<<(x_v[0]<=m?1:0)<<endl;
    }else{
        auto x_leq_m=[x_v,m](ll base){
            vector<ll>m_v;
            for(ll m_c=m;m_c;m_c/=base)m_v.push_back(m_c%base);
            while(m_v.back()==0)m_v.pop_back();
            reverse(m_v.begin(),m_v.end());
            return
                x_v.size()<m_v.size()||
                x_v.size()==m_v.size()&&(
                    lexicographical_compare(x_v.begin(),x_v.end(),m_v.begin(),m_v.end())||
                    equal(x_v.begin(),x_v.end(),m_v.begin())
                );
        };
        if(ll base_min=*max_element(x_v.begin(),x_v.end())+1;!x_leq_m(base_min)){
            cout<<0<<endl;
        }else{
            ll left=base_min,right=numeric_limits<ll>::max();
            while(right-left>1){
                ll base=left+(right-left)/2;
                (x_leq_m(base)?left:right)=base;
            }
            cout<<right-base_min<<endl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;
#define int  long long int
#define mod 998244353
#define pb push_back
#define mp make_pair
#define bs binary_search
#define lb lower_bound
#define ub upper_bound
#define gcd __gcd
#define fastio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define all(i) i.begin(),i.end()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define set_bit __builtin_popcount //gives number of set_bits
#define ff first
#define ss second
#define cases(t) int t;cin>>t; rep(casn,1,t+1)
#define el cout<<"\n";
#define deci(n) int n; scanf("%lld",&n);
#define decs(n) string n; cin>>n;
#define sz(v) v.size()
#define repit(m) for(auto it=m.begin();it!=m.end();it++)
#define cYES cout<<"YES\n";
#define cNO cout<<"NO\n";
#define ins insert
#define imax 10000
#define imin 1000
#define mid 50000
#define NP 50000

//  cerr << "\nTime : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
//  IIITA
//SUNEET KUMAR -->  ਸੁਨੀਤ
// scanning numbers extremely fast/////////

void fs(int &number)
{
    bool negative = false;
    register int c;
    number = 0;
    c = getchar();
    if (c=='-')
    {
        negative = true;
        c = getchar();
    }
    for (; (c>47 && c<58); c=getchar())
        number = number *10 + c - 48;
    if (negative)
        number *= -1;
}
// ========================================
int power(int a,int x)
{
        a%=mod;
    int res=1;
    while(x>0)
    {
        if(x&1)
        res=res*a%mod;
        a=a*a%mod;
        x>>=1;
    }
    return res;
}

int max(int a,int b)
{
    if(a>b)
    {
        return a;
    }
    else
    {
        return b;
    }
}
int madd(int a,int b)
{
    int ans;
    ans=(a%mod+b%mod)%mod;
    return ans;
}
int mmult(int a,int b)
{
    int ans=1;
    ans=((a%mod)*(b%mod))%mod;
    return ans%mod;
}
int modinverse(int a)
{
    return power(a,mod-2);
}
void its_her_choice()
{
    int n;cin>>n;
    string s;cin>>s;
    stack<char>st;
    char c1,c2,c3;
    for(int i=0;i<s.length();i++)
    {
        st.push(s[i]);
        if(st.top()=='x')
        {
            st.pop();
            if(!st.empty()&&st.top()=='o')
            {
                st.pop();
                if(!st.empty()&&st.top()=='f')
                {
                    st.pop();
                }
                else
                {
                    st.push('o');
                    st.push('x');
                }
            }
            else
            {
                st.push('x');
            }
        }
    }
    int count=0;
    while(!st.empty())
    {
        st.pop();
        count++;
    }
    cout<<count;el
}
signed main()
{
    // cases(t)
    {
        its_her_choice();
    }
}

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

#define int long long
#define debug(x) cerr << #x << ": " << x << '\n'
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()

using ll = long long;
using ld = long double;
using intp = pair<int, int>;
using intmap = map<int, int>;
using intset = set<int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;

constexpr int MOD = 1e9 + 7;
constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 62;
inline int mod(ll a, int m = MOD) { return (a % m + m) % m; }
inline bool bit(ll b, ll i) { return b & (1 << i); }
inline ll ceiv(ll a, ll b) { return (a + b - 1) / b; }
template<class T> bool chmin(T &a, T b) {if(a > b){a = b; return 1;} return 0;}
template<class T> bool chmax(T &a, T b) {if(a < b){a = b; return 1;} return 0;}

template <class T, T (*operation)(T, T), T (*e)()>
class SegmentTree {
    vector<T> v;
    int n = 1;

public:
    SegmentTree(int size) {
        while(n < size) n <<= 1;
        v.resize(n << 1, e());
    }

    SegmentTree(const vector<T> &init_v) {
        n = 1;
        while(n < init_v.size()) n <<= 1;
        v.resize(n << 1, e());
        for(int i = 0; i < init_v.size(); i++) {
            v[n + i] = init_v[i];
        }
        for(int i = 2*n-2; i > 0; i -= 2) {
            v[i >> 1] = operation(v[i], v[i|1]);
        }
    }

    void update(int idx, int val) {
        idx += n;
        v[idx] = val;
        while(idx > 0) {
            idx >>= 1;
            v[idx] = operation(v[(idx << 1)|0], v[(idx << 1)|1]);
        }
    }

    T get(int l, int r) {
        T valL = e(), valR = e();
        for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if(l & 1) valL = operation(valL, v[l++]);
            if(r & 1) valR = operation(v[--r], valR);
        }
        return operation(valL, valR);
    }

    T get_all() {
        return v[1];
    }

    T operator[](const int &i) const { return v[n + i]; }
};

int f(int a, int b) { return a xor b; }

int e() { return 0; }

signed main() {
    int n, Q;
    cin >> n >> Q;

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

    SegmentTree<int, f, e> tree(a);

    while(Q--) {
        int t; cin >> t;
        int x, y; cin >> x >> y;
        if(t == 1) {
            x--;
            tree.update(x, tree.get(x, x+1) xor y);
        } else if(t == 2) {
            x--;
            cout << tree.get(x, y) << endl;
        }
    }
}


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

int main()
{
  string ufo;
  cin >> ufo;

  int compCount = 0;
  for(int i = 0; i+3 < ufo.size(); i++)
  {
    if(ufo.at(i) == 'Z' && ufo.at(i+1) == 'O' && ufo.at(i+2) == 'N' && ufo.at(i+3) == 'e')
    {
      compCount++;
    }

  }

  cout << compCount << endl;

}

#include <iostream>
using namespace std;

bool check(int x, string s){
    int size;
    size = s.size();
    if (x + 4 > size){
        return false;
    }

    if (s.substr(x, 4) == "ZONe"){
        return true;
    }else{
        return false;
    }
}

int main(){
    string a;
    int count;
    count = 0;
    cin >> a;
    if (a.size() < 3){
    	cout << 0 << endl;
        return 0;
    }
    for(int i=0; i < a.size()-3; i++){
        if (check(i, a)){
            count += 1;
        }
    }

    cout << count << endl;

    return 0;
}


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

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

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

template <typename T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template <typename T> bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; }
template <typename T> void coutall(T v) { if(v.empty()){cout << endl; return;} for(auto i = v.begin(); i != --v.end(); i++){cout << *i << " ";} cout << *--v.end() << endl; }
void yes(bool ok = true){ cout << (ok ? "yes" : "no") << endl; }
void Yes(bool ok = true){ cout << (ok ? "Yes" : "No") << endl; }
void YES(bool ok = true){ cout << (ok ? "YES" : "NO") << endl; }
ll myceil(ll a, ll b) { return a >= 0 ? (a+b-1)/b : -((-a)/b); }
ll myfloor(ll a, ll b) { return a >= 0 ? a/b : -myceil(-a, b); }

int keta(ll x){
    int res = 0;
    while(x){
        x /= 10;
        res++;
    }
    return res;
}

void Main(){
    ll n; cin >> n;
    if(n < 1000){
        cout << 0 << endl;
        return;
    }
    int k = keta(n);
    ll p = pow(10ll, k-1);
    ll t = n / p;
    ll ans = ((n % p) + 1) * ((k-1)/3);
    ans += (t-1) * p * ((k-1)/3);
    //cout << k << " " << ans << endl;
    for(ll i = k-1; i >= 4; i--){
        ans += ll((i-1)/3) * 9 * pow(10ll, i-1);
        //cout << i << " " << ll((i-1)/3) * 9 * pow(10ll, i-1) << endl;
    }
    cout << ans << endl;
    return;
}

int main(){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}

// clang-format off
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define mp make_pair
#define fst first
#define snd second
#define forn(i,n) for (int i = 0; i < int(n); i++)
#define forn1(i,n) for (int i = 1; i <= int(n); i++)
#define popcnt __builtin_popcount
#define ffs __builtin_ffs
#define ctz __builtin_ctz
#define clz __builtin_clz
#define all(a) (a).begin(), (a).end()

using namespace std;
using namespace __gnu_pbds;

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pli = pair<ll,int>;
using pil = pair<int,ll>;
using pll = pair<ll,ll>;
template <typename T> using vec = vector<T>;
using vi = vec<int>;
using vl = vec<ll>;
template <typename T> using que = queue<T>;
template <typename T> using deq = deque<T>;

template <typename T> T id(T b) {return b;};
template <typename T> void chmax(T &x, T y) {if (x < y) x = y;}
template <typename T> void chmin(T &x, T y) {if (x > y) x = y;}
template <typename S, typename K> bool contains(S &s, K k) { return s.find(k) != s.end(); }
void fastio() { ios_base::sync_with_stdio(false); cin.tie(nullptr); }
constexpr ll TEN(int n) { if (n == 0) return 1LL; else return 10LL*TEN(n-1); }
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename K, typename V> using ordered_map = tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
// clang-format on

int n;
int x[17], y[17], z[17];

int dist(int i, int j) {
    return abs(x[i] - x[j]) + abs(y[i] - y[j]) + max(0, z[j] - z[i]);
}

int main() {
    fastio();

    cin >> n;
    forn(i, n) cin >> x[i] >> y[i] >> z[i];

    vec<vi> dp(1 << n, vi(n, INT_MAX));
    dp[1][0] = 0;

    forn1(sz, n - 1) {
        int s = (1 << sz) - 1;
        while (s < 1 << n) {
            forn(last, n) {
                int c = dp[s][last];
                if (c == INT_MAX) continue;
                forn(next, n) {
                    if (last == next) continue;

                    if ((s >> next) & 1) continue;

                    int x = c + dist(last, next);

                    if (sz == n - 1) {
                        x += dist(next, 0);
                    }

                    auto t = s | (1 << next);
                    chmin(dp[t][next], x);
                }
            }

            int x = s & -s, y = s + x;
            s = ((s & ~y) / x >> 1) | y;
        }
    }

    int ans = INT_MAX;
    forn(i, n) {
        chmin(ans, dp[(1 << n) - 1][i]);
    }

    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long INF = 1000000000000000000;
int main(){
    int N, Q;
    cin >> N;
    vector<long long> a(N), t(N);
    for(int i = 0; i < N; i++) cin >> a[i] >> t[i];
    long long MIN = -INF;//上限
    long long MAX = INF;//下限
    long long temp = 0;
    bool flag = false;
    long long f = -INF;
    for(int i = 0; i < N; i++){
        if(t[i] == 1) {
            temp += a[i];
            MAX += a[i];
            MIN += a[i];
            if(flag) f += a[i];
        }
        else if(t[i] == 2){
            MIN = max(MIN, a[i]);
            if(MIN >= MAX){
                flag = true;
                if(f == -INF) f = a[i];
            }
            if(flag) f = max(f, a[i]);
        }
        else{
            MAX = min(MAX, a[i]);
            if(MIN >= MAX) {
                flag = true;
                if(f == -INF) f = a[i];
            }
            if(flag)f = min(f, a[i]);
        }
    }
    long long mi = MIN, ma = MAX;
    if(!flag){
        for(int i = N - 1; i >= 0; i--){
            if(t[i] == 1){
                mi -= a[i];
                ma -= a[i];
            }
        }
    }
    cin >> Q;
    for(int i = 0; i < Q; i++){
        long long x;
        cin >> x;
        if(flag) cout << f << endl;
        else{
            if(x > ma) cout << MAX << endl;
            else if(x < mi) cout << MIN << endl;
            else cout << x + temp << endl;
        }
    }
}

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

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

    ll max=-INF, min=INF, bias=0;
    rep(i, n){
        ll a; int t;
        cin >> a >> t;
        if(t==1){
            bias+=a;
            if(max>-INF) max += a;
            if(min<INF) min += a;
        }
        else if(t==2){
            if(a>max) max=a;
            if(a>min) min=a;
        }
        else if(t==3){
            if(a<min) min=a;
            if(a<max) max=a;
        }
    }
    ll q;
    cin >> q;
    rep(i, q){
        ll x;
        cin >> x;
        ll y = x+bias;
        if(y>=min) y=min;
        else if(y<=max) y=max;
        cout << y << endl;
    }

    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define ff first
#define ar array
#define ss second
#define ll long long int
#define mem(x) memset(x,0,sizeof(x))
#define setbits(x)  __builtin_popcount(x)
#define ull unsigned long long int
#define all(c) (c).begin(),(c).end()
#define debug(x) cout<<#x<<" :: "<<x<<endl;
#define debug2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
#define db double
#define endl "\n"
#define  fbo(x) find_by_order(x)
#define  ook(x) order_of_key(x)

using namespace __gnu_pbds;
using namespace std;
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair<ll,ll > iil;
typedef pair<ll,int> li;
typedef pair<int,int> ii;
typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
    os1;
typedef tree<int, null_type, greater<int>, rb_tree_tag,
            tree_order_statistics_node_update>
    os2;

inline void fast()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}
const ll pi=31;
const ll MOD[2] = {1187278649, 1149736019};//for double hashing
const ll mod=(ll)(1e9)+7;
ll pmod(ll x,ll n){
  ll r=1;
  while(n>0)
  {
    if(n&1LL)
      r=(r*x)%mod;
    x=(x*x)%mod;
    n=n/2;
    n%=mod;
  }
  return r;
}
ll pMod(ll x,ll n,ll mod)
{ ll r=1;
  while(n>0)
  {
    if(n&1LL)
      r=(r*x)%mod;
    x=(x*x)%mod;
    n=n/2;
    n%=mod;
  }
  return r;

}

//think recursively also;

void solve()
{

 int n;
 cin>>n;
 int N=2*n;
 bool vis[N+1]={0};
 int a[n+1],b[n+1];
 bool flag=0;
 for(int i=1;i<=n;i++)
 {
  cin>>a[i]>>b[i];
  if(a[i]!=-1)
  {
     if(vis[a[i]])
     {
       flag=1;
     }
     vis[a[i]]=1;
  }
  if(b[i]!=-1)
  {
     if(vis[b[i]])
     {
      flag=1;
     }
     vis[b[i]]=1;
  }
   if(a[i]!=-1 && b[i]!=-1)
    {
      if(a[i]>=b[i])
      {
        flag=1;
      }
    }
 }

   if(flag)
   {
    cout<<"No\n";
    return;
   }
   for(int i=1;i<=n;i++)
    {
      for(int j=1;j<=n;j++)
      {

        if(i==j)
          continue;
        if(a[i]==-1 || b[i]==-1)
          continue;
        if(b[j]!=-1 && a[j]!=-1 && a[i]<=a[j] && a[j]<=b[i] && a[i]<=b[j] && b[j]<=b[i])
        {
          cout<<"No\n";
          return;
        }
        if(a[j]!=-1)
        {
            if(b[j]!=-1)
              {
                 if(a[i]<=a[j] && a[j]<=b[i])
                 {if(abs(a[i]-b[i])!=abs(a[j]-b[j]))
                 {
                  cout<<"No\n";
                  return;
                 }
                }
              }
            else
            {
              if(a[i]<=a[j] && a[j]<=b[i])
               {int g=b[i]-a[i];
               int req=g+a[j];
               if(req<=0 || req>N || vis[req])
               {
                cout<<"No\n";
                return;
               }
               //vis[req]=1;
             }
            }
        }
         if(b[j]!=-1)
         {
            if(a[j]!=-1)
              {
                 if(a[i]<=b[j] && b[j]<=b[i])
                 {if(abs(a[i]-b[i])!=abs(a[j]-b[j]))
                 {
                  cout<<"No\n";
                  return;
                 }
                 }
              }
            else
            {
              if(a[i]<=b[j] && b[j]<=b[i])
              {int g=b[i]-a[i];
               int req=b[j]-g;
               if(req<=0 || req>N || vis[req])
               {
                cout<<"No\n";
                return;
               }
              // vis[req]=1;
             }
            }
         }
      }
    }
    cout<<"Yes\n";
    return;

}
int main()
{
  fast();
   // int t;
   // cin>>t;
   // for(int casee=1;casee<=t;casee++)
  {//cout<<"Case #"<<casee<<": ";
    solve();
  }

}

#pragma region head
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
template <class T>
using vv = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rrepi(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define bit(n) (1LL << (n))
template <class T>
inline bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T& a, const T& b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
const int INF = 1002003004;
const ll LINF = 1002003004005006007ll;
struct preprocess {
    preprocess() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} ____;
#pragma endregion head

#pragma region library
const int MOD = 1000000007;
struct mint {
    ll x;  // typedef long long ll;
    mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD - a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= MOD;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res += a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res -= a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res *= a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }
    // for prime MOD
    mint inv() const {
        return pow(MOD - 2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res /= a;
    }
    bool operator==(const mint a) const {
        return x == a.x;
    }
    bool operator!=(const mint a) const {
        return x != a.x;
    }
    friend ostream& operator<<(ostream& os, const mint& value) {
        os << value.x;
        return os;
    }
    friend istream& operator>>(istream& is, mint& value) {
        ll t;
        is >> t;
        value = mint(t);
        return is;
    }
};

#pragma endregion library

int main() {
    int _t;
    cin >> _t;
    while (_t--) {
        ll n, a, b;
        cin >> n >> a >> b;
        if(n<a+b){
            cout << 0 << '\n';
            continue;
        }
        mint ans = 0, sub = 0;
        ans = (mint)(n - a + 1) * (n - a + 1);
        ans *= (mint)(n - b + 1) * (n - b + 1);
        sub = (mint)(n - a + 1) * (n - b + 1) - (mint)(n - a - b + 2) * (n - a - b + 1);
        ans -= sub * sub;
        cout << ans << '\n';
    }
}


#include<bits/stdc++.h>

using namespace std;

int main()
{
	string s;
   cin>>s;
   set<char> s1;
  int i;
  for(i=0;i<s.length();i++)
  {
    s1.insert(s.at(i));
  }
              if(s1.size()==1)
              cout<<"Won";
              else
              cout<<"Lost";
}

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

int main(){
  int n; cin >> n;
  if(n<191)
    cout << "Yay!" << endl;
  else if(n==191)
    cout << "so-so" << endl;
  else
    cout << ":(" << endl;
}


#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;
using ll = long long;

int main(){
    ll n; cin >> n;
    ll a[n], mod[200] = {0};

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

    for (ll i = 0; i < n; i++) {
        mod[a[i]%200]++;
    }

    ll ans = 0;
    for (ll i = 0; i < 200; i++){
        ans += (mod[i] * (mod[i]-1))/2;
    }

    cout << ans << endl;
    return 0;
}

#include <bits/stdc++.h>             /*Fuck ratings Feel the magic in the code...*/
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
// template<typename T>
// using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/*   in log(n) time...
    find_by_order() - Returns an iterator to the k-th largest element (counting from zero)
    order_of_key()  - The number of items in a set that are strictly smaller than our item
    // Rest same as set
*/
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mod 1000000007
#define inf 2e18
#define minf -2e18
#define PI 3.141592653589793238462
#define ll long long
#define all(x) x.begin(), x.end()
#define min3(a, b, c) min(min(a, b), c)
#define max3(a, b, c) max(max(a, b), c)
#define ff first
#define ss second
#define pb push_back
#define ppb pop_back
#define sz(v) (int)v.size()
#define nline "\n"

ll powr(ll a, ll b, ll p) { if(b == 0) return 1; else if(b == 1) return a; else if(!(b & 1)) return powr((a * a) % p, b / 2, p); else return (a * powr(a, b - 1, p)) % p; }
ll gcd(ll a, ll b){ if(b == 0) return a; else return gcd(b, a % b); }
ll lcm(ll a, ll b){ return a * b / gcd(a, b); }
void extendgcd(ll a, ll b, ll *v) {if (b == 0) {v[0] = 1; v[1] = 0; v[2] = a; return ;} extendgcd(b, a % b, v); ll x = v[1]; v[1] = v[0] - v[1] * (a / b); v[0] = x; return;} //pass an arry of size1 3
ll mminv(ll a, ll b) {ll arr[3]; extendgcd(a, b, arr); return arr[0]; } //for non prime b
ll mod_inv(ll a, ll p){ return powr(a,p-2,p); } /*For prime p  "Fermet lill theorem"*/
ll mod_add(ll a, ll b, ll m) { a = a % m; b = b % m; return ((a + b) % m + m) % m; }
ll mod_mul(ll a, ll b, ll m) { a = a % m; b = b % m; return ((a * b) % m + m) % m; }
ll mod_sub(ll a, ll b, ll m) { a = a % m; b = b % m; return ((a - b) % m + m) % m; }
ll mod_div(ll a, ll b, ll m) { a = a % m; b = b % m; return (mod_mul(a, mod_inv(b, m), m) + m) % m; } /*For prime p*/
// void swap(int &a, int &b) { int temp = a; a = b; b = temp; }
ll spf(ll n) { for(int i = 2; i * i <= n; i++) { if(n % i == 0) return i; } return n; }/* spf in sqrt(n) time... */
vector<ll> fact_modp(ll n, ll p) { vector<ll> fact(n + 1); fact[0] = 1; for(int i = 1; i <= n; i++){ fact[i] = (i * fact[i - 1]) % p; } return fact; }
/*For prime p "Fermet lill theorem..."*/
ll nCr_modp(ll n, ll r, ll p, vector<ll> fact) {  if(n < r) return 0; if(r == 0) return 1; else { return ((fact[n] * mod_inv(fact[r], p)) % p * mod_inv(fact[n - r], p)) % p; } }

vector<ll> prime_sieve(ll n) { /*Returns vector of prime <= n  in nloglogn time*/
    vector<ll> t;
    vector<bool> a(n + 1, true);
    a[0] = a[1] = false;
    for(int i = 2; i * i <= n; i++) {
        if(a[i]) {
            for(int j = i * i; j <= n; j += i)
            a[j] = false;
        }
    }
    for(int i = 0; i <= n; i++) {
        if(a[i])
        t.push_back(i);
    }
    return t;
}
vector<ll> spf_sieve(ll n) { /*Returns vector of spf[n] in nloglogn time*/
    vector<ll> a(n + 1, -1);
    for(int i = 2; i * i <= n; i++) {
        if(a[i] == -1) {
            a[i] = i;
            for(int j =i * i; j <= n; j += i) { if(a[j] == -1) a[j] = i;}
        }
    }
    for(int i = 2; i <= n; i++) { if(a[i] == -1) a[i] = i;}
    return a;
}
     /* --------------------------------------Template ends, Dee codes---------------------------------------*/

int main()
{
    fastio();

    int tests = 1;
    // cin >> tests;
    while(tests--)
    {
    	int n;
    	cin >> n;
    	vector<int> a(n);
    	ll res = 0;
    	map<int, ll> mp;
    	for(int i = 0; i < n; i++) {
    		cin >> a[i];
    		int r1 = (200 - (a[i] % 200));
    		if(r1 != 200) {
    			res += mp[200 - r1];
    		}
    		mp[a[i] % 200]++;
    	}
    	res += ((mp[0] * (mp[0] - 1)) / 2);
    cout << res << nline;

    }
    return 0;
}


#include<iostream>
#include<vector>
using namespace std;
const int BUF = 105;
const int EDGE = BUF * BUF;
const int MOD = 1000000007;

int add(int a, int b) {
    return (a + b) % MOD;
}

int mul(int a, int b) {
    return 1LL * a * b % MOD;
}

int modpow(int p, int n) {
    if (n == 0) return 1;
    int t = modpow(p, n / 2);
    return n % 2 == 0 ? mul(t, t) : mul(p, mul(t, t));
}

vector<vector<int> > matmul(const vector<vector<int> > &a, const vector<vector<int> > &b) {
    int n = a.size();
    vector<vector<int> > c = vector<vector<int> >(n, vector<int>(n, 0));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            for (int k = 0; k < n; ++k) {
                c[i][j] = add(c[i][j], mul(a[i][k], b[k][j]));
            }
        }
    }
    return c;
}

vector<vector<int> > modpow(vector<vector<int> > &p, int n) {
    if (n == 0) {
        vector<vector<int> > ret = vector< vector<int> >(p.size(), vector<int>(p.size(), 0));
        for (int i = 0; i < p.size(); ++i) ret[i][i] = 1;
        return ret;
    }
    vector<vector<int> > t = modpow(p, n / 2);
    return n % 2 == 0 ? matmul(t, t) : matmul(p, matmul(t, t));
}

int inv(int v) {
    return modpow(v, MOD - 2);
}

int divide(int a, int b) {
    return mul(a, inv(b));
}

int nVal, nEdge, nPow;
int val[BUF];
int edge[EDGE][2];

void read() {
    cin >> nVal >> nEdge >> nPow;
    for (int i = 0; i < nVal; ++i) cin >> val[i];
    for (int i = 0; i < nEdge; ++i) {
        cin >> edge[i][0] >> edge[i][1];
        --edge[i][0];
        --edge[i][1];
    }
}

void work() {
    vector< vector<int> > mat(nVal, vector<int>(nVal, 0));

    for (int i = 0; i < nEdge; ++i) {
        mat[edge[i][0]][edge[i][0]] = add(mat[edge[i][0]][edge[i][0]], 1);
        mat[edge[i][0]][edge[i][1]] = add(mat[edge[i][0]][edge[i][1]], 1);
        mat[edge[i][1]][edge[i][0]] = add(mat[edge[i][1]][edge[i][0]], 1);
        mat[edge[i][1]][edge[i][1]] = add(mat[edge[i][1]][edge[i][1]], 1);
        for (int j = 0; j < nVal; ++j) {
            if (j == edge[i][0] || j == edge[i][1]) continue;
            mat[j][j] = add(mat[j][j], 2);
        }
    }

    for (int i = 0; i < nVal; ++i) {
        for (int j = 0; j < nVal; ++j) {
            mat[i][j] = divide(mat[i][j], 2 * nEdge);
        }
    }

    vector<vector<int> > ret = modpow(mat, nPow);

    vector<int> ans(nVal, 0);
    for (int i = 0; i < nVal; ++i) {
        for (int j = 0; j < nVal; ++j) {
            ans[i] = add(ans[i], mul(ret[i][j], val[j]));
        }
    }

    for (int i = 0; i < nVal; ++i) {
        cout << ans[i] << endl;
    }
}

int main() {
    read();
    work();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define forn(i,x,n) for(int i = x;i <= n;++i)
typedef pair<int,int> pii;
#define forr(i,x,n) for(int i = n;i >= x;--i)
#define Angel_Dust ios::sync_with_stdio(0);cin.tie(0)
#define x first
#define y second

const int N = 105,MOD = 1e9+7;
pii edges[N * N];
int deg[N];

struct Mat
{
    int c[N][N];
    Mat()
    {
        memset(c,0,sizeof c);
    }
    Mat operator*(const Mat& r) const
    {
        Mat res;
        forn(i,1,N - 1) forn(j,1,N - 1) forn(k,1,N - 1) res.c[i][j] = (res.c[i][j] + 1ll*c[i][k] * r.c[k][j] % MOD) % MOD;
        return res;
    }
    void operator*(int r)
    {
        forn(i,1,N - 1) forn(j,1,N - 1) c[i][j] = 1ll*c[i][j] * r % MOD;
    }
};

Mat qpow(Mat a,int b,int MOD)
{
    Mat res;forn(i,1,N - 1) res.c[i][i] = 1;
    while(b)
    {
        if(b & 1)   res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}

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

int main()
{
    // freopen("D://OJCodes//TTT//in.txt","r",stdin);
    // freopen("D://OJCodes//TTT//out.txt","w",stdout);

    int n,m,k;scanf("%d%d%d",&n,&m,&k);
    Mat f,B;forn(i,1,n)   scanf("%d",&f.c[1][i]);

    forn(i,1,m)
    {
        int u,v;scanf("%d%d",&u,&v);
        ++deg[u];++deg[v];
        edges[i] = {u,v};
    }

    int m_2fact = qpow(m * 2,MOD - 2,MOD);

    forn(i,1,m)
    {
        int u = edges[i].x,v = edges[i].y;
        B.c[u][v] = m_2fact;
        B.c[v][u] = m_2fact;
    }

    forn(i,1,n) B.c[i][i] = (2 * m - deg[i]) * 1ll * m_2fact % MOD;

    B = qpow(B,k,MOD);
    f = f * B;
    forn(i,1,n) printf("%d\n",f.c[1][i]);

    // fclose(stdin);
    // fclose(stdout);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define dump(...)
#endif
#define endl '\n'
//#define int long long
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, x) for (int i = 0; i < (int)(x); i++)
#define REPS(i, x) for (int i = 1; i <= (int)(x); i++)
#define RREP(i, x) for (int i = ((int)(x)-1); i >= 0; i--)
#define RREPS(i, x) for (int i = ((int)(x)); i > 0; i--)
#define INF 2147483647
#define LLINF 9223372036854775807LL
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define BIT(n) (1LL << (n))
#define TOUPPER(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define TOLOWER(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define FILLZERO(s, n)                      \
    do {                                    \
        ostringstream os;                   \
        os << setw(n) << setfill('0') << s; \
        s = os.str();                       \
    } while (0);
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてからつかうこと

using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pi = pair<int, int>;
template <typename T>
using Pq = priority_queue<T>;
template <typename T>
using pQ = priority_queue<T, vector<T>, greater<T>>;

template <typename T>
vector<T> make_vec(size_t a, T n) { return vector<T>(a, n); }
template <typename T, typename... Ts>
auto make_vec(size_t a, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...)); }
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;
}
template <typename T>
bool isProductOverflow(T a, T b) {
    return (a > numeric_limits<T>::max() / b);
}
template <typename T>
istream &operator>>(istream &is, vector<T> &vec) {
    for (T &x : vec)
        is >> x;
    return is;
}

__attribute__((constructor)) void ioInit() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    cout << std::fixed << std::setprecision(10);
}

constexpr ll MOD = 1e9 + 7;
//constexpr ll MOD = 998244353;
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
ll A, B, C, D, E, F, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

signed main() {
    cin >> N;
    S = to_string(N);
    K = S.size();
    vi v(K);
    REP(i, K) {
        v[i] = S[i] - '0';
    }
    ans = INF;
    REP(bit, 1 << K) {
        ll tmp = 0;
        REP(i, K) {
            if ((bit >> i) & 1) {
                tmp += v[i];
            }
        }
        int cnt = __builtin_popcount(bit);
        if (cnt != 0 and tmp % 3 == 0) {
            chmin(ans, K - cnt);
        }
    }
    if (ans == INF)
        ans = -1;
    cout << ans << endl;
}

#include "bits/stdc++.h"

using namespace std;
using LL = long long;

constexpr int N = 1e5 + 5;

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

	int n;
	cin >> n;

	cout << (1 << n) - 1 << endl;

	for (int i = 1; i < (1 << n); i++) {
		for (int j = 0; j < (1 << n); j++) {
			cout << char('A' + (__builtin_popcount(i & j) % 2));
		}
		cout << endl;
	}
}


#include <iostream>
#include <cstring>

using namespace std;

string s[61];
long long n, memo[61][2];

long long f(int idx, int a)
{
    if (idx == n) return a;
    if (memo[idx][a] != -1) return memo[idx][a];
    return memo[idx][a] = (s[idx] == "OR" ? f(idx + 1, a|1) + f(idx + 1, a|0) : f(idx + 1, a&1) + f(idx + 1, a&0));
}

int main()
{
    cin >> n;
    memset(memo, -1, sizeof(memo));
    for (int i = 0; i < n; i++) cin >> s[i];
    cout << f(0, 1) + f(0, 0) << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define boostIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define rrep(i, b, a) for (int i = b; i >= a; i--)
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define sz(v) (int)(v.size())
#define all(v) v.begin(), v.end()
#define mem(a, val) memset(a, val, sizeof(a))
#define trav(a, x) for (auto &a : x)
#define ppcll(x) __builtin_popcountll(x)
void amn(int &x, int y) { x = min(x, y); }
void amx(int &x, int y) { x = max(x, y); }

#define mod 1000000007
const long long INF = 1e18;
// const int N = 5e5 + 5;

signed main() {
	boostIO;
	int n;
	cin >> n;
	vector<pair<int, string> > v;
	for (int i = 0; i < n; i++) {
		string s;
		int t;
		cin >> s >> t;
		v.push_back({t, s});
	}
	sort(v.begin(), v.end());
	cout << v[n - 2].se << endl;
	return 0;
}
// Play Hard Kiddo...


#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl "\n"
using namespace std;

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

    ll n;
    cin >> n;
    ll arr[n];
    ll sum=0;
    for (ll i=0;i<=n-1;i++)
    {
        cin >> arr[i];
        sum+=arr[i];
    }

    sort(arr,arr+n);
    ll ans=0;
    for (ll i=0;i<=n-1;i++)
    {
        sum-=arr[i];
        ans+=sum-((n-i-1)*arr[i]);
    }

    cout << ans << endl;
    return 0;
}

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

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

  ll ans = -1;
  for(int i=0; i<N; ++i){
    ll max_x = A[i];
    for(int j=i; j<N; ++j){
      max_x = min(max_x, A[j]);
      ans = max(ans, max_x*(j-i+1));
    }
  }

  cout << ans << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define lowbit(x) (x&-x)
#define reg register
#define mp make_pair
#define fir first
#define sec second
typedef long long LL;
typedef unsigned long long uLL;
const int INF=0x3f3f3f3f;
const int mo=1e9+7;
const int iv2=5e8+4;
const int lim=1000000;
const int jzm=1e6+7;
const int orG=3,invG=332748118;
const double Pi=acos(-1.0);
typedef pair<LL,LL> pii;
const double PI=acos(-1.0);
template<typename _T>
_T Fabs(_T x){return x<0?-x:x;}
template<typename _T>
void read(_T &x){
	_T f=1;x=0;char s=getchar();
	while(s>'9'||s<'0'){if(s=='-')f=-1;s=getchar();}
	while('0'<=s&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();}
	x*=f;
}
template<typename _T>
void print(_T x){if(x<0){x=(~x)+1;putchar('-');}if(x>9)print(x/10);putchar(x%10+'0');}
int gcd(int x,int y){return !y?x:gcd(y,x%y);}
int add(int x,int y){return x+y<mo?x+y:x+y-mo;}
LL n,ans[2005],tot;pii f[2005];
pii solve1(pii tmp){return mp(tmp.fir+1LL,tmp.sec);}
pii solve2(pii tmp){return mp(tmp.fir,tmp.sec+1LL);}
pii solve3(pii tmp){return mp(tmp.fir+tmp.sec,tmp.sec);}
pii solve4(pii tmp){return mp(tmp.fir,tmp.sec+tmp.fir);}
void extend(LL id){
	tot=id;
	while(f[tot].fir+f[tot].sec<=n){
		if(f[tot].fir<=f[tot].sec)tot++,f[tot]=solve3(f[tot-1]),ans[tot]=3;
		else tot++,f[tot]=solve4(f[tot-1]),ans[tot]=4;
	}
}
LL check(LL id){return n-f[id].fir+id;}
void print(){
	while(f[tot].fir<n)++tot,f[tot]=solve1(f[tot-1]),ans[tot]=1;
	printf("%d\n",tot);for(int i=1;i<=tot;i++)printf("%d\n",ans[i]);
}
signed main(){
	read(n);f[1]=solve2(f[0]);ans[1]=2;f[2]=solve1(f[1]);ans[2]=1;tot=2;extend(tot);
	if(check(tot)<=130){print();return 0;}
	for(int i=3;i<=tot;i++){
		LL las=check(tot);pii ls=f[i];int lasway=ans[i];f[i]=solve1(f[i-1]);ans[i]=1;extend(i);
		if(check(tot)>=las)f[i]=ls,ans[i]=lasway,extend(i);
		//printf("%d:%lld %lld %lld %lld\n",i,f[i].fir,f[i].sec,check(tot),las);
		las=check(tot);ls=f[i];lasway=ans[i];f[i]=solve2(f[i-1]);ans[i]=2;extend(i);
		if(check(tot)>=las)f[i]=ls,ans[i]=lasway,extend(i);
		//printf("%d:%lld %lld %lld %lld\n",i,f[i].fir,f[i].sec,check(tot),las);
		las=check(tot);ls=f[i];lasway=ans[i];f[i]=solve3(f[i-1]);ans[i]=3;extend(i);
		if(check(tot)>=las)f[i]=ls,ans[i]=lasway,extend(i);
		//printf("%d:%lld %lld %lld %lld\n",i,f[i].fir,f[i].sec,check(tot),las);
		las=check(tot);ls=f[i];lasway=ans[i];f[i]=solve4(f[i-1]);ans[i]=4;extend(i);
		if(check(tot)>=las)f[i]=ls,ans[i]=lasway,extend(i);
		//printf("%d:%lld %lld %lld %lld\n",i,f[i].fir,f[i].sec,check(tot),las);
		if(check(tot)<=130){print();return 0;}
		//printf("nxt %lld %lld\n",f[i+1].fir,f[i+1].sec);
	}
	print();
	return 0;
}
/*
0 1
1 1
1 2
*/

#include <bits/stdc++.h>
#include <bits/extc++.h>
#define double long double
#define rbtree __gnu_pbds::tree<int,__gnu_pbds::null_type,less<int>,__gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>
#define int ll
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);
#define pb push_back
#define ALL(X) X.begin(),X.end()
#define F(i,n)  FF(i,0,n)
#define F1(i,n)  FF(i,1,n+1)
#define FF(i,n,m) for(int i=(int)n;i<(int)m;++i)
#ifndef LOCAL
//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
#else
#endif // LOCOL
//#define mp make_pair
using namespace std;
//using namespace __gnu_pbds;
template<typename T> bool remax(T& a, const T& b) {return b>a?a=b,1:0;}
template<typename T> bool remin(T& a, const T& b) {return b<a?a=b,1:0;}
inline ostream& operator << (ostream& os,__int128_t a){if(a==0) {return (os<<'0');}bool flag=0;if(a<0) {a=-a,flag=1;}string s;while(a){s+=a%10+'0';a/=10;}s+=flag?"-":"";reverse(ALL(s));return os<<s;}
inline istream& operator >>(istream& is,__int128_t& a){string s;cin>>s;a=0;for(auto c:s) a=a*__int128_t(10)+__int128_t(c-'0');return is;}
template<typename T,typename P> inline ostream& operator << (ostream& os,pair<T,P> a){os<<a.first<<" "<<a.second; return os;}
template<typename T,typename P> inline istream& operator >> (istream& is,pair<T,P> &a){is>>a.first>>a.second; return is;}
using ll=long long;
using ull=unsigned long long;
using int128= __int128_t;
using uint128= __uint128_t;
using pii =pair<int,int>;
const double pi=acos(-1);
const int N=1E5+5;
const ll M=1000000000;
const ll INF_64=0x3f3f3f3f3f3f3f3f;
const int INF_32=0x3f3f3f3f;
const int16_t INF_16=0x3f3f;
const int klog=20;
const int mod=1E9+7;//998244353
const double eps=1E-8;
void gen(){
}
vector<int> an;
bool DD(int x,int y){
    if(x==0||y==0){
        if(an.size()+x+y>130) return false;
        while(x--) an.pb(1);
        while(y--) an.pb(2);
        return true;
    }
    if(x>=y)
        x-=y,an.pb(3);
    else y-=x,an.pb(4);
    return DD(x,y);
}
int re(){
    int x=0,y=0;
    F(i,an.size()){
        if(an[i]==1) x++;
        else if(an[i]==2) y++;
        else if(an[i]==3) x+=y;
        else if(an[i]==4) y+=x;
    }

    return x;
}
void sol(){
    int n;
    cin>>n;
    std::random_device rd;
    std::default_random_engine gen = std::default_random_engine(rd());
    std::uniform_int_distribution<int> dis(n/4,3*n/4);
    int tt;

    if(n<=130){
        cout<<n<<"\n";
        F(i,n) cout<<1<<"\n";
        return;
    }
    while(tt=dis(gen)){
        an.clear();
        an.pb(3);
        if(!DD(tt,n-tt)) continue;
        //cerr<<tt<<"\n";
        cout<<an.size()<<"\n";
        reverse(ALL(an));
        assert(re()==n);
        F(i,an.size()) cout<<an[i]<<"\n";
        return;
    }

}
int32_t main(){
    #ifdef LOCAL
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    #endif // LOCAL
    IOS;
    int t=1;
    gen();
    //cin>>t;
    FF(i,1,t+1){
        //cout<<"Case #"<<i<<": ";
        sol();
    }
}


#include<iostream>
#include<iomanip>
#include<string.h>
#include<string>
#include<algorithm>
#include<map>
#include<iterator>
#include<cmath>
#include<vector>
#include<utility>
#include<stack>
#include<queue>
#define li long int
#define ll long long int
#define lli unsigned long long int
#define mod 1e9+7
using namespace std;

int main() {
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin>>n>>k;
    for(int j=1; j<=k; j++){
        vector<int>v,m;
        while(n!=0){
            v.push_back(n%10);
            n/=10;
        }
        m=v;
        sort(v.begin(),v.end());
        sort(m.rbegin(),m.rend());
        int len=v.size(),len2;
        int num=0,num1=0;
        len--;
        len2=len;
        int p=pow(10,len),p1;
        int i=0;
        p1=p;
        while(len>=0){
            num+=v[i++]*p;
            p/=10;
            len--;
        }
        i=0;
        while(len2>=0){
            num1+=m[i++]*p1;
            p1/=10;
            len2--;
        }
        n=num1-num;
    }
    cout<<n<<endl;
    return 0;
}

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

const int N = 20;

mt19937 mt;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    mt.seed(time(nullptr));
    int n, m;
    cin >> n >> m;
    vector<string> v(m);
    for (auto& i : v) {
        cin >> i;
    }
    //sort(v.begin(), v.end(), [](string a, string b){return a.size() < b.size();});
    shuffle(v.begin(), v.end(), mt);
    int ind[10] = {};
    for (auto& i : v) {
        for (auto& j : i) {
            ++ind[j - 'A'];
        }
    }
    char a[N][N];
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            a[i][j] = char(int(max_element(ind, ind + 10) - ind) + int('A'));
        }
    }
    int i = 0, j = 0;
    for (auto s : v) {
        if (j + s.size() >= n) {
            j = 0;
            ++i;
        }
        if (i >= n) {
            break;
        }
        for (int k = 0; k < s.size(); ++k, ++j) {
            a[i][j] = s[k];
        }
    }
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cout << a[i][j];
        }
        cout << '\n';
    }
	return 0;
}


#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cassert>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
#include<list>
#include <cstring>
#include <functional>
#include<unordered_map>
#include<unordered_set>
#include<bitset>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1<<30)-1)
#define LINF ((Int)1<<60)
#define EPS (1e-10)
#define REP(i,n) for(int i=0; i<(int)(n); ++i)
#define RREP(i,n) for(int i=1; i<=(int)(n); ++i)
typedef long long Int;
typedef pair<Int, Int> PI;
typedef pair<int, int> pi;
typedef tuple<int, int, int> TP;
typedef vector<double> vec;
typedef vector<vec> mat;
const int N = 201005;
//////////////////////////////

int n, m;
int x[100], y[100], z[100];
Int dp[1 << 18];

void solve()
{
	cin >> n >> m;
	REP(i, m) cin >> x[i] >> y[i] >> z[i];

	dp[0] = 1;
	REP(mask, (1 << n)) {
		REP(nxt, n) {
			if (((1 << nxt) & mask) != 0) continue;
			int comb = (1 << nxt) | mask;
			vector<int> nums;
			REP(i, n) {
				if ((comb & (1 << i))) {
					nums.push_back(i + 1);
				}
			}
			int flag = 1;
			REP(j, m) {
				if (x[j] != nums.size()) continue;
				int cnt = 0;
				for (int a : nums) {
					if (a <= y[j]) cnt++;
				}
				if (cnt > z[j]) {
					flag = 0;
					break;
				}
			}
			if (flag) {
				dp[comb] += dp[mask];
			}
		}
	}

	cout << dp[(1 << n) - 1] << endl;
}

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(13);
	solve();
	return 0;
}

#include "bits/stdc++.h"
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;

template <typename T>
void print_vec2(const vector<vector<T>>& v){
  cout << endl; cout << "       ";
  for(ll i=0; i<v[0].size(); i++) cout << i << "   ";
  cout << endl;
	for(ll i=0; i<v.size(); i++){
		cout << "i=" << i << ":   ";
		for(ll j=0; j<v[i].size(); j++){
			if(v[i][j] == 0) cout << "\x1B[0m" << v[i][j] << "   ";
      else cout << "\x1B[31m" << v[i][j] << "   ";//https://stackoverrun.com/ja/q/12618775
		}
		cout << "\x1B[0m" << endl;
	}
}
// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
int main(){
  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  for(int i=0; i<H; i++) cin >> S[i];

  vector<vector<mint>> dp(H+1, vector<mint>(W+1, 0));
  dp[0][0] = 1;
  vector<vector<mint>> tate=dp, yoko=dp, naname=dp;//累積和
  for(int i=0; i<H; i++){
    for(int j=0; j<W; j++){
      if(S[i][j]=='#') continue;

      //もらうDP??
      if(i==0 && j==0) continue;//初期化で計算済み
      if(i-1>=0) tate[i][j] += tate[i-1][j];
      if(j-1>=0) yoko[i][j] += yoko[i][j-1];
      if(i-1>=0 && j-1>=0) naname[i][j] += naname[i-1][j-1];
      dp[i][j] += tate[i][j];
      dp[i][j] += yoko[i][j];
      dp[i][j] += naname[i][j];
      tate[i][j] += dp[i][j];
      yoko[i][j] += dp[i][j];
      naname[i][j] += dp[i][j];

      //配るDP?? できない　順番がおかしい?
      // tate[i+1][j] += tate[i][j];
      // yoko[i][j+1] += yoko[i][j];
      // naname[i+1][j+1] += naname[i][j];
      // dp[i+1][j] += tate[i+1][j];
      // dp[i][j+1] += yoko[i][j+1];
      // dp[i+1][j+1] += naname[i+1][j+1];
      // tate[i+1][j] += dp[i+1][j];
      // yoko[i][j+1] += dp[i][j+1];
      // naname[i+1][j+1] += dp[i+1][j+1];

      // if(i-1>=0) dp[i][j] += tate[i-1][j];
      // if(j-1>=0) dp[i][j] += yoko[i][j-1];
      // if(i-1>=0 && j-1>=0) dp[i][j] += naname[i-1][j-1];
      // // if(i-1>=0) tate[i][j] = tate[i-1][j] + dp[i][j];
      // // if(j-1>=0) yoko[i][j] = yoko[i][j-1] + dp[i][j];
      // // if(i-1>=0 && j-1>=0) naname[i][j] = naname[i-1][j-1] + dp[i][j];
      // if(i-1>=0) tate[i][j] += tate[i-1][j] + dp[i][j];
      // if(j-1>=0) yoko[i][j] += yoko[i][j-1] + dp[i][j];
      // if(i-1>=0 && j-1>=0) naname[i][j] += naname[i-1][j-1] + dp[i][j];

    }
  }
  // cout << " tate: " << endl; print_vec2(tate);
  // cout << " yoko: " << endl; print_vec2(yoko);
  // cout << " naname: " << endl; print_vec2(naname);
  // cout << " dp: " << endl; print_vec2(dp);
  cout << dp[H-1][W-1] << endl;
  return 0;

  // cout << fixed;//小数点を確実に10桁で表示するためのもの
  // cout << setprecision(10);
}


#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define srep(i,s,t) for(int i = s; i < t; ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
using namespace std;
// using namespace atcoder;
typedef long long int ll;
typedef pair<int,int> P;
#define yn {puts("Yes");}else{puts("No");}
#define MAX_N 200005

int main() {
    int n;
    cin >> n;
    int a[n+1], b[n+1];
    srep(i,1,n+1){
        a[i] = i * 2 % n;
        b[i] = (i*2+1) % n;
        if(a[i] == 0) a[i] = n;
        if(b[i] == 0) b[i] = n;
    }

    srep(i,1,n+1) cout << a[i] << ' ' << b[i] << endl;

    return 0;
}

#include <bits/stdc++.h>
#define LL long long
#define pb push_back
#define st first
#define nd second
#define INF 0x3f3f3f3f
#define LINF 0x3f3f3f3f3f3f3f3f
template <class T> T read(T &a) {
    a=0;char x=getchar();bool f=0;
    for(;x<'0'||x>'9';x=getchar())f|=x=='-';
	for(;x>='0'&&x<='9';x=getchar())a=(a<<3)+(a<<1)+x-'0';
    if(f)a=-a;
    return a;
}
using namespace std;

int main() {
	int n;
	read(n);
	for (int i = 1; i <= n; ++i) {
		int x = i * 2, y = i * 2 + 1;
		while (x > n) x -= n;
		while (y > n) y -= n;
		printf("%d %d\n", x, y);
	}
    return 0;
}


#include<iostream>
using namespace std;
int main()
{
	int n,kk;
	int sum=0;
	cin >> n >> kk;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=kk; j++)
		{
			sum+=i*100+j;
		}
	}

	cout <<sum;
}

#include <iostream>

int main()
{
    int N, K;
    std::cin >> N >> K;

    long long sum = 100 * K * N * (N + 1) / 2 + N * K * (K + 1) / 2;
    std::cout << sum;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<long long, long long>;
constexpr char ln =  '\n';
//constexpr long long MOD = 1000000007;
constexpr long long MOD = 998244353;
constexpr long long INF = 1000000000 + 100;
constexpr long long LINF = 1000000000000000000 + 100;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
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 main(){
    ll n, k; cin >> n >> k;
    vector<ll> C(n);
    rep(i, n){
        ll a; cin >> a; C[a]++;
    }
    vector<ll> box(k); ll last = k;
    rep(i, n){
        last = min(last, C[i]);
        rep(j, last)box[j] = i+1;
    }
    ll res = accumulate(all(box), 0LL);
    cout << res << ln;
}

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <array>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <random>
#include <chrono>
#include <utility>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <sstream>
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

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

template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";}
template <typename T, typename... V>void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef HOME
#warning CHECK int:ll::INT_MAX:LLONG_MAX
#define maxn 20
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define maxn 2000006
#define gcd __gcd
#define debug(x...)
#endif

#define ff first
#define endl '\n'
#define ss second
#define inf 0x3f3f3f3f
#define MOD 1000000007
#define PI 3.14159265358979323846264338327950L
#define f(i,x,n) for(int i=x;i<=n;i++)
#define fr(i,x,n) for(int i=x;i>=n;i--)
struct _ { ios_base::Init i; _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } } _;

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

int main() {

	int n, k; cin >> n >> k;

	map<int, int> freq;
	vector<int> a(n, 0); for (int i = 0; i <= n - 1; i++) cin >> a[i], freq[a[i]]++;

	vector<pair<int, int>> v(freq.begin(), freq.end());

	if (v[0].ff != 0) {
		cout << 0;
		return 0;
	}

	v[0].ss = min(k, v[0].ss);
	long long ans = v[0].ss;

	for (int i = 1; i < v.size(); i++) {
		if (v[i].ff != i) break;
		v[i].ss = min(v[i - 1].ss, v[i].ss);
		ans += v[i].ss;
	}

	cout << ans;

	return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<string.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cmath>
typedef long long ll;
using namespace std;
int main()
{
	int n;
	cin >> n;
	int ans=0x3f3f3f3f;
	for(int i=1;i<=n;i++){
		int a,b,c;
		cin >> a >> b >> c;
		if(c-a>0){
			ans=min(ans,b);
		}
	}
	if(ans==0x3f3f3f3f){
		ans=-1;
	}
	cout << ans << endl;
	return 0;
 }

#pragma GCC optimize("Ofast")
//#ifndef ONLINE_JUDGE
//#define _GLIBCXX_DEBUG
//#endif
#ifdef ONLINE_JUDGE
#include <atcoder/all>
#endif
#include <bits/stdc++.h>
#include <chrono>
#include <random>
#include <math.h>
#include <complex>

using namespace std;
#ifdef ONLINE_JUDGE
using namespace atcoder;
#endif
#define rep(i,n) for (int i = 0;i < (int)(n);i++)
using ll = long long;
#ifdef ONLINE_JUDGE
//using mint = modint998244353;
//using mint = modint;
using mint = modint1000000007;
#endif
const ll MOD=1000000007;
//const ll MOD=998244353;
const long long INF = 1LL << 60;
const double pi=acos(-1.0);
int dx[9] = {1, 0, -1, 0, 1, 1, -1, -1, 0};
int dy[9] = {0, 1, 0, -1, 1, -1, -1, 1, 0};

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; }

int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(15);

  ll N; cin>>N;
  vector<tuple<ll,ll,ll>> v(N);
  rep(i,N) {
    ll a,p,x; cin>>a>>p>>x;
    v[i]={a,p,x};
  }
  sort(v.begin(),v.end());

  ll ans=INF;
  for(auto &[a,p,x]:v) {
    if(x-a>0) chmin(ans,p);
  }

  cout<<(ans<INF?ans:-1)<<'\n';

  return 0;
}


#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define ull unsigned ll
#define ld long double
#define mod 1000000007
#define pb push_back
#define pf push_front
#define dll deque<ll>
#define vll vector<ll>
#define vvll vector<vll>
#define pll pair<ll,ll>
#define vpll vector<pll>
#define dpll deque<pll>
#define mapll map<ll,ll>
#define umapll unordered_map<ll,ll,custom_hash>
#define nl "\n"
#define all(v) v.begin(),v.end()
#define fr for(ll i=0;i<n;i++)
#define ms(a,x) memset(a,x,sizeof(a))
//#define fr(aa,bb) for(ll i=aa;i<=bb;i++)
#define ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace __gnu_pbds;
vector<string> split(const string& s, char c) {
    vector<string> v; stringstream ss(s); string x;
    while (getline(ss, x, c)) v.push_back(x); return move(v);
}
template<typename T, typename... Args>
inline string arrStr(T arr, int n) {
    stringstream s; s << "[";
    for(int i = 0; i < n - 1; i++) s << arr[i] << ",";
    s << arr[n - 1] << "]";
    return s.str();
}
#define EVARS(args...) {__evars_begin(__LINE__); __evars(split(#args, ',').begin(), args);}
inline void __evars_begin(int line) { cerr << "#" << line << ": "; }
template<typename T> inline void __evars_out_var(vector<T> val) { cerr << arrStr(val, val.size()); }
template<typename T> inline void __evars_out_var(T* val) { cerr << arrStr(val, 10); }
template<typename T> inline void __evars_out_var(T val) { cerr << val; }
inline void __evars(vector<string>::iterator it) { cerr << endl; }
template<typename T, typename... Args>
inline void __evars(vector<string>::iterator it, T a, Args... args) {
    cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
    __evars_out_var(a);
    cerr << "; ";
    __evars(++it, args...);
}
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
//template<class T, class H>using umap=unordered_map<T,H,custom_hash>;
//template<class T>using uset=unordered_set<T,custom_hash>;
ll dmod(ll x){
    return ((x+mod)%mod);
}
ll modular_power(ll x,ll y){
    ll ans=1;
    while(y){
        if(y&1)ans=dmod(ans*x);
        y/=2;
        x=dmod(x*x);
    }
    return ans;
}
ll inv(ll x){
    return modular_power(dmod(x),mod-2);
}

int main()
{
    //clock_t clk = clock();
    fio
    //cerr << '\n'<<"Time (in s): " << double(clock() - clk) * 1.0 / CLOCKS_PER_SEC << '\n';
    ll t=1;
    //cin>>t;
    while(t--)
    {
        ll n,ans=0;cin>>n;ll a[n];ll b[n];ll c[n];ll d[n];
        fr
            cin>>a[i];
        fr
            cin>>b[i];
        fr
            cin>>c[i];
        fr
        {
            d[i]=b[c[i]-1];
        }
        unordered_map<ll,ll,custom_hash>mp1;
        unordered_map<ll,ll,custom_hash>mp2;
        fr
        {
            mp1[a[i]]++;
            mp2[d[i]]++;
        }
        for(auto it:mp1)
        {
            ans+=(it.second*mp2[it.first]);
        }
        cout<<ans;
    }
    return 0;
}

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

int main() {
	int n;
	cin >> n;
	vector<int> a(n),b(n),c(n);
	rep(i,n) cin >> a[i];
	rep(i,n) cin >> b[i];
	rep(i,n) cin >> c[i];
	// 0<= i <=n and 0<=j <n
	// a[i] == b[c[j]]
	// a[i] == b[k] b[k]の値が a[i] に等しい i の数をam[k] に貯める
	map<int,int> am,bm;
	// a の値 の数 値 でindex b の値にとマッチさせる
	rep(i,n) am[a[i]]++;
	// c の値 の 数 値でindex でもb のindex にする
	rep(i,n) bm[c[i]-1]++;
	ll ans = 0;
	rep(i,n) ans += am[b[i]]*bm[i];
	cout << ans << endl;
	return 0;
}


#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define fp(x, a, b) for(int x = a; x < b; x++)
#define fn(x, a, b) for(int x = a; x > b; x--)
#define f(x, m) for(auto x : m)
#define cpu() ios::sync_with_stdio(false); cin.tie(nullptr)
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pair<int ,int>>
#define vll vector<pair<ll ,ll>>
#define all(v) v.begin(),v.end()
#define sor(a) sort( a.begin(), a.end() )
#define ros(a) sort( a.rbegin(), a.rend())
#define prec(n) fixed << setprecision(n)
#define ff first
#define ss second
// #define tt third
#define print(x) for(auto it : x) cout << it << " ";
#define debug(x) cerr << #x << " is " << x << endl;
// #define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>

typedef long long ll;
using namespace std;
// using namespace __gnu_pbds;
#define dbg(args...){ string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << "NEXT\n"; }
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
cerr << *it << " = " << a << ", ";
err(++it, args...);
}
template<typename... T>
void rd(T& ... args){
  ((cin >> args), ...);
}
template<typename... T>
void ps(T ... args){
  ((cout << args << ' '), ...);
  cout<<'\n';
}
const ll MOD = 1e9 + 7, MOD1 = 998244353LL, MAXI = 5e6 + 100, MAX = 1e5 + 5;
const char nl = '\n';
const int INF = 1e9 + 5;
int a, b, c;
void solve(){
  cin >> a >> b >> c;
  if((c == 0 && a >= b + 1) || (c == 1 && a >= b)){
    cout << "Takahashi\n";
  }
  else{
    cout << "Aoki\n";
  }
}
int main(){
  cpu();
  // int __;
  // cin >> __;
  // while(__--){
  //   solve();
  // }
  solve();
  // cout << nl;

  return 0;
}

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define eb emplace_back
using namespace std;
template <class T = int>
using V = vector<T>;
template <class T = int>
using VV = V<V<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using pdd = pair<ld,ld>;

const int mod = 1000000007;
struct mint {
    ll x;
    mint(ll x=0):x((x%mod+mod)%mod){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
    mint operator+(const mint a) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(ll t) const {
        if(!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if(t&1) a *= *this;
        return a;
    }

    mint inv() const { return pow(mod-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
struct combination {
    vector<mint> fact, ifact;
    combination(int n):fact(n+1),ifact(n+1) {
        assert(n < mod);
        fact[0] = 1;
        for(int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
        ifact[n] = fact[n].inv();
        for(int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
    }
    mint operator()(int n, int k) {
        if(k < 0 || k > n) return 0;
        return fact[n]*ifact[k]*ifact[n-k];
    }
    mint p(int n, int k) {
        return fact[n]*ifact[n-k];
    }
} c(2000005);

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n, m, k;
    cin >> n >> m >> k;
    if(n > m+k){
        cout << 0 << endl;
    }
    else cout << c(n+m, n) - c(n+m, m+k+1) << endl;
}


#include<stdio.h>
int main(void){
  int a,b,c;
  scanf("%d %d %d",&a,&b,&c);
  printf("%d",21-(a+b+c));
  return 0;
}

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

int main() {
  int a,b,c;
  cin>>a>>b>>c;

  cout<<21-a-b-c<<endl;
}


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

#define int long long
#define forn(i,a,n) for (int i=a; i<n; i++)

int n;
int a[1001],b[1001];

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

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

	cin>>n;
	forn(i,0,n)
		cin>>a[i]>>b[i];
	int cnt=0;
	forn(i,0,n){
		forn(j,i+1,n){
			long double x=a[i]-a[j];
			long double y=b[i]-b[j];
			if (y/x>=-1 && y/x<=1){
				cnt++;
			}
		}
	}
	cout<<cnt<<endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
template<typename T, typename V> using umap = unordered_map<T, V>;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vll = vector<ll>;
using vs = vector<string>;
using mapii = map<int, int>;
using umapii = umap<int, int>;
#define _rep(i, n) _repi(i, 0, n)
#define _repi(i, a, b) for(ll i = (ll) (a); i < (ll) (b); i++)
#define _get_rep(_1, _2, _3, NAME, ...) NAME
#define rep(...) _get_rep(__VA_ARGS__, _repi, _rep) (__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define chmax(x, y) (x) = max((x), (y))
#define chmin(x, y) (x) = min((x), (y))
#define mkpair make_pair
#define print(...) osout(cout, __VA_ARGS__)
#define dbg(...) osout(cerr, __VA_ARGS__)
template<typename T> istream& operator >>(istream& is, vector<T>& v) {
    rep(i, v.size()) { cin >> v[i]; } return is;
}
template<typename T1, typename T2> ostream& operator <<(ostream& os, pair<T1, T2> p) {
    os << p.first << ", " << p.second; return os;
}
template<typename T> ostream& operator <<(ostream& os, vector<T> v) {
    if(v.size()==0){ return os; } rep(i, v.size()-1) { os << (v[i]) << ' '; } os << v[v.size()-1]; return os;
}
void osout(ostream& os) { os << endl; }
template<class S, class... T> void osout(ostream& os, S s, T... t) {
    os << (s); if (sizeof...(t)) os << ' '; osout(os, t...);
}
void solve();

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // cout << fixed << setprecision(15);
    solve();
    return 0;
}

void solve() {
    ll a,b,c,d; cin>>a>>b>>c>>d;
    print(min({a,b,c,d}));
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define pb                    push_back
#define ll                    long long
#define ld                    long double
#define sz(x)                 (ll)x.size()
#define vll                   vector<long long>

#define infi                  INT_MAX
#define infl                  LONG_LONG_MAX
#define infd                  LDBL_MAX
#define f(i,a,b)              for(ll i=a;i<b;i++)
#define fi(i,a,b)             for(ll i=(b-1);i>=a;i--)
#define F                     first
#define S                     second
#define G(a,b)                get<a>(b)
#define MP                    make_pair
#define MT                    make_tuple
#define pll                   pair<ll,ll>
#define endl                  "\n"
#define ALL(v)                v.begin(),v.end()
#define nl                    cout<<"\n";
#define pr(x)                 cout<<x;
#define pr1(x)                cout<<x<<" ";
#define pr2(x,y)              cout<<x<<" "<<y;
#define pr3(x,y,z)            cout<<x<<" "<<y<<" "<<z;
#define pr4(x,y,z,w)          cout<<x<<" "<<y<<" "<<z<<" "<<w;
#define deb                   cout<<"***************************************************************************************\n";
#define moshi                 cout<<"moshi moshi ! \n";
#define hi                    cout<<"hi ! \n";
#define bye                   cout<<"bye bye ! \n";
#define kawai                 cout<<"O kawai koto ! \n";
#define o_set                 tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
#define number_of_set_bits  __builtin_popcountll
#define eb                    emplace_back
const ld PI=3.1415926535897932384626433;

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

auto clk=clock();
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 <<"\n";
        //use cerr if u want to display at the bottom
    }
    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...);
    }
#else
#define trace(...)
#endif

//**********************************************************  Code begins  ***********************************************

// trick for traversing all submasks
// for (ll j=mask ;j; j=(j-1)&mask)  // also include 0 manually
// v.resize(unique(v.begin(), v.end()) - v.begin());    // remove duplicates

char s[15];

void solve()
{
  f(i,0,12) cin>>s[i];
  ll ans=0;
  f(i,0,9)  if(s[i]=='Z' && s[i+1]=='O' && s[i+2]=='N' && s[i+3]=='e') ans++;
  cout<<ans<<"\n";
}

int main()
{
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout<<fixed<<setprecision(8);

    ll test_cases=1;
    //cin>>test_cases;

    f(i,1,test_cases+1)
    {
        solve();
        //clr();
    }

    #ifdef rd
    cout<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
    #endif

    return 0;
}

#include <bits/stdc++.h>

#define DeltaCube23 ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define pb push_back
#define mp make_pair
#define minimum(a) *min_element(a.begin(), a.end())
#define maximum(a) *max_element(a.begin(), a.end())
#define MOD 1000000007
#define forf(i,a,b) for(int i = (a); i < (b); i++)
#define forr(i,a,b) for(int i = (a); i >= (b); i--)

typedef long long int lli;
lli binpow(lli a,lli b,lli m){lli res=1;for(b%=m-1,a%=m;b>0;b>>=1,a=a*a%m)if(b&1)res=res*a%m;return res;}
lli gcd(lli a, lli b) {return b? gcd(b, a%b) : a;}

#define int lli //CAREFUL
using namespace std;

const int mx=5e5+5;

signed main()
{
    DeltaCube23
    int t=1;
    //cin>>t;

	while(t--)
    {
		int n;
		cin>>n;

		vector<int> a(n);
		int count = 1;

		a[0] = 105;

		forf(i,6,10000)
		{
			if(i % 2 == 0)
			{
				if(i % 3 == 0 || i % 5 == 0 || i % 7 == 0)
				{
					a[count] = i;
					count++;
					if(count == n)
					break;
				}
			}
		}

		forf(i,0,n)
		cout<<a[i]<<" ";
	}

    return 0;
}


#include<bits/stdc++.h>
#define w(x)            int x; cin>>x; for(int tc=1;tc<=x;tc++)
#define trace(x) 		cerr<<#x<<": "<<x<<" "<<endl;
#define FIO             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
#define pb push_back
#define pqg priority_queue<int>
#define pqs priority_queue<int,vector<int>,greater<int>>
#define mpii map<int,int>
#define endl "\n"
#define YES cout<<"YES\n";
#define Yes cout<<"Yes\n";
#define NO cout<<"NO\n";
#define No cout<<"No\n";
#define mod 1000000007
#define inp(x) cin>>x;

using namespace std;
void solve(int tc) {
	int a, b;
	cin >> a >> b;
	int c = b;
	a += b;
	b = c;
	if (a >= 15 and b >= 8) {
		cout << 1 << endl;
	} else if (a >= 10 and b >= 3) {
		cout << 2 << endl;
	} else if (a >= 3) {
		cout << 3 << endl;
	} else
		cout << 4 << endl;

}

int32_t main() {
	FIO
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	// w(x){
	solve(1);
	// }
	return 0;
}


#pragma GCC optimize(2)
#include<iostream>
#include<unordered_map>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<map>
#include<set>
#define Buff ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define rush() int Case = 0; int T; cin >> T;  while(T--)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define per(i, a, b) for(int i = a; i >= b; i --)
#define reps(i, a, b) for(int i = a; b; i ++)
#define clc(a, b) memset(a, b, sizeof(a))
#define Buff ios::sync_with_stdio(false)
#define readl(a) scanf("%lld", &a)
#define readd(a) scanf("%lf", &a)
#define readc(a) scanf("%c", &a)
#define reads(a) scanf("%s", a)
#define read(a) scanf("%d", &a)
#define lowbit(n) (n&(-n))
#define pb push_back
#define sqr(x) x*x
#define lson rt<<1
#define rson rt<<1|1
#define ls lson, l, mid
#define rs rson, mid+1, r
#define y second
#define x first
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>PII;
const int mod = 1e9+7;
const double eps = 1e-6;
const int N = 1e6+7;
string s;
string t = "atcoder";
bool judge()
{
    reps(i, 0, s[i])    if(s[i] != 'a') return false;
    return true;
}
int main()
{
    int T;
    cin >> T;
    while(T --)
    {
        cin >> s;
        if(judge())
        {
            puts("-1");
            continue;
        }
        int res = mod;
        for(int i = 0; s[i]; i ++)
        {
            for(int j = i; s[j]; j ++)
            {
                swap(s[i], s[j]);
                if(s > t)
                {
                    res = min(res, j-i);
                    swap(s[i], s[j]);
                    break;
                }
                swap(s[i], s[j]);
            }
        }
        cout << res <<endl;
    }
	return 0;
}
/*
aaaaaaatcoder
*/

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

#define REP(i, s) for (int i = 0; i < s; ++i)
#define ALL(v) (v).begin(), (v).end()
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define DEBUG
#define int long long
#define INF 1e18
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; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ EACH(it, P) { s << "<" << *it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<"\n";}
typedef long long ll;

string x="atcoder";

void solve(){
    string s;
    cin>>s;
    //cerr<<string(s.size(),'a')<<endl;;
    if(s>x){
        cout<<0<<endl;
        return ;
    }
   if(s==string(s.size(),'a')){
       cout<<-1<<endl;
   }
   else{
      for (int i = 0; i < s.size(); i++)
      {
          if(s[i]!='a'){
              if(s[i]>'t'){
                  cout<<i-1<<endl;
              }
              else{
                  cout<<i<<endl;
              }
              return ;
          }
      }

   }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}


/*
Author : Aditya Gupta
Team : TechTurtle
Date : 29-08-2020
Time : 20:05:54
*/

#include<bits/stdc++.h>
#define ll long long int
#define ld long double
#define vll vector<ll>
#define vvll vector< vll >
#define vld vector< ld >
#define vvld vector< vld >
#define pll pair<ll ,ll >
#define vpll vector< pll >
#define mp make_pair
#define pb push_back
#define MOD 1000000007
#define endl "\n"
#define flash ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test ll t;cin>>t;while(t--)
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define fst first
#define scd second
#define mk(arr,n,type)  type *arr=new type[n];

#define forn(i,n) for(ll (i) = 0 ; (i) < (n) ; ++(i))
#define for1(i,n) for(ll (i) = 1 ; (i) <= (n) ; ++(i))
#define forr(i,n) for(ll (i) = (n)-1 ; (i)>=0 ; --(i))
#define forab(i,a,b,c) for(ll (i) = a ; (i) <= (b) ; (i)+=(c))

using namespace std;

///////////////////////////////////////////////////////////////////////// Maths //////////////////////////////////////////////////////////////////////////////

//vll sieve;
//void Sieve(int N){  //smallest prime factor
//	const ll maxn = N;
//	sieve.resize(maxn);
//	forn(i,maxn) sieve[i] = i;
//	sieve[1] = -1;
//	sieve[0] = -1;
//	forab(i,2,maxn,1) if(i == sieve[i]) for(ll j = i*i ; j < maxn ; j+=i) if(sieve[j] == j) sieve[j] = i;
//
////	for(auto i:sieve){
////		cout<<i<<" ";
////	}
//
//}
vll sieve;
vll* Sieve(int N) {
	const ll maxn = N;
	sieve.resize(maxn);
	forn(i,maxn) sieve[i] = 1;
	sieve[1] = 0;
	sieve[0] = 0;
	forab(i,2,maxn,1) if(1 == sieve[i]) for(ll j = i*i ; j < maxn ; j+=i) sieve[j] = 0;

	vll* primes=new vll();
	primes->pb(2);
	for(int i=3; i<maxn; i+=2)
		if(sieve[i]) {
			primes->pb(i);
		}
	return primes;

}

ll extended_GCD(ll a , ll b , ll &x , ll &y) {
	if(a == 0) {
		x = 0;
		y = 1;
		return b;
	}
	ll x1 , y1;
	ll gcd = extended_GCD(b%a , a , x1 , y1);
	x = y1 - (b/a)*x1;
	y = x1;
	return gcd;
}

ll ncr(ll n,ll k) {
	ll r=1;
	if(k>n-k)
		k=n-k;
	for(ll i=0; i<k; i++) {
		r*=(n-i);
		r/=(i+1);
	}
	return r;
}

ll power(ll a, ll b,ll m = MOD) {
	ll res = 1;
	while (b > 0) {
		if (b & 1)
			res = (res * a)%m;
		a = (a * a)%m;
		b >>= 1;
	}

	return res;
}

ll modinv(ll a , ll mod = MOD) {
	ll x , y;
	extended_GCD(a , mod , x , y);
	if(x < 0) x += mod;
	return x;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
bool prime(int n) {
	int c=0;
	for(int i=2; (i*i)<=n; i++) {
		if(n%i==0) {
			c++;
			return false;
		}
	}
	if(c==0)
		return true;
}
// bool compareInterval(char i1, char i2)
//{
//    return (i1 < i2);
//}
// int countoperation(int n,m){
// 	vector<int> d<m+1,-1>;
// }
vector<int> v[200006];
int vis[200006];
//int c;
int in[200006];
vector<int> res;

bool khan(int n){
	priority_queue<int,vector<int>,greater<int>> q;
	for(int i=1;i<=n;i++){
		if(in[i]==0)
		q.push(i);
	}

	while(!q.empty()){
		int curr=q.top();
		res.pb(curr);
		q.pop();

		for(int node:v[curr]){
			in[node]--;
			if(in[node]==0)
			q.push(node);
		}
	}

	return n==res.size();

}

void dfs(int n){
	vis[n]=1;
	cout<<n<<" ";
	for(int i:v[n]){
		if(!vis[i])
		dfs(i);
	}

}

void solve() {
	ll n,m,t;
	vector<pll> v;
	cin>>n>>m>>t;
	ll f=1;
	ll l=0;
	ll mx=n;
	forn(i,m)
	{
		ll a,b;
		cin>>a>>b;
		v.pb({a,b});

		n-=(a-l);

		if(n<=0)
		f=0;

		if(n+(b-a)>mx)
		n=mx;
		else
		n+=(b-a);

//		cout<<n<<"*";

		l=b;

	if(i==m-1){
		n-=t-b;
		if(n<=0)
		f=0;
	}
	}
	if(f)
	cout<<"Yes\n";
	else
	cout<<"No\n";

}

int main() {
	flash
//	freopen("input.txt","r",stdin);
//	freopen("output.txt","w",stdout);

//	test
	solve();
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll         = long long;
constexpr ll MOD = 1000000007;
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define REP2(i, x, n) for (ll i = x; i < (n); i++)
#define PR(x) cout << (x) << "\n"
#define PS(x) cout << (x) << " "
#define PRYES(x) PR((x) ? "Yes" : "No")
const long long INF          = numeric_limits<long long>::max();
template <class T> using arr = vector<vector<T> >;
using vint                   = vector<int>;
using vll                    = vector<ll>;
// SourceTest用出力マクロ
#ifdef SOURCE_TEST
#define SPR(x) PR(x)
#define SPS(x) PS(x)
#define STRACE(var) cout << #var << "=" << var << endl;
#else
#define SPR(x) \
    {}
#define SPS(x) \
    {}
#define STRACE(var) \
    {}
#endif
bool isOK(ll N, ll M, ll T) {
    ll nMax    = N;
    ll nowTime = 0;
    for (ll i = 0; i < M; ++i) {
        ll A, B;
        cin >> A >> B;

        N -= A - nowTime;
        SPR(N);
        if (N <= 0) return false;
        N += B - A;
        N = min(N, nMax);

        SPR(N);
        nowTime = B;
    }
    N -= T - nowTime;
    SPR(N);
    return N > 0;
}
int main() {
    ll N, M, T;
    cin >> N >> M >> T;

    PR(isOK(N, M, T) ? "Yes" : "No");
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define mp make_pair
#define f(i,n) for(int i=0;i<n;i++)
#define F first
#define S second
#define pb push_back

using namespace std;

void test(){
	ll n,m;
	cin>>n>>m;
	ll o = 0, e = 0;
	f(i,n){
		string s;
		cin>>s;
		int cnt = 0;
		f(j,m){
			if(s[j]=='1')cnt++;
		}
		if(cnt%2==0)e++;
		else o++;
	}
	ll res = o*e;
	cout<<res<<"\n";

}

int main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int tests=1;
	// cin>>tests;
	while(tests--){
		test();
	}
}

// 2021-04-17 16:11:14
// clang-format off
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

// clang-format on
void answer() {
  ll x, y, z;
  cin >> x >> y >> z;
  ll ans = -1;
  rep(a, 2000000) {
    if (a * x < y * z) chmax(ans, ll(a));
  }
  cout << ans << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}


using namespace std;
#include<string>
#include<cmath>
#include<list>
#include <map>
#include <unordered_map>
#include <set>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ml map<ll,ll>
#define pb push_back
#define mp make_pair
#define ppl pair<ll,ll>
#define F first
#define S second
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)

#define fr(i,a,b) for(ll i=a;i<b;i++)
#define ffr(i,a,b) for(ll i=a;i>=b;i--)
#include<bits/stdc++.h>
#include<vector>
#define vl vector<ll>
#define PI 3.141592654
const ll N = 1e9 + 7;
#include<iterator>
#define EPSILON numeric_limits<double>::epsilon()

// vl b;
// vector<vl> v;
// ll n, m;
// vl link, val;
// vector<char>a;
// vector<bool> visited;

// void dfs(ll pnt, ll lin) {
// 	link[pnt] = lin;
// 	visited[pnt] = true;
// 	fr(i, 0, v[pnt].size()) {
// 		if (!visited[v[pnt][i]])dfs(v[pnt][i], lin);
// 	}
// }

// vl pr(N, 0);

// void precompute() {
// 	pr[1] = 1;
// 	fr(i, 2, N + 1) {
// 		ll d = i, g = 0;
// 		while (d > 0) {
// 			if (d % 10 == 7) {g = 1; break;}
// 			d /= 10;
// 		}
// 		if (g) {pr[i] = pr[i - 1]; continue;}
// 		d = i;
// 		while (d > 0) {
// 			if (d % 8 == 7) {g = 1; break;}
// 			d /= 8;
// 		}
// 		if (g) {pr[i] = pr[i - 1]; continue;}
// 		else pr[i] = pr[i - 1] + 1;
// 	}
// }

// void prime() {
// 	pr[1] = 1;
// 	fr(i, 2, N + 1)pr[i] = i;
// 	fr(i, 4, N + 1) {pr[i] = 2; i++;}
// 	fr(i, 3, N + 1) {
// 		if (pr[i] == i) {
// 			for (ll j = i * i; j <= N; j += i) {if (pr[j] == j)pr[j] = i;}
// 		}
// 	}
// }

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

// ll fact(ll n) {
// 	ll res = 1;
// 	fr (i, 2, n + 1) res = (res * i) % N;
// 	return res;
// }

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

void solve() {
	ll n; cin >> n;
	ll a[n], b[n], ma = 0, maxi = 0, prev = 0;
	fr(i, 0, n)cin >> a[i];
	fr(i, 0, n)cin >> b[i];
	fr(i, 0, n) {
		if (a[i] > ma) {
			ma = a[i];
			maxi = 0;
		}
		maxi = max(maxi, b[i]);
		cout << max(prev, maxi * ma) << '\n';
		prev = max(maxi * ma, prev);
	}
}

int main() {
	fio;
#ifndef ONLINE_JUDGE
	// for getting input
	freopen("input.txt", "r", stdin);
	//for writing output
	freopen("jout.txt", "w", stdout);
#endif
	// prime();
	// precompute();
	ll t;
	if (1)
		t = 1;
	else
		cin >> t;
	while (t--) {
		//cout << "Case #" << h << ": ";
		solve();
		//h++;
		cout << '\n';
	}
	return 0;
}

#include <iostream>
#include <cstdio>

using namespace std;

typedef long long LL;

const int N = 200010;

LL a[N], b[N];
long long c[N];

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

    for (int i = 0; i < n; i ++)    scanf("%d", &a[i]);
    for (int i = 0; i < n; i ++)    scanf("%d", &b[i]);

    LL res = 0, maxd = 0;
    res = c[0] = a[0] * b[0];
    maxd = a[0];
    for (int i = 1; i < n; i ++) {
        maxd = max(a[i], maxd);
        if (maxd * b[i] > res) {
            c[i] = res = maxd * b[i];
        }
        else c[i] = res;
    }

    for (int i = 0; i < n; i ++)    printf("%lld\n", c[i]);
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
//reverse(s.begin(),s.end())
ll gcd(ll a,ll b){
    ll s=1,c;
    while(a&&b){
        if((~a&1)&&(~b&1))
            a>>=1,b>>=1,s<<=1;
        else if(~a&1)a>>=1;
        else if(~b&1)b>>=1;
        else if(a>b)a=a-b;
        else c=b-a,b=a,a=c;
    }
    if(!a)return b*s;
    if(!b)return a*s;
}
int main(){
	int n,m;
	cin>>n>>m;
	int x,y;
	cin>>x>>y;
	char mz[n+1][m+1];
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>mz[i][j];
		}
	}
	int ans=1;
	int tx=x;
	int ty=y;
	while(mz[tx+1][y]=='.'){
		ans++;
		tx++;
	}
	tx=x;
	while(mz[tx-1][y]=='.'){
		ans++;
		tx--;
	}
	ty=y;
	while(mz[x][ty+1]=='.'){
		ans++;
		ty++;
	}
	ty=y;
	while(mz[x][ty-1]=='.'){
		ans++;
		ty--;
	}
	cout<<ans;
}

#include <algorithm>
#include <array>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>

using namespace std;

using ll = long long;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define sz(x) (int)(x).size()
#define ms(arr, v) memset(arr, v, sizeof(arr))
#define f first
#define s second
#define pb push_back

// clang-format off
template<typename T> ostream &operator<<(ostream &os, const vector<T> &vec){ os << "["; for (auto v : vec) os << v << ","; os << "]"; return os; }
template<typename T_vector>void output_vector(const T_vector &v, bool add_one = false, int start = -1, int end = -1) {if (start < 0) start = 0; if (end < 0) end = int(v.size()); for (int i = start; i < end; i++) cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n');}
template<typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &pa){ os << "(" << pa.first << "," << pa.second << ")"; return os; }
template<typename TK, typename TV> ostream &operator<<(ostream &os, const map<TK, TV> &mp){ os << "{"; for (auto v : mp) os << "(" << v.first << ":" << v.second << ")" << ","; os << "}"; return os; }

auto random_address = [] {char *p = new char; delete p;return uint64_t(p);};
const uint64_t SEED = chrono::steady_clock::now().time_since_epoch().count() * (random_address() | 1);
mt19937_64 rng(SEED);
// clang-format on

void solve() { }

int main() {
     ios::sync_with_stdio(0);
     cin.tie(0);
     int m, n, x, y;
     cin >> m >> n >> x >> y;

     string mat[m];
     for (int i = 0; i < m; i++) {
          cin >> mat[i];
     }
     int ans = 0;
     --x;
     --y;
     int t1 = x, t2 = y, t3 = x, t4 = y;
     while (t1 + 1 < m) {
          if (mat[t1 + 1][y] == '#')
               break;
          else if (mat[t1 + 1][y] == '.') {
               ans++;
          }
          t1++;
     }
     while (t2 + 1 < n) {
          if (mat[x][t2 + 1] == '#')
               break;
          else if (mat[x][t2 + 1] == '.') {
               ans++;
          }
          t2++;
     }
     while (t3 - 1 >= 0) {
          if (mat[t3 - 1][y] == '#')
               break;
          else if (mat[t3 - 1][y] == '.') {
               ans++;
          }
          t3--;
     }
     while (t4 - 1 >= 0) {
          if (mat[x][t4 - 1] == '#')
               break;
          else if (mat[x][t4 - 1] == '.') {
               ans++;
          }
          t4--;
     }
     cout << ans + 1;
}