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

int main(void)
{
    int h,w;
    ll n,m;
    cin >> h >> w >> n >> m;

    vector<vector<bool>>ab(h,vector<bool>(w,false));
    vector<vector<bool>>cd(h,vector<bool>(w,false));

    rep(i,n)
    {
        int ai,bi;
        cin >> ai >> bi;
        ab[ai-1][bi-1] = true;
    }
    rep(i,m)
    {
        int ci,di;
        cin >> ci >> di;
        cd[ci-1][di-1] = true;
    }

    int ans = 0;

    rep(i,h)
    {
        rep(j,w)
        {
            if(cd[i][j])
            {
                continue;
            }
            if(ab[i][j])
            {
                ans++;
                continue;
            }

            //上下左右に探索
            bool ok = false;
            for(int hh = i; hh < h; hh++)
            {
                if(cd[hh][j]) break;
                if(ab[hh][j])
                {
                    ans++;
                    ok =true;
                    break;
                }
            }
            if(ok) continue;

            for(int hh = i; hh >= 0; hh--)
            {
                if(cd[hh][j]) break;
                if(ab[hh][j])
                {
                    ans++;
                    ok =true;
                    break;
                }
            }
            if(ok) continue;
            for(int ww= j; ww >= 0; ww--)
            {
                if(cd[i][ww]) break;
                if(ab[i][ww])
                {
                    ans++;
                    ok =true;
                    break;
                }
            }
            if(ok) continue;
            for(int ww= j; ww < w; ww++)
            {
                if(cd[i][ww]) break;
                if(ab[i][ww])
                {
                    ans++;
                    ok =true;
                    break;
                }
            }
            if(ok) continue;
        }
    }

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

#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<map>
#include<tuple>
#include<algorithm>
#include<cmath>
#include<limits>
#include<set>
#include<deque>
#include<queue>
#include<stack>
using namespace std;
#define int long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define dup(x,y) (((x)+(y)-1)/(y))
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a / gcd(a, b) * b; }
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; }
typedef pair<int, int>P;
const int MOD = 1e9 + 7;
//const int MOD = 998244353;
const int INF = 1e18;
const long double PI = (acos(-1));

signed main() {
	int X, Y, A, B;
	cin >> X >> Y >> A >> B;
	int ans = 0;
	while (1) {
		if (X * A < Y && (double)A <= 1e18 / (double)X && X * A < X + B) {
			ans++;
			X = X * A;
		}
		else break;
	}
	ans += (Y - X - 1) / B;
	cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;
using P = pair<int,int>;
#define rep(i,n) for (int i = 0; i < (int)(n) ; i++)
#define rep2(i,a,b) for (int i = (a); i < (int)(b); i++)
#define repR(i,n) for (int i = (int)(n)-1; i >= 0; i--)
#define repR2(i,a,b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ENDL '\n'
#define pb push_back
#define SIZE(a) (int)(a.size())
#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define UNIQUE(a) a.erase(unique(all(a)),a.end());
#define debug(x) cout<<(#x)<<" is : "<<x<<endl;
#define debugc(vec) {cout<<(#vec)<<" is below; ---"<<endl;for(auto elements:vec)cout<<elements<<" ";cout<<endl<<"----------------"<<endl;}
inline string rev(string s) {string t(s.rbegin(), s.rend()); return t;}
template<class T> inline bool chmin(T &a, T b) { if(a>b) {a=b; return 1;} return 0;}
template<class T> inline bool chmax(T &a, T b) { if(a<b) {a=b; return 1;} return 0;}
constexpr ll INF = (1LL<<30)-1;
constexpr ll INFLL = (1LL<<62)-1;
constexpr ld PI = 3.14159265358979323846L;

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

    int n,m; cin>>n>>m;
    ll ans=INFLL;
    vll a(n), w(m), lsum(n), rsum(n);
    rep(i, n) cin>>a[i];
    rep(i, m) cin>>w[i];
    sort(ALL(a));
    sort(ALL(w));
    for(int i=1; i<=n-2; i+=2) {
        if(i==1) lsum[i]=a[i]-a[i-1];
        else lsum[i]=lsum[i-2] + a[i]-a[i-1];
    }
    for(int i=n-2; i>=1; i-=2) {
        if(i==n-2) rsum[i]=a[i+1]-a[i];
        else rsum[i]=rsum[i+2] + a[i+1]-a[i];
    }

    auto floorodd =  [](int x) -> int {
        if(x<=0) return 0;
        if(x%2==0) return x-1;
        return x;
    };
    auto ceilodd = [&](int x) -> int {
        if(x>=n-1) return n-1;
        if(x%2==0) return x+1;
        return x;
    };

    int left=0, right=1;
    ll sum=0;
    if(w[0]<a[0]) {left=-1; right=0;}
    else {
        while(left<n-1 && a[left+1]<w[0]) {
            left++;
            right++;
        }
    }
    sum += lsum[floorodd(left)] + rsum[ceilodd(right)];
    if(left%2==0) sum+=w[0]-a[left];
    else sum+=a[right]-w[0];
    chmin(ans, sum);

    for(int i=1; i<m; i++) {
        sum=0;
        while(left<n-1 && a[left+1]<w[i]) {
            left++;
            right++;
        }
        sum += lsum[floorodd(left)] + rsum[ceilodd(right)];
        if(left%2==0) sum+=w[i]-a[left];
        else sum+=a[right]-w[i];

        chmin(ans, sum);
    }
    cout << ans << endl;
}

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

constexpr ll mod=1e9+7;

char c[2010][2010];
int yoko[2010][2010];
int tate[2010][2010];

ll mod_pow(ll a,ll b){
	a%=mod;
	if(b==0)return 1;
	if(b==1)return a;
	ll res=mod_pow(a,b/2)%mod;
	res*=res; res%=mod;
	if(b%2)res*=a;
	return res%mod;
}

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int h,w; cin >> h >> w;
	int al=0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			cin >> c[i][j];
			if(c[i][j]=='.')al++;
		}
	}
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(c[i][j]=='.'){
				int cnt=0;
				int jj=j;
				while(jj<w and c[i][jj]=='.'){
					cnt++; jj++;
				}
				while(j<jj){
					yoko[i][j]=cnt;
					j++;
				}
			}
		}
	}
	for(int i=0;i<w;i++){
		for(int j=0;j<h;j++){
			if(c[j][i]=='.'){
				int cnt=0;
				int jj=j;
				while(jj<h and c[jj][i]=='.'){
					cnt++; jj++;
				}
				while(j<jj){
					tate[j][i]=cnt;
					j++;
				}
			}
		}
	}
	ll res=0;
	ll aaa=mod_pow(2,al);
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(c[i][j]=='.'){
				res+=aaa;
				res-=mod_pow(2,al-tate[i][j]-yoko[i][j]+1);
				if(res>mod)res-=mod;
				if(res<0)res+=mod;
			}
		}
	}
	if(res<0)res+=mod;
	cout << res << endl;
}


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

const int maxl=4e5+10;
const int mod=998244353;

int n,m;ll ans;
int p[maxl],dy[maxl];
ll fac[maxl],inv[maxl],dp[maxl];
bool no[maxl];

inline void shai()
{
	for(int i=2;i<maxl;i++)
	{
		if(!no[i]) p[++p[0]]=i,dy[i]=i;
		int j=1,t=p[1]*i;
		while(j<=p[0] && t<maxl)
		{
			no[t]=true;dy[t]=p[j];
			if(i%p[j]==0)
				break;
			t=p[++j]*i;
		}
	}
}

inline ll qp(ll a,ll b)
{
	ll ans=1,cnt=a;
	while(b)
	{
		if(b&1) ans=ans*cnt%mod;
		cnt=cnt*cnt%mod;
		b>>=1;
	}
	return ans;
}
inline void prework()
{
	shai();
	fac[0]=1;
	for(int i=1;i<maxl;i++)
		fac[i]=fac[i-1]*i%mod;
	inv[maxl-1]=qp(fac[maxl-1],mod-2);
	for(int i=maxl-2;i>=0;i--)
		inv[i]=inv[i+1]*(i+1)%mod;
	scanf("%d%d",&n,&m);
}

inline ll c(int n,int r)
{
	if(r>n || r<0) return 0;
	return fac[n]*inv[n-r]%mod*inv[r]%mod;
}
inline void add(ll &a,ll b){a=(a+b)%mod;}

inline void mainwork()
{
	dp[1]=1;ans=1;
	for(int i=2;i<=m;i++)
	{
		int x=i,d=dy[i],cnt=0;
		while(x%d==0 && x>1)
			cnt++,x/=d;
		dp[i]=dp[x]*c(cnt+n-1,n-1)%mod;
		add(ans,dp[i]);
	}
}

inline void print()
{
	printf("%lld\n",ans);
}

int main()
{
	prework();
	mainwork();
	print();
	return 0;
}

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

int mod = 998244353;

long long fac[200005], finv[200005], inv[200005];

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

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

long long dp[35][200005];

int main() {
    int N,M;
    cin >> N >> M;
    COMinit(mod);
    for(int i = 1; i <= M; i++) {
        dp[0][i] = 1;
    }
    for(int i = 0; i < 30; i++) {
        for(int j = 1; j <= M; j++) {
            for(int k = 2; j*k <= M; k++) {
                dp[i+1][j*k] += dp[i][j];
            }
        }
    }
    long long ans = 0;
    for(int i = 0; i < 30; i++) {
        long long sum = 0;
        for(int j = 1; j <= M; j++) {
            sum += dp[i][j];
        }
        ans += sum*COM(N-1,i,mod)%mod;
        ans %= mod;
    }
    cout << ans << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rapido ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"

void solve()
{
    int a,b,c,d;cin>>a>>b>>c>>d;
    int ans=INT_MIN;
    for(int i=a;i<=b;i++)
    {
        for(int j=c;j<=d;j++)
        {
            ans=max(ans,i-j);
        }
    }
    cout<<ans<<endl;
}
int32_t main()
{
    rapido;
    int t=1;
    //cin>>t;
    while(t--)
    solve();
	return 0;
}

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

int main(){
  int a,b,c,d;
  cin >> a >> b >> c >> d;
  cout << b - c << endl;
return 0;
}


#include<bits/stdc++.h>
using namespace std;
bool flag;
string x[10000010];
int main(void){
	long long n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>x[i];
		reverse(x[i].begin(),x[i].end());
	}
	sort(x+1,x+n+1);
	for(int i=1;i<n;i++){
		if(x[i]+'!'==x[i+1]){
			reverse(x[i].begin(),x[i].end());
			cout<<x[i]<<endl;
			flag=1;
			break;
		}
	}
	if(flag!=1){
		cout<<"satisfiable";
	}
}

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

#define ll long long int
#define ld long double
#define mod 998244353
#define big 1e18 + 1000000
#define small -big
#define pb push_back
#define endl "\n"

template <typename T>

void prarr(T a)
{
     cerr << "[ ";

     for(auto i : a){
         cerr << i << " ";
     }

     cerr << "]" << endl;
}

#define trace(...) _er(#__VA_ARGS__, __VA_ARGS__)

template <typename Arg1>

void _er(const char* name, Arg1&& arg1){
	cerr << "[" << name << " : " << arg1 << "]" << endl;
}

template <typename Arg1, typename... Args>

void _er(const char* names, Arg1&& arg1, Args&&... args){

	const char* comma = strchr(names + 1, ',');
	cerr << "[" ; cerr.write(names, comma - names) << " : " << arg1 << "] ";
	_er(comma+2, args...);
}

void solve() {

         ll N;
         cin >> N;

         char A[N + 1];

         for(ll i = 1; i <= N; i++) {
                cin >> A[i];

         }

          ll ans[N + 1][26];
         memset(ans, 0, sizeof(ans));

         for(ll i = 1; i <= N; i++) {

                for(ll j = 0; j < 26; j++) {
                        ans[i][j] += ans[i - 1][j];
                }
                ans[i][A[i] - 65]++;

         }

         ll d = 0;

         for(ll i = 1; i <= N; i++) {
                for(ll j = i; j <= N; j++) {
                        ll a = ans[j]['A' - 65] - ans[j - i]['A' - 65];
                        ll c = ans[j]['C' - 65] - ans[j - i]['C' - 65];
                        ll g = ans[j]['G' - 65] - ans[j - i]['G' - 65];
                        ll t = ans[j]['T' - 65] - ans[j - i]['T' - 65];

                        if(a == t && g == c) {
                                d++;
                        }
                }
         }

         cout << d << endl;

}

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

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

    for(int i = 1; i <= t; i++) {

         solve();
    }

    return 0;

}


