#include <bits/stdc++.h>

using namespace std;

#define MP make_pair
#define PB push_back
#define REP(i, L, R) for (int i = L; i < (R); ++i)
#define SZ(x) (int)x.size()

using ll = long long;
using ld = long double;
using P = pair<ll, int>;

template <typename T>
bool ckmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template <typename T>
bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

ll ternary_search(double lo, double hi, ll c, ll d)
{
    double eps = 1e-3;
    while (hi - lo > eps)
    {
        double m1 = lo + (hi - lo) / 3;
        double m2 = hi - (hi - lo) / 3;
        double f1 = m1 + c + d / (m1 + 1.);
        double f2 = m2 + c + d / (m2 + 1.);
        if (f1 < f2)
        {
            hi = m2;
        }
        else
        {
            lo = m1;
        }
    }

    return (ll)floor(lo);
}

struct e
{
    int to;
    ll c, d;
    e(int to, ll c, ll d) : to(to), c(c), d(d) {}

    ll f(ll t)
    {
        return t + c + d / (t + 1);
    }

    ll get(ll t)
    {
        if (t >= d)
        {
            return f(t); // always = t+c
        }

        ll lo = ternary_search((double)t, (double)(d + 1.), c, d);
        return min(f(lo), f(lo + 1));
        /*
        // find t' such that f(t') is minimized.
        ll lo = t, hi = d + 1;
        while (lo + 1 < hi)
        {
            ll mid = (hi - lo) / 2 + lo;
            if (mid * mid + mid < d)
            {
                lo = mid;
            }
            else
            {
                hi = mid;
            }
        }

        return min(f(lo), f(lo + 1));
*/
    }
};

const int MAXN = 1e5 + 1;
const ll INF = 1e18;
ll d[MAXN];

void dijkstra(int s, int n, const vector<vector<e>> &g)
{
    priority_queue<P, vector<P>, greater<P>> que;
    fill(d, d + n, INF);
    d[s] = 0;
    que.push(MP(0, s));

    while (!que.empty())
    {
        auto [dv, v] = que.top();
        que.pop();
        if (dv > d[v])
        {
            continue;
        }
        for (auto e : g[v])
        {
            ll nextt = e.get(d[v]);
            if (nextt < d[e.to])
            {
                d[e.to] = nextt;
                que.push(MP(d[e.to], e.to));
            }
        }
    }
}

void solve()
{
    int n, m;
    cin >> n >> m;
    vector<vector<e>> g(n, vector<e>());

    REP(i, 0, m)
    {
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        g[a - 1].emplace_back(b - 1, c, d);
        g[b - 1].emplace_back(a - 1, c, d);
    }
    dijkstra(0, n, g);
    cout << (d[n - 1] == INF ? -1 : d[n - 1]) << endl;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T = 1;
    while (T--)
    {
        solve();
    }
    return 0;
}

//in dp prefix suffix sum helps..
//map adds a logn factor.Try using 2d array.
#include<iostream>
#include<vector>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<stack>
#include <chrono>
#include <random>
#include <iterator>
#include <map>
#include<list>
#include<sstream>
#include <algorithm>
#include <fstream>
#include<unordered_map>
#include<set>
#include<queue>
#define int  long long
#define double long double
#define pb   push_back
#define mp   make_pair
#define pii  pair<int,int>
#define pip pair<int,pii>
#define vip vector<pip>
#define vi vector<int>
#define vs vector<string>
#define vd vector<double>
#define mii  map<pii, int>
#define qu queue<int>
#define set set<pii>
#define multiset multiset<int>
#define rep(i, a, b) for(int i=a;i<b;i++)
#define rep1(i, b, a) for(int i=b;i>=a;i--)
#define viv vector<viv>
#define stack stack<int>
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define it iterator
#define pq priority_queue<int>
#define pq1 priority_queue <pii, vector<pii>, greater<pii> >
#define mem(x, y) memset(x, (int)y, sizeof(x))
#define ps(x,y) fixed<<setprecision(y)<<x
// author :: Anurag Anand.
using namespace std;
int z=  1e9+7;
//int z= 998244353;
int gcd(int a, int b){
    if(a==0)return b;
    if(b==0)return a;
    return gcd(b, a%b);
}
int power(int a,int b){
	int res=1;
	while(b){
		if(b&1) res=(res*a)%z, b--;
		a=(a*a)%z;
		b=b>>1;
	}
	return res;
}
vi sv;
int fact[1000001];
int ncr(int n, int r){
	int p=fact[n];
	p*=power(fact[r], z-2);
	p%=z;
	p*=power(fact[n-r], z-2);
	p%=z;
	return p;
}

// vi seg;
// void arrange(int i, int v, int x, int lx, int rx){
// 	if(rx-lx==1){
// 		seg[x]=v;
// 		return;
// 	}
// 	int mid=(lx+rx)/2;
// 	if(i<mid) arrange(i, v, 2*x+1, lx, mid);
// 	else arrange(i, v, 2*x+2, mid, rx);
// 	seg[x]= min(seg[2*x+1], seg[2*x+2]);
// }

// int calc(int l, int r, int x, int lx, int rx){
// 	if(lx>=r||l>=rx) return 1e9;
// 	if(lx>=l&&rx<=r) return seg[x];
// 	int mid=(lx+rx)/2;
// 	int k1=calc(l, r, 2*x+1, lx, mid), k2=calc(l, r, 2*x+2, mid, rx);
// 	return min(k1, k2);
// }
vi pp;
int dist[100001];
vip v1[100001];
mii mc, md;
void dijkstra(int root){
	int a, d;
	dist[root]=0;
	set s;
	s.insert(mp(0, root));
	while(!s.empty()){
		a= (*s.begin()).se;
		s.erase(s.begin());
		for(int i=0;i<v1[a].size();i++){
			int y=v1[a][i].fi;
			int c=v1[a][i].se.fi, d=v1[a][i].se.se;
			int p2=ceil(sqrt(d)-1);
			p2= max(p2, dist[a]);
			int t1=p2+c+d/(p2+1);
			if(t1<dist[y]){
				s.erase(mp(dist[y], y));
				dist[y]=t1;
				s.insert(mp(dist[y], y));
			}
		}
	}
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
    cout.tie(NULL);
	// freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
	pp.assign(1000001, 0);
	for(int i=2;i<1000;i++){
		if(!pp[i]){
			for(int j=i*i;j<=1000000;j+=i){
				pp[j]=1;
			}
		}
	}
	fact[0]=1;
	rep(i,1,1000001) fact[i]=i*fact[i-1], fact[i]%=z;
	for(int i=2;i<=1000000;i++) if(!pp[i]) sv.pb(i);
	int t;
	t=1;
	//cin>>t;
	while(t--){
		int n, m, x, y, c, d;
		cin>>n>>m;
		rep(i,0,m){
			cin>>x>>y>>c>>d;
			v1[x].pb(mp(y, mp(c,d)));
			v1[y].pb(mp(x, mp(c,d)));
		}
		rep(i,1,n+1) dist[i]=1e16;
		dijkstra(1);
		if(dist[n]==1e16) dist[n]=-1;
		cout<<dist[n]<<"\n";
	}
}


    #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<ll,ll>;
    const int INF=1001001001;
    const int mod=1e9+7;

    void solve(){
      int n;cin>>n;
      vector<vector<int>>t(n,vector<int>(5));
      rep(i,n){
        rep(j,5){cin>>t[i][j];}
      }
      vector<int>mx(1<<5);
      rep(mask,1<<5){
        rep(i,n){
          int tmp=INF;
          rep(j,5){
            if((~mask>>j)&1){continue;}
            chmin(tmp,t[i][j]);
          }
          chmax(mx[mask],tmp);
        }
      }
      int ans=0;
      rep(i,1<<5){
        rep(j,1<<5){
          rep(k,1<<5){
            if((i|j|k)!=(1<<5)-1){continue;}
            int tmp=min({mx[i],mx[j],mx[k]});
            chmax(ans,tmp);
          }
        }
      }
      cout<<ans<<endl;
    }
    int main(){
      solve();
      return 0;
    }

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
#define fi(i,n) for(int i=0;i<n;i++)
#define f(i, a, b) for(int i=a;i<b;i++)
#define vi vector<int>
#define pb push_back
#define MOD 1000000007
#define pii pair<int, int>
#define ff first
#define ss second
#define setzero(a) memset(a,0,sizeof(a))
ll digits(ll n)
{
    return floor(log10(double(n))) + 1;
}
void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
}
ll modularExponentiation(ll x,ll n,ll M)
{
    ll result = 1;
    while(n>0)
    {
        if(n % 2 ==1)
        {
            result = ((result%M)*(x%M))%M;
        }
        x  = ((x%M)*(x%M))%M;
        n = n/2;
    }
    return result%M;
}
ll d, x, y;
void extendedEuclid(ll A, ll B)
{
    if(B == 0)
    {
        d = A;
        x = 1;
        y = 0;
    }
    else
    {
        extendedEuclid(B, A%B);
        int temp = x;
        x = y;
        y = temp - (A/B)*y;
    }
}
// DP solution for nCr
ll nCrModp(ll n, ll r)
{
    if (r > n - r)
        r = n - r;
    ll C[r + 1];
    memset(C, 0, sizeof(C));

    C[0] = 1;
    for (ll i = 1; i <= n; i++)
    {
        for (ll j = min(i,r); j > 0; j--)
        {
            C[j] = (C[j] + C[j - 1]);
        }
    }
    return C[r];
}
ll modInverse(ll A, ll M)
{
    extendedEuclid(A,M);
    return (x%M+M)%M;    //x may be negative
}
// primefactors[a] = primefactors[a/primedivisor[a]] + 1
ll sumdigits(ll n)
{
    ll ans = 0;
    while(n>0)
    {
        ans += n%10;
        n = n/10;
    }
    return ans;
}
ll less_power2(ll n)
{
    ll i = 1;
    while(i*2<=n)
    {
        i = i*2;
    }
    return i;
}
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
    if (n % 2 == 0 || n % 3 == 0)
        return false;

    for (int i = 5; i * i <= n; i = i + 6)
        if (n % i == 0 || n % (i + 2) == 0)
            return false;
    return true;
}

vector<int>adj[101];
int visited[100];
void dfs(string ans,int v,int d)
{
    ans += v + '0';
    if(v==d)
    {
        cout<<ans<<endl;
        return;
    }
    visited[v] = 1;
    for(auto x : adj[v])
    {
        if(visited[x]==0)
        {
            dfs(ans,x,d);
        }
    }
    visited[v] = 0;
}
bool fun(int arr[],int in, bool ma, int n)
{
    if(arr[in])
        in++;
    if(in==n)
        return ma;
    fun(arr,in,ma,n);
}
int main()
{
    ll n;
    cin>>n;
    n--;
    ll ans = nCrModp(n,11);
    cout<<ans;
}