//#include <atcoder/all>
#include <bits/stdc++.h>

using namespace std;

#define rep2(x,fr,to) for(int x=(fr);x<(to);x++)
#define rep(x,to) for(int x=0;x<(to);x++)
#define repr(x,fr,to) for(int x=(fr);x>=(to);x--)
#define all(c) c.begin(),c.end()
#define sz(v) (int)v.size()

typedef long long  ll; typedef vector<int> VI; typedef pair<int,int> pii; typedef vector<ll> VL; const int MD = 1e9 +7; const ll INF = 1e18;
void dbg(){cerr<<"\n";} template <class F,class ...S> void dbg(const F& f, const S&...s){cerr <<f <<": "; dbg(s...);}

//using mint = atcoder::modint1000000007;

int main()
{
	cin.tie(0); ios_base::sync_with_stdio(false);
	int m,h;
	cin >>m >>h;
	int ok = 0;
	if(h%m==0) ok=1;
	puts(ok? "Yes":"No");
	return 0;
}

#include <cstdio>
#include <iostream>
using namespace std;
int n, m;
int main() {
	cin >> n >> m;
	if (m % n == 0) {
		cout << "Yes" << endl;
	} else {
		cout << "No" << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i)
static const ll INFTY = 1L << 62L;
ll solver(ll n, ll k)
{
    ll a1, a2, b1, b2, c1, c2, d1, d2, p, q, ans = 0;
    for (p = 2; p <= 2 * n - k; ++p)
    {
        q = p + k;
        b1 = min(n, p - 1);
        a1 = p - b1;
        d1 = min(n, q - 1);
        c1 = q - d1;
        ans += (b1 - a1 + 1) * (d1 - c1 + 1);
    }
    return ans;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, k;
    cin >> n >> k;
    cout << solver(n, abs(k)) << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ll N,K;
  cin>>N>>K;
  for(int i=0;i<K;i++){
    if(N%200==0)
      N/=200;
    else{
      N=N*1000+200;
    }
  }
  cout<<N;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define re
#define X first
#define Y second
#define mp std::make_pair
#define pb push_back
#define ohh(hhh...) fprintf(stderr,hhh)
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<int,int> pii;
template<class T1> inline bool cmax(T1 &x,T1 y) {return x<y?(x=y,1):0;}
template<class T1> inline bool cmin(T1 &x,T1 y) {return x>y?(x=y,1):0;}
int read()
{
	int x=0,w=0;
	char ch=0;
	while(ch<'0'||ch>'9') w|=ch=='-',ch=getchar();
	while('0'<=ch&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return w?-x:x;
}
int main()
{
	re int n=read();
	n=floor(1.08*n);
	if(n<206) printf("Yay!\n");
	else if(n==206) printf("so-so\n");
	else printf(":(\n");
	return 0;
}

#include<iostream>
#include<stdlib.h>
#include<cassert>
#include<time.h>
#include<bitset>
using namespace std;
int t[210000];
int main(){
    int a;scanf("%d",&a);
    int at=0;
    for(int i=0;i<a;i++){
        int s;scanf("%d",&s);
        t[s]++;
        while(t[at]){
            at++;
        }
        printf("%d\n",at);
    }
}


#include<iostream>
using namespace std;
typedef long long LL;
const int N=1e6+5;
LL l,r;
LL cnt[N],num[N];
void init(){
	for(int i=2;i<=N;i++)
	 for(int j=i;j<=r;j+=i){
	 	if(j<l)continue;
	 	cnt[i]++;
	 }
}
int main(){
	cin>>l>>r;
	init();
	LL ans=0;
	for(int i=r;i>=2;i--){
		num[i]=cnt[i]*cnt[i];
		for(int j=i+i;j<=r;j+=i){
			num[i]-=num[j];
		}
		ans+=num[i];
		if(i>=l)ans-=cnt[i]*2-1;
	}
	cout<<ans;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
#include <cstdint>
#include <string>
#include <sstream>

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; }
#define rep(i,n) for (int i = 0; i < (n); ++i)
typedef long long ll;
using P = pair<int,int>;

const int INF=1001001001;
const int mod =998244353;

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];
  }
 } c(200005);

int main() {
  ll N,K;
  cin>>N>>K;
  vector<mint>A(N);
  rep(i,N){
    int a;
    cin>>a;
    A[i]=a;
  }
  vector<mint>sum(K+1);
  rep(i,N){
    mint ki=1;
    rep(k,K+1){
      sum[k]=(sum[k]+ki);
      ki=ki*A[i];
    }
  }
  for(int x=1;x<=K;x++){
    mint ans=0;
    rep(k,x+1){
      ans+=c(x,k)*sum[k]*sum[x-k];
    }
    mint p=mint(2).pow(x)*sum[x];
    cout<<(ans-p)/2<<endl;
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define int long long
void read (int &x) {
    char ch = getchar(); x = 0; int f = 0;
    while (!isdigit(ch)) { if (ch == '-') f = 1; ch = getchar(); }
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
    if (f) x = -x;
} const int N = 3e5 + 5;
int n, k, res, a[N], c[N];
signed main() {
    read (n), read (k);
    for (int i = 1; i <= n; ++i)
        read (a[i]), ++c[a[i]];
    int now = k;
    for (int i = 0; i <= n; ++i) {
        if (now > c[i]) res += (now - c[i]) * i, now = c[i];
    }
    printf ("%lld\n", res);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define TEZ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define f(i,a,b) for(ll i=a;i<b;i++)
#define rf(i,a,b) for(ll i=a;i>=b;i--)
#define pb push_back
#define vi vector<pair<ll,ll>>
#define ff first
#define ss second
#define mp map<ll,ll>
#define test ll t; cin>>t; while(t--)
#define srt(v) sort(v.begin(),v.end())
double pi=acos(-1);
const ll MOD=1000000007;
void OJ(){
	#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    #endif
}
int main(){
    TEZ;
    ll ans=0;
    string s;
    cin>>s;
    f(i,0,10000){
        vector<bool> present(10,false);
        ll x=i;
        f(j,0,4){
            present[x%10]=true;
            x/=10;
        }
        bool fl=true;
        f(i,0,s.length()){
            if(s[i]=='o' and present[i]==false)
                fl=false;
            if(s[i]=='x' and present[i]==true)
                fl=false;
        }
        ans+=fl;
    }
    cout<<ans;
}


#include<iostream>
#include<sstream>
#include<iomanip>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<map>
#include<cmath>
#include<string>
#include<numeric>
#include<queue>

#define rep(i,p) for(long long int i=0;i<p;i++)
#define reep(i,p) for(long long int i=1;i<=p;i++)
#define ll long long
#define mod 1000000007

using namespace std;

int main(){

ll int N;
cin >> N;

if(N==1){
	cout << "2";
	return 0;
}

ll int  n;
ll int karii=0;
ll int counter=0;
for(ll int i=1;i<=100000000;i++){

	if((i*i-i)%2==0){
		n=N-(i*i-i)/2;
		if(n<=0){
			break;
		}
		if(n%i==0){
			//	cout << n << endl;
			if(n==floor(n)){
				counter+=2;

		//		cout << "dfd"<< i-karii << endl;
				karii=i;

			}
		}
	}

}
cout << counter;

	return 0;

}

// Problem: D - Staircase Sequences
// Contest: AtCoder - AtCoder Beginner Contest 190
// URL: https://atcoder.jp/contests/abc190/tasks/abc190_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mod 1e9+7
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define ff first
#define ss second

int main() {
	fast;
	ll t=1;
	// cin>>t;
	while(t--){
		ll n;
		cin>>n;
		ll ans=0;
		for(ll i=2;i*(i+1)/2<=n;i++) if((n-i*(i+1)/2)%i==0)	ans++;
		cout<<2*(ans+1)<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define deb(x)        cerr << #x << " = " << x << "\n";
#define deb2(x, y)    cerr << #x << " = " << x << ", " << #y << " = " << y << "\n";
#define deb3(x, y, z) cerr << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << "\n";

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define f  first
#define s  second

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

template<typename T>
using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;

void solve(int tc) {
    int n;
    cin >> n;
    vector<ll> v(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
    ll g = v[0];
    for (int i = 0; i < n-1; ++i) {
        g = __gcd(g, v[i+1]);
    }
    cout << g << '\n';
}

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

    int tc = 1;
    //cin >> tc;

    for (int i = 1; i <= tc; ++i) {
        solve(i);
    }

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

const ll INF = 1e18;
const int MX = 2005;

ll factorial(ll l){
    if(l == 1 || l == 0){
        return 1;
    }else{
        return l * factorial(l-1);
    }
}

int main() {
    ll L;
    cin >> L;
    ll ans = 1;
    vector<bool> ch(11,true);
    rep(i,11){
        ans *= L - i - 1;
        rep(j,11){
            if(ch[j]){
                if(ans % (j+1) == 0){
                    ans /= j+1;
                    ch[j] = false;
                }
            }
        }
    }
    cout << ans << endl;
}


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

const int N = 4e5 + 5, mod = 1e9 + 7;

vector<int> adj[N];
int in[N];

int main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin >> n;
    for(int i = 0; i < n; i++){
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        in[u]++;
        in[v]++;
    }
    queue<int> q;
    for(int i = 1; i < N; i++){
        if(in[i] == 1){
            q.push(i);
        }
    }
    int ans = 0;
    while(q.size()){
        int node = q.front();
        q.pop();
        if(in[node] == 0) continue;
        ans++;
        for(auto i: adj[node]) {
            in[i]--;
            if (in[i] == 1) {
                q.push(i);
            }
        }
    }
    for(int i = 1; i < N; i++){
        ans += (in[i] > 1);
    }
    cout << ans;

    return 0;
}

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define setIO(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout)
#define closefile fclose(stdin), fclose(stdout)
#define m_p make_pair
#define sz(x) (int)x.size()
#define see(x) cerr << x << " "
#define seeln(x) cerr << x << endl
#define out(x) cerr << #x << " = " << x << " "
#define outln(x) cerr << #x << " = " << x << endl
#define outarr(x, l, r) {cerr<< #x"[" << l << " ~ " << r << "] = "; for (int _i = l; _i <= r; ++_i) cerr << x[_i] << " "; cerr << endl;}
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define gc() getchar()
//#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 21], *p1 = buf, *p2 = buf;
template <class T> void read(T &x)
{
    x = 0; int c = gc(); int flag = 0;
    while (c < '0' || c > '9') flag |= (c == '-'), c = gc();
    while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) +(c ^ 48), c = gc();
    if (flag) x = -x;
}
template <class T> T _max(T a, T b){return b < a ? a : b;}
template <class T> T _min(T a, T b){return a < b ? a : b;}
template <class T> bool checkmax(T &a, T b){return a < b ? a = b, 1 : 0;}
template <class T> bool checkmin(T &a, T b){return b < a ? a = b, 1 : 0;}
const int N = 200005, inf = 0x3f3f3f3f;
int n, k;
vector<int> G[N];
int l[N], z1, Q;
int dis[N], dp[N], far[N];
void add_edge(int x, int y)
{
	G[x].push_back(y);
}

void dfs(int x, int f, int d)
{
	l[x] = d;
	for (int i = 0; i < sz(G[x]); ++i)
	{
		int to = G[x][i];
		if (to == f) continue;
		dfs(to, x, d + 1);
	}
}

void init()
{
	read(n); read(k);
	for (int i = 1, v, u; i < n; ++i)
	{
		read(u); read(v);
		add_edge(u, v);
		add_edge(v, u);
	}
	dfs(1, 0, 0);
	int mx = 0;
	for (int i = 1; i <= n; ++i) if (checkmax(mx, l[i])) z1 = i;
}

void run(int x, int f)
{
	dp[x] = 0; far[x] = -inf; dis[x] = inf;
	int child = 0;
	for (int i = 0; i < sz(G[x]); ++i)
	{
		int to = G[x][i];
		if (to == f) continue;
		++child;
		run(to, x);
		checkmin(dis[x], dis[to] + 1);
		dp[x] += dp[to];
	}
	if (child == 0)
	{
		dp[x] = far[x] = 0;
		dis[x] = inf;
	}
	else
	{
		for (int i = 0; i < sz(G[x]); ++i)
		{
			int to = G[x][i];
			if (to == f) continue;
			if (Q < dis[x] + far[to] + 1) checkmax(far[x], far[to] + 1);
		}
		//out(x); out(far[x]); outln(dis[x]);
		if (far[x] == Q || dis[x] == Q + Q + 1)
		{
			++dp[x];
			dis[x] = 0; far[x] = -inf;
		}
	}
	//out(Q); out(x); out(dp[x]); out(dis[x]); outln(far[x]);
}

bool check(int mid)
{
	Q = mid;
	run(z1, 0);
	if (dis[z1] > Q) ++dp[z1];
	if (dp[z1] <= k) return 1;
	else return 0;
}

void solve()
{
	int l = 1, r = n - 1, mid, best = n - 1;
	while (l <= r)
	{
		mid = (l + r) >> 1;
		if (check(mid)) best = mid, r = mid - 1;
		else l = mid + 1;
	}
	printf("%d\n", best);
}

int main()
{
	//setIO("");
	init();
	solve();
	return 0;
}


#include <iostream>
using namespace std;

int main() {
string s;
cin>>s;
if ((s[0]==s[1])&&(s[1]==s[2])&&(s[2]==s[0] )) cout<<"Won";
else cout<<"Lost";
	return 0;
}

//KEEP IT SIMPLE STUPID

#include<bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define print(v) for(auto it=(v).begin();it!=(v).end();it++) {cout<<*it<<" ";}
#define ll long long
#define w(t) ll t;cin>>t;while(t--)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
using namespace std;

bool prime(int n)
{
    if (n < 2)
        return false;
    for (int x = 2; x * x <= n; x++)
    {
        if (n % x == 0)
            return false;
    }
    return true;
}

/* Function to get prime factorization of n */
vector<int> getFactors(int n)
{
    vector<int> f;
    for (int x = 2; x * x <= n; x++)
    {
        while (n % x == 0)
        {
            f.push_back(x);
            n /= x;
        }
    }
    if (n > 1)
        f.push_back(n);
    return f;
}
ll k=0;
ll su(ll a, ll x)
{
    if(a%x !=0)
        return k;
    else
    {
        return k+su(a/x,x);
    }
}

void solve()
{
   string s;
   cin>>s;
   ll c=0;
   forn(i,s.length())
   {
       if(s[0]==s[i])
        c++;
   }
   if(c==s.length()) cout<<"Won";
   else cout<<"Lost";
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    //cout<<su(12,2);
    //st("2");
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
*/


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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
typedef vector < vector<ll> > Mat;
#define fi first
#define se second
const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);
void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

ll GCD(ll A, ll B){
  if (A < B) swap(A, B);
  while (A % B != 0){
    A %= B; swap(A, B);
  }
  return B;
}

ll extgcd(ll a, ll b, ll &x, ll &y){
  if (b == 0) {
    x = 1; y = 0; return a;
  }
  ll d = extgcd(b, a % b, y, x);
  y -= a / b * x;
  return d;
}

int T;

int Solve(){
  ll N, S, K; cin >> N >> S >> K;

  ll G = GCD(GCD(N, S), K);
  N /= G; S /= G; K /= G;

  if (GCD(K, N) != 1) {
    cout << -1 << endl;
    return 0;
  }

  ll x, y; extgcd(K, N, x, y);
  while (x < 0) x += N;
  cout << x * (N - S) % N << endl;

  return 0;
}

int main(){
  cin >> T;
  for (int i = 0; i < T; i++){
    Solve();
  }

  return 0;
}

#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);cout.precision(10);cout << fixed;
#define dbset(x) for(int i=0 ; i<x.size(); i++) cerr << x[i] << " "; cerr << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector < pair<int,int> >
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for(int x = 0; x < (y); ++x)
#define FOR(x, y, z) for(int x = y; x <= (z); ++x)
#define FORR(x, y, z) for(int x = y; x >= (z); --x)
using namespace std;

#define int long long
#define N 200007

int test;

int n;
string s,ss;

VPI g[N];

ull dp[N]; // ilosc zapalonych w wierzcholku
ull siz[N];

ull res;

void dfs(int v,int p,ull val){
    siz[v] = 1;

    for(auto it:g[v]){
        if (it.st != p){
            dfs(it.st,v,val);

            if (it.nd&val){
                res += 1LL*(((siz[it.st] - dp[it.st]) * (siz[v] - dp[v]))%mod) * (val%mod);
                res %= mod;

                res += ((dp[v] * dp[it.st])%mod) * (val%mod);
                res %= mod;
            }else{
                res += (((siz[it.st] - dp[it.st]) * (dp[v]))%mod) * (val%mod);
                res %= mod;

                res += ((dp[it.st] * (siz[v] - dp[v]))%mod) * (val%mod);
                res %= mod;
            }

            siz[v] += siz[it.st];

            if (it.nd&val){
                dp[v] += (siz[it.st] - dp[it.st] + mod);
                dp[v] %= mod;
            }else{
                dp[v] += dp[it.st];
                dp[v] %= mod;
            }
        }
    }

    return;
}

void solve()
{
    cin >> n;

    FOR(i,1,n-1){
        ull a,b,c;

        cin >> a >> b >> c;

        g[a].eb(b,c);
        g[b].eb(a,c);
    }

    FOR(i,0,61){
        FOR(j,1,n){
            dp[j] = siz[j] = 0;
        }

        dfs(1,1,(1ll<<i));
    }

    cout << res;

    return;
}

int32_t main()
{
    boost

    test=1;

    while(test--)
    {
        solve();
    }

  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const long long N = 1e5 + 5, INF = 1e17 + 5;

long long n, m, a, b, c, d, dist[N];

bool vis[N];

struct Edge{
    long long b, c, d;
};

vector<Edge> graph[N];

long double findtime(long long t, long long wait, Edge e){
    return wait + e.c + ((e.d / (long double)(t + 1 + wait)));
}

long long findwait(long long t, Edge e){
    long long l = 0, r = INF, m1, m2;
    long double m1ans, m2ans;

    while(r - l > 2){
        m1 = l + (r - l) / 3;
        m2 = r - (r - l) / 3;

        m1ans = findtime(t, m1, e);
        m2ans = findtime(t, m2, e);

        if(m1ans <= m2ans) r = m2;
        else l = m1;
    }

    vector<pair<long double, long long>> v;

    for(long long i = l; i <= r; i++){
        v.push_back({findtime(t, i, e), i});
    }

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

    return v.front().second;
}

void dijkstra(){
    for(int i = 1; i <= n; i++) dist[i] = INF;

    priority_queue<pair<long long, long long>> pq;

    dist[1] = 0;

    pq.push({0, 1});

    long long v;

    while(!pq.empty()){
        v = pq.top().second, pq.pop();

        if(!vis[v]){
            vis[v] = true;

            for(auto e : graph[v]){
                if(!vis[e.b]){
                    long long waitt = findwait(dist[v], e);
                    long long edget = findtime(dist[v], waitt, e);

                    if(dist[v] + edget < dist[e.b]){
                        dist[e.b] = dist[v] + edget;
                        pq.push({-dist[e.b], e.b});
                    }
                }
            }
        }
    }
}

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

    cin >> n >> m;

    for(int i = 0; i < m; i++){
        cin >> a >> b >> c >> d;

        graph[a].push_back({b, c, d});
        graph[b].push_back({a, c, d});
    }

    dijkstra();

    cout << (dist[n] == INF ? -1 : dist[n]);
}

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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int               long long
#define endl              "\n"
#define pb                push_back
#define ppb               pop_back
#define pf                push_front
#define ppf               pop_front
#define all(x)            (x).begin(),(x).end()
#define uniq(v)           (v).erase(unique(all(v)),(v).end())
#define sz(x)             (int)((x).size())
#define ff                first
#define ss                second
#define pii               pair<int,int>
#define vi                vector<int>
#define vpi               vector<pair<int,int>>
#define mii               map<int,int>
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define repe(i,a,b)        for(int i=a;i<=b;i++)
#define mem1(a)           memset(a,-1,sizeof(a))
#define mem0(a)           memset(a,0,sizeof(a))
#define ppc               __builtin_popcount
#define ppcll             __builtin_popcountll
#define INF 100000000000000000
#define mod 1000000007
#define esp 10e-7
const int mx=3e5+7;
int a[mx];
int st[4*mx];
void construct_st(int ss,int se,int si)
{
  if(ss==se)
  {
    st[si]=a[ss];
    return;
  }
  int mid=(ss+se)/2;
  construct_st(ss,mid,2*si+1);
  construct_st(mid+1,se,2*si+2);
  st[si]=st[2*si+1]^st[2*si+2];
}
int get_xor(int qs,int qe,int ss,int se,int si)
{
  if(ss>qe||qs>se)
    return 0;

  if(qs<=ss&&se<=qe)
    {
      return st[si];}
  int mid=(ss+se)/2;
  return get_xor(qs,qe,ss,mid,2*si+1)^
    get_xor(qs,qe,mid+1,se,2*si+2);
  // return 0;
}
void update_st(int si,int idx,int y,int ss,int se)
{
  if(idx<ss||idx>se)
    return;
  st[si]^=y;
  int mid=(ss+se)/2;
  if(ss!=se)
  {update_st(2*si+1,idx,y,ss,mid);
    update_st(2*si+2,idx,y,mid+1,se);
  }
}
signed main()
{
    //#ifndef ONLINE_JUDGE
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    //freopen("error.txt" , "w" , stderr);
    //#endif
    IOS
    int t=1,n,q;
    // cin>>t;

    while(t--)
    {
      cin>>n>>q;
      for(int i=0;i<n;i++)
      {
        cin>>a[i];
      }
      int type,x,y;
      construct_st(0,n-1,0);
      // for(int i=0;i<=n;i++)
      //   cout<<st[i]<<" ";
      // cout<<endl;
      while(q--)
      {
        cin>>type>>x>>y;

        if(type==1)
        {
          x--;
          update_st(0,x,y,0,n-1);
        }else
        {
           x--;
           y--;
          cout<<get_xor(x,y,0,n-1,0)<<endl;
        }
      }
    }

    return 0;
}


#include <iostream>
#include <math.h>
using namespace std;

void aaa(int *a,int start,int end){
   if(start<end){
	for(int i=start;i<end; i++){
       if(a[i]>a[i+1]){
         int tmp=a[i];
         a[i]=a[i+1];
         a[i+1]=tmp;
       }
     }
     aaa(a,start,end-1);
   }
}

int main() {
  long long h, w;
  int a[10000];
  int sum=0;

  cin >> h >> w;

  for(int i=0;i<h*w;i++){
    cin >> a[i];
    //cout << a[i];
    sum=sum+a[i];
  }
  aaa(a, 0, h*w-1);
  cout << sum - a[0]*h*w << endl;

}

#include<bits/stdc++.h>
using namespace std;
int max(int a,int b){
    return a>b?a:b;
}
int min(int a,int b){
    return a<b?a:b;
}

int main()
{
    int h,w,m=2000000000;
    scanf("%d %d",&h,&w);
    int a[102][102];
    for(int i=1;i<=h;i++)
        for(int j=1;j<=w;j++) {scanf("%d",&a[i][j]);m=min(m,a[i][j]);}
    int sum=0;
    for(int i=1;i<=h;i++)
        for(int j=1;j<=w;j++) sum+=(a[i][j]-m);
    cout<<sum<<endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
#define mems(p) memset(p,-1,sizeof(p))
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define nl "\n"

const int mxN = 8e5 + 100;
const ll MOD = 1e9 + 7;

void fast()
{ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);}

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

vector<ll> factorial;

ll expo(ll x, ll exp) {
    if (exp <= 0) return 1;
    ll tmp = (x*x)% MOD;
    if (exp%2) {
        return (expo(tmp, exp/2) * x) % MOD;
    }
    return (expo(tmp, exp/2)) % MOD;
}

ll choice(ll n, ll r) {
    if (n < r) return 0;
    ll numer = factorial[n];
    ll denom = (factorial[r] * factorial[n-r]) % MOD;
    return numer * expo(denom, MOD - 2) % MOD;
}

int main() {
    fast();
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    cout<<a*d-b*c<<nl;
}

#include <iostream>
using namespace std;
long long dp[16*16] = {0};

int H, W;
bool used[16][16] = {false};

int dfs(int h, int w, int a, int b)
{
  if ((a < 0) || (b < 0)) return 0;
  if (w == W) w = 0, h++;
  if (h == H) return 1;
  if (used[h][w]) return dfs(h, w+1, a, b);
  int res = 0;
  used[h][w] = true;
  res += dfs(h, w+1, a, b-1);
  if (w+1 < W && !used[h][w+1]) {
    used[h][w+1] = true;
    res += dfs(h, w+1, a-1, b);
    used[h][w+1] = false;
  }
  if ((h+1 < H && !used[h+1][w])) {
    used[h+1][w] = true;
    res += dfs(h, w+1, a-1, b);
    used[h+1][w] = false;
  }
  used[h][w] = false;
  return res;
}
int main(void) {
  int A, B;
  cin >> H >> W >> A >> B;
  int ans = dfs(0, 0, A, B);
  cout << ans << endl;
  return 0;
}


// Problem :
// bit.ly/pentagon03
#include<bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(),(v).end()
#define sz(v) (int)((v).size())
#define ini(st,en,X) fill(&st,&en,decltype(st)(X))
#define rep(i,n) for(int i=0;i<(n);i++)
#define repr(i,a,b) for(int i=(a);i<=(b);i++)
#define nl '\n'
#define sp ' '
using ll = long long;
using pii = pair<int,int>;
void dbg(string s = "STILL WORKING"){puts(s.c_str());}

void solve(){
	int n,m;
	cin>>n>>m;
	vector<int> arr(n);
	vector<pii> v(m+1);
	rep(i,n) cin>>arr[i];
	repr(i,1,m) cin>>v[i].second>>v[i].first;
	rep(i,n){
		int flag=0;
		repr(j,1,m){
			if(arr[i]>v[j].first){
				flag=1;
				break;
			}
		}
		if(flag){
			cout<<-1<<nl;
			return;
		}
	}
	sort(all(v));
	int dp[m+1]={};
	repr(i,1,m)
		dp[i] = max(dp[i-1],v[i].second);
	sort(all(arr));
	int ans = 2e9;
	do{
		int psum[n],dis[n]={0,},sum=0;
		rep(i,n){
			sum+=arr[i];
			psum[i]=sum;
		}
		rep(i,n){
			repr(j,i+1,n-1){
				int rsum = psum[j];
				if(i) rsum-=psum[i-1];
				int idx = lower_bound(all(v),pii(rsum,0)) - v.begin() - 1;
				dis[j] = max(dis[j],dis[i]+dp[idx]);
			}
		}
		ans = min(ans,dis[n-1]);
	}while(next_permutation(all(arr)));
	cout<<ans<<nl;
}

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

	repr(tc,1,TC){

		solve();
	}

	return 0;
}

// Solution
/*

*/

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

typedef long long ll;
typedef std::pair<ll, ll> pll;
const ll INF = (1LL << 60);

struct segtree {
  int i, j;
  ll val;
  segtree *l, *r;
  segtree(std::vector<pll> &ar, int _i, int _j) : i(_i), j(_j) {
    if (i == j) {
      val = ar[i].second;
      l = r = NULL;
    } else {
      int k = (i+j) >> 1;
      l = new segtree(ar, i, k);
      r = new segtree(ar, k+1, j);
      val = std::max(l->val, r->val);
    }
  }
  ll query(int _i, int _j) {
    if (_i <= i and j <= _j) {
      return val;
    } else if (_j < i or j < _i) {
      return 0;
    } else {
      return std::max(l->query(_i, _j), r->query(_i, _j));
    }
  }
};

struct graph {
  int n;
  ll **mat;
  ll *dp;
  graph (int n) : n(n) {
    mat = new ll*[n];
    for (int u = 0; u < n; ++u)
      mat[u] = new ll[n];
    dp = new ll[n];
  }
  void change_edge(int u, int v, ll w) {
    mat[u][v] = w;
  }
  ll dfs(int u) {
    if (u == n-1) return 0;
    if (dp[u] != -1)  return dp[u];
    dp[u] = 0;
    for (int v = u+1; v < n; ++v)
      dp[u] = std::max(dp[u], dfs(v) + mat[u][v]);
    return dp[u];
  }
};

int N, M;
std::vector<ll> W;
std::vector<pll> P;

int main() {
  std::cin >> N >> M;
  ll maxw = 0;
  for (int i = 0; i < N; ++i) {
    ll w; std::cin >> w;
    W.push_back(w);
    maxw = std::max(maxw, w);
  }
  std::sort(W.begin(), W.end());

  bool unavoid_collapse = false;

  for (int i = 0; i < M; ++i) {
    ll l, v;  std::cin >> l >> v;
    P.push_back({v, l});
    if (maxw > v)
      unavoid_collapse = true;
  }
  std::sort(P.begin(), P.end());
  segtree *root = new segtree(P, 0, M-1);

  if (unavoid_collapse) {
    std::cout << "-1\n";
    return 0;
  }

  graph g(N);
  ll ans = (1LL << 60);
  do {
    /*for (int w : W)
      std::cout << w << " ";
    std::cout << "\n";*/
    for (int i = 0; i < N; ++i) {
      ll s = W[i];
      for (int j = i+1; j < N; ++j) {
        s += W[j];
        int k = -1;
        for (int jump = M; jump >= 1; jump >>= 1)
          while (k + jump < M and P[k + jump].first < s)
            k += jump;
        //std::cout << i << " " << j << " " << k << "\n";
        g.change_edge(i, j, root->query(0, k));
      }
    }
    for (int u = 0; u < N; ++u) g.dp[u] = -1;
    ll dist = g.dfs(0);
    ans = std::min(ans, dist);
    //std::cout << dist << "\n";
    //std::cout << "-----\n";
  } while (std::next_permutation(W.begin(), W.end()));
  std::cout << ans << "\n";

  return 0;
}


#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")

using namespace std;
typedef long double ld;
typedef long long int ll;
typedef unsigned long long int ull;
typedef vector<int> vi;
typedef vector<char> vc;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<ll> vll;
typedef vector<pair<int, int> > vpii;
typedef vector<vector<int> > vvi;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<ll> > vvll;
typedef vector<vector<bool> > vvb;

#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rrep(i, n) for (int i = 1; i <= int(n); ++i)
#define irep(it, stl) for (auto it = stl.begin(); it != stl.end(); it++)
#define drep(i, n) for (int i = int(n)-1; i >= 0; --i)

#define MES(a) MES2 a
#define MES2(a0,a1,a2,a3,a4,x,...) x
#define mes_1(x1) cout<<x1<<endl
#define mes_2(x1,x2) cout<<x1<<" "<<x2<<endl
#define mes_3(x1,x2,x3) cout<<x1<<" "<<x2<<" "<<x3<<endl
#define mes_4(x1,x2,x3,x4) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<endl
#define mes_5(x1,x2,x3,x4,x5) cout<<x1<<" "<<x2<<" "<<x3<<" "<<x4<<" "<<x5<<endl
#define mes(...) CHOOSE((__VA_ARGS__,mes_5,mes_4,mes_3,mes_2,mes_1,~))(__VA_ARGS__)

#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0,a1,a2,a3,a4,x,...) x
#define debug_1(x1) cout<<#x1<<": "<<x1<<endl
#define debug_2(x1,x2) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<endl
#define debug_3(x1,x2,x3) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<endl
#define debug_4(x1,x2,x3,x4) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<endl
#define debug_5(x1,x2,x3,x4,x5) cout<<#x1<<": "<<x1<<", "#x2<<": "<<x2<<", "#x3<<": "<<x3<<", "#x4<<": "<<x4<<", "#x5<<": "<<x5<<endl
#define debug(...) CHOOSE((__VA_ARGS__,debug_5,debug_4,debug_3,debug_2,debug_1,~))(__VA_ARGS__)

#define ynmes(a) (a) ? mes("Yes") : mes("No")
#define YNmes(a) (a) ? mes("YES") : mes("NO")
#define re0 return 0
#define mp(p, q) make_pair(p, q)
#define pb(n) push_back(n)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define Sort(a) sort(a.begin(), a.end())
#define rSort(a) sort(a.rbegin(), a.rend())
#define Rev(a) reverse(a.begin(), a.end())
#define MATHPI acos(-1)
#define itn int;
int dx[8] = { 1, 0, -1, 0, 1, -1, -1, 1 };
int dy[8] = { 0, 1, 0, -1, 1, 1, -1, -1 };
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; }
struct io { io() { ios::sync_with_stdio(false); cin.tie(0); } };
const int INF = INT_MAX;
const ll LLINF = 1LL << 60;
const ll MOD = 1000000007;
const double EPS = 1e-9;

ll fact_mod(ll n, ll mod) {
    ll f = 1; for (ll i = 2; i <= n; i++) f = f * (i % mod) % mod;
    return f;
}

ll modpow(ll x, ll n, ll mod) {
    if(n == 0) return 1;
    ll res = modpow((x * x) % mod, n / 2 , mod);
    if(n & 1) res = (res * x) % mod;
    return res;
}

ll modncr(ll n, ll r, ll mod) {
    if(r > n-r) r = n-r;
    if(r == 0) return 1;
    ll a = 1;
    rep(i, r) a = a * ((n-i) % mod) % mod;
    ll b = modpow(fact_mod(r, mod), mod-2, mod);
    return (a % mod) * (b % mod) % mod;
}

signed main() {
	string s;
	cin >> s;
	ll r = 0;
	ll n = s.size();
	stack <char> stk;

	rep(i, n) {
		if (i && s[i] == s[i-1]) stk.push(s[i]);
	}

	if (stk.empty()) {
		mes(0);
		re0;
	}

	ll tmp = 0;
	drep(i, n) {
		if (stk.empty()) break;

		if (i && s[i] == s[i-1]) {
			stk.pop();
			r += tmp;
			tmp = 0;
			if (!stk.empty() && stk.top() != s[i-1]) tmp += n-i;
		} else {
			if (stk.top() != s[i]) tmp++;
		}
	}

	mes(r);

}

#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
string s;
ll ans,raod[200];
int main(){
    ios::sync_with_stdio(false);
    cin >> s;

    for(int i=(int)s.size() - 1; i>=1; i--){
        if(s[i] == s[i - 1]){
            ans += (int)s.size() - i - 1 - raod[s[i] - 'a'];
            for(int j=0; j<26; j++)
                raod[j] = 0;
            raod[s[i] - 'a'] = (int)s.size() - i;
        }
        else {
            raod[s[i] - 'a']++;
        }
    }

    cout << ans << '\n';

    return 0;
}


	//IQ134高知能系Vtuberの高井茅乃です。
	//Twitter: https://twitter.com/takaichino
	//YouTube: https://www.youtube.com/channel/UCTOxnI3eOI_o1HRgzq-LEZw

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

	#define INF INT_MAX
	#define LLINF LLONG_MAX
	#define REP(i,n) for(int i=0;i<n;i++)
	#define REP1(i,n) for(int i=1;i<=n;i++)
	#define MODA 1000000007
	#define MODB  998244353

	template <typename T>
	std::istream& operator>>(std::istream& is, std::vector<T>& vec) {
		for (T& x: vec) { is >> x; }
		return is;
	}

	int main() {
		ll ans = 0;
		ll tmp = 0;
		int n; cin >> n;
		string s; cin >> s;
		vector<int> a(n+1); cin >> a;
		int mi[n+1] = {};
		REP(i, n) if(s[i] == '<') mi[i+1] = max(mi[i+1], mi[i]+1);
		for(int i = n; i >= 0; i--) if(s[i] == '>') mi[i] = max(mi[i], mi[i+1]+1);
		ans = INF;
		//REP(i, n+1) if(mi[i] > 0) ans = min(ans, (ll)(a[i]/mi[i]));
		REP(i, n) ans = min(ans, (ll)abs(a[i] - a[i+1]));
		cout << ans << endl;
		REP(i, ans){ REP(j, n+1){cout << (a[j] + i)/ ans << " ";} cout << endl;}
		//REP(i, ans - 1){ REP(j, n+1){cout << mi[j] << " ";} cout << endl;}
		//REP(i, n+1){cout << a[i] - mi[i] *(ans - 1) << " ";} cout << endl;
	}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,N) for(int i=0;i<int(N);++i)
#define rep1(i,N) for(int i=1;i<int(N);++i)
#define all(a) (a).begin(),(a).end()
#define bit(k) (1LL<<(k))
#define SUM(v) accumulate(all(v), 0LL)

typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
struct fast_ios{ fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; }fast_ios_;

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

#define TOSTRING(x) string(#x)
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os  << "["; for(auto _: v) os << _ << ", "; os << "]"; return os; };
template <typename T> ostream &operator<<(ostream &os, set<T> &st) { os << "("; for(auto _: st) { os << _ << ", "; } os << ")";return os;}
template <typename T> ostream &operator<<(ostream &os, multiset<T> &st) { os << "("; for(auto _: st) { os << _ << ", "; } os << ")";return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair< T, U >& p){os << "{" <<p.first << ", " << p.second << "}";return os; }
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &mp){ os << "["; for(auto _: mp){ os << _ << ", "; } os << "]" << endl; return os; }