#include<bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma comment(linker, "/stack:200000000")
using namespace std;
#define F first
#define S second
#define EB emplace_back
#define MP make_pair
#define all(o) (o).begin(), (o).end()
#define mset(m,v) memset(m,v,sizeof(m))
#define rep(i,n) for(ll i=0;i<(n);++i)
#define repe(i,a,b) for(ll i=a;i<=b;++i)
#define revlp(i,a,b) for(ll i=a;i>=b;i--)
#define remin(a,b) (a=min((a),(b)))
#define remax(a,b) (a=max((a),(b)))
#define sz(x) (ll)(x).size()
#define endl '\n'
typedef long long ll;        typedef long double ld;
typedef pair<ll,ll> pi;     typedef vector<ll> vi;
typedef vector<pi> vpi;       typedef vector<vi> graph;
long long mod=1000000007;     long double EPS=1e-9;
#ifndef ONLINE_JUDGE
#define debarr(a,n)cerr<<#a<<":";for(int i=0;i<n;i++)cerr<<a[i]<<" ";cerr<<endl;
#define debmat(mat,row,col)cerr<<#mat<<":\n";for(int i=0;i<row;i++){for(int j=0;j<col;j++)cerr<<mat[i][j]<<" ";cerr<<endl;}
#define pr(...)dbs(#__VA_ARGS__,__VA_ARGS__)
template<class S,class T>ostream &operator<<(ostream &os,const pair<S,T> &p){return os<<"("<<p.first<<","<<p.second<<")";}
template<class T>ostream &operator<<(ostream &os,const vector<T> &p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class T>ostream &operator<<(ostream &os,const set<T>&p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class T>ostream &operator<<(ostream &os,const multiset<T>&p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class S,class T>ostream &operator<<(ostream &os,const map<S,T>&p){os<<"[";for(auto&it:p)os<<it<<" ";return os<<"]";}
template<class T>void dbs(string str,T t){cerr<<str<<":"<<t<<"\n";}
template<class T,class...S>void dbs(string str,T t,S... s){int idx=str.find(',');cerr<<str.substr(0,idx)<<":"<<t<<",";dbs(str.substr(idx+1),s...);}
#else
#define pr(...){}
#define debarr(a,n){}
#define debmat(mat,row,col){}
#endif

bool check(string s){
    if(sz(s)==0)    return 1;
    string t=s;
    reverse(all(t));
    return s==t;
}
void solve(){
    string s;
    cin>>s;
    ll n=sz(s);
    ll i=n;
    while(i-1>=0 and s[i-1]=='0'){
        i--;
    }
    string rem=s.substr(0,i);
    if(check(rem)){
        cout<<"Yes"<<endl;
    }
    else cout<<"No"<<endl;
}

signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //cout<<fixed<<setprecision(15);
    //clock_t begin = clock();
    ll tc=1;//cin>>tc;
    for(ll i=1;i<=tc;i++){
        //cout<<"Case #"<<i<<": ";
        solve();
    }
    // clock_t end = clock();double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;cerr << elapsed_secs;
}

#include<bits/stdc++.h>
#define endl '\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;
#define MAX 100005
int arr[13];
string s;
vector<int> v;
void solve(int i)
{
    if(i==4)
    {
        int temp[13];
        for(int k=1;k<=9;k++) temp[k]=arr[k];
        for(auto k: v)
        {
            if(temp[k]==0) return ;
            temp[k]--;
        }
        ll no=v[0]*100+ v[1]*10 + v[2];
        if(no%8==0)  { cout<<"Yes"<<endl; exit(0); }
        return ;
    }
    for(int  k=1;k<=9;k++){
        v.push_back(k);
        solve(i+1);
        v.pop_back();
    }
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>s;
    if(s.size()==1) {
        if(s[0]=='8') cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
        return 0;
    }
    if(s.size()==2)
    {
        int val=(s[0] - '0' )  *10  + (s[1]-'0') ;
        int fck=(s[1] - '0' )  *10  + (s[0]-'0') ;
        if(val%8==0 || fck%8==0) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
        return 0;

    }
    for(auto i: s)
    {
        arr[i-'0']++;
    }
    solve(1);
    cout<<"No"<<endl;
}


#include<bits/stdc++.h>
using namespace std;
using ll = long long;
int gcd(int a,int b)
{
	if(b==0)
	return a;

	return gcd(b,a%b);
}
int power(int x,int y,int p)
{
	int res=1;
	x=x%p;
	while(y>0)
	{
		if(y&1)
		res=(res*x)%p;

		y=y>>1;
		x=(x*x)%p;
	}
	return res;
}
struct Matrix{
	int a[2][2] = {{0,0},{0,0}};
	Matrix operator *(const Matrix& other){
		Matrix product;
		for(int i=0;i<2;i++){
			for(int j=0;j<2;j++){
				for(int k=0;k<2;k++){
						product.a[i][k] =product.a[i][k] + (ll)a[i][j]*other.a[j][k];
				}
			}
		}
		return product;
	}
};
int modInverse(int n,int p)
{
	return power(n,p-2,p);
}
vector<int> fact(10);
void factorial(){
	fact[0] = 1;
	for(int i=1;i<10;i++){
		fact[i] = fact[i-1] * i;
	}
}

void test_case(){
	ll n;
	cin>>n;
	vector<ll> v(n);
	for(int i=0;i<n;i++){
		cin>>v[i];
	}
	sort(v.begin(),v.end());
	vector<ll> suff(n);
	suff[n-1] = v[n-1];
	for(ll i=n-2;i>=0;i--){
		suff[i] = v[i] + suff[i + 1];
	}
	ll ans = 0;
	for(ll i=0;i<n;i++){
		ans = ans + suff[i] - (n - i) * v[i];
	}
	cout<<ans<<'\n';
}
int main() {
	// your code goes here
	 ios_base::sync_with_stdio(0);
	 cin.tie(0);
	 int t = 1;
	 //cin>>t;
	 while(t--){
		 test_case();
	 }
	 return 0;
}

#include<bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#define LOCAL
#endif
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef vector<ll> vi;
typedef vector<pll> vpll;
const long infl=0x3f3f3f3f3f3f3f3fLL;
const int infi=0x3f3f3f3f;
#define endl        '\n'
#define pb          push_back
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define rs(v,n)     v.resize(n)
#define hell        1000000007
//#define hell        998244353
#define peak        9e18
#define pii         acos(-1)
#define clr(a,x)    memset(a,x,sizeof(a))
auto clk=clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T>
using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

namespace Ops{
template<class y,class z> ll expo(ll b,y e,z m){ll a=1;b%=m;while(e!=0){if((e&1)==1){a=a*b;a=a%m;}b=b*b;b%=m;e>>=1;}return a%m;}
template<class y> ll power(ll b,y e){ll a=1;while(e!=0){if((e&1)==1){a=a*b;}b=b*b;e>>=1;}return a;}
template<class x,class z> ll inv(x b,z m){return expo(b,m-2,m);}
template<class x,class y> x invGeneral(x a,y b){if(a==0)return b==1?0:-1;x m=invGeneral(b%a,a);return m==-1?-1:((1-(ll)b*m)/a+b)%b;}
template<class x,class y> x min(x a,y b){x c;if(a<=b)c=a;else c=b;return c;}
template<class x,class y> x max(x a,y b){x c;if(a>=b)c=a;else c=b;return c;}
ll mul(ll x,ll y,ll m){x%=m;y%=m;if(x==0)return 0;if(x%2==0)return (mul(x/2,y,m)*2)%m;return ((mul(x/2,y,m)*2)%m+y)%m;}
}
using namespace Ops;

namespace InOp{
template<class x>istream &operator>>(istream &in,vector<x> &v) {for(auto& i:v)in>>i;return in;}
template<class x,class y> ostream &operator<<(ostream &out,pair<x,y> &p) {out<<"("<<p.F<<","<<p.S<<")";return out;}
template<class x> ostream &operator<<(ostream &out,vector<x> &v) {out<<"Size : "<<v.size()<<endl;for(auto i:v)out<<i<<" ";out<<endl;return out;}
}
using namespace InOp;

namespace Debug{
#ifdef LOCAL
#define deb(...) printall(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void printall(const char* name, Arg1&& arg1){cerr<<name<<" : "<<arg1<<endl;}
template <typename Arg1, typename... Args>
void printall(const char* names,Arg1&& arg1,Args&&... args){const char* comma=strchr(names,',');cerr.write(names,comma-names)<<" : " <<arg1<<" | ";printall(comma+1,args...);}
#else
#define deb(...) void(0)
#endif
}
using namespace Debug;

namespace Bits{
template<class x> constexpr int onbits(x a){return __builtin_popcount(a);}
constexpr int bits(long long x){return 64-__builtin_clzll(x);}//take care of 0
constexpr int bits(int x){return 32-__builtin_clz(x);}//take care of 0
constexpr int traz(int a){return __builtin_ctz(a);}
constexpr int traz(long long a){return __builtin_ctzll(a);}
}
using namespace Bits;

namespace YesNo{
//#define CAPITAL
void yes(){
    #ifdef CAPITAL
    cout<<"YES"<<"\n";
    #else
    cout<<"Yes"<<"\n";
    #endif
}
void no(){
    #ifdef CAPITAL
    cout<<"NO"<<"\n";
    #else
    cout<<"No"<<"\n";
    #endif
}
}
using namespace YesNo;

void maester();
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    srand(chrono::high_resolution_clock::now().time_since_epoch().count());
    cout<<fixed<<setprecision(6);
    cerr<<fixed<<setprecision(6);
    ll test=1;
    //cin>>test;
    for(ll tt=1;tt<=test;tt++)
    {
        //cout<<"Case #"<<tt<<": ";
    	maester();
    }
    #ifdef LOCAL
    cerr<<endl<<endl<<endl<<endl<<"Time elapsed: "<<(double)(clock()-clk)/CLOCKS_PER_SEC<<endl;
    #endif
    return 0;
}
#define N  1000005

void maester()
{
    ll i,j,k,l,r,m,n,x,y;
    cin>>n;
    vi v(n);
    cin>>v;
    sort(all(v));
    vi vv;
    for(i=1;i<n;i++)vv.pb(v[i]-v[i-1]);
    ll ans=0;
	for(i=0;i<n-1;i++)
	{
		ans+=(n-1-i)*(i+1)*vv[i];
	}
	cout<<ans<<endl;
}


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

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

#define pb push_back
#define fi first
#define se second
#define ll long long
#define tp top()
#define fr front()
#define vi vector<int>
#define sz size()
#define rep(i,a,b) for(int i = a; i < b; ++i)
#define mem(a,  b) memset(a,  (b),  sizeof(a))
#define clr(a) memset(a,  0,  sizeof(a))
#define sqr(x) ( (x) * (x) )
#define all(v) v.begin(),  v.end()

typedef pair<int, int> pii;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef pair<pii,pii> ppp;

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

#define N 200020
vector<int> adj[N];
vector<pii> qq[N];
int h[N], ans[N];

void dfs(int cur, int hh){
	for(auto x : qq[cur]) ans[x.se] -= h[x.fi];
	h[hh]++;
	for(int to : adj[cur]) dfs(to,hh+1);
	for(auto x : qq[cur]) ans[x.se] += h[x.fi];
}

void solve(){
	int n; cin>>n;
	rep(i,2,n+1){
		int u; cin>>u;
		adj[u].pb(i);
	}
	int q; cin>>q;
	rep(i,0,q){
		int u,d; cin>>u>>d;
		qq[u].pb({d,i});
	}
	dfs(1,0);
	rep(i,0,q){
		cout<<ans[i]<<"\n";
	}
}

int main()
{
	base();
//  freopen("input.txt", "r", stdin);
//  freopen("output.txt", "w", stdout);
	int t=1;
//	cin>>t;
	while(t--){
		solve();
	}
	return 0;
}

#include <bits/stdc++.h>
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
using namespace std;
using ll = long long;
struct Edge
{
    ll to;
    ll cost;
};
using Graph = vector<vector<ll>>;
using P = pair<ll, ll>;
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ALL(v) (v).begin(), v.end()
const ll MOD = 1000000007;
const ll nmax = 8;
const ll INF = LLONG_MAX;
const int MAX = 510000;
bool graph[nmax][nmax];
long long fac[MAX], finv[MAX], inv[MAX];

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

ll COM(int n, int k)
{
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;

    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

// vector<vector<ll>> dist = vector<vector<ll>>(nmax, vector<ll>(nmax, INF));
// void warshall_floyd(ll n)
// {
//     for (size_t i = 0; i < n; i++)
//     {
//         for (size_t j = 0; j < n; j++)
//         {
//             for (size_t k = 0; k < n; k++)
//             {
//                 dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k]);
//             }
//         }
//     }
// }

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

ll lcm(ll a, ll b)
{
    ll g = gcd(a, b);
    return a / g * b;
}
struct Matrix
{
    int sz;
    ll x[33][33];
};

Matrix multiply(Matrix A, Matrix B)
{
    Matrix C;
    C.sz = A.sz;
    for (int i = 0; i < C.sz; i++)
    {
        for (int j = 0; j < C.sz; j++)
        {
            C.x[i][j] = 0;
        }
    }

    for (int i = 0; i < A.sz; i++)
    {
        for (int j = 0; j < A.sz; j++)
        {
            for (int k = 0; k < A.sz; k++)
            {
                C.x[i][k] += A.x[i][j] * B.x[j][k];
                C.x[i][k] %= MOD;
            }
        }
    }

    return C;
}

Matrix powers(Matrix A, ll T)
{
    Matrix E[64], F;
    E[0] = A;
    for (int i = 1; i < 62; i++)
    {
        E[i] = multiply(E[i - 1], E[i - 1]);
    }

    F.sz = E[0].sz;

    for (int i = 0; i < F.sz; i++)
    {
        for (int j = 0; j < F.sz; j++)
        {
            if (i == j)
                F.x[i][j] = 1;
            else
                F.x[i][j] = 0;
        }
    }

    for (int i = 62; i >= 0; i--)
    {
        if ((T & (1LL << i)) != 0LL)
        {
            F = multiply(F, E[i]);
        }
    }
    return F;
}

ll ceil(ll a, ll b)
{
    return (a + b - 1) / b;
}

vector<bool> seen;

vector<vector<ll>> children, lst;
vector<ll> in, out, depth;
ll timer;

void dfs(int v)
{
    in[v] = timer++;
    lst[depth[v]].push_back(in[v]);
    for (auto &&i : children[v])
    {
        depth[i] = depth[v] + 1;
        dfs(i);
    }
    out[v] = timer++;
}
int main()
{

    long long N;
    std::scanf("%lld", &N);
    children = lst = vector<vector<ll>>(N);
    in = out = depth = vector<ll>(N);
    for (int i = 1; i < N; i++)
    {
        ll p;
        cin >> p;
        --p;
        children[p].push_back(i);
    }
    dfs(0);

    ll Q;
    cin >> Q;
    while (Q--)
    {
        ll u, d;
        cin >> u >> d;
        --u;

        const auto &v = lst[d];
        cout << lower_bound(ALL(v), out[u]) - lower_bound(ALL(v), in[u]) << endl;
    }

    return 0;
}


// Generated by 2.3.1 https://github.com/kyuridenamida/atcoder-tools

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

using namespace std;

using i64 = long long;

const long long MOD = 998244353;
const i64 INF = i64(1e18) + 7;

template <typename T>
bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

template <typename T>
bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}

// doc: https://shibh308.github.io/library/library/lib/classes/modint.cpp.html
template <i64 mod = MOD>
struct ModInt{
    i64 p;

    ModInt() : p(0){}
    ModInt(i64 x){p = x >= 0 ? x % mod : x + (-x + mod - 1) / mod * mod;}

    ModInt& operator+=(const ModInt& y){p = p + *y - ((p + *y) >= mod ? mod : 0); return *this;}
    ModInt& operator-=(const ModInt& y){p = p - *y + (p - *y < 0 ? mod : 0); return *this;}
    ModInt& operator*=(const ModInt& y){p = (p * *y) % mod; return *this;}
    ModInt& operator%=(const ModInt& y){if(y)p %= *y; return *this;}

    ModInt operator+(const ModInt& y) const{ModInt x = *this; return x += y;}
    ModInt operator-(const ModInt& y) const{ModInt x = *this; return x -= y;}
    ModInt operator*(const ModInt& y) const{ModInt x = *this; return x *= y;}
    ModInt operator%(const ModInt& y) const{ModInt x = *this; return x %= y;}

    friend ostream& operator<<(ostream& stream, const ModInt<mod>& x){
        stream << *x;
        return stream;
    }

    friend ostream& operator>>(ostream& stream, const ModInt<mod>& x){
        stream >> *x;
        return stream;
    }

    ModInt& operator++(){p = (p + 1) % mod; return *this;}
    ModInt& operator--(){p = (p - 1 + mod) % mod; return *this;}

    bool operator==(const ModInt& y) const{return p == *y;}
    bool operator!=(const ModInt& y) const{return p != *y;}

    const i64& operator*() const{return p;}
    i64& operator*(){return p;}

};

using mint = ModInt<>;

signed main(){
    int h, w;
    cin >> h >> w;
    vector<string> s(h);
    for(int i = 0; i < h; ++i){
        cin >> s[i];
    }
    vector<vector<int>> v(h + w - 1, vector<int>(3, 0));
    for(int i = 0; i < h; ++i){
        for(int j = 0; j < w; ++j){
            if(s[i][j] == 'R')
                ++v[i + j][0];
            if(s[i][j] == 'B')
                ++v[i + j][1];
            if(s[i][j] == '.')
                ++v[i + j][2];
        }
    }
    mint ans = 1;
    for(int i = 0; i < h + w - 1; ++i){
        if(v[i][0] && v[i][1])
            ans *= 0;
        else if(!v[i][0] && !v[i][1])
            ans *= 2;
        else
            ans *= 1;
    }
    cout << ans << endl;

    return 0;
}

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

using namespace __gnu_pbds;
using namespace std;

// #define int           long long
#define S             second
#define F             first
#define pb            push_back
#define all(c)        (c).begin(),(c).end()
#define rall(c)       (c).rbegin(),(c).rend()
#define lb            lower_bound
#define ub            upper_bound
#define si(c)         (int)((c).size())
#define lcm(a, b)      (a * (b / __gcd(a,b)))
#define inf           (int)(1e8)
#define endl          '\n'
#define mp            make_pair
#define time(s)       (double(clock()-s)/double(CLOCKS_PER_SEC))
#define debug(args...)    _F(#args, args)
#define vi            std::vector<int>
#define pii           pair<int, int>
#define vpi           vector<pii>
#define ordered_set   tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

clock_t start;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());