#define DUMPOUT cerr
void dump_func(){ DUMPOUT << endl; }
template <class Head, class... Tail> void dump_func(Head &&head, Tail &&... tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); }

#ifdef DEBUG
#define dbg(...) { dump_func(__VA_ARGS__) }
#define dump(...) DUMPOUT << string(#__VA_ARGS__) << ": "; dump_func(__VA_ARGS__)
#else
#define dbg(...)
#define dump(...)
#endif

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18+1;
const ll MOD = 1000000007;
// const ll MOD = 998244353;
const long double PI = acos(-1.0);

/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/

int main() {
    int N;
    string S;
    cin >> N >> S;
    vector<int> A(N+1);
    rep(i,N+1)cin >> A[i];
    // x個に分けることができるか
    auto check = [&](int x, bool out = false) -> bool{
        vector<vector<int>> vv(x, vector<int>(N+1, 0));
        rep(i,x)rep(j,N+1){
            vv[i][j] = A[j]/x;
        }
        rep(j,N+1){
            int rem = A[j]%x;
            rep(i,rem)vv[i][j] += 1;
        }
        if(out){
            rep(i,x){
                rep(j,N+1){
                    cout << vv[i][j] << " ";
                }
                cout << endl;
            }
        }
        //
        dump(x);
        rep(i,x)rep(j,N+1)if(vv[i][j] < 0)return false;
        rep(i,x){
            rep(j,N){
                if(S[j] == '<'){
                    if(vv[i][j] >= vv[i][j+1])return false;
                }
                else{
                    if(vv[i][j] <= vv[i][j+1])return false;
                }
            }
        }
        return true;
    };
    int ok = 1;
    int ng = 1e5;
    while(ng - ok > 1){
        int mid = (ok + ng) / 2;
        dump(mid);
        if(check(mid))ok = mid;
        else ng = mid;
    }
    cout << ok << endl;
    check(ok, true);
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef pair<int,int> pi;
typedef pair<lld,lld> pl;
typedef pair<int,lld> pil;
typedef pair<lld,int> pli;
typedef vector<int> vit;
typedef vector<vit> vitt;
typedef vector<lld> vlt;
typedef vector<vlt> vltt;
typedef vector<pi> vpit;
typedef vector<vpit> vpitt;
typedef long double ld;
#define x first
#define y second
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(x) (int)x.size()
#define mk(a,b) make_pair(a,b)
bool isrange(int y,int x,int n,int m){
	 if(0<=y&&y<n&&0<=x&&x<m) return true;
	 return false;
}
int dy[4] = {1,0,-1,0},dx[4]={0,1,0,-1},ddy[8] = {1,0,-1,0,1,1,-1,-1},ddx[8] = {0,1,0,-1,1,-1,1,-1};
const int MAX = 202020;
int vis1[MAX],vis2[MAX],seg[MAX*4],start = 1;
bool tmr(pi a,pi b){
	return a.x > b.x;
}
void upd(int x){
	x += start;
	seg[x]--;
	for(int e=(x/2);e>=1;e/=2) seg[e] = seg[e*2] + seg[e*2+1];
}
int getn(int l,int r){
	l += start;
	r += start;
	int ans = 0;
	while(l<=r){
		if(l&1) ans += seg[l++];
		if(!(r&1)) ans += seg[r--];
		l >>= 1;
		r >>= 1;
	}
	return ans;
}
int main(void){
	while(start<MAX) start *= 2;
	int n,m,que;
	scanf("%d%d%d",&n,&m,&que);
	for(int e=1;e<=n;e++){
		vis1[e] = m;
	}
	for(int e=1;e<=m;e++){
		vis2[e] = n;
	}
	while(que--){
		int y,x;
		scanf("%d%d",&y,&x);
		vis1[y] = min(vis1[y],x-1);
		vis2[x] = min(vis2[x],y-1);
	}
	lld ans = 0;
	vector<pi> v;
	for(int e=1;e<=vis1[1];e++){
		ans += vis2[e];
		v.push_back(mk(vis2[e],e));
	}
	sort(all(v),tmr);
	for(int e=start;e<2*start;e++) seg[e] = 1;
	for(int e=start-1;e>=1;e--) seg[e] = seg[e*2] + seg[e*2+1];
	int st = 0;
	for(int p=vis2[1];p>=1;p--){
		for(;st<sz(v)&&v[st].x>=p;st++) upd(v[st].y);
		ans += getn(1,vis1[p]);
	}
	printf("%lld",ans);
	return 0;
}

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 //10^9+7
#define INF LONG_MAX/3
#define bit(n) (1LL<<(n))

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; }
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

struct segtree {
	vector<long long> data;
	long long n;
	segtree(long long nin){
		n = pow( 2, ceil( log(nin) / log(2) ) );
		long long size = 2 * n - 1;
		this->data.resize(size, 0);
	}

	//API for update val
	void update(long long idx, long long val){
		update(idx, val, 0, 0, n);
	}

	long long update(long long idx, long long val, long long node_id, long long left, long long right){
		//if query idx is within range return val
		if( !( left <= idx && idx < right ) ){
			return data[node_id];
		}
		if( left + 1 < right ) {
			//when node range is not 1, get update children val and update node data
			long long mid = (left + right) / 2 ;
			long long L = update(idx, val, 2 * node_id + 1, left, mid);
			long long R = update(idx, val, 2 * node_id + 2, mid, right);
			data[node_id] = L + R;
		} else {
			//when node range is 1, update node data
			data[node_id] = data[node_id] + val;
		}
		return data[node_id];
	}

	//API for get value of [l, r)
	long long get(long long l, long long r){
		return get(l, r, 0, 0, n);
	}

	/*
	 *internaly used function
	 *query_l: query left range
	 *query_r: query right range
	 *node_id: node id of
	 *node_l:  node left range
	 *node_r:  node right range
	 */
	long long get(long long query_l, long long query_r, long long node_id, long long node_l, long long node_r){
		//query range is outside of node range: return INF
		if( query_r <= node_l || node_r <= query_l ){
			return 0;
		}
		//query range is within node range: return node val
		if( query_l <= node_l && node_r <= query_r ){
			return data[node_id];
		}
		long long mid = (node_l + node_r) / 2;
		long long L = get(query_l, query_r, 2 * node_id + 1, node_l, mid);
		long long R = get(query_l, query_r, 2 * node_id + 2, mid, node_r);
		return L + R;
	}
};

vector<ll> yvec[200005];

int main(){
	ll H, W, M;
	cin >> H >> W >> M;
	vector<ll>X(M);
	vector<ll>Y(M);
	ll maxx = W;
	ll maxy = H;

	segtree ysg(H+5);;

	rep(i, M) cin >> Y[i] >> X[i];
	rep(i, M) X[i]--;
	rep(i, M) Y[i]--;

	rep(i, M) {
		yvec[X[i]].push_back(Y[i]);
		if(Y[i] == 0) chmin(maxx, X[i]);
		if(X[i] == 0) chmin(maxy, Y[i]);
	}
	rep(i, W) sort(yvec[i].begin(), yvec[i].end());

	set<ll> ngy;
	rep(i, maxy) ysg.update(i, 1);
	ll ans = 0;

	for(ll y = maxy; y < H; y++) ngy.insert(y);

	for(ll x = 0; x < W; x++) {
		ll miny = H;
		if(x >= maxx) miny = 0;
		for(ll y: yvec[x]) {
			chmin(miny, y);
			if(!ngy.count(y)) {
				ngy.insert(y);
				ysg.update(y, -1);
			}
		}
		//cout << "x = " << x << " uekara " << miny << " yokokara " << ysg.get(miny, H) << endl;
		//上からいけるやつ
		ans += miny;

		//上からいけなくて横からいけるやつ
		ans += ysg.get(miny, H);
	}

	cout << ans << endl;

}


#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
int n;
ll ans=1;
ll gcd(ll ta,ll tb){
    return tb?gcd(tb,ta%tb):ta;
}
int main(){
    scanf("%d",&n);
    for(int i=2;i<=n;i++) ans=ans*i/gcd(ans,i);
    if(ans+1<=n) ans=(n/ans+1)*ans;
    printf("%lld\n",ans+1);
    return 0;
}

#include <cctype>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <iostream>
#include <utility>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <climits>
#include <cmath>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <regex>
#include <set>
#include <bitset>

const int64_t infl = 1LL << 60;
const int64_t cc = pow(10, 9) + 7;
bool sort_pair(std::pair<int64_t, int64_t> origin, std::pair<int64_t, int64_t> another){return origin.first < another.first;}
template <typename T> bool chmin(T &a, const T& b) {if(a > b){a = b;return true;}return false;}
template <typename T> bool chmax(T &a, const T& b) {if(a < b) {a = b;return true;}return false;}

int main()
{
    int64_t n,m,q,k;
    std::vector< int64_t > list;

    n = 0;
    std::string s;
    std::cin>> s;

    for( int i = 0; i < s.size(); i++ )
    {
        if( ( i % 2 == 0 ) && std::isupper( s[i] ) )
        {
            n = -1;
            break;
        }

        if( ( i % 2 == 1 ) && std::islower( s[i] ) )
        {
            n = -1;
            break;
        }
    }

    if( n == -1 ) std::cout<< "No";
    else std::cout<< "Yes";
}


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

int N, T;
long long A[44];

auto calc(int S, int T) {
  vector<long long> v;
  for (int b = 0, e = 1 << (T - S); b < e; ++b) {
    long long q = 0;
    for (int i = 0; i < T - S; ++i) {
      if (b & (1 << i)) q += A[S + i];
    }
    v.push_back(q);
  }
  sort(v.begin(), v.end());
  return v;
}

int main() {
  cin >> N >> T;
  for (int i = 0; i < N; ++i) cin >> A[i];
  auto a = calc(0, N / 2);
  auto b = calc(N / 2, N);
  long long x = 0;
  auto set = [&](long long v) {
    if (v > T) return;
    x = max(x, v);
  };
  for (long long z : a) {
    set(z);
    set(z + b.back());
    auto i = upper_bound(b.begin(), b.end(), T - z);
    if (*i == 0) continue;
    set(z + *(i - 1));
  }
  cout << x << endl;
}

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define ll long long
#define ln cout<<endl
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}void pr(){ln;}
template<class A,class...B>void pr(const A &a,const B&...b){cout<<a<<(sizeof...(b)?" ":"");pr(b...);}
template<class A>void PR(A a,ll n){rep(i,n)cout<<(i?" ":"")<<a[i];ln;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[8]={-1,0,1,0,-1,-1,1,1},dy[8]={0,1,0,-1,-1,1,1,-1};
typedef pair<ll,ll> P;

vector<string> C(vector<string> s) {
  ll n=s.size();
  rep(i,n)REP(j,i+1,n) swap(s[i][j],s[j][i]);
  return s;
}

ll n,m;
string s[1000];

ll calc(vector<string> t) {
  ll cnt=0;
  rep(k,m) {
    ll f=0;
    rep(l,2) {
      rep(i,n) {
        rep(j,n) {
          if(t[i].substr(0,s[k].size())==s[k]) f=1;
          rotate(t[i].begin(),t[i].begin()+1,t[i].end());
        }
      }
      t=C(t);
    }
    cnt+=f;
  }
  return cnt;
}

vector<string> ans;
ll MM;

void solve(vector<string> t) {
  ll d=calc(t);
  if(MM<d) {
    MM=d;
    ans=t;
  }
  rep(k,5) {
    random_shuffle(all(t));
    rep(i,n) rotate(t[i].begin(),t[i].begin()+rand()%n,t[i].end());
    ll d=calc(t);
    if(MM<d) {
      MM=d;
      ans=t;
    }
  }
}

void Main() {
  srand((unsigned)time(NULL));
  cin >> n >> m;
  rep(i,m) R s[i];
  vector<string> v[15];
  rep(i,m) v[s[i].size()].pb(s[i]);
  rep(e,35) {
    vector<string> t(n);
    rrep(i,15) {
      random_shuffle(all(v[i]));
      rep(j,v[i].size()) {
        ll M=MAX,x=0;
        string rr;
        rep(k,n) {
          rep(l,t[k].size()+1) {
            string r1=t[k].substr(l);
            if(r1.size()<=v[i][j].size()) {
              string r2=v[i][j].substr(0,r1.size());
              if(r1==r2) {
                ll d=t[k].size()+v[i][j].size()-r1.size();
                if(d<=n&&M>d) {
                  M=d;
                  x=k;
                  rr=t[k]+v[i][j].substr(r1.size());
                }
              }
            }
            if(l+v[i][j].size()<=t[k].size()) {
              r1=t[k].substr(l,v[i][j].size());
              if(r1==v[i][j]) {
                M=0;
                x=k;
                rr=t[k];
              }
            }
          }
        }
        if(rand()%(i+1)==0) M=MAX;
        if(M!=MAX) t[x]=rr;
      }
    }
    rep(i,n) {
      while(t[i].size()<n) {
        //t[i]+='.';
        t[i]+=(char)(rand()%8+'A');
      }
    }
    solve(t);
  }
  rep(i,n) pr(ans[i]);
}

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


#include <bits/stdc++.h>
#define ff first
#define ss second
#define endl '\n'
#define INF 1e9

using namespace std;
using ll = long long;
using pii = pair<int,int>;
using vi = vector<int>;

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

    int h, w;
    cin >> h >> w;

    vi arr(h*w);

    for(auto& x: arr)
        cin >> x;

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

    int md = arr[0], ans = 0;

    for(auto x: arr)
        ans += abs(x-md);

    cout << ans << endl;

}