template<typename T> void _F(const char *name, T arg1){ cerr << name << " = " << arg1 << endl;}
template<typename T, typename... Args> void _F(const char *names, T arg1, Args... args)
{ const char *name = strchr(names, ',');cerr.write(names, name-names) << " = " << arg1 << endl;_F(name+2, args...);}

template< typename T1, typename T2 > istream& operator>>(istream& in, pair<T1, T2> &q){ in >> q.F >> q.S; return in;}
template< typename T1, typename T2 > ostream& operator<<(ostream& out, pair<T1, T2> &q){ out << q.F << " " << q.S; return out;}
template< typename T1, typename T2 > pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F+p2.F, p1.S+p2.S};}
template< typename T1, typename T2 > pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2){ return {p1.F-p2.F, p1.S-p2.S};}
template< typename T1, typename T2 > bool operator<(pair<T1, T2> p1, pair<T1, T2> p2){ return p1 < p2 ;}

template<typename T> void Unique(vector<T> &v) {
    sort(all(v)), v.resize(distance(v.begin(), unique(all(v))));
}

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

    int mn = inf, a[n][m];
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            cin >> a[i][j], mn = min(mn, a[i][j]);
    int ans = 0;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            ans += a[i][j]-mn;
    cout << ans << endl;
}

signed main() {

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    start = clock();

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

    int test = 1;
    // cin >> test;

    cout << fixed << setprecision(12);

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

    cerr << time(start);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vlli vector<ll>
#define vi vector<ll>
#define vs vector<string>
#define vplli vector< pair< ll,ll> >
#define plli pair< ll,ll >
#define vps vector< pair< string, string> >
#define vpi vector< pair< ll, ll> >
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define forn(i,a,n) for(ll i=a;i<n;i++)
#define forr(i,n,a) for(ll i=n-1;i>=a;i--)
#define scan(arr,a,n) for(ll i=(a);i<(n);i++)cin>>(arr)[i];
#define prll(arr,a,n) for(ll i=(a);i<(n);i++)cout<<(arr)[i]<<" ";

const ll inf = 1e18;

ll add(ll x, ll y) {ll res = x + y; return (res >= mod ? res - mod : res);}
ll mul(ll x, ll y) {ll res = x * y; return (res >= mod ? res % mod : res);}
ll sub(ll x, ll y) {ll res = x - y; return (res < 0 ? res + mod : res);}
ll power(ll x, ll y) {ll res = 1; x %= mod; while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}
ll mod_inv(ll x) {return power(x, mod - 2);}

int main(){
	fast;
    #ifndef ONLINE_JUDGE
    freopen("input1.txt","r",stdin);
    freopen("output1.txt","w",stdout);
    #endif
	ll n, m;
	cin>>n>>m;
	vlli a(n), b(n+2, 0);
	scan(a, 0, n);
	forn(i, 0, m){
		b[a[i]]++;
	}
	ll ans = inf;
	set<ll> p;
	forn(i, 0, n+2){
		if(!b[i]){
			ans = min(ans, i);
			p.insert(i);
		}
	}
	forn(i, m, n){
		b[a[i-m]]--, b[a[i]]++;
		if(b[a[i-m]]==0){
			p.insert(a[i-m]);
		}
		if(b[a[i]]==1){
			p.erase(a[i]);
		}
		auto it = p.begin();
		ans = min(ans, *it);
	}
	cout<<ans<<endl;
}

// Problem: E - Mex Min
// Contest: AtCoder - AtCoder Beginner Contest 194
// URL: https://atcoder.jp/contests/abc194/tasks/abc194_e
// Memory Limit: 1024 MB
// Time Limit: 4000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

const int N = 2e6+7;
int a[N],cnt[N];

int main()
{
	int n,m;scanf("%d%d",&n,&m);
	forn(i,1,n)	scanf("%d",&a[i]);
	int mex = 0,res = 0;
	forn(i,1,m)	++cnt[a[i]];
	while(cnt[mex])	++mex;
	res = mex;

	forn(i,1,n - m)
	{
		--cnt[a[i]];
		++cnt[a[i + m]];
		if(cnt[a[i]] == 0 && mex > a[i])	mex = a[i];
		else	while(cnt[mex])	++mex;
		res = min(res,mex);
	}
	printf("%d\n",res);
	return 0;
}


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

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

#define pb push_back
#define mp make_pair
#define cotu cout
#define itn int
#define Red ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for(int i = 0; i < n; i++)
#define repr(i,n) for(int i = n - 1; i >= 0; --i)
#define Rep(i, a, n) for(int i = (a); i <=(n); ++i)
#define repst(i, n) for(auto it = n.begin(); it != n.end(); ++it)
#define Repr(i, a, n) for(int i = (n); i >= (a); --i)
#define setp(x) fixed << setprecision(x)
#define ordered_set tree<pair<long double, int> , null_type,less<pair<long double, int> >, rb_tree_tag,tree_order_statistics_node_update>

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

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

const int inf = int(1e9);
const ll INF = ll(1e16);
const ll mod = 1e9 + 181;
const double PI = acos(-1.0);

ll bp(ll a, ll n, ll cm){
  ll r = 1;
  while(n){
    if(n & 1) r = r * a % cm;
    a = a * a % cm;
    n >>= 1;
  }
  return r;
}
template<typename T_vector> // neal
void output_vector(const T_vector &v, bool add_one = false, int start = -1, int end = -1) {
    if (start < 0) start = 0;
    if (end < 0) end = int(v.size());

    for (int i = start; i < end; i++)
        cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n');
}

int ask(int x, int y, string str){
  cout << str << " " << x << ' ' << y << endl;
  int ans;
  cin >> ans;
  return ans;
}
void solve(){
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  cout << a * d - b * c;
}

int main()
{
  // freopen("input.txt", "r", stdin)
  // freopen("output.txt", "w", stdout);
    Red;
    int T = 1;
    // cin >> T;
    for(int i = 1; i <= T; ++i){
      solve();
    }
    return 0;

}

#include<iostream>
using namespace std;

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

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

	cout<<d*a-c*b;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    long long total = 0;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= k; ++j) {
            total += i * 100 + j;
        }
    }
    cout << total << endl;

    return 0;
}

//Har Har Mahadev
using namespace std;
#include <bits/stdc++.h>
#define booga cerr << "booga" << endl
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define safai(...) Clearing_out(__VA_ARGS__)

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

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

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

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

string to_string(char c) {
	string s;
	s += c;
	return s;
}

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

string to_string(bool b) {
	return (b ? "1" : "0");
}

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

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

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

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

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

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

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	cerr << " " << to_string(H);
	debug_out(T...);
}

void Clearing_out() { return; }

template <typename Head, typename... Tail>
void Clearing_out(Head &H, Tail & ... T) {
	H.clear();
	Clearing_out(T...);
}

void testcase(){
	int h, w;
	cin >>h >> w;
	int g[h][w];
	int mini = 101;
	int total = 0;
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			cin >> g[i][j];
			total += g[i][j];
			mini = min(mini, g[i][j]);
		}
	}
	cout << total - mini*h*w << '\n';
}

int32_t main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int tt = 1;
	//cin >> tt;
	while(tt--){
		testcase();
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
    vector<string> s(3);
    for (int i = 0; i < 3; ++i) {
        cin>>s[i];
    }
    map<char,int> mp;
    int cnt=0;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < s[i].size(); ++j) {
            char c=s[i][j];
            if(mp.find(c)==mp.end()) {
                mp[c]=cnt;
                ++cnt;
            }
            s[i][j]='0'+mp[c];
        }
    }
    if(cnt>=11) {
        cout<<"UNSOLVABLE"<<endl;
        return 0;
    }

    vector<int> perm(10);
    for (int i = 0; i < 10; ++i) {
        perm[i]=i;
    }
    vector<string> t(3,"");
    do{
        t.assign(3,"");
        for (int i = 0; i < 3; ++i) {
            for(auto c:s[i]) {
                t[i].push_back('0'+perm[c-'0']);
            }
        }
        if(t[0][0]=='0'||t[1][0]=='0'||t[2][0]=='0')continue;
        ll a=stoll(t[0]);
        ll b=stoll(t[1]);
        ll c=stoll(t[2]);
        if(a+b==c) {
            cout<<a<<endl;
            cout<<b<<endl;
            cout<<c<<endl;
            return 0;
        }
    }while(next_permutation(perm.begin(),perm.end()));
    cout<<"UNSOLVABLE"<<endl;
    return 0;
}