#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
//indexed set is using "int" here---------
// typedef tree <int,null_type,less <int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
// #include <ext/rope>
// using namespace __gnu_cxx;
typedef long long int ll;
typedef long double ld;
#define FAST                 \
    ios::sync_with_stdio(0); \
    cin.tie(0);              \
    cout.tie(0);
#define rep(g, i, n) for (ll g = i; g < n; g++)
#define rev(g, n, i) for (ll g = n - 1; g >= i; g--)
#define all(v) v.begin(), v.end()
#define pb push_back
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define ve vector
#define lb lower_bound
#define ub upper_bound
#define rem 1000000007
#define PI 3.141592653589793238462643383279502
ll power(ll x, ll y, ll p)
{
    ll res = 1; // Initialize result
    x = x % p;  // Update x if it is more than or
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % p;
        y = y >> 1; // y = y/2
        x = (x * x) % p;
    }
    return res;
}
ll modInverse(ll n, ll p)
{
    return power(n, p - 2, p);
}
int main()
{
    FAST
    // freopen("input1.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    /*ll tests;
    cin>>tests;
    rep (gg,0,tests)
    {}*/
    ll h,w;
    cin>>h>>w;
    ll arr[h][w];
    ll val=4e18;
    rep (i,0,h)
    {
        rep (j,0,w)
        {
            cin>>arr[i][j];
            val=min(val,arr[i][j]);
        }
    }
    ll ans=0;
    rep (i,0,h)
    {
        rep (j,0,w)
        {
            ans+=abs(val-arr[i][j]);
        }
    }
    cout<<ans<<"\n";
}


#pragma GCC optimize("Ofast")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
#define IOS ios::sync_with_stdio(0); cin.tie(0); mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count());
#define F first
#define S second
#define PI  4.0*atan(1.0);
#define pb push_back
using namespace std;
typedef pair<int, int> pi;
int mod=1e9+7,gaymod=998244353,MAX=1000000000,pomod=1073741824;
//long long int gcd(long long int x,long long int  y){
  // if(y==0)return x;
   //return gcd(y,x%y);
//}
template<typename T>
T gcd(T x , T y){
if(y==0)return x;
return gcd(y,x%y);
}
template<typename L>
L lcm(L x,L y){
 L g=gcd(x,y);
 return x/g*y;
}
ll binexp(ll a,ll b){
if(b==0)return 1;
ll res=binexp(a,b/2);
if(b%2)return((res%mod)*(res%mod)*(a%mod));
else return((res%mod)*(res%mod));
}
int prime(ll x){
if(x%2==0&&x!=2)
return 0;
else if(x==1)return 0;
else{
    for(int i=3;ll(i*i)<=x;i+=2){
        if(x%i==0)
            return 0;
    }
}
return 1;
}
bool secsort(const pair<int,int> &a,const pair<int,int> &b){
    return(a.S<b.S);
}
//main
/*
vector<char> is_prime(N+1, true);
void sieve(int n){
is_prime[0] = is_prime[1] = false;
for (int i = 2; i <= n; i++) {
    if (is_prime[i] && (long long)i * i <= n) {
        for (int j = i * i; j <= n; j += i)
            is_prime[j] = false;
    }
}
}
*/
//freopen("input.txt","r",stdin);
//freopen("input.txt","w",stdin);
const int N=2e5+5;
const int Z=1e6+5;

//
int main(){
IOS;
int t=1;
//cin>>t;
while(t--){
int n;
cin>>n;
int arr[n];
map<int,ll> M;
ll ans=0;
for(int i=0;i<n;i++){
cin>>arr[i];
}
M[arr[0]]++;
for(ll i=1;i<n;i++){
 M[arr[i]]++;
 ans+=(i+1)-M[arr[i]];
}
cout<<ans;
cout<<"\n";
}
return 0;
}

//IF YOU GIVE UP,THEN YOU DON'T DESERVE IT

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define endl "\n"
#define IOS ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define PI 3.14159265358979323846264
#define isz(x) (int)x.size()
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define typ int
#define ordered_set tree<typ, null_type, less<typ>, rb_tree_tag, tree_order_statistics_node_update>
//*s.find_by_order(x) --> value at position x(0-indexed)
//s.order_of_key(x)   --> number of elements less than x
#define eps 1e-9
#define int long long

int32_t main()
{
   int n;
   cin>>n;
   int u = 0,v = 0,mx =-INT_MAX;
   for(int i=1;i<=n;i++)
   {
     int x;
     cin>>x;
    mx = max(mx,abs(x));
    u+=abs(x);
    v+=(x*x);
   }
   cout<<u<<" \n"<<setprecision(50)<<sqrt(v)<<" \n"<<mx;
}


/*
after dusk passed,
there is a starry sky.
*/
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define m_k make_pair
using namespace std;
const int N=2*1e5+100;
int n,a[N],b[N],p[N],vi[N],wh[N];
vector <pair<int,int> > ans;
priority_queue <pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
inline int read()
{
	int f=1,x=0;char s=getchar();
	while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
	while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
	return x*f;
}
signed main()
{
	n=read();
	for (int i=1;i<=n;i++) a[i]=read();
	for (int i=1;i<=n;i++) b[i]=read();
	for (int i=1;i<=n;i++) p[i]=read();
	for (int i=1;i<=n;i++)
	{
		if (p[i]==i){vi[i]=1;continue;}
		if (a[i]<=b[p[i]])
		{
			printf("-1\n");
			return 0;
		}
		q.push(m_k(a[i],i));wh[p[i]]=i;
	}
	while (!q.empty())
	{
		int x=q.top().second,y;q.pop();
		if (vi[x]) continue;
		vi[x]=1;y=wh[x];
		ans.push_back(m_k(x,y));
		wh[p[x]]=y;
		swap(p[x],p[y]);
		if (y==p[y]) vi[y]=1;
	}
	printf("%d\n",(int)ans.size());
	for (int i=0;i<(int)ans.size();i++) printf("%d %d\n",ans[i].first,ans[i].second);
}

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

using namespace __gnu_pbds;
using namespace std;

using ll = long long;
using ld = long double;

typedef tree<
        int,
        null_type,
        less<int>,
        rb_tree_tag,
        tree_order_statistics_node_update>
        ordered_set;

#define mp make_pair

const int MOD =  998244353;

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

int add(int a, int b) {
    int s = (a+b);
    if (s>=MOD) s-=MOD;
    return s;
}

int sub(int a, int b) {
    int s = (a+MOD-b);
    if (s>=MOD) s-=MOD;
    return s;
}

int po(int a, ll deg)
{
    if (deg==0) return 1;
    if (deg%2==1) return mul(a, po(a, deg-1));
    int t = po(a, deg/2);
    return mul(t, t);
}

int inv(int n)
{
    return po(n, MOD-2);
}

mt19937 rnd(time(0));

const int LIM = 200000;

vector<int> facs(LIM), invfacs(LIM);

void init()
{
    facs[0] = 1;
    for (int i = 1; i<LIM; i++) facs[i] = mul(facs[i-1], i);
    invfacs[LIM-1] = inv(facs[LIM-1]);
    for (int i = LIM-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1);
}

int C(int n, int k)
{
    if (n<k) return 0;
    if (n<0 || k<0) return 0;
    return mul(facs[n], mul(invfacs[k], invfacs[n-k]));
}

/*
struct DSU
{
    vector<int> sz;
    vector<int> parent;
    void make_set(int v) {
        parent[v] = v;
        sz[v] = 1;
    }

    int find_set(int v) {
        if (v == parent[v])
            return v;
        return find_set(parent[v]);
    }

    void union_sets(int a, int b) {
        a = find_set(a);
        b = find_set(b);

        if (a != b) {
            if (sz[a] < sz[b])
                swap(a, b);
            parent[b] = a;
            sz[a] += sz[b];
        }
    }

    DSU (int n)
    {
        parent.resize(n);
        sz.resize(n);
        for (int i = 0; i<n; i++) make_set(i);
    }
};*/

const int N = 10000;

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

    init();
    int n; cin>>n;
    vector<int> w(n);
    for (int i = 0; i<n; i++) cin>>w[i];
    vector<vector<int>> dp(N+1, vector<int>(n+1));

    dp[0][0] = 1;
    for (int i = 0; i<n; i++)
    {
        for (int W = N; W>=w[i]; W--)
            for (int cnt = i+1; cnt>=1; cnt--) dp[W][cnt] = add(dp[W][cnt], dp[W-w[i]][cnt-1]);
    }

    int tot = 0; for (auto it: w) tot+=it;
    if (tot%2==1) {cout<<0<<endl; return 0;}
    int need = tot/2;

    int ans = 0;

    for (int take = 0; take<=n; take++)
    {
        ans = add(ans, mul(dp[need][take], mul(facs[take], facs[n-take])));
    }

    cout<<ans;
}


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

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

#define fastio() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define test() int t;cin>>t;for(int test=1;test<=t;test++)
#define pb push_back
#define nl cout<<"\n"
#define all(x) x.begin(),x.end()

template<class C> void min_self( C &a, C b ){ a = min(a,b); }
template<class C> void max_self( C &a, C b ){ a = max(a,b); }

const ll MOD = 1000000007;
ll mod( ll n, ll m=MOD ){ n%=m,n+=m,n%=m;return n; }

const int MAXN = 1e5+5;
const int LOGN = 21;
const ll INF = 1e14;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};

template<class T1, class T2> void add( T1 &x, T2 y, ll m = MOD )
{
    x += y;
    if( x >= m )
        x -= m;
}

template<class T1, class T2> void sub( T1 &x, T2 y, ll m = MOD )
{
    x -= y;
    if( x < 0 )
        x += m;
}

char grid[2002][2002];
int up[2002][2002], lft[2002][2002], diag[2002][2002];
ll dp[2002][2002], prefix_row[2002][2002], prefix_col[2002][2002], prefix_diag[4004][2002];

int main()
{
    #ifdef gupta_samarth
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    fastio();

    int h,w;
    cin>>h>>w;
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            cin>>grid[i][j];
            if( grid[i][j] == '#' )
            {
                lft[i][j] = j;
                up[i][j] = i;
                diag[i][j] = i;
            }
            else
            {
                lft[i][j] = ( j-1 >= 0 ? lft[i][j-1] : 0 );
                up[i][j] = ( i-1 >= 0 ? up[i-1][j] : 0 );
                diag[i][j] = ( i-1 >= 0 && j-1 >= 0 ? diag[i-1][j-1] : 0 );
            }
            dp[i][j] = 0;
        }
    }

    auto sum_row = [&]( int row, int lc, int rc ) -> ll
    {
        if( rc < lc )
            return 0;
        ll ans = prefix_row[row][rc];
        if( lc )
            sub( ans, prefix_row[row][lc] );
        return ans;
    };

    auto sum_col = [&]( int col, int lr, int rr ) -> ll
    {
        if( rr < lr )
            return 0;
        ll ans = prefix_col[rr][col];
        if( lr )
            sub( ans, prefix_row[lr][col] );
        return ans;
    };

    auto sum_diag = [&]( int dg, int l, int r ) -> ll
    {
        if( r < l )
            return 0;
        ll ans = prefix_diag[dg][r];
        if( l )
            sub( ans, prefix_diag[dg][l] );
        return ans;
    };

    auto update = [&]( int i, int j, ll val )
    {
        if( j-1 >= 0 )
            prefix_row[i][j] = prefix_row[i][j-1];
        add( prefix_row[i][j], val );

        if( i-1 >= 0 )
            prefix_col[i][j] = prefix_col[i-1][j];
        add( prefix_col[i][j], val );

        if( i-1 >= 0 )
            prefix_diag[j-i+2000][i] = prefix_diag[j-i+2000][i-1];
        add( prefix_diag[j-i+2000][i], val );
    };

    dp[0][0] = 1;
    update(0,0,1);
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            if( grid[i][j] == '#' || ( i == 0 && j == 0 ) )
                continue;

            // cout<<"NOW: "<<i<<" "<<j,nl;
            // cout<<"Left: "<<i<<" "<<lft[i][j],nl;
            // cout<<"Up: "<<up[i][j]<<" "<<j,nl;

            // for(int a=0;a<=j;a++)
            //     cout<<prefix_row[i][a]<<" ";
            // nl;nl;

            ll now = 0;
            add( now, sum_row(i,lft[i][j],j-1) );
            add( now, sum_col(j,up[i][j],i-1) );
            add( now, sum_diag(j-i+2000,diag[i][j],i-1));
            add( dp[i][j], now );
            update(i,j,dp[i][j]);

        }
    }
    cout<<dp[h-1][w-1],nl;

    cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
    return 0;
}

#include<bits/stdc++.h>
#define _USE_MATH_DEFINES
using namespace std;
// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O3")
// #pragma GCC optimization ("unroll-loops")

#define ll long long int
#define ld double
#define pb push_back
#define rep(i , j , n) for(ll i = j ; i < n ; i++)
#define pre(i , j , n) for(ll i = j ; i >= n ; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vll;
typedef vector<char>	vc;
typedef vector<bool>	vb;
typedef pair<ll,ll> 	pll;
#define br "\n"
#define ff first
#define ss second
#define gcd __gcd
ll mod = 1e9 + 7;

ll dirx[] = {-1,-1,0};
ll diry[] = {-1,0,-1};
ll sum[2000][2000][3];
ll dp[2000][2000];
vector<vll> grid;

void solve(){
    ll n,m;
    cin >> n >> m;
    grid.assign(n,vll(m));
    rep(i,0,n){
        rep(j,0,m){
            char c;
            cin >> c;
            if(c == '.') grid[i][j] = 1;
            rep(k,0,3){
                sum[i][j][k] = 0;
            }
            dp[i][j] = 0;
        }
    }
    dp[0][0] = 1;
    rep(i,0,n){
        rep(j,0,m){
            rep(k,0,3){
                if(!grid[i][j]){
                    continue;
                }
                ll x = i + dirx[k], y = j + diry[k];
                if(x < 0 || y < 0 || x >= n || y >= m) continue;
                (dp[i][j] += sum[x][y][k]) %= mod;
                (sum[i][j][k] += sum[x][y][k]) %= mod;
            }
            rep(k,0,3){
                (sum[i][j][k] += dp[i][j]) %= mod;
            }
        }
    }
    cout << dp[n - 1][m - 1];

}

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

	ll t = 1;
    // cin >> t;
	rep(i,0,t){
        // cout << "Case #" << i + 1 << ": ";
        solve();
        // test();
	}

}


#include <bits/stdc++.h>
using namespace std;
void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]"<<endl;}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#define deb(x...) cout << #x << " = ["; _print(x)
template<typename T,typename T1>T amax(T &a,T1 b){if(b>a)a=b;return a;}
template<typename T,typename T1>T amin(T &a,T1 b){if(b<a)a=b;return a;}
// =================
#define f(i, k, n) for (int i = k; i < n; i++)
#define r(i, k, n) for (int i = k; i >= n; i--)
#define ll long long
#define pb push_back
#define fr first
#define sc second
#define len(s) s.size()
#define all(v) v.begin(), v.end()
#define tr(it, v) for (auto &it : v)
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef vector<pii> vpii;
typedef vector<string> vs;
typedef vector<vi> vvi;
// =================

const int mod = 1e9+7;
const int N = 3e5 + 1;

void solve()
{
    ll x,y;
    int a,b;
    cin>>x>>y>>a>>b;
    int req=(b+a-2)/(a-1);
    ll days=0;
    while (1ll*x*a<req && 1ll*x*a<y && 1ll*x*a>0)
    {
        x*=a;
        days++;
    }
    // if(x*a<x+b && x*a<y && 1ll*x*a>0) {
    //     x*=a;
    //     days++;
    // }
    // else if(x+b<y && x+b>0) {
    //     x+=b;
    //     days++;
    // }
    if(x<y && x>0) {
        days+=(y-1-x)/b;
    }
    cout<<days;
}

signed main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    while (t--)
        solve();
    return 0;
}

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

#include <fstream>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <cmath>
#include <cstring>

using namespace std;

const int MOD = 1e9 + 7;
const int INF = 200001;
using ll = long long;
using db = double;
#define FOR(i, x, n) for (int i = x; i < n; i++)
#define TRAV(it, x) for (auto it = x.begin(); it != x.end(); it++)
#define endl "\n"

int n;
ll a[INF];
ll ps1[INF], ps2[INF];

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

  // ifstream fin(".in");
  // ofstream fout(".out");

	cin >> n;
	FOR(i, 1, n + 1) {
		cin >> a[i];
	}

	FOR(i, 1, n + 1) {
		ps1[i] = ps1[i - 1] + a[i];
	}
	FOR(i, 1, n + 1) {
		ps2[i] = max(ps1[i], ps2[i - 1]);
	}

	ll current = 0;
	ll ans = -1e9;
	FOR(i, 1, n + 1) {
		ans = max(ans, current + ps2[i]);
		current += ps1[i];
	}

	cout << ans << endl;

	return 0;
}

/*
	*
*/


#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#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 ll long long
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define REPI(i, a, b) for (ll i = b - 1; i >= a; i--)
#define i_os ios::sync_with_stdio(0);  cin.tie(0);  cout.tie(0);
#define INF (ll)1e18 + 100
#define endl "\n"
#define p0(a) cout << a << " "
#define p1(a) cout << a << endl
#define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
// SOME BITMASK KNOWLEDGE
// 1)x & (x - 1):sets the last one bit of x to zero
// power of two exactly when x & (x − 1) = 0.
// 2)x & -x:sets all the one bits to zero, except last one bit
// 3)x | (x - 1):inverts all the bits after the last one bit
#define o_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define o_setll tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>
typedef tree<pair<ll, ll>,null_type,less<pair<ll, ll>>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
typedef tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update> indexed_set1;
typedef tree<string,null_type,less<string>,rb_tree_tag,tree_order_statistics_node_update> indexed_set2;
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);

ll power(ll a,ll n,ll c){
    if(n == 0) return 1 % c;
    ll s = power(a,n/2,c);
    s = (s * s) % c;
    if(n % 2) s = (s * a) % c;
    return s;
}