/**
 ____ ____ ____ ____ ____
||a |||t |||o |||d |||o ||
||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|

**/

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector <vector <bool>> edge (n, vector <bool> (n));
    for(int u = 0; u < n; u++)
    {
        string row;
        cin >> row;
        for(int v = 0; v < n; v++)
            edge[u][v] = (row[v] == '1');
        edge[u][u] = true;
    }

    for(int w = 0; w < n; w++)
        for(int u = 0; u < n; u++)
            for(int v = 0; v < n; v++)
                if(edge[u][w] && edge[w][v])
                    edge[u][v] = true;

    long double answer = 0;
    for(int u = 0; u < n; u++)
    {
        int cnt = 0;
        for(int v = 0; v < n; v++)
            cnt += edge[v][u];

        answer += (long double) 1 / cnt;
    }
    cout << fixed << setprecision(10) << answer << "\n";
    return 0;
}


/******************************
Author: jhnah917(Justice_Hui)
g++ -std=c++17 -DLOCAL
******************************/

#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define compress(v) sort(all(v)), v.erase(unique(all(v)), v.end())
#define IDX(v, x) (lower_bound(all(v), x) - v.begin())
using namespace std;

using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
using PII = pair<int, int>;
constexpr ll MOD = 1e9+7;

ll N, A[3030], S[3030][3030];
int D[3030][3030];
vector<int> G[3030][3030];

void Add(int &a, int b){
    a += b; if(a >= MOD) a -= MOD;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    cin >> N;
    for(int i=1; i<=N; i++) cin >> A[i];
    for(int i=1; i<=N; i++){
        partial_sum(A+1, A+N+1, S[i]+1);
        for(int j=1; j<=N; j++) S[i][j] %= i;
        for(int j=1; j<=N; j++) G[i][S[i][j]].push_back(j);
    }
    for(int i=1; i<=N; i++) D[1][i] = 1;
    for(int i=1; i<=N; i++){
        for(int j=1; j<=N; j++){
            if(!D[i][j]) continue;
            auto &vec = G[i+1][S[i+1][j]];
            for(auto it=upper_bound(all(vec), j); it!=vec.end(); ++it) Add(D[i+1][*it], D[i][j]);
        }
    }
    ll ans = 0;
    for(int i=1; i<=N; i++) ans += D[i][N];
    cout << ans % MOD;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define w(x)    int x; cin>>x; while(x--)
#define pi (3.141592653589)
#define mod 1000000007
#define int long long
#define float double
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define mii             map<int,int>
#define msi          map<string,int>
#define vi               vector<int>
#define vs            vector<string>
#define si                  set<int>
#define all(c) c.begin(), c.end()
#define min3(a, b, c) min(c, min(a, b))
#define min4(a, b, c, d) min(d, min(c, min(a, b)))
#define rrep(i, n) for(int i=n-1;i>=0;i--)
#define rep(i,n) for(int i=0;i<n;i++)
#define print(x)        for(auto it=x.begin();it!=x.end();it++) cout<<*it<<' '; cout<<endl;
#define printii(x)      for(auto it=x.begin();it!=x.end();it++) cout<<it->F<<' '<<it->S<<endl;
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define lamba INT_MAX
#define chota INT_MIN
#define yes "YES"
#define no "NO"
#define MX 200010
//vector < int > graph[MX];
//bool vis[MX];
//vector<pair<int,int>>Graph[MX];

long double a[2002][3];

void solve()
{
  int n,i,j,cnt=0,t;
  cin>>n;
  double delta =0.1;
  for(i=0;i<n;i++)
  {
      cin>>t;
      if(t==1)
      cin>>a[i][0]>>a[i][1];
      else if(t==2)
      {
          cin>>a[i][0]>>a[i][1];
          a[i][1]-=delta;
      }
      else if(t==3)
      {
          cin>>a[i][0]>>a[i][1];
          a[i][0]+=delta;
      }
      else if(t==4)
      {
          cin>>a[i][0]>>a[i][1];
          a[i][0]+=delta;
          a[i][1]-=delta;
      }
  }
  for(i=0;i<n;i++)
  {
      double l,r;
      l=a[i][0],r=a[i][1];
      for(j=i+1;j<n;j++)
      {
          double L,R;
          L=a[j][0];
          R=a[j][1];
          cnt += (max(l,L) <= min(r,R));
        //   if(L>=l&&R<=r||L<=l&&R>=r)
        //   {
        //       if(L>=l&&R<=r)
        //       {
        //           cnt+=ceil(R-L);
        //       }
        //       else if(L<=l&&R>=r)
        //       {
        //           cnt+=ceil(r-l);
        //       }
        //   }
        //   else if(L>=l&&r>=L||R>=l&&r>=R)
        //   {
        //       if(L>=l&&r>=L)
        //       cnt+=ceil(r-l);
        //       else if(R>=l&&r>=R)
        //       cnt+=ceil(R-l);
        //   }
      }
  }
  cout<<cnt<<endl;
  return ;
}

int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//precalc();
  //w(x)
    // {
solve();
 //}
return 0;
}


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

typedef long long ll;
const ll MOD = 1e9+7;
const ll MAXN = 5e6;

ll n, m, k;

ll fac[MAXN];
void init_fac() {
    fac[0] = 1;
    for (ll i = 1; i < MAXN; i++)
        fac[i] = fac[i-1] * i % MOD;
}

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

ll modinv(ll n) { return modpow(n, MOD-2); }

ll comb(ll n, ll r) {
    return fac[n] * modinv(fac[r] * fac[n - r] % MOD) % MOD;
}

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

    init_fac();
    cin >> n >> m >> k;
    if (n > m+k)
        cout << 0 << '\n';
    else
        cout << (MOD + comb(n+m, n) - comb(n+m, m+k+1)) % MOD << '\n';
    return 0;
}

//clear adj and visited vector declared globally after each test case
//check for long long overflow
//Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
//Incase of close mle change language to c++17 or c++14
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007ll //998244353ll
#define lld long double
#define mii map<int, int>
#define pii pair<int, int>
#define ll long long
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
typedef std::numeric_limits< double > dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set
const long long N=2000005, INF=2000000000000000000;
lld pi=3.1415926535897932;
int lcm(int a, int b)
{
    int g=__gcd(a, b);
    return a/g*b;
}
int power(int a, int b, int p)
    {
        if(a==0)
        return 0;
        int res=1;
        a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a)%p;
            b>>=1;
            a=(a*a)%p;
        }
        return res;
    }
int fact[N],inv[N];
void pre()
{
    fact[0]=1;
    inv[0]=1;
    for(int i=1;i<N;i++)
    fact[i]=(i*fact[i-1])%mod;
    for(int i=1;i<N;i++)
    inv[i]=power(fact[i], mod-2, mod);
}
int nCr(int n, int r, int p)
{
    if(r>n)
    return 0;
    if(n==r)
    return 1;
    if (r==0)
    return 1;
    return (((fact[n]*inv[r]) % p )*inv[n-r])%p;
}

int32_t main()
{
    IOS;
    pre();
    int n, m, k;
    cin>>n>>m>>k;
    int ans=0;
    rep(i,max(0ll, n-m),k+1)
    {
        ans+=(nCr(n+m, m+i, mod) - nCr(n+m, m+i+1, mod) + mod)%mod;
        ans%=mod;
    }
    cout<<ans;
}


#define _USE_MATH_DEFINES
#include <bits/stdc++.h>

using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); (i)++)
#define per(i, n) for (ll i = n - 1; i >= 0; (i)--)
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define ROF(i, a, b) for (ll i = (b) - 1; i >= (a); i--)
#define ALL(x) std::begin(x), std::end(x)
#define rALL(x) std::rbegin(x), std::rend(x)
#define fi first
#define se second
typedef long long ll;
typedef long double ld;

const ld eps = 1e-9;
inline ld cross(ld x1, ld y1, ld x2, ld y2) { return x1 * y2 - x2 * y1; } // (x1, y1)と(x2, y2)の外積, 1->2で反時計回りなら正
inline ld dot(ld x1, ld y1, ld x2, ld y2) { return x1 * x2 + y1 * y2; } // (x1, y1)と(x2, y2)の内積
inline ld norm2(ld x, ld y) { return x * x + y * y; } // ノルム二乗
inline ld cross(pair<ld, ld> a, pair<ld, ld> b) { return a.fi * b.se - b.fi * a.se; }
inline ld dot(pair<ld, ld> a, pair<ld, ld> b) { return a.fi * b.fi + a.se * b.se; }
inline ld norm2(pair<ld, ld> a) { return a.fi * a.fi + a.se * a.se; }
inline bool isSame(ld a, ld b) { if(std::abs(a-b) < eps) return true; else return false; }
inline bool comp(ld a, ld b) { if(a + eps < b) return true; else return false; }
inline bool compp(pair<ld, ld> a, pair<ld, ld> b) { if(a.first + eps < b.first || (isSame(a.first, b.first) && a.second + eps < b.second)) return true; else return false;}

int main(){
    //input, initialize
    int N; cin >> N;
    vector<ld> a(N), b(N), c(N), d(N);
    rep(i, N) cin >> a[i] >> b[i];
    rep(i, N) cin >> c[i] >> d[i];
    //solve
    if(N == 1) {cout << "Yes\n"; return 0;}
    pair<ld, ld> c1, c2;
    rep(i, N) {c1.fi += a[i]; c1.se += b[i]; c2.fi += c[i]; c2.se += d[i];}
    c1.fi /= N; c1.se /= N; c2.fi /= N; c2.se /= N;
    rep(i, N) {a[i] -= c1.fi; b[i] -= c1.se; c[i] -= c2.fi; d[i] -= c2.se;}

    if(isSame(a[0], 0.0l) && isSame(b[0], 0.0l)) {swap(a[0], a[1]); swap(b[0], b[1]);}
    vector<pair<ld, ld>> p1, p2;
    rep(i, N)
        p1.emplace_back(make_pair( cross(a[0],b[0],a[i],b[i]), dot(a[0],b[0],a[i],b[i]) ));
    sort(ALL(p1), compp);

    rep(i, N){
        p2 = {};
        if(isSame(c[i], 0.0) && isSame(d[i], 0.0)) continue;
        rep(j, N)
            p2.emplace_back(make_pair( cross(c[i],d[i],c[j],d[j]), dot(c[i],d[i],c[j],d[j]) ));
        sort(ALL(p2), compp);

        bool isok = true;
        rep(i, N){
            if( !isSame(p1[i].fi, p2[i].fi) || !isSame(p1[i].se, p2[i].se) ){
                isok = false;
                break;
            }
        }
        if(isok) {cout << "Yes\n"; return 0;}
    }
    //output
    cout << "No\n";
    //debug
}

#include<bits/stdc++.h>
using namespace std;
#define N 2005
#define db double
const db eps=1e-3,pi=3.14159265358979323846264337328451;
int n,t,j,i,cnt,tot,a[N],b[N],ans,c[N],d[N];
db alp,ang,dis;
struct dot{db x,y;}dt[N],R[N];
inline bool eq(db aa,db bb){
	return aa<bb+eps&&bb<aa+eps;
}
inline bool cmp(dot aa,dot bb){
	if(eq(aa.x,bb.x))return aa.y<bb.y;
	return aa.x<bb.x;
}
bool check(db alp){
	int i;
	for(i=1;i<=n;++i){
		dis=sqrt((a[i]*a[i]+b[i]*b[i]));
		ang=atan2(b[i],a[i])+alp;
		dt[i].x=cos(ang)*dis;
		dt[i].y=sin(ang)*dis;
	}
	sort(dt+1,dt+n+1,cmp);
	db x=dt[1].x-R[1].x,y=dt[1].y-R[1].y;
	for(i=2;i<=n;++i){
		if(eq(dt[i].x-R[i].x,x)&&eq(dt[i].y-R[i].y,y))continue;
		else return 0;
	}
	return 1;
}
int main(){
	cin>>n;
	for(i=1;i<=n;++i){
		cin>>a[i]>>b[i];
	}
	for(i=1;i<=n;++i){
		cin>>c[i]>>d[i];
		R[i].x=c[i];R[i].y=d[i];
	}
	sort(R+1,R+n+1,cmp);
	for(i=-40;i<=40;++i){
		for(j=-40;j<=40;++j){
			if(check(atan2(j,i))){
				return cout<<"Yes",0;
			}
		}
	}
	cout<<"No";
}


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