int main()
{
    ll n,t;
    cin>>n>>t;
    ll arr1[n/2],arr2[n-n/2];
    REP(i,0,n/2) cin>>arr1[i];
    REP(i,0,n-n/2) cin>>arr2[i];
    set<ll> s;
    REP(i,0,(1 << (n/2))){
        ll sum = 0;
        REP(j,0,n/2){
            if((i >> j) & 1){
                sum += arr1[j];
            }
        }
        s.insert(sum);
    }
    vector<ll> v;
    for(auto it:s) v.push_back(it);
    sort(v.begin(),v.end());
    ll ans = 0;
    REP(i,0,(1 << (n - n/2))){
        ll sum = 0;
        REP(j,0,n - n/2){
            if((i >> j) & 1){
                sum += arr2[j];
            }
        }
        ll p = t - sum;
        if(p >= 0){
            ll p1 = upper_bound(v.begin(),v.end(),p) - v.begin();
            if(p1 > 0){
                ans = max(ans,sum + v[p1-1]);
            }
            else {
                ans = max(ans,sum);
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}

#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int mod = 998244353;
constexpr ll modpow(ll A, ll B) {
	ll kotae = 1;
	while (B > 0) {
		if (B & 1) kotae = kotae * A % mod;
		A = A * A % mod;
		B >>= 1;
	}
	return kotae;
}

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

	ll N, M;
	cin >> N >> M;

	ll MI = modpow(M, mod - 2);
	ll kotae = 0;
	rep1(k, M) {
		ll tmp = 0;
		ll pre = 1;
		ll MK = (M - k) * MI % mod;
		rep(i, N) {
			kotae += tmp + pre;
			tmp += (k - 1) * MI % mod * pre % mod;
			tmp %= mod;
			pre = pre * MK % mod;
		}
	}

	kotae = kotae % mod * modpow(M, N - 1) % mod;

	co(kotae);

	Would you please return 0;
}


#include <bits/stdc++.h>
using namespace std;
#pragma optimize("-O3")
#define int long long int
#define f first
#define s second
#define pb push_back
#define endl '\n'

main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n;
	cin>>n;
	for(int i=1; i<=n; i++){
		cout<<((2*i)%n)+1<<" "<<((2*i+1)%n)+1<<endl;
	}
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#define ll long long

#define pb        push_back

#define ppb       pop_back

#define pf        push_front

#define ppf       pop_front

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

#define uniq(v)   (v).erase(unique(all(v)),(v).end())

#define ff        first

#define ss        second

#define pii       pair<int,int>

#define vi        vector<int>

#define vpi       vector<pair<int,int>>

#define mii          map<int,int>

#define rep(i,a,b)   for(int i=a;i<b;i++)

#define repe(i,a,b)   for(int i=a;i<=b;i++)

#define mem1(a)      memset(a,-1,sizeof(a))

#define mem0(a)      memset(a,0,sizeof(a))

#define ppc          __builtin_popcount

#define ppcll        __builtin_popcountll

#define INF 100000000000000000

#define mod 1000000007

#define esp 10e-7

const int mx=1e5+7;
bool isseven(int n){

    while(n){
        if(n%10==7){
            return true;
        }
        n/=10;
    }
    return false;
}
bool isoctalseven(int n){
    while(n){
        if(n%8==7){
            return true;
        }
        n/=8;
    }
    return false;
}
int main(){

    int n;
    cin>>n;
    int m=7;
    unordered_set<int> st;
    // int c1=0;
    // while(m<=n){
    //     c1++;
    //     st.insert(m);
    //     m+=8;
    // }
    // cout<<c1;
    int c2=0;
    rep(i,1,n+1){
        if((isseven(i) )|| isoctalseven(i)){
            // cout<<i<<" ";
            c2++;
        }
    }
    cout<<n-(c2);

    return 0;

}


///////////////////////Loading Payloads.......////////////////////////////
#include<bits/stdc++.h>
#define f(i,a,b) for(int i = a; i < b; i++)
#define fd(i,a,b) for(int i = a; i > b;i--)
#define fld(i,a,b) for(ll i = a ; i > b;i--)
#define ll long long
#define fl(i,a,b) for(ll i = a; i < b; i++)
#define VI vector<int>
#define VP vector<pair<int,int>>
#define VPL vector<pair<ll,ll>>
#define VS vector<string>
#define VL vector<long long>
#define VC vector<char>
#define VF vector<float>
#define VD vector<double>
#define VB vector<bool>
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define fa(i,skill) for(auto i : skill)
#define fr(i,skill) for(VI :: reverse_iterator i = skill.rbegin(); i != skill.rend(); i++)
#define PI pair<int,int>
#define mi map<int,int>
#define ml map<ll,ll>
#define mci map<char,int>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define MAX 1000000007
#define INF 1000000000
#define debug(x) cout<<"This side    ----> "<<#x<<" -> "<<x<<endl
using namespace std;

///////////////////////Engine Check.......////////////////////////////
void edit(VI &skill,int n)
{
    f(i,0,n)
        {
            int x;
            cin >> x;
            skill.PB(x);
        }
}

bool cmp(pair<ll,ll>p1,pair<ll,ll>p2)
{
    if(p1.S > p2.S)
        return true;
    if(p1.S < p2.S)
        return false;
    if(p1.F > p2.F)
        return true;
    else
        return false;
}
void editl(VL &skill,ll n)
{
    fl(i,0,n)
        {
            ll x;
            cin >> x;
            skill.PB(x);
        }
}
void editpl(VPL &skill,ll n)
{
    fl(i,0,n)
        {
            ll x,y;
            cin >> x >> y;
            skill.PB({x,y});
        }
}

bool isGoogles = 0;
bool testcase = 0;
void achilles()
{

    VL arr;editl(arr,3);
    sort(all(arr));

    if(arr[1]-arr[0] == arr[2]-arr[1])
        cout << "Yes\n";
    else
        cout << "No\n";

}

///////////////////////3....2....1...Ignition////////////////////////////

int main()
{

    ios :: sync_with_stdio(0);
    cin.tie(0);
    //freopen("out.txt","r",stdin);
    ll t=1,w=1;
    if(testcase)
    cin >>t;
    // precalc();
    while(t--)
    {   if (isGoogles) {cout << "Case #" << w << ": ";}

        achilles();

        w++;

    }
    return 0;
}

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp> // Common file
#include<ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<ll,ll>
#define F first
#define S second
#define ld long double
#define rep(i,a,n) for (int i = (a) ; i < (n); ++i)
#define vi vector<ll>
#define all(v) (v).begin(),(v).end()
#define M_PI 3.14159265358979323846
#define int ll
typedef tree <
pair<int, int>,
     null_type,
     less<pair<int, int>>,
     rb_tree_tag,
     tree_order_statistics_node_update > od_st; // find_by_order() and order_of_key()

const ll mod = 1e9 + 7;
const ll inf = 1e18;

void solve() {
	vector<int> a(3);
	for ( auto &k : a)cin >> k;
	sort(all(a));

	if ( 2 * a[1] == a[0] + a[2]) {
		cout << "Yes" << endl;
	} else {
		cout << "No" << endl;
	}

}

int32_t main() {
	FASTIO
	int t = 1;
	// create();
	// cin >> t;
	ll k = 1;
	while (t--) {
		// cout << "Case #" << k++ << ": ";
		solve();
	}
	return 0;
}


// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 11/22/20 20:12:05
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
constexpr int INF = 2e9;

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

  int a,b,c,d; cin>>a>>b>>c>>d;
  auto dist = [](int a, int b, int c, int d) {
    if (((a+b)%2) != ((c+d)%2))
      return INF;
    if (a==c and b==d)
      return 0;
    if (abs(a-c) == abs(b-d))
      return 1;
    return 2;
  };
  int ans = INF;
  for (int i = a-3; i <= a+3; i++) {
    for (int j = b-3; j <= b+3; j++) {
      if (abs(i-a)+abs(j-b) > 3)
        continue;
      int s = (i==a and j==b) ? 0 : 1;
      for (int k = c-3; k <= c+3; k++) {
        for (int l = d-3; l <= d+3; l++) {
          if (abs(k-c) + abs(d-l) > 3)
            continue;
          int g = (k == c and l == d ? 0 : 1);
          int aux = dist(i,j,k,l);
          if (aux == INF)
            continue;
          ans = min(ans, s+g+aux);
        }
      }
    }
  }
  cout << ans << endl;

  return 0;
}

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

int main() {
  ll r1, r2, c1, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  ll r = r2-r1, c = c2-c1;
  if (r == 0 && c == 0) cout << 0 << endl;
  else if (r == c || r == -c || abs(r) + abs(c) <= 3) cout << 1 << endl;
  else if ((r+c)%2 == 0 || abs(r+c) <= 3 || abs(r-c) <= 3 || abs(r) + abs(c) <= 6) cout << 2 << endl;
  else cout << 3 << endl;
}


#include <bits/stdc++.h>
#define rep0(i,r) for (int i=0,i##end=r;i<i##end;++i)
#define rep(i,l,r) for (int i=l;i<=r;++i)
#define per(i,r,l) for (int i=r;i>=l;--i)
#define pr pair<int,int>
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pb push_back
using namespace std;
const int N=2e5+5;
int e[N][2],c[N],tag[N],dfn[N],dc; vector<pr> G[N];
void dfs(int u,int f){
	dfn[u]=++dc;
	rep0(i,G[u].size()){
		int v=G[u][i].fi,t=G[u][i].se;
		if (t){
			if (!dfn[v]) tag[t]=1;
			else if (dfn[v]<dfn[u]&&v!=f) tag[t]=1;
		}
		if (!dfn[v]) dfs(v,u);
	}
}
int main(){
	int n,m; scanf("%d%d",&n,&m);
	rep(i,1,m) scanf("%d%d",&e[i][0],&e[i][1]);
	rep(i,1,n) scanf("%d",&c[i]);
	rep(i,1,m){
		int u=e[i][0],v=e[i][1];
		if (c[u]==c[v]) G[u].pb(mp(v,i)),G[v].pb(mp(u,0));
	}
	rep(i,1,n) if (!dfn[i]) dfs(i,0);
	rep(i,1,m){
		int u=e[i][0],v=e[i][1];
		if (c[u]==c[v]) puts(tag[i]?"->":"<-");
		else if (c[u]<c[v]) puts("<-");
		else puts("->");
	}
	return 0;
}

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

#define repi(i, a, n) for (int i = a; i < (int)(n); ++i)
#define rep(i, n) repi(i, 0, n)
#define all(v) begin(v), end(v)

using ll = long long;

int main() {
  // freopen("1.txt", "rb", stdin);
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int V, T, S, D;
  cin >> V >> T >> S >> D;

  if (D < V * T || D > V * S) {
    cout << "Yes" << '\n';
  } else {
    cout << "No" << '\n';
  }

  return 0;
}


///Bismillahir Rahmanir Rahim
#include "bits/stdc++.h"

#define                           ll                          long long
#define                           int                         ll
#define                           fi                          first
#define                           si                          second
#define                           mp                          make_pair
#define                           pb                          push_back
#define                           pi                          pair<int,int>
#define                           nd(a,b,c)                   mp(mp(a,b),c)
#define                           clr(x)                      memset(x,0,sizeof(x));
#define                           f(i,l,r)                    for(int i=l;i<=r;i++)
#define                           rf(i,r,l)                   for(int i=r;i>=l;i--)
#define                           done(i)                     cout<<"done = "<<i<<endl;
#define                           show(x,y)                   cout<<x<<" : ";for(auto z:y)cout<<z<<" ";cout<<endl;
#define                           fast                        ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

const ll inf=2e18;
const int mod=1e9+7;
const int M=3005;

inline ll bigmod(ll B,ll P){ll R=1;while(P>0){if(P&1){R=(R*B)%mod;}P>>=1;B=(B*B)%mod;}return R;}
inline ll ad(ll x,ll y){ll ret=(x%mod+y%mod)%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;}
inline ll sub(ll x,ll y){ll ret=((x%mod)-(y%mod)+mod)%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;}
inline ll gun(ll x,ll y){ll ret=((x%mod)*(y%mod))%mod;if(ret<0){ret+=mod,ret=ret%mod;}return ret;}

int n;
int a[M],pre[M];

int dp[M][M];

int sum[M],nxt[M];

 main()

{
    fast
    cin>>n;
    f(i,1,n)cin>>a[i],pre[i]=a[i]+pre[i-1];

    rf(k,n,1)
    {
        f(pos,1,n)
        {
            int val=pre[pos-1]%k;

            sum[val]=sub(sum[val],dp[pos][k+1]);

            dp[pos][k]=sum[val];

            int z=pre[n]-pre[pos-1];
            z=z%k;

            if(z==0)dp[pos][k]=(dp[pos][k]+1)%mod;

            if(k!=1){

            val=pre[pos-1]%(k-1);
            nxt[val]=ad(nxt[val],dp[pos][k]);

            }
        }

        f(j,0,k+1)
        {
            sum[j]=nxt[j];
            nxt[j]=0;
        }
    }

    cout<<dp[1][1]<<"\n";
    return 0;

}

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
struct MB{
    int x;
    MB(int _x=0):x(_x){}
    void operator +=(const MB& o){ *this = *this + o; }
    void operator -=(const MB& o){ *this = *this - o; }
    void operator *=(const MB& o){ *this = *this * o; }
    MB& operator ++(){ return *this = *this + MB(1); }
    MB operator ++(int){ MB r=*this; ++(*this); return r; }
    MB operator +(const MB& o){ return x+o.x >=mod ? x+o.x-mod : x+o.x; }
    MB operator -(const MB& o){ return x-o.x < 0 ? x-o.x+mod : x-o.x; }
    MB operator *(const MB& o){ return int(1ll*x*o.x%mod); }
    operator int(){return x;}
    friend ostream& operator<< (ostream& os, MB& o) { return os << o.x; }
    friend istream& operator>> (istream& is, MB& o) { return is >> o.x; }
};

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    using ll = long long;
    int n;cin>>n;
    vector<ll>a(n),p(n+1);
    for(int i=0;i<n;++i)cin>>a[i];
    for(int i=0;i<n;++i)p[i+1] = a[i] + p[i];
    vector<vector<MB>>dp(2,vector<MB>(n+1));
    //dp[i][j] - #formas de fazer i grupos em [0..j)
    dp[0][0] = 1;
    MB ans = 0;
    for(int i=1,o=1;i<=n;++i,o^=1){
        vector<MB>s(n);
        for(int j=0;j<=n;++j){
            //dp[i][j] = somatorio de dp[i-1][x], soma( [x,j) ) == 0 mod i x < j
            //dp[i][j] = somatorio de dp[i-1][x], p[j] == p[x] mod i x < j
            dp[o][j] = s[ p[j] % i ];
            s[p[j] % i] += dp[o^1][j];
        }
        ans += dp[o][n];
    }
    cout<<ans<<'\n';
    return 0;
}


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

using P = pair<int,int>;

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<vector<int>> g(n);
  vector<int> root(n,-1);
  root[0] = 0;
  for (int i=0;i<n-1;i++) {
    int p;
    cin >> p;
    p--;
    g[i+1].emplace_back(p);
    g[p].emplace_back(i+1);
    root[i+1] = p;
  }
  stack<int> st;
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    st.push(now);
    for (int i=0;i<g[now].size();i++) {
      int to = g[now][i];
      if (to != root[now]) {
        q.push(to);
      }
    }
  }
  vector<int> subt(n,1);
  vector<int> suba(n,0);
  vector<int> turn(n,1);
  while (!st.empty()) {
    int now = st.top();
    st.pop();
    vector<P> ps;
    int rett = 0;
    int reta = 0;
    int kt = 0;
    for (int i=0;i<g[now].size();i++) {
      int to = g[now][i];
      if (to == root[now]) continue;
      if (turn[to] == 1) {
        ps.emplace_back(P(subt[to]-suba[to],to));
        kt++;
      }
      else {
        if (subt[to] < suba[to]) {
          subt[now] += subt[to];
          suba[now] += suba[to];
        }
        else {
          rett += subt[to];
          reta += suba[to];
        }
      }
    }
    sort(ps.begin(),ps.end());
    for (int j=0;j<ps.size();j++) {
      P pst = ps[j];
      int ind = pst.second;
      if (j%2 == 0) {
        subt[now] += subt[ind];
        suba[now] += suba[ind];
      }
      else {
        subt[now] += suba[ind];
        suba[now] += subt[ind];
      }
    }
    if (kt%2 == 0) {
      subt[now] += rett;
      suba[now] += reta;
    }
    else {
      subt[now] += reta;
      suba[now] += rett;
    }
    turn[now] = (kt+1)%2;
  }
  cout << subt[0] << endl;
}

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

#define pb push_back
#define MP make_pair
#define N 200005
typedef long long LL;

int n, p[N];
int f[N], sz[N];
vector<int> E[N];

void dfs(int x)
{
	++sz[x];
	vector<int> v, v2;
	for (auto &y: E[x])
	{
	 	dfs(y);
	 	if (sz[y]&1) v.push_back(f[y]);
	 	else v2.push_back(f[y]);
	 	sz[x] += sz[y];
    }
    sort(v2.begin(), v2.end());
    f[x]++;
    int ss = 0;
    for (auto val: v2)
	{
		if (val < 0) f[x] += val;
		else ss+=val;
	}
	sort(v.begin(), v.end());
	v.push_back(ss);
	int o = 1;
	for (auto val: v)
		f[x] += o*val, o*=-1;
	//cout << x << " " << f[x] << endl;
}
int main()
{
	scanf("%d", &n);
	for (int i = 2; i <= n; ++i)
		scanf("%d", &p[i]), E[p[i]].push_back(i);
	dfs(1);
	cout << (n+f[1])/2<<endl;
	return 0;
}