const int N = 2e5+5;
int n,w;
long long s[N];

int main() {
    cin >> n >> w;
    for(int i = 1; i <= n; ++i) {
        int l,r,p;
        cin >> l >> r >> p;
        s[l] += p;
        s[r] -= p;
    }
    int flag = 1;
    for(int i = 0; i < N; ++i) {
        s[i] += s[i-1];
        if(s[i] > w) {
            flag = 0;
            break;
        }
    }
    if(flag)
        cout << "Yes\n";
    else
        cout << "No\n";
    return 0;
}

#include<bits/stdc++.h>
#define lld long long int
#define ll long double
#define vlld vector <lld>
#define vll vector <ll>
#define vvlld vector <vlld>
#define vvll vector <vll>
#define plld pair <lld,lld>
#define pll pair <ll,ll>
#define vplld vector <plld>
#define vpll vector <pll>
#define F first
#define S second
#define mod 1000000007
#define modd 998244353
#define all(v) v.begin(),v.end()
#define rep(i,a,b)    for(lld i=a;i<b;i++)
#define repr(i,a,b) for(lld i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
    ios;
    lld n,w;
    cin>>n>>w;

    vlld s(n),t(n),p(n);

    lld mx=2*pow(10,5)+5;
    vlld v(mx,0);

    rep(i,0,n){
        cin>>s[i]>>t[i]>>p[i];
        v[s[i]]+=p[i];
        v[t[i]]-=p[i];
    }

    rep(i,1,v.size())v[i]+=v[i-1];

    lld mmax=*max_element(all(v));

    if(mmax>w)cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
}


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

#define rep(i,n) for(int i = 0; i < n; i++)
#define Rep(i,n) for(int i = 1; i <= n; i++)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define debug(a) { cerr << #a << ": " << a << endl; }
#define endl '\n'
#define fi first
#define se second
using lint = long long;
using P = pair<int,int>;
template<class T> using V = vector<T>;
template<class T> using priq     = priority_queue<T>;
template<class T> using priq_inv = priority_queue<T, vector<T>, greater<T>>;
template<class T> T ceil(const T &a, const T &b) { return (a+b-1)/b; }
template<class T> bool chmin(T &a, T b) { if(a > b) { a = b; return 1; } return 0; }
template<class T> bool chmax(T &a, T b) { if(a < b) { a = b; return 1; } return 0; }
struct INF { template<class T> operator T() { return numeric_limits<T>::max() / 2; } } INF;

template<class T, class U> istream& operator>>(istream &in, pair<T,U> &p) {
    return in >> p.first >> p.second;
}
template<class T, class U> ostream& operator<<(ostream &out, const pair<T,U> &p) {
    return out << p.first << ' ' << p.second;
}
template<class T> istream& operator>>(istream &in, vector<T> &v) {
    for(auto &&e: v) in >> e;
    return in;
}
template<class T> ostream& operator<<(ostream &out, const vector<T> &v) {
    for(const auto &e: v) out << e << ' ';
    return out;
}

/*----------------------------------------------------------------------------------------------------*/

const int c = 50;

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

int si, sj;
V<V<int>> t(c,V<int>(c)), p(c,V<int>(c));

void write_to_text(string ans) {
    ofstream out("AHC002_out.txt");
    out << ans << endl;
    out.close();
}

void print(string ans) {
    cout << ans << endl;
}

string solve() {

    V<bool> seen(c*c);
    string ans = "";

    int y = si, x = sj;
    seen[t[y][x]] = true;

    while(true) {
        P mx = P(-1,-1);
        rep(di,4) {
            int ny = y + dy[di];
            int nx = x + dx[di];
            if(ny < 0 || nx < 0 || ny >= c || nx >= c) continue;
            if(seen[t[ny][nx]]) continue;
            int score;
            if(di == 0) score = (c-ny) * (c-si); // D
            if(di == 1) score = (c-nx) * (c-sj); // R
            if(di == 2) score = ny * si; // U
            if(di == 3) score = nx * sj; // L
            chmax(mx, make_pair(score, di));
            // chmax(mx, make_pair(p[ny][nx], di));
        }
        if(mx == P(-1,-1)) {
            return ans;
        }
        else {
            y += dy[mx.se];
            x += dx[mx.se];
            seen[t[y][x]] = true;
            ans += string(1,mx.se+'0');
        }
    }
}

int calc(string s) {
    int y = si, x = sj;
    int res = 0;
    rep(i,sz(s)) {
        res += p[y][x];
        y += dy[s[i]-'0'];
        x += dx[s[i]-'0'];
    }
    res += p[y][x];
    return res;
}

clock_t start;

pair<int,string> dfs(string s, int y, int x, V<bool> &seen) {
    if(clock() - start > 1980000) return pair<int,string>();
    pair<int,string> res;
    for(int d = 3; d >= 0; d--) {
        int ny = y + dy[d];
        int nx = x + dx[d];
        if(ny < 0 || nx < 0 || ny >= c || nx >= c) continue;
        if(seen[t[ny][nx]]) continue;
        seen[t[ny][nx]] = true;
        chmax(res, dfs(s+string(1,d+'0'), ny, nx, seen));
    }
    if(res.fi == 0) res = make_pair(calc(s),s);
    seen[t[y][x]] = false;
    return res;
}

int main() {
    start = clock();

    cin >> si >> sj;
    cin >> t >> p;
    // string ans = solve();
    V<bool> seen(c*c);
    seen[t[si][sj]] = true;
    auto ans = dfs("",si,sj,seen);
    rep(i,sz(ans.se)) ans.se[i] = dir[ans.se[i]-'0'];
    cout << ans.se << endl;
}


// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx")
// #pragma comment(linker, "/stack:200000000"]

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <bitset>
#include <stack>
#include <random>
#include <fstream>
#include <sstream>
#include <chrono>

#define fi first
#define se second
#define pb push_back
#define ll long long
#define ld long double
#define hm unordered_map
#define pii pair<int, int>
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define cinv(v) for (auto& x: v) cin >> x
#define fr(i, n) for (int i = 0; i < n; ++i)
#define fl(i, l, n) for (int i = l; i < n; ++i)

#define int ll

template <typename T1, typename T2> inline bool chkmin(T1 &x, const T2 &y) {if (x > y) {x = y; return 1;} return 0;}
template <typename T1, typename T2> inline bool chkmax(T1 &x, const T2 &y) {if (x < y) {x = y; return 1;} return 0;}

using namespace std;

#ifdef __LOCAL
	#define dbg(x) cerr << #x << " : " << x << '\n'
	const int maxn = 20;
#else
	#define dbg(x)
	const int maxn = 2e5 + 20;
#endif

//tg: @galebickosikasa

ostream& operator << (ostream& out, vector<int>& v) {
	for (auto& x: v) out << x << ' ';
	return out;
}

ostream& operator << (ostream& out, pii& v) {
	out << v.fi << ", " << v.se;
	return out;
}

istream& operator >> (istream& in, pii& a) {
	in >> a.fi >> a.se;
	return in;
}

const ll inf = (ll) 2e9;
const ld pi = asin (1) * 2;
const ld eps = 1e-8;
const ll mod = (ll)1e9 + 7;
const ll ns = 97;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

signed main () {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n;
	cin >> n;
	vector<int> goo (n);
	cinv (goo);
	int g = 0;
	for (auto& x: goo) g = __gcd (g, x);
	cout << g << '\n';

}

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

using namespace std;
int printNcR(int n, int r)
{

	// p holds the value of n*(n-1)*(n-2)...,
	// k holds the value of r*(r-1)...
	long long p = 1, k = 1;

	// C(n, r) == C(n, n-r),
	// choosing the smaller value
	if (n - r < r)
		r = n - r;

	if (r != 0) {
		while (r) {
			p *= n;
			k *= r;

			// gcd of p, k
			long long m = __gcd(p, k);

			// dividing by gcd, to simplify
			// product division by their gcd
			// saves from the overflow
			p /= m;
			k /= m;

			n--;
			r--;
		}

		// k should be simplified to 1
		// as C(n, r) is a natural number
		// (denominator should be 1 ) .
	}

	else
		p = 1;

	// if our approach is correct p = ans and k =1
	return p;
}

void solve(int t) {
	int n;
	cin >> n;
	cout << printNcR(n - 1, 11 );

}

int32_t main() {
	FIO
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif

	// w(t){
	solve(1);
	// }
	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<ll,ll>;
    const int mod=998244353;
    const int INF=1001001001;

    struct edge{
      int to;
      ll w;
      edge(int to,ll w):to(to),w(w){}
    };
    using Graph=vector<vector<edge>>;

    int N,M;

    void dijkstra(int s,Graph G,vector<ll>& dist){
      dist.resize(N,INF);
      dist[s]=0;
      priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>>q;
      q.push({dist[s],s});
      while(!q.empty()){
        int v=q.top().second;
        ll d=q.top().first;
        q.pop();
        if(d>dist[v]){continue;}
        for(auto e:G[v]){
          if(chmin(dist[e.to],dist[v]+e.w)){
            q.push({dist[e.to],e.to});
          }
        }
      }
    }

    int main() {
      cin>>N>>M;
      Graph G(N);
      rep(i,M){
        ll A,B;
        cin>>A>>B;
        A--;B--;
        G[A].push_back(edge(B,1));
        G[B].push_back(edge(A,1));
      }
      int K;
      cin>>K;
      vector<int>C(K);
      rep(i,K){cin>>C[i];C[i]--;}

      vector<vector<int>>dist(K);
      rep(i,K){
        vector<ll>d(N,INF);
        dijkstra(C[i],G,d);
        rep(j,K){dist[i].push_back(d[C[j]]);}
      }
      vector<vector<ll>>dp(1<<K,vector<ll>(K,INF));
      rep(i,K){dp[1<<i][i]=0;}
      rep(s,1<<K){
        rep(i,K){
          if((s>>i)&1){
            ll res=s^(1<<i);
            rep(j,K){
              if(res&(1<<j)){chmin(dp[s][i],dp[res][j]+dist[i][j]);}
            }
          }
        }
      }
      ll ans=*min_element(dp.back().begin(),dp.back().end());
      cout<<(ans<INF?ans+1:-1)<<endl;
      return 0;
    }

#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 ordered_set tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>
*/
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define rep(i,a,b) for(ll i=a;i<=b;++i)
#define rrep(i,a,b) for(ll i=a;i>=b;--i)
#define FOR(i,n)  for(ll i=0;i<n;i++)
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979323846
#define fi first
#define se second
#define all(x)  x.begin(),x.end()
#define IOS ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

const ll INF = 1e18+7;
const ll mod = 1e9+7;
const ll MAXN = 1e7+10;

ll poww(ll a, ll b) {
    if(b<0) return 0LL;
    ll ans = 1;
    while (b) {
        if (b & 1)ans = ans * a;
        a = a * a;
        b >>= 1;
    }
    return ans;
}
ll binpow(ll a, ll b, ll m)
{
    if (b < 0) return 0LL;
    if (a <= 0)return 0LL;
    a %= m;

    ll ans = 1LL;
    while (b) {
        if (b & 1)ans = ans * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return ans;
}
ll modinv(ll n) {
    return binpow(n, mod - 2, mod);
}

vector<vector<ll>> adj;
vector<ll> res,ans;

void dfs(ll u, ll p, ll c[]) {
    if(res[c[u]]==0) ans.pb(u);
    res[c[u]]++;
    for(ll v : adj[u]) {
        if(v!=p) {
            dfs(v,u,c);
        }
    }
    res[c[u]]--;
}

void solve() {
    ll n; cin>>n;
    adj.resize(n+1);
    ll c[n+1]; FOR(i,n) cin>>c[i+1];
    res.assign(1e5+10,0);
    FOR(i,n-1) {
        ll x,y; cin>>x>>y;
        adj[x].pb(y); adj[y].pb(x);
    }
    dfs(1LL,-1,c);
    sort(all(ans));
    for(ll v : ans) cout<<v<<"\n";
}

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

    //ll no_of_test_cases; cin>>no_of_test_cases;
    ll no_of_test_cases = 1;
    for(ll i=1; i<=no_of_test_cases; i++) {
        //cout<<"Case #"<<i<<": ";
        solve();
    }
    return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<bitset>
#include<unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fio ios_base::sync_with_stdio(false)
#define li long long int
#define ll long long int
//#define readi(x) scanf("%lld",&x)
#define  reads(x)  scanf("%s", x)
#define readl(x) scanf("%lld",&x)
#define rep(i,n) for(i=0;i<n;i++)
#define revp(i,n) for(i=(n-1);i>=0;i--)
#define myrep1(i,a,b) for(i=a;i<=b;i++)
#define myrep2(i,a,b) for(i=b;i>=a;i--)
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define MAXN 100000000000000005
#define MINN -10000000000000000
#define INTMAX 1000001000
#define pii pair

#define pdd pair<double,double>
#define pic pair<int,char>
//#define N 5005
#define lgn 20
#define ddouble long double
#define minus minu
#define PI 3.1415926535
#define lgn 20
using namespace std;
//li bit[300004];
li mod=1000000007;
li mod2=998244353;

li pp(li a,li b,li modd){
    if(b==0) return(1);
    li ans=pp(a,b/2,modd);ans=(ans*ans)%modd;if(b%2) ans=(ans*a)%modd;return(ans);
}

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

vector
 z_function(string s){
     li n=s.size();
     vector
z(n);
     for(li i=1,l=0,r=0;i<n;i++){
         if(i<=r){z[i]=min(r-i+1,z[i-l]);}
         while((i+z[i])<n && s[z[i]]==s[i+z[i]]){++z[i];}
         if((i+z[i]-1)>r){l=i;r=i+z[i]-1;}
     }
     return(z);
}
/*vector
p(2005,0);
li parent(li a){
    if(p[a]==0 || p[a]==a){p[a]=a;return(a);}
    return(p[a]=parent(p[a]));
}

void unionn(li a,li b){p[a]=b;}*/

/*vector<pii>MM;li N;*/

li ff(li pos,li bit,vector<vector<pii>>& v,vector
& P,vector
& dp,li& n,vector
& C)
{
    if(pos>=n)
    {
        return(1);
    }
    if(dp[bit]!=-1) return(dp[bit]);
    li i,j,ans=0;
    for(i=0;i<n;i++)
    {
        if((bit>>i)&1){}
        else
        {
            for(j=i;j<n;j++)
            {
                P[j]++;
            }
            for(j=0;j<C[pos];j++)
            {
                if(P[v[pos][j].fi]>v[pos][j].sec)
                {
                    break;
                }
            }
            if(j>=C[pos])
            {
                ans=ans+ff(pos+1,bit|(1<<i),v,P,dp,n,C);
            }
            for(j=i;j<n;j++)
            {
                P[j]--;
            }
        }
    }
    dp[bit]=ans;
    return(ans);
}

int main(){
li i,j,a,b,c,d,g,h,k,m,n,q,s,t,x,y,z,X,Y,sz,st,end,mid,ans,W,pa,pb,l,r,p;
    string T,SN,S,TN;
    //char S1[100004];
    t=1;
    //readl(t);
    n=100005;
    /*vector
v(n+2,0);
    for(i=2;i<=n;i++)
    {
        if(v[i]==0)
        {
            for(j=i;j<=n;j+=i)
            {
                v[j]=i;
            }
            //cout<<v[i]<<" ";
        }
    }*/

    x=0;
    while(t--)
    {
        readl(n);readl(m);
        //vector
A(n);
        //vector
L(n,-1);
        vector<vector<pii>>v(n+1);
        vector
C(n+1,0);
        for(i=0;i<m;i++)
        {
            readl(a);readl(b);readl(c);
            b--;
            v[a-1].pb({b,c});
            C[a-1]++;
        }
        vector
P(n,0);
        vector
dp((1<<n)+2,-1);
        ans=ff(0,0,v,P,dp,n,C);
        cout<<ans<<endl;
       //x++;
       //cout<<"Case #"<<x<<": "<<
    }
}

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author tatsumack
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>

#define int long long
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define FOR(i, a, b) for (int i = (a), i##_len = (b); i <= i##_len; ++i)
#define REV(i, a, b) for (int i = (a); i >= (b); --i)
#define CLR(a, b) memset((a), (b), sizeof(a))
#define DUMP(x) cout << #x << " = " << (x) << endl;
#define INF 1001001001001001001ll
#define fcout cout << fixed << setprecision(12)

using namespace std;

typedef pair<int, int> P;

class ATwoSequences2 {
public:
    void solve(std::istream& cin, std::ostream& cout) {
        int N;
        cin >> N;
        vector<int> a(N), b(N);
        REP(i, N) cin >> a[i];
        REP(i, N) cin >> b[i];

        int maxa = 0;
        int cur = 0;
        REP(i, N) {
            maxa = max(maxa, a[i]);
            cur = max(cur, b[i] * maxa);
            cout << cur << endl;
        }

    }
};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    std::istream& in(std::cin);
    std::ostream& out(std::cout);

    ATwoSequences2 solver;
    solver.solve(in, out);
    return 0;
}


#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
#define int int64_t
#define ff first
#define ss second
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define IM LLONG_MAX
#define endl "\n"
#define all(v) v.begin(),v.end()
#define REP(i,a,n) for(int i=a;i<n;i++)
#define deb(x) cout<<"["#x<<":"<<x<<"]"<<"  "<<endl;
#define deb2(x,y) cout<<"["#x<<":"<<x<<" "#y<<":"<<y<<"]"<<endl;
#define out(a) for(auto x:a)cout<<x<<" "; cout<<endl;
#define outyes cout<<"Yes"<<endl;
#define outno cout<<"No"<<endl;
#define mod 1000000007
using namespace std;

void M416()
{
   int n,k;
   cin>>n>>k;
   int c=0;
   for(int i=1;i<=n;i++){
    for(int j=1;j<=k;j++){
        int x=i*100;
       c+=x+j;
    }
   }
   cout<<c<<endl;

}

int32_t main()
{
    fastio;
    cout<<fixed;
    cout.precision(15);
    int __TESTCASES=1;
    // cin>>__TESTCASES;
    while(__TESTCASES--){
        M416();
}
}

#include <vector>
#include <string>
#include <iostream>
#include <sstream>

int main() {
  std::string s, foo, buffer;
  std::getline(std::cin, s);

  std::vector<int> hoge;
  std::stringstream ss{s};
  while( std::getline(ss, buffer, ' ') ) {
    hoge.push_back(std::stoi(buffer));
  }

  int n = 0;
  int k = 0;
  for(int i = 0; i <= hoge[0]; i++) {
    n += i*hoge[1];
  }
  for(int i = 0; i <= hoge[1]; i++) {
    k += i;
  }

  std::cout << (n*100) + k*hoge[0] << std::endl;

  return 0;
}


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

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

  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

int main() {
	int t;
	cin >> t;
	rep(i,t) {
		int n;
		int a,b;
		cin >> n >> a >> b;
		mint x1,x2,x3,x4;
		if (n-a-b >= 0) x4 = mint(n-a-b+2)*mint(n-a-b+1)/mint(2);
		else x4=0;
		x3 = mint(2)*x4;
		x2 = mint(n-a+1)*mint(n-b+1)-x3;
		x1 = x2*x2;
		mint ans;
		ans = mint(n-a+1)*mint(n-a+1)*mint(n-b+1)*mint(n-b+1)-x1;
		cout << ans << endl;
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define watch(x) cout << (#x) << " is " << (x) << endl
#define all(x) x.begin(), x.end()
#define printVec(vec) {for(auto &x : vec) cout << x <<" "; cout << endl; }
#define printMap(mp) {for(auto &p : mp) cout << p.first <<" " <<p.second <<endl;}
#define MOD 1000000007
#define hM 9999999900000001
#define inf 1e17
// const int maxN = 1;
// int BIT[maxN];
// void add(int x){while(x<N)BIT[x]++,x+=x&-x;}
// int sum(int x){return x?BIT[x]+sum(x-(x&-x)):0;}

void solve(int T) {
  int n;
  cin >> n;
  vector<vector<double>> arr(n);
  double corr =  0.01;
  for(int i = 0 ; i < n ; i++){
    double t, x, y;
    cin >> t >> x >> y;
    if(t == 2){
      y-=corr;
    }else if(t == 3){
      x+=corr;
    }else if(t == 4){
      x+=corr, y-=corr;
    }
    arr[i] = {x, y};
  }
  int cnt = 0;
  for(int i = 0 ; i < n ; i++){
    double myS = arr[i][0];
    double myE = arr[i][1];
    if(myE < myS)continue;
    for(int j = i+1; j < n ; j++){
      if(arr[j][0] > arr[j][1])continue;
      if(myS >= arr[j][0] && myS <= arr[j][1])
        cnt++;
      else if(myE >= arr[j][0] && myE <= arr[j][1])
        cnt++;
      else if(myS <= arr[j][0] && myE >= arr[j][1])
        cnt++;

    }
  }
  cout << cnt <<endl;
}

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

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

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

    cerr << "time taken : " << (float) clock() / CLOCKS_PER_SEC << " secs" << endl;
    return 0;
}


//Author : RAJEEV KUMAR
#include <bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef long long ll;
typedef long double lld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define sz(x) ((long long) x.size())
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define bitcount(x)  __builtin_popcount(x)
#define bitcountll(x)  __builtin_popcountll(x)
#define has_bit(bit_mask, x) ((bit_mask) & (1Uint << (x)))
#define turn_on_bit(bit_mask, x) (bit_mask |= (1Uint << (x)))
#define turn_off_bit(bit_mask, x) (bit_mask &= (~(1Uint << (x))))
#define smallest_on_bit(bit_mask) (__builtin_ctzint((bit_mask) & (-(bit_mask))))
//DBG
template<typename A> void dbg(vector<A> arr) {for(auto x:arr){cout<<x<<' ';}cout<<endl;}
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v);
template<typename A, typename B> ostream& operator<<(ostream &cout, pair<A, B> const &p) { return cout << '(' << p.f << ',' << p.s << ')'; }
template<typename A> ostream& operator<<(ostream &cout, vector<A> const &v) {
cout << '['; for(int i = 0; i < v.size(); i++) {if (i) cout << ','; cout << v[i];} return cout << ']';}
template<typename A, typename B> istream& operator>>(istream& cin, pair<A, B> &p) {cin >> p.first;return cin >> p.second;}
// Constant
const ll INF = 1e18;
const ll mod = 1e9+7;
//const ll mod = 1000000009;
//const ll mod = 998244353;
//Clock
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define SC auto start = chrono::high_resolution_clock::now();
#define TC auto end = chrono::high_resolution_clock::now() ;double time_taken = (chrono::duration_cast<chrono::nanoseconds>(end - start).count())*1e-9;
#define DT cout << fixed << time_taken << setprecision(9)<< endl;
//----------------------------------------------------------------------------//

//What to do in each testcase.
void solve(){
    int a[3] ={0};
    cin>>a[0]>>a[1]>>a[2];

    sort(a,a+3);

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

int32_t main() {
    fastio;
    //SC
    //int t; cin>>t; while (t--)solve();
    solve();
    //TC
    //DT
    return 0;
}

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

void solve()
{
	Int n, x;
	cin >> n >> x;
	vector<Int> a(n), b(n);
	REP(i, n) cin >> a[i];
	REP(i, n - 1) b[i] = (x%a[i + 1]) / a[i];

	Int dp[51][2] = { 0 };
	dp[0][0] = 1;
	REP(i, n) {
		dp[i + 1][0] += dp[i][0];
		if(b[i] == 0) dp[i + 1][1] += dp[i][1];

		if (b[i]) dp[i + 1][1] += dp[i][1] + dp[i][0];

		if (i == n - 1 || b[i] != a[i + 1] / a[i] - 1) dp[i + 1][0] += dp[i][1];
	}
	cout << dp[n][0] << endl;
}

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


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;
//constexpr ll MOD = 998244353;
//constexpr ll MOD = ;
ll mod(ll A, ll M) {return (A % M + M) % M;}
constexpr ll INF = 1LL << 60;
template<class T> bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
ll divceil(ll A, ll B) {return (A + (B - 1)) / B;}
#define FINALANS(A) do {cout << (A) << '\n'; exit(0);} while (false)

ll zarts(vector<ll> &V, vector<ll> &W)
{
  ll NN = V.size();

  vector<pll> Vi(NN);
  for (ll i = 0; i < NN; i++)
  {
    Vi.at(i) = make_pair(V.at(i), i);
  }
  sort(Vi.begin(), Vi.end());

  vector<pll> iW(NN);
  ll cnt = 1;
  for (ll i = 0; i < NN; i++)
  {
    iW.at(i) = make_pair(Vi.at(i).second, cnt);

    if (i != NN - 1 && Vi.at(i).first != Vi.at(i + 1).first)
      cnt++;
  }
  sort(iW.begin(), iW.end());

  for (ll i = 0; i < NN; i++)
  {
    W.at(i) = iW.at(i).second;
  }

  return cnt;
}

class BinaryIndexedTree
{
public:
  ll N;
  vector<ll> bit;

  BinaryIndexedTree(ll n)
  {
    N = n;
    bit = vector<ll>(N + 1, 0); // 1 ~ N なので注意
  }

  ll _sum(ll i)
  {
    ll s = 0;
    while (i > 0)
    {
      s += bit.at(i);
      i -= i & -i;
    }
    return s;
  }

  ll sum(ll L, ll R) // [L, R) の総和
  {
    return _sum(R - 1) - _sum(L - 1);
  }

  ll get(ll i)
  {
    return sum(i, i + 1);
  }

  void add(ll i, ll x)
  {
    while (i <= N)
    {
      bit.at(i) += x;
      i += i & -i;
    }
  }
};

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

  vector<ll> T(Q), X(Q), Y(Q);
  for (ll i = 0; i < Q; i++)
  {
    cin >> T.at(i) >> X.at(i) >> Y.at(i);
    X.at(i)--;
  }

  vector<ll> Z(Q);
  ll K = zarts(Y, Z);
  map<ll, ll> YtoZ;
  vector<ll> ZtoY(K + 1);
  YtoZ[0] = 0;
  ZtoY.at(0) = 0;
  for (ll i = 0; i < Q; i++)
  {
    YtoZ[Y.at(i)] = Z.at(i);
    ZtoY.at(Z.at(i)) = Y.at(i);
  }

  vector<ll> A(N, 0), B(M, 0);
  BinaryIndexedTree C(K + 2), D(K + 2), E(K + 2), F(K + 2);
  C.add(1, N);
  D.add(1, M);

  ll ans = 0;
  for (ll q = 0; q < Q; q++)
  {
    ll t = T.at(q), x = X.at(q), y = Y.at(q);
    if (t == 1)
    {
      ll a = A.at(x);
      A.at(x) = y;

      ans -= a * D._sum(YtoZ[a] + 1);

      C.add(YtoZ[a] + 1, -1);
      C.add(YtoZ[y] + 1, 1);
      E.add(YtoZ[a] + 1, -a);
      E.add(YtoZ[y] + 1, y);

      ans += y * D._sum(YtoZ[y] + 1);

      //cerr << ans << endl;

      if (a < y)
        ans -= F.sum(YtoZ[a] + 2, YtoZ[y] + 2);
      else if (a > y)
        ans += F.sum(YtoZ[y] + 2, YtoZ[a] + 2);
    }
    else
    {
      ll a = B.at(x);
      B.at(x) = y;

      ans -= a * C._sum(YtoZ[a] + 0);

      D.add(YtoZ[a] + 1, -1);
      D.add(YtoZ[y] + 1, 1);
      F.add(YtoZ[a] + 1, -a);
      F.add(YtoZ[y] + 1, y);

      ans += y * C._sum(YtoZ[y] + 0);

      if (a < y)
        ans -= E.sum(YtoZ[a] + 1, YtoZ[y] + 1);
      else if (a > y)
        ans += E.sum(YtoZ[y] + 1, YtoZ[a] + 1);
    }

    cout << ans << endl;

    /*
    for (ll i = 0; i < K + 1; i++)
    {
      cerr << C.get(i + 1) << " ";
    }
    cerr << endl;
    for (ll i = 0; i < K + 1; i++)
    {
      cerr << D.get(i + 1) << " ";
    }
    cerr << endl;
    for (ll i = 0; i < K + 1; i++)
    {
      cerr << E.get(i + 1) << " ";
    }
    cerr << endl;
    for (ll i = 0; i < K + 1; i++)
    {
      cerr << F.get(i + 1) << " ";
    }
    cerr << endl
         << endl;
    //*/
  }
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair <int, int> pin;

const int N = 2e5 + 5;
const ll P = 998244353LL;
const int inf = 1 << 30;

int n, k, tot, cnt, head[N], f[N];

struct Edge {
    int to, nxt;
} e[N << 1];

inline void add(int from, int to) {
    e[++tot].to = to;
    e[tot].nxt = head[from];
    head[from] = tot;
}

template <typename T>
inline void read(T &X) {
    char ch = 0; T op = 1;
    for (X = 0; ch > '9' || ch < '0'; ch = getchar())
        if (ch == '-') op = -1;
    for (; ch >= '0' && ch <= '9'; ch = getchar())
        X = (X * 10) + ch - '0';
    X *= op;
}

void dfs(int x, int fat, int mid) {
    int mx = -inf, mn = inf;
    for (int i = head[x]; i; i = e[i].nxt) {
        int y = e[i].to;
        if (y == fat) continue;
        dfs(y, x, mid);
        mx = max(mx, f[y]);
        mn = min(mn, f[y]);
    }
    if (mx == -inf) mx = 0;
    ++mn;
    // if (mn <= 0) {
    //     if (mn + mx > 0) {
    //         ++cnt;
    //         f[x] = -mid;
    //     } else f[x] = mn;
    // } else {
    //     if (mx + 1 > mid) {
    //         ++cnt;
    //         f[x] = -mid;
    //     } else f[x] = mx + 1;
    // }
    if (mx + 1 > mid) {
        ++cnt;
        f[x] = -mid;
    } else if (mn + mx <= 0) f[x] = mn;
    else f[x] = mx + 1;
}

inline bool chk(int mid) {
    cnt = 0;
    for (int i = 1; i <= n; i++) f[i] = 0;
    dfs(1, 0, mid);
    if (f[1] > 0) ++cnt;
    // printf("%d:\n", mid);
    // for (int i = 1; i <= n; i++) printf("%d%c", f[i], " \n"[i == n]);
    return cnt <= k;
}

int main() {
// #ifndef ONLINE_JUDGE
//     freopen("sample.in", "r", stdin);
//     clock_t st_clock = clock();
// #endif

    read(n), read(k);
    for (int x, y, i = 1; i < n; i++) {
        read(x), read(y);
        add(x, y), add(y, x);
    }
    int l = 1, r = n, mid, res = n;
    for (; l <= r; ) {
        mid = (l + r) / 2;
        if (chk(mid)) res = mid, r = mid - 1;
        else l = mid + 1;
    }
    printf("%d\n", res);

// #ifndef ONLINE_JUDGE
//     clock_t ed_clock = clock();
//     printf("time = %f ms\n", (double)(ed_clock - st_clock) / CLOCKS_PER_SEC * 1000);
// #endif
    return 0;
}


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

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

template<unsigned mod=998244353>struct fp {
   using uint=unsigned; uint v;
   static uint get_mod(){return mod;}
   int inv() const{
      int tmp,a=v,b=mod,x=1,y=0;
      while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
      if(x<0){x+=mod;} return x;
   }
   fp(ll x=0){init(x%mod+mod);}
   fp& init(uint x){v=(x<mod?x:x-mod); return *this;}
   fp operator-()const{return fp()-*this;}
   fp pow(ll t){fp res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;} return res;}
   fp& operator+=(const fp& x){return init(v+x.v);}
   fp& operator-=(const fp& x){return init(v+mod-x.v);}
   fp& operator*=(const fp& x){v=ll(v)*x.v%mod; return *this;}
   fp& operator/=(const fp& x){v=ll(v)*x.inv()%mod; return *this;}
   fp operator+(const fp& x)const{return fp(*this)+=x;}
   fp operator-(const fp& x)const{return fp(*this)-=x;}
   fp operator*(const fp& x)const{return fp(*this)*=x;}
   fp operator/(const fp& x)const{return fp(*this)/=x;}
   bool operator==(const fp& x)const{return v==x.v;}
   bool operator!=(const fp& x)const{return v!=x.v;}
   friend istream& operator>>(istream& is,fp& x){is>>x.v; return is;}
   friend ostream& operator<<(ostream& os,const fp& x){os<<x.v; return os;}
}; using Fp=fp<>;
template<typename T>struct factorial {
   vector<T> Fact,Finv,Inv;
   factorial(int maxx){
      Fact.resize(maxx); Finv.resize(maxx); Inv.resize(maxx);
      Fact[0]=Fact[1]=Finv[0]=Finv[1]=Inv[1]=1;
      rep(i,2,maxx){Fact[i]=Fact[i-1]*i;} Finv[maxx-1]=Fact[maxx-1].inv();
      for(int i=maxx-1;i>=2;i--){Finv[i-1]=Finv[i]*i; Inv[i]=Finv[i]*Fact[i-1];}
   }
   T fact(int n,bool inv=0){if(n<0)return 0; return (inv?Finv[n]:Fact[n]);}
   T inv(int n){if(n<0)return 0; return Inv[n];}
   T nPr(int n,int r,bool inv=0){if(n<0||n<r||r<0)return 0; return fact(n,inv)*fact(n-r,inv^1);}
   T nCr(int n,int r,bool inv=0){if(n<0||n<r||r<0)return 0; return fact(n,inv)*fact(r,inv^1)*fact(n-r,inv^1);}
};

Fp memo[3010][3010];
bitset<3010> used[3010];
Fp rec(int n,int k){
   if(n<k)return 0;
   if(used[n][k])return memo[n][k];
   used[n][k]=1;
   if(k==0){
      return memo[n][k]=(n==0);
   }
   return memo[n][k]=rec(n-1,k-1)+rec(n,k*2);
}

int main(){
   int n,k;
   cin>>n>>k;
   cout<<rec(n,k)<<'\n';
   return 0;
}

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <iomanip>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <functional>
#define rep(i,n) for (int i = 0; i < (n); ++i)

using namespace std;
const int MOD = 1000000007;

typedef long long int ll;
typedef pair<int,int> Pii;
typedef pair<ll,ll> Pll;

void floatOutPutSetup(){
    cout << fixed;
    cout << setprecision(10);
}

int main(){
    int a,b;
    cin>>a>>b;
    int m = 2*a+100;
    cout<<m-b<<endl;

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

//つねに10億7などの剰余をとる構造体
//参考: https://www.youtube.com/watch?v=L8grWxBlIZ4&t=9858
//参考: https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a#4-%E7%B4%AF%E4%B9%97-an
int mod  = MODA;
struct modint {
	ll x;
	modint (ll x):x(x%mod){}
	modint& operator+=(const modint a){
		(x += a.x ) %= mod;
		return *this;
	}
	modint& operator-=(const modint a){
		(x -= a.x ) %= mod;
		if (x < 0) x += mod;
		return *this;
	}
	modint& operator*=(const modint a){
		(x *= a.x ) %= mod;
		return *this;
	}
	modint& operator/=(modint a){ //除算のみO(log mod)なので注意。割る数が小さいならnCrのinvを使うこと
		ll exp = mod - 2;
		while(exp > 0){
			if(exp & 1) *this *= a.x;
			a *= a.x;
			exp >>= 1;
		}
		return *this;
	}
	modint operator+(const modint a) const{
		modint res(*this);
		return res+=a;
	}
	modint operator-(const modint a) const{
		modint res(*this);
		return res-=a;
	}
	modint operator*(const modint a) const{
		modint res(*this);
		return res*=a;
	}
	modint operator/(const modint a) const{
		modint res(*this);
		return res/=a;
	}
};
modint modpow(modint x, ll n){
	modint res=1;
	while(n > 0){
		if(n & 1) res = res * x.x;
		x = x * x;
		n >>= 1;
	}
	return res;
}

//さらにnCrのために、x!とその逆元を配列で持つ
const int NMAX = 3000000; // 10^7くらいまではいけそう
ll fact[NMAX], finv[NMAX], inv[NMAX];

void ncr_init(){ //はじめにinitすること
	fact[0] = fact[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for(int i = 2; i < NMAX; i++){
		fact[i] = fact[i-1] * i % mod;
		inv[i] = mod - inv[mod%i] * (mod / i) % mod;
		finv[i] = finv[i-1]* inv[i] % mod;
	}
}
ll ncr(int n, int r){
	if(n<r) return 0;
	if(n<0 || r<0) return 0;
	return fact[n] * (finv[n-r] * finv[r] % mod) % mod;
}
//

int main() {
	modint ans = 0;
	ll tmp = 0;
	int n; cin >> n;
	int m, k; cin >> m >> k;
	ncr_init();
	if(n - m <= k){
		ans = ncr(m+n, m);
		ans -= ncr(m+n, n-k-1);
	}
	else ans.x = 0;
	cout << ans.x << endl;
}

#include<bits/stdc++.h>

using namespace std;
#define int long long
// typedef long long int;

int const N = 4e6 + 10, mod = 1e9 + 7;
int fact[N], infact[N];
int n, m, k;

int qmi(int a, int k, int p) {
    int res = 1;
    while (k) {
        if (k & 1) res = res * a % p;
        k >>= 1;
        a = a * a % p;
    }
    return res;
}

// 预处理
void init() {
    fact[0] = infact[0] = 1;
    for (int i = 1; i < N; ++i) {
        fact[i] = (int)fact[i - 1] * i % mod;
        infact[i] = (int)infact[i - 1] * qmi(i, mod - 2, mod) % mod;
    }
}

int cnm(int a, int b) {
    return (int)fact[a] * infact[b] % mod * infact[a - b] % mod;
}

signed main() {
    init();  // 预处理
    cin >> n >> m >> k;  // c(n + m, n) - c(n + m, n - k - 1)
    if (n >= m + k + 1) cout << 0;
    else cout << ((cnm(n + m, n) % mod - cnm(n + m, m + k + 1) % mod) % mod + mod) % mod ;
    return 0;
}


#include <iostream>
#include <unordered_map>

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

  std::unordered_map<int,int>m;
  for(int i=0;i<N;i++)
  {
    int A;
    std::cin>>A;
    m[A%200]++;
  }

  int64_t res=0;
  for(int i=0;i<200;i++)
  {
    int64_t e=m[i];
    res+=e*(e-1)/2;
  }

  std::cout<<res<<std::endl;

  return 0;
}

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define forn(i,n) for(int i=0;i<n;i++)
#define pb emplace_back
#define sz(x) ((int) (x).size())
#define dbg(x) cerr << #x << "->" << x << "\n";

int gcd(int a, int b) {if (b == 0) return a; return gcd(b, a % b);}
int mod = (int)1e9 + 7;

void solve() {
	int n; cin >> n; int a[n];
	forn(i, n)cin >> a[i];
	int mo[n];
	forn(i, n) mo[i] = a[i] % 200;
	map<int, int> f;
	forn(i, n) {
		f[mo[i]]++;
	}
	int ans = 0;
	for (auto i : f) {
		ans += (i.second * (i.second - 1)) / 2;
	}
	cout << ans;
}

int32_t main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	int t = 1;
	// cin >> t;
	forn(i, t)
	solve();
	return 0;
}


#include <bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);
typedef long long int ll;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define loop(i,a, n) for(ll i = a; i < n; ++i)
ll N=1000000007;
using namespace std;
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>

using namespace __gnu_pbds;

template <typename num_t>
using ordered_set = tree<num_t, null_type, less_equal<num_t>, rb_tree_tag, tree_order_statistics_node_update>;
void init_code(){
    //copy(list.begin(),list.end(),ostream_iterator<int>(cout," "));
    fast_io;
    #ifndef ONLINE_JUDGE
        freopen("INPUT$.txt", "r", stdin);
        freopen("OUTPUT$.txt", "w", stdout);
        freopen("ERROR.txt","w",stderr);
    #endif
}
/*ll msb(ll n){
	for(int i=55;i>=0;i--){
		if((n&(1LL<<i))){
			return i;
		}
	}
	return 0;
}*/
void solve(){
	ll n,m,k,a,b,c,d;
	cin>>a>>b>>c>>d;
	ll blue=a,red=0;
	loop(x,1,a+1){
		blue+=b;
		red+=c;
		if(blue<=red*d){
			cout<<x<<endl;
			return;
		}
	}
	cout<<-1<<endl;
}
int main(){
	init_code();
	ll t;
	//cin>>t;
	//while(t-->0){
		solve();
	//}
}

#include <bits/stdc++.h>

using namespace std;

template <typename T> inline void read(T &x)
{
	int f = 1; x = 0; char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') f = -1;
	for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
	x *= f;
}

const int N = 4e5 + 5;

int n, m, k, a[N];

struct Function { double k, b; } p[N];

int main()
{
	read(n); read(m); read(k);
	for (int i = 1; i <= k; i++)
	{
		read(a[i]);
		p[a[i]].k = 1;

	}

	double tk = 0, tb = 0;
	for (int i = n - 1; i >= 0; i--)
	{
		if (!p[i].k)
		{
			p[i].k = tk / m;
			p[i].b = tb / m + 1;
		}

		tk += p[i].k;
		tb += p[i].b;
		tk -= p[i + m].k;
		tb -= p[i + m].b;
	}

	p[0].k -= 1;
	if (fabs(p[0].k) < 1e-9) puts("-1");
	else printf ("%.9f\n", -p[0].b / p[0].k);

  	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <utility>
using ll=long long;
using namespace std;
using P=pair<int,int>;

// auto mod int
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
//const int mod = 1000000007;
const int mod = 998244353;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  //mint inv() const { return pow(mod-2);}

  //supports not prime
  mint modinv(ll a) {
    long long b = mod, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= mod;
    if (u < 0) u += mod;
    mint res(u);
    return res;
}
  mint& operator/=(const mint a) { return *this *= modinv(a.x);}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

//combination
//under mint
vector<mint> fact;
vector<mint> finv;

void comb_init(int n){
    fact=vector<mint>(n+1);
    finv=vector<mint>(n+1);
    fact[0]=1;
    for(int i=1;i<=n;i++){
        fact[i]=fact[i-1]*i;
    }
    finv[0]=1;
    finv[n]=mint(1)/fact[n];
    for(int i=n;i>=2;i--){
        finv[i-1]=finv[i]*i;
    }
}

mint comb(int a,int b){
    return fact[a]*finv[b]*finv[a-b];
}

int main(){
    int N,K;
    cin>>N>>K;
    comb_init(K);
    vector<mint> A(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
    }
    vector<vector<mint>> powA(N,vector<mint>(K+1,1));
    for(int i=0;i<N;i++){
        for(int j=1;j<=K;j++){
            powA[i][j]=powA[i][j-1]*A[i];
        }
    }
    vector<mint> sum(K+1,0);
    for(int i=0;i<=K;i++){
        for(int j=0;j<N;j++){
            sum[i]+=powA[j][i];
        }
    }

    for(int i=1;i<=K;i++){
        mint ans=0;
        for(int j=0;j<=i;j++){
            ans+=(comb(i,j)*(sum[i-j]*sum[j]-sum[i]));
        }
        ans/=2;
        cout<<ans<<endl;
    }

    return 0;
}

// Problem: D - Powers
// Contest: AtCoder - AtCoder Regular Contest 106
// URL: https://atcoder.jp/contests/arc106/tasks/arc106_d
// Memory Limit: 1024 MB
// Time Limit: 3000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

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

const int N = 2e5+7,M = 305,MOD = 998244353;
ll fx[M],fc[M],fsa[M],fsb[M],C[M][M];
int a[N],b[N];
ll fa[N][M],fb[N][M];
ll qmul(ll a, ll b, ll P)
{
  	ll L = a * (b >> 25LL) % P * (1LL << 25) % P;
  	ll R = a * (b & ((1LL << 25) - 1)) % P;
  	return (L + R) % P;
}
ll qpow(ll a,ll b,ll p)
{
	ll res = 1 % p;
	while(b)
	{
		if(b & 1)	res = qmul(res,a,p);
		a = qmul(a,a,p);
		b >>= 1;
	}
	return res;
}
int fact[N],infact[N];
int qpow(int a,int b,int p)
{
    int res = 1 % p;
    while(b)
    {
        if(b & 1)   res = (ll)res * a % p;
        a = (ll)a * a % p;
        b >>= 1;
    }
    return res;
}

int main()
{
	int n,k,rev2 = qpow(2,MOD - 2,MOD);scanf("%d%d",&n,&k);
    forn(i,1,n)	scanf("%d",&a[i]),b[i] = 2 * a[i];
	fsa[0] = n,fsb[0] = n;
	C[0][0] = 1 ;
	forn( i, 1, k ) forn( j, 0, i )
		C[i][j] = (!j) ? 1 : (C[i - 1][j - 1] + C[i - 1][j]) % MOD ;
	forn(i,1,n)
	{
		ll _a = 1,_b = 1;
		forn(j,1,k)
		{
			_a = _a * a[i] % MOD,_b = _b * b[i] % MOD;
			fsa[j] = (fsa[j] + _a) % MOD,fsb[j] = (fsb[j] + _b) % MOD;
		}
	}

    forn(x,1,k)
    {
		ll lf = 0;
		forn(i,0,k)
		{
			ll t = qmul(fsa[i],fsa[x - i],MOD);
			t = qmul(C[x][i],t,MOD);
			lf = (lf + t) % MOD;
		}
		// cout << lf << endl;
		lf = ((lf - fsb[x]) % MOD + MOD) % MOD;
		printf("%lld\n",qmul(rev2,lf,MOD));
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
string s;
int a,b,c;
int must[100];
signed main()
{
	cin>>s;
	for(int i=0;i<=9;i++)
	{
		if(s[i]=='o') a++;
		if(s[i]=='x') b++;
		if(s[i]=='?') c++;
	}
	if(a>4)
	{
		printf("0");
		return 0;
	}
	else
	{
		int ans=0;

		for(int i=0;i<=9999;i++)
		{
			for(int j=0;j<=9;j++)
			{
				if(s[j]=='o') must[j]=true;
			}
			int now=i;
			int aa=now%10,bb=0,cc=0,dd=0;
			now/=10;
			if(now)
			{
				bb=now%10;
				now/=10;
			}
			if(now)
			{
				cc=now%10;
				now/=10;
			}
			if(now)
			{
				dd=now%10;
				now/=10;
			}
			if(s[aa]!='x'&&s[bb]!='x'&&s[cc]!='x'&&s[dd]!='x')
			{
				int flag=0;
				for(int j=0;j<=9;j++)
				{
					if(must[j]==true)
					{
						if(j!=aa&&j!=bb&&j!=cc&&j!=dd)
						{
							flag=1;
							break;
						}
					//	printf("%lld\n",j);
					}
				}
				if(flag==0)
				{
				//	cout<<s[9]<<endl;
				//	cout<<aa<<" "<<bb<<" "<<cc<<" "<<dd<<" "<<i<<" "<<endl;
					ans++;
				//	printf("%lld %lld %lld %lld\n",aa,bb,cc,dd);
				//	printf("%lld\n",i);
				}
			}
		}
		printf("%lld",ans);
	}
	return 0;
}

#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
typedef long long ll;
ll n;
int main(){
	scanf("%lld",&n);
	ll x = 0;
	for(ll i = 1LL;i <= n;i++){
		if((i + 1)*i <= 2*(n + 1)) x = i;
		else break;
	}
	printf("%lld\n",n - x + 1);
	return 0;
}


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

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

int main(){
    ll n;
    cin >> n;
    vector<ll> ans;
    ans.push_back(6);
    ans.push_back(10);
    ans.push_back(15);
    ll kazu=n;
    n=n-3;
    for(ll i=1;i<=10000;i++){
        if(i==6||i==10||i==15) continue;
        if(n==0) break;
        if(i%6==0){
            ans.push_back(i);
            n--;
            continue;
        }
        if(i%10==0){
            ans.push_back(i);
            n--;
            continue;
        }
        if(i%15==0){
            ans.push_back(i);
            n--;
            continue;
        }
    }

    //cout << n << endl;

    for(ll i=0;i<kazu;i++){
        cout << ans[i] << " ";
    }

    cout << endl;

    return 0;
}

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

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

int main(){
    int t;
    cin >> t;
    rep(t){
        ll l,r;
        cin >> l >> r;
        if (2*l > r) cout << 0 << endl;
        else cout << (r - 2*l + 2)*(r - 2*l + 1) / 2 << endl;
    }
}