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

using namespace std;

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

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

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

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

#if 1

/*
グループにできない、相性の悪い頂点ペアが複数ある
（相性が悪い＝辺がない）

相性の悪いペアが別のグループになるようにグループ分けしたときの、
最小のグループ数を求めよ。

1<=N<=18

2^18 = 262,144

*/

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

  int N, M; cin >> N >> M;
  vv(bool) as(N, v(bool)(N, true));
  F0R(i, M) {
    int a, b; cin >> a >> b;
    a--; b--;
    as[a][b] = as[b][a] = false;
  }
  vi dp(1 << N, N);
  dp[0] = 0;
  F0R(b, 1 << N) {
    F0R(i, N) {
      if ((b >> i) & 1) {
        int b2 = b ^ (1 << i);
        if (dp[b2] == 0) {
          dp[b] = 1;
          break;
        }
        if (dp[b2] == 1) {
          bool ok = true;
          F0R(j, N) {
            if ((b2 >> j) & 1 && as[i][j]) {
              ok = false;
              break;
            }
          }
          if (ok) {
            dp[b] = 1;
            break;
          }
          dp[b] = 2;
        }
      }
    }
    if (dp[b] > 2) {
      int ib = ~b;
      for (auto c = 0; c < b; c = (c + 1 + ib) & b) {
        auto c2 = c ^ b;
        chmin(dp[b], dp[c] + dp[c2]);
      }
    }
  }
  //cout << dp;
  cout << dp[(1 << N) - 1] << newl;
}
#endif

#include <bits/stdc++.h>

#ifdef _DEBUG_
#define debug(x) cerr<<#x<<" : "<<x<<" "
#define cout_n cerr<<endl
#else
#define debug(x)
#define cout_n
#endif
using namespace std;
const int MAXN=1e5+100;
vector<int>G[MAXN];
void add(int u,int v){
	G[u].push_back(v);G[v].push_back(u);
}
int n,m,k;
int c2v[20],v2c[MAXN];
int f[20][20];
bool vis[MAXN];
void bfs(int u){
	memset(vis,false,sizeof(vis));
	vis[u]=true;
	queue<int>q;q.push(u);
	int d=0;
	while(!q.empty()){
		int sz=q.size();
		++d;
		while(sz--){
			int nowu=q.front();q.pop();
			// debug(nowu);
			for(auto v:G[nowu]){
				if(!vis[v]){
					q.push(v);
					vis[v]=true;
					if(v2c[v]){
						// debug(v);
						// debug(c2v[u]);debug(c2v[v]);cout_n;
						f[v2c[u]][v2c[v]]=d;
					}
				}
			}
		}
	}
	// cout_n;
}
int dp[(1<<19)][20];
const int INF=0x3f3f3f3f;
int get1idx(int idx){
	return 1<<(idx-1);
}
int work(){
	int condmax=(1<<k)-1;
	memset(dp,INF,sizeof(dp));
	for(int i=1;i<=k;++i){
		dp[get1idx(i)][i]=0;
	}
	for(int i=1;i<=condmax;++i){
		for(int x=1;x<=k;++x){
			// debug(x);debug(get1idx(x));cout_n;
			if(!(i&(get1idx(x)))){
				continue;
			}
			int cond=(i&(~get1idx(x)));
			for(int y=1;y<=k;++y){
				if(!(cond&(get1idx(y)))){
					continue;
				}
				debug(i);debug(c2v[x]);debug(c2v[y]);debug(cond);cout_n;
				debug(dp[i][x]);debug(dp[cond][y]);debug(f[y][x]);cout_n;
				dp[i][x]=min(dp[i][x],dp[cond][y]+f[y][x]);
				debug(dp[i][x]);cout_n;cout_n;
			}
		}
	}
	int res=INF;
	for(int i=1;i<=k;++i){
		res=min(res,dp[condmax][i]);
	}
	return res==INF?-1:res+1;
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#ifdef _DEBUG_
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
#endif
	cin>>n>>m;
	debug(n);debug(m);cout_n;
	for(int i=1;i<=m;++i){
		int u,v;cin>>u>>v;
		add(u,v);
	}
	cin>>k;
	for(int i=1;i<=k;++i){
		cin>>c2v[i];v2c[c2v[i]]=i;
	}

//	for(int i=1;i<=n;++i){
//		debug(i);debug(v2c[i]);cout_n;
//	}
	memset(f,INF,sizeof(f));
	for(int u=1;u<=n;++u){
		if(v2c[u]){
			bfs(u);
		}
	}
	for(int i=1;i<=k;++i){
		for(int j=1;j<=k;++j){
			debug(i);debug(j);debug(f[i][j]);cout_n;
		}
	}
	cout<<work()<<'\n';
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define endl "\n"

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

   long long int n;
   cin>>n;

   long long int a[n],b[n],c[n],i;
   for(i=0;i<n;++i)
       cin>>a[i];

   for(i=0;i<n;++i)
       cin>>b[i];

   for(i=0;i<n;++i)
       cin>>c[i];

   sort(a,a+n);

   long long int ans=0;

   for(i=0;i<n;++i)
     {
         if(binary_search(a,a+n,b[c[i]-1]))
            ans+=upper_bound(a,a+n,b[c[i]-1])-lower_bound(a,a+n,b[c[i]-1]);
     }

   cout<<ans;

     return 0;
}

#include <cstdio>
#include <algorithm>
#define N 200007
int n;
long long a[N];
long long max[N];
long long sum[N];
int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
        max[i] = std::max(max[i - 1], a[i]);
        sum[i] = sum[i - 1] + a[i];
    }
    long long tmp = 0;
    for(int i = 1; i <= n; i++)
    {
        tmp += sum[i];
        printf("%lld\n", tmp + i * max[i]);
    }
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
using namespace std;
vector<int> graph[400001];

void dfs(int k, bool visited[], int par, bool &cond, int &si)
{
    if (visited[k])
    {
        return;
    }
    si++;
    visited[k] = true;
    for (auto i : graph[k])
    {
        if (i != par && visited[i] == true)
        {
            cond = true;
        }

        else
        {
            dfs(i, visited, k, cond, si);
        }
    }
}

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

    int t;
    cin >> t;
    pair<int, int> a[t];
    for (int i = 0; i < t; i++)
    {
        int a1, b;
        cin >> a1 >> b;
        graph[a1].push_back(b);
        graph[b].push_back(a1);
        a[i] = {a1, b};
    }
    bool visited[400001];
    memset(visited, false, sizeof(visited));
    int ans = 0;
    for (auto i : a)
    {
        if (!visited[i.first])
        {
            int si = 0;
            bool cond = false;
            dfs(i.first, visited, -1, cond, si);
            // cout << cond << " " << i.first << " " << si << endl;
            if (cond)
            {
                ans += si;
            }
            else
            {
                ans += si - 1;
            }
        }
        if (!visited[i.second])
        {
            int si = 0;
            bool cond = false;
            dfs(i.second, visited, -1, cond, si);
            // cout << cond << " " << i.second << " " << si << endl;
            if (cond)
            {
                ans += si;
            }
            else
            {
                ans += si - 1;
            }
        }
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>

using namespace std;

/**templates**/
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;

#define si(x) scanf("%d",&x)
#define sl(x) scanf("%lld",&x)
#define sull(x) scanf("%llu",&x)
#define sf(x) scanf("%lf",&x)
#define ss(x) scanf(" %s",x)
#define sc(x) scanf(" %c",&x)
#define pi acos(-1.0)
#define pb push_back
#define aa first
#define bb second
#define sz(x) (ll)(x).size()
#define cas printf("Case %lld: ",++t)
#define casline printf("Case %lld:\n",++t)
//#define cas cout<<"Case "<<++t<<": "
//#define casline cout<<"Case "<<++t<<":"<<endl;
#define distance(x1,y1,x2,y2)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))
#define endl "\n"
#define bug cout<<"bugggggggggggg "<<endl;
#define fast ios_base::sync_with_stdio(0);cin.tie(nullptr); ///no printf/scanf
#define clean fflush(stdout)

const ll mod = 1000000007;
const ll inf = 1LL<<62;
const ll mx = 100005;
const double eps = 1e-10;

ll dx[10]={1,0,-1,0};
ll dy[10]={0,-1,0,1};
///ll dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; /// 8 Direction
///ll dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; /// Knight Direction

ll power(ll a,ll b)
{
    if(b==0)return 1;
    ll x=power(a,b/2);
    x=x*x;
    if(b%2)x=x*a;
    return x;
}

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

ll Set(ll N,ll pos){return N=N | (1LL<<pos);}
ll reset(ll N,ll pos){return N= N & ~(1LL<<pos);}
bool check(ll N,ll pos){return (bool)(N & (1LL<<pos));}

/**
        ...ENDING OF TEMPLATE...
*/

ll arr[3*mx];
ll tree[4*3*mx];
void build(ll at,ll L,ll R)
{
    if(L==R)
    {
        tree[at]=arr[L];
        return;
    }
    ll mid=(L+R)/2;
    build(2*at,L,mid);
    build(2*at+1,mid+1,R);
    tree[at]=tree[2*at]^tree[2*at+1];
}
void update(ll at,ll L,ll R,ll id,ll val)
{
    if(R<id||L>id)return;
    if(L==R&&id==R)
    {
        tree[at]^=val;
        return;
    }
    ll mid=(L+R)/2;
    update(2*at,L,mid,id,val);
    update(2*at+1,mid+1,R,id,val);
    tree[at]=tree[2*at]^tree[2*at+1];
}
ll query(ll at,ll L,ll R,ll l,ll r)
{
    if(l>R||r<L)return 0;
    if(l<=L&&r>=R)return tree[at];
    ll mid=(L+R)/2;
    ll a=query(2*at,L,mid,l,r);
    ll b=query(2*at+1,mid+1,R,l,r);
//    cout<<a+b<<endl;
    return a^b;
}

int main()
{
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
//    fast; ///no printf/scanf
    /** Use "long double"(%Lf) instead of double for precision safety*/
    ll tst,a,b,c,k,n,m,res=1,ans=0,t=0;
    sl(n),sl(m);
    for(ll i=1;i<=n;i++)
    {
        sl(arr[i]);
    }
    build(1,1,n);
    while(m--)
    {
        sl(c),sl(a),sl(b);
        if(c==1)
        {
            update(1,1,n,a,b);
        }
        else
        {
            res=query(1,1,n,a,b);
            printf("%lld\n",res);
        }
    }

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

#define  ll long long
#define ld long double
#define f first
#define s second
const int N = 3e5+5;

ll n , m , q , a[N] , BIT[N];

void update(int idx , ll val)
{
    a[idx] = val;

    for(idx; idx<=n; idx = (idx + (idx&-idx)))
    {
        BIT[idx] ^= val;
    }
}

ll query(int idx)
{
    ll res = 0;
    for(idx; idx; idx = (idx - (idx&-idx)))
    {
        res ^= BIT[idx];
    }
    return res;
}

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

    cin >> n >> q;
    for(int i=1; i<=n; i++)
    {
        cin >> a[i];
        update(i , a[i]);
    }

    while(q--)
    {
        ll t , l , r;
        cin >> t >> l >> r;
        if (t==1)
        {
            update(l , r);
        }
        else
        {
            cout << (query(r)^query(l-1)) << '\n';
        }
    }

    return 0;
}


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<list>
#include<bitset>
#include<sstream>

using namespace std;
using ll = long long;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define REP(i, n) for(int i = 0; i < n; i++)
#define endl "\n"
using Graph = vector<vector<int>>;

int main(){
    double a,b;
    cin>>a>>b;
    printf("%.10lf\n",a*b/100);
}

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

#pragma region library
#pragma endregion library

int main() {
    int n;
    cin >> n;
    vi a(n), b(n);
    rep(i, n) cin >> a[i] >> b[i];
    ll now = accumulate(all(a), 0ll);
    vll diff(n);
    rep(i, n) {
        diff[i] = a[i] * 2ll + b[i];
    }
    sort(rall(diff));
    int ans = 0;
    int idx = 0;
    while (idx < n && now >= 0) {
        now -= diff[idx++];
        ans++;
    }
    cout << ans << '\n';
}


#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using pl = pair<ll, ll>;
constexpr int MX = 5005, MOD = 998244353;

pl DP[MX][2];

int main() {
	if (fopen("in", "r")) freopen("in", "r", stdin), freopen("out", "w", stdout);
	cin.tie(0)->sync_with_stdio(0);

	int N, M; cin >> N >> M;
	ll ans = 0;
	for (int i = 1; i <= M; ++i) {
		DP[0][0] = pl(0, M-1), DP[0][1] = pl(1, 1);
		for (int j = 0; j < N-1; ++j) {
			DP[j+1][0] = pl((M-1)*DP[j][0].f+(i-1)*DP[j][1].f, (M-1)*DP[j][0].s+(i-1)*DP[j][1].s);
			DP[j+1][1] = pl(DP[j][0].f+DP[j][0].s+(M-i+1)*DP[j][1].f, DP[j][0].s+(M-i+1)*DP[j][1].s);
			DP[j+1][0].f %= MOD, DP[j+1][0].s %= MOD;
			DP[j+1][1].f %= MOD, DP[j+1][1].s %= MOD;
		}
		(ans += DP[N-1][0].f+DP[N-1][1].f) %= MOD;
	}
	cout << ans;
}

#include <bits/stdc++.h>

#define task "F"
#define all(v) (v).begin(), (v).end()
#define rep(i, l, r) for (int i = (l); i <= (r); ++i)
#define Rep(i, r, l) for (int i = (r); i >= (l); --i)
#define DB(X) { cerr << #X << " = " << (X) << '\n'; }
#define DB1(A, _) { cerr << #A << "[" << _ << "] = " << (A[_]) << '\n'; }
#define DB2(A, _, __) { cerr << #A << "[" << _ << "][" << __ << "] = " << (A[_][__]) << '\n'; }
#define DB3(A, _, __, ___) { cerr << #A << "[" << _ << "][" << __ << "][" << ___ << "] = " << (A[_][__][___]) << '\n'; }
#define PR(A, l, r) { cerr << '\n'; rep(_, l, r) DB1(A, _); cerr << '\n';}
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back
#define pf push_front
#define F first
#define S second
#define by(x) [](const auto& a, const auto& b) { return a.x < b.x; } // sort(arr, arr + N, by(a));
#define next ___next
#define prev ___prev
#define y1 ___y1
#define left ___left
#define right ___right
#define y0 ___y0
#define div ___div
#define j0 ___j0
#define jn ___jn

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using namespace std;
typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vl;
int n, T, a[41];
int main()
{
#ifdef HynDuf
    freopen(task".in", "r", stdin);
    //freopen(task".out", "w", stdout);
#else
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
#endif
    cin >> n >> T;
    rep(i, 0, n - 1) cin >> a[i];
    vl vec;
    int nn = n / 2;
    rep(x, 0, (1 << nn) - 1)
    {
        ll sum = 0;
        rep(i, 0, nn - 1) if (x >> i & 1) sum += a[i];
        vec.eb(sum);
    }
    sort(all(vec));
    int nn1 = n - nn, ans = 0;
    rep(x, 0, (1 << nn1) - 1)
    {
        ll sum = 0;
        rep(i, 0, nn1 - 1) if (x >> i & 1) sum += a[nn + i];
        auto it = upper_bound(all(vec), T - sum);
        if (it != vec.begin())
        {
            it--;
            ans = max(1ll * ans, *it + sum);
        }
    }
    cout << ans;
    return 0;
}


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

ll C(int n, int k) {
  int f1 = n, f2 = 1;
  ll ret = 1;
  if (k>n/2) {
    ret = C(n,n-k);
  } else {
    for (int i=0;i<k;i++) {
      ret *= f1;
      ret /= f2;
      f1--;
      f2++;
    }
  }
  return ret;
}

int main() {
  int A,B,i;
  ll K;
  cin>>A>>B>>K;
  string ans = "";
  int a=0,b=0;
  for (i=1;i<=A+B;i++) {
    if (a<A) {
      ll c = C(A-a+B-b-1, A-a-1);
      if (c<K) {
        ans += 'b';
        b++;
        K -= c;
      } else {
        ans += 'a';
        a++;
      }
    } else {
      ans += 'b';
    }
  }
  cout<<ans<<endl;
  return 0;
}

#include <bits/stdc++.h>
#define eb emplace_back
using namespace std;
template<class T> using vc = vector<T>;
template<class T> using vv = vector<vector<T>>;
template<class T> using PQ = priority_queue<T, vc<T>, greater<T>>;
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using tiii = tuple<int, int, int>;
using tlll = tuple<ll, ll, ll>;
using vi = vc<int>;
using vl = vc<ll>;
using vs = vc<string>;
const ld PI = 3.141592653589793;
const ll LINF = 1001001001001001001LL;
const int INF = 1001001001;

constexpr int MOD = 1e9 + 7;
//constexpr int MOD = 998244353;

struct mint {
  long long x;
  mint(long long x = 0) noexcept : x((x % MOD + MOD) % MOD) {}

  // basis
  mint operator-() const { return mint(-x); }
  mint& operator+=(const mint a) noexcept {
    if ((x += a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator-=(const mint a) noexcept {
    if ((x += MOD - a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator*=(const mint a) noexcept {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const noexcept { return mint(*this) += a; }
  mint operator-(const mint a) const noexcept { return mint(*this) -= a; }
  mint operator*(const mint a) const noexcept { return mint(*this) *= a; }
  mint mpow(long long t) const noexcept {
    if (!t) return 1;
    mint a = mpow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const noexcept { return mpow(MOD - 2); }
  mint& operator/=(const mint a) noexcept { return *this *= a.inv(); }
  mint operator/(const mint a) const noexcept { return mint(*this) /= a; }

  // comparison
  bool operator==(const mint& a) const noexcept { return this->x == a.x; }
  bool operator!=(const mint& a) const noexcept { return this->x != a.x; }

  // I/O stream
  friend istream& operator>>(istream& is, mint& a) noexcept { return is >> a.x; }
  friend ostream& operator<<(ostream& os, const mint& a) noexcept { return os << a.x; }
};
// additional
mint mpow(const long long& a, long long n) noexcept { return mint(a).mpow(n); }
mint mpow(const mint& a, long long n) noexcept { return a.mpow(n); }

int main() {
  ll N, P;
  cin >> N >> P;
  mint ans = (mint)(P - 1) * mpow(P - 2, N - 1);
  cout << ans << endl;
}


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

int main(){
    cout << fixed << setprecision(15);
    int K;
    string S, T;
    cin >> K;
    cin >> S;
    cin >> T;

    map<int,int> card,tkh,aok;
    for(int i=1; i<=9; i++){
        card[i] = K;
        tkh[i] = 0;
        aok[i] = 0;
    }
    for(int i=0; i<4; i++){
        int a = S.at(i)-'0';
        tkh[a] += 1;
        card[a] -= 1;
        a = T.at(i)-'0';
        aok[a] += 1;
        card[a] -= 1;
    }

    double ans=0;
    for(int i=1; i<=9; i++){
        int anst=0;
        if( card[i] > 0 ){
            if( tkh[i] < K ){
                card[i] -= 1;
                tkh[i] += 1;
                for(int j=1; j<=9; j++){
                    anst += j*pow(10,tkh[j]);
                }
                for(int l=1; l<=9; l++){
                    int ansa=0;
                    if( card[l] > 0 ){
                        if( aok[l] < K ){
                            aok[l] += 1;
                            for(int m=1; m<=9; m++){
                                ansa += m*pow(10,aok[m]);
                            }
                            if( anst > ansa ){
                               ans += (double(card[i]+1)/double(9*K-8))*(double(card[l])/double(9*K-9));
                            }
                            aok[l] -= 1;
                        }
                    }
                }
                card[i] += 1;
                tkh[i] -= 1;
            }
        }
    }
    cout << ans << endl;
}

#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define deb(x) cout<<#x<<" "<<x<<endl;
#define print(arr,n) for(int i=0;i<n;++i) { cout<<arr[i]<<" "; } cout<<endl;
#define ub(x,el) upper_bound(x.begin(),x.end(),el) - x.begin()
#define lb(x,el) lower_bound(x.begin(),x.end(),el) - x.begin();
#define bs(x,el) binary_search(x.begin(),x.end(),el)
#define MOD 1000000007
#define finish return 0
#define all(x) x.begin(),x.end()
using namespace std;
//using namespace __gnu_pbds;
typedef long long int ll;
//typedef tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
template<int D, typename T> struct Vec : public vector<Vec<D - 1, T>> { static_assert(D >= 1, "Vector dimension must be greater than zero!"); template<typename... Args> Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...)) { }}; template<typename T> struct Vec<1, T> : public vector<T> { Vec(int n = 0, T val = T()) : vector<T>(n, val) { } };
inline ll mod(const ll& x) { return (x%MOD + MOD)%MOD; }
ll gcd(ll a, ll b, ll &x, ll &y){if(a == 0){x = 0, y = 1; return b;} ll x1, y1;  ll d = gcd(b%a, a, x1, y1); x = y1 - (b/a) * x1; y = x1; return d;}
ll power(ll x,ll y, ll p) { ll res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res*x) % p; y = y>>1; x = (x*x) % p;   } return res; }
ll power(ll x,ll y) { ll res = 1; while (y > 0) { if (y & 1) res = (res*x); y = y>>1; x = (x*x);   } return res; }
ll mod(ll num, ll den){ return mod(mod(num) * power(den,MOD-2,MOD)); }
vector<ll> primes(ll n) { vector<ll> p; vector<bool> v(n+1,true); for(ll i=2;i*i<=n;++i) { if(v[i]) { for(ll j=i*i;j<=n;j+=i) v[j] = false; } } for(ll i=2;i<=n;++i) if(v[i]) p.push_back(i); return p; }
vector<ll> lpf(ll n){ vector<ll> v(n+1); for(ll i=1;i<=n;++i) v[i] = i; for(ll i=2;i*i<=n;++i) { if(v[i] == i) { for(ll j=i*i;j<=n;j+=i) { v[j] = i; } } } return v; }
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //ll case_no = 1;
    ll k;
    cin>>k;
    string s,t;
    cin>>s>>t;
    unordered_map<ll,ll> mp;
    for(ll i=0;i<4;++i)
    {
        mp[s[i]-'0']++;
        mp[t[i]-'0']++;
    }
    ll total = 0;
    ll count = 0;
    for(ll i=1;i<=9;++i)
    {
        for(ll j=1;j<=9;++j)
        {
            ll s1,s2;
            s1 = s2 = 0;
            vector<ll> mp1(10),mp2(10);
            for(ll z=0;z<4;++z)
            {
                mp1[s[z]-'0']++;
                mp2[t[z]-'0']++;
            }
            ll val1 = mp1[i] + mp2[i];
            ll val2 = mp2[j] + mp1[j];

            mp1[i]++;
            mp2[j]++;
            bool flag = true;
            for(ll z=1;z<10;++z)
            {
                s1 += (power(10,mp1[z])*z);
                s2 += (power(10,mp2[z])*z);
            }
            for(ll z=0;z<10;++z)
            {
                if((mp1[z] + mp2[z]) > k)
                {
                    flag = false;
                    break;
                }
            }
            if(!flag) continue;

            if(i!=j)
            {
                ll v1 = k-val1;
                ll v2 = k-val2;
                if(s1 > s2) count += (v1*v2);

                total += (v1*v2);
            }
            else
            {
                ll v = k-val1;
                total += (v*(v-1));
                if(s1 > s2) count += (v*(v-1));
            }

        }
    }
    double ans = count;
    ans /= total;
   // deb(count);
    //deb(total);
    cout.precision(10);
    cout<<ans<<endl;
}


//2021. by ljz
//email 573902690@qq.com
//if you find any bug in my code
//please tell me
#include<bits/stdc++.h>
//#include<ext/pb_ds/tree_policy.hpp>
//#include<ext/pb_ds/assoc_container.hpp>
using namespace std;
//using namespace __gnu_pbds;
//using namespace __gnu_cxx;
#define res register int
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f
#define unl __int128
#define eps 5.6e-8
#define RG register
#define db double
#define pc(x) __builtin_popcount(x)
#define ctz(x) __builtin_ctz(x)
//#define pc(x) __builtin_popcountll(x)
typedef pair<int,int> Pair;
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pb push_back
#define ull unsigned LL
#define lowbit(x) (x&-x)
#define gc getchar
#define kcz 1000000007
//template <class T>using Tree=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;
// inline char gc() {
//    static char buf[100000],*p1,*p2;
//    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
// }
inline int read() {
    res s=0,ch=gc();
    while(ch<'0'||ch>'9')ch=gc();
    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
    return s;
}
// char sr[1<<21],z[20];
// int C=-1,Z=0;
// inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
// inline void print(res x){
//    if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
//    while(z[++Z]=x%10+48,x/=10);
//    while(sr[++C]=z[Z],--Z);
// }
//inline int read() {
//    res s=0,ch=gc(),w=1;
//    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=gc();}
//    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//    return s*w;
//}
//inline LL Read() {
//    RG LL s=0;
//    res ch=gc();
//    while(ch<'0'||ch>'9')ch=gc();
//    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//    return s;
//}
//inline LL Read() {
//    RG LL s=0;
//    res ch=gc(),w=1;
//    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=gc();}
//    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//    return s*w;
//}
//inline void write(RG unl x){
//    if(x>10)write(x/10);
//    putchar(int(x%10)+'0');
//}
inline void swap(res &x,res &y) {
    x^=y^=x^=y;
}
inline void add(res &x,const res &y){
    x+=y,x>=kcz?x-=kcz:(x<0?x+=kcz:1);
}
inline int Add(const res &x,const res &y){
    return x+y>=kcz?x+y-kcz:(x+y<0?x+y+kcz:x+y);
}
inline int mul(const res &x,const res &y){
    return int(1LL*x*y%kcz);
}
inline int mul(const res &x,const res &y,const res &d){
    return int(1LL*x*y/d%kcz);
}
inline int qpow(res x,res y){
    if(y<=0)return 1;
    res ret=1;
    while(y){
        if(y&1)ret=mul(ret,x);
        x=mul(x,x),y>>=1;
    }
    return ret;
}
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//clock_t start=clock();
//inline void ck(){
//    if(1.0*(clock()-start)/CLOCKS_PER_SEC>0.1)exit(0);
//}
const int N=1e5+10;
namespace MAIN{
    int n,a[N];
    LL sum[N];
    inline void MAIN(){
        n=read();
        for(res i=1;i<=n;i++)a[i]=read();
        sort(a+1,a+n+1);
        for(res i=1;i<=n;i++)sum[i]=sum[i-1]+a[i];
        RG LL ans=INF;
//        printf("%.10f\n",(3.0-2.0*2/n)*1.0*a[2]/2.0-1.0*sum[2]/n);
        for(res m=0;m<=n;m++){
            ans=min(ans,sum[n]*2+1LL*n*a[m]-2LL*a[m]*(n-m)-2LL*sum[m]);
        }
//        printf("%lld\n",ans/2);
        printf("%.10Lf\n",(long double)ans/(2.0*n));
    }
}
int main(){
//    srand(19260817);
    // freopen("tracing.in","r",stdin);
    // freopen("tracing.out","w",stdout);
    MAIN::MAIN();
//    Ot();
    return 0;
}

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

using namespace std;
using namespace __gnu_pbds;

using ll = long long;
using ld = long double;
template <typename T>
using orderedSet = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using orderedMultiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;

const ld pi = 4.0 * atan(1.0);
#define xlr8                          \
	ios_base::sync_with_stdio(false); \
	cin.tie(NULL);                    \
	cout.tie(NULL);                   \
	cout.precision(10);               \
	cout << fixed

//76149
template <int D, typename T>
struct Vec : public vector<Vec<D - 1, T>>
{
	static_assert(D >= 1, "Vector dimension must be greater than zero!");
	template <typename... Args>
	Vec(int n = 0, Args... args) : vector<Vec<D - 1, T>>(n, Vec<D - 1, T>(args...))
	{
	}
};
template <typename T>
struct Vec<1, T> : public vector<T>
{
	Vec(int n = 0, T val = T()) : vector<T>(n, val)
	{
	}
};

int main()
{
	xlr8;
	int testCases = 1;
	// cin >> testCases;
	while (testCases--)
	{
		int n;
		cin>>n;
		vector<int> a(n);
		for(int i = 0 ; i < n ; i++)
			cin>>a[i];
		nth_element(a.begin() , a.begin() + n/2 , a.end());
		nth_element(a.begin() , a.begin() + n/2 - 1,  a.end());
		double md = (n%2 ? a[n/2] : (a[n/2 - 1] + a[n/2])/2)/2.0;
		double ans = 0;
		for(int i = 0 ; i < n ; i++)
			ans += (a[i] + md  - min((double)a[i] , 2*md));
		ans/=n;
		cout<<ans<<"\n";
	}
}


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

int main()
{
    int V, T, S, D;
    cin >> V >> T >> S >> D;

    if (V*T <= D and D <= V*S) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }
    return 0;
}

#include<bits/stdc++.h>
#define ll long long int
using namespace std;
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define endl "\n"
#define maxpq priority_queue<ll>
#define minpq priority_queue<ll, vector<ll>, greater<ll> >
#define vi vector<ll>
#define pii pair<ll, ll>
#define vii vector<pii>
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define MOD 1000000007
#define INT_MAXI 1000000000000000000
#define INT_MINI -9000000000000000000

ll max(ll a, ll b) {if (a > b) return a; else return b;}
ll min(ll a, ll b) {if (a < b) return a; else return b;}

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

char arr[1005][1005];

int main()
{
	fio;
	ll t,n,i,j,k,x,l,r,b,h,y,m,a,c,d;
	cin>>a>>b>>c>>d;
	double val=d*1.0/a;
	if(val>=b&&val<=c)
	{
		cout<<"No";
	}
	else
	cout<<"Yes";

}


#include <stdio.h>

typedef long long ll;
int num[105][105], max = -1, vis[20], k, n, m;
struct node {
	int x1, x2;
} a[20], b[105];

void dfs(int now) {
	if (now >= k) {
		int sum = 0;
		//for (int i = 0; i < 17; i ++) printf("%d ", vis[i]);printf("\n");
		for (int i = 0; i < m; i ++)
			if (vis[b[i].x1] && vis[b[i].x2])
				//printf("sum : %d + num[%d][%d] (%d)\n", sum, b[i].x1, b[i].x2, num[b[i].x1][b[i].x2]);
				sum ++;

		if (sum > max) max = sum;
		//printf("%d..sum : %d\n", now, sum);
		return;
	}
	int px1 = a[now].x1, px2 = a[now].x2;

	vis[px1] ++;
	//printf("%d pick : %d\n", now, px1);
	dfs(now + 1);
	vis[px1] --;

	vis[px2] ++;
	//printf("%d pick : %d\n", now, px2);
	dfs(now + 1);
	vis[px2] --;

}
int main() {
	int cnt = 0;
	scanf("%d %d", &n, &m);
	while (cnt < m) {
		scanf("%d %d", &b[cnt].x1, &b[cnt].x2);
		//num[b[cnt].x1][b[cnt].x2] ++;
		cnt ++;
	}
	int ind = 0;
	scanf("%d", &k);
	while (ind < k) {
		scanf("%d %d", &a[ind].x1, &a[ind].x2);
		ind ++;
	}
	dfs(0);
	//dfs(1);
	printf("%d\n", max);
	return 0;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long int;

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

int main() {
  double n;
  double x2, y2, ans = 0;
  cin >> n >> x2 >> y2;

  for (ll i = 0; i < n; i++) {
    double x1, y1;
    cin >> x1 >> y1;

    double y3 = ((y2 - y1) / (x2 - x1)) * (0 - x1) + y1;

    if (y3 > ans) {
      ans = y3;
    }
  }

  cout << fixed << setprecision(9) << ans << endl;
}


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

const int N = 2e5+7;
int x[N],y[N];
map<int,vector<int>> E;

int main()
{
    int n,m;scanf("%d%d",&n,&m);
    forn(i,1,m)
    {
        int x,y;scanf("%d%d",&x,&y);
        E[x].push_back(y);
    }
    set<int> st;
    st.insert(n);

    for(auto& _ : E)
    {
        vector<int> nxt;
        for(auto& v : _.second) if(st.count(v - 1) || st.count(v + 1))    nxt.push_back(v);
        for(auto& v : _.second) st.erase(v);
        for(auto& v : nxt)  st.insert(v);
    }
    printf("%d\n",(int)st.size());

    return 0;
}

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

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;

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

map<lli,vector<lli>> G;

void func(long long N, long long M, std::vector<long long> X, std::vector<long long> Y){
    REP(i,0,M){
        G[X[i]].push_back(Y[i]);
    }

    set<lli> nowS;
    nowS.insert(N);

    for(auto &e:G){
        set<lli> eraseS;
        set<lli> nextPushS;
        vector<lli> &nowV = e.second;
        for(auto now:nowV){
            if(nowS.find(now-1) != nowS.end()){
                nextPushS.insert(now);
            }
            if(nowS.find(now+1) != nowS.end()){
                nextPushS.insert(now);
            }
            if(nowS.find(now) != nowS.end()){
                eraseS.insert(now);
            }
        }
        for(auto e:eraseS){
            nowS.erase(e);
        }
        for(auto e:nextPushS){
            nowS.insert(e);
        }
    }
    cout<<nowS.size()<<endl;

}

int main(){
    // cout << fixed << setprecision(5);

    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> X(M);
    std::vector<long long> Y(M);
    for(int i = 0 ; i < M ; i++){
        scanf("%lld",&X[i]);
        scanf("%lld",&Y[i]);
    }
    func(N, M, std::move(X), std::move(Y));
    return 0;
}


#include<bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(),(a).end()
#define SZ(a) (int)(a).size()
#define FOR(i, a, b) for(int i=(a); i<=(b); ++i)
#define iFOR(i, a, b) for(int i=(a); i>=(b); --i)
#define make_unique(a) sort(all((a))), (a).resize(unique(all((a)))-(a).begin())

using namespace std;

typedef pair<int,int> PII;
typedef long long LL;
typedef unsigned long long UL; // ULLONG_MAX, %llu
typedef unsigned int UI; //UINT_MAX %u
typedef double DD;
typedef long double LD;
typedef pair<LL,LL> PLL;
typedef pair<DD,DD> PDD;
typedef vector<int> VI;
typedef vector<LL> VL;

int mod;
void adding(int &a, int b){
    if((a+=b) >= mod) a -= mod;
}
int add(int a, int b){
    if((a+=b) >= mod) return a - mod;
    return a;
}
int mul(int a, int b){
    return a * 1ll * b % mod;
}
const int N = 101;
int dp[N][130000];
int sum[N][130000];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n, K;
    cin >> n >> K >> mod;
    int Max = 129999;
    dp[0][0] = sum[0][0] = 1;
    FOR(i, 1, n){
        dp[i][0] = sum[i][0] = 1;
        FOR(j, 1, Max){
            sum[i-1][j] = dp[i-1][j];
            if(j >= i) adding(sum[i-1][j],sum[i-1][j-i]);
        }
        FOR(j, 1, Max){
            dp[i][j] = sum[i-1][j];
            if(j >= (K+1)*i) adding(dp[i][j], mod-sum[i-1][j-(K+1)*i]);
        }
    }
    FOR(x, 1, n){
        // try all mean
        int ans = K;
        FOR(i, 1, Max) adding(ans, mul( mul(dp[x-1][i], dp[n-x][i]), K+1));
        cout << ans << endl;
    }
	return 0;
}
/*
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */


#pragma GCC optimize("Ofast")
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <cmath>
#include <iomanip>
#include <string>
#include <deque>
#include <assert.h>
#include <random>
#include <ctime>
#include <unordered_map>
#include <complex>
#include <chrono>

#define pnode node*

using namespace std;

typedef long long ll;
typedef long double ld;

const ll mod = 998244353;

ll n, k;

ll pw(ll a, ll p) {
    if (p == 0) {
		return 1;
    }
    ll b = pw(a, p / 2);
    if (p % 2 == 0) {
        return (b * b) % mod;
    } else {
        return (((b * b) % mod) * a) % mod;
    }
}

ll rev(ll a) {
    return pw(a, mod - 2);
}

vector < ll > smpw;
vector < ll > pwsm;
vector < vector < ll > > c;
vector < ll > a;

void solve(){
	cin >> n >> k;
	a.resize(n);
	smpw.resize(k + 1);
    pwsm.resize(k + 1);
    c.resize(k + 1, vector < ll > (k + 1));
    for (ll i = 0; i < n; ++i) {
        cin >> a[i];
    }
    for (ll i = 0; i <= k; ++i) {
		ll cursm = 0;
        ll curpw = 0;
        for (ll j = 0; j < n; ++j) {
            cursm += a[j];
        }
        cursm = pw(cursm, i);
        cursm %= mod;
        for (ll j = 0; j < n; ++j) {
            curpw += pw(a[j], i);
        }
        curpw %= mod;
        smpw[i] = cursm;
        pwsm[i] = curpw;
    }
    for (ll i = 0; i <= k; ++i){
        for (ll j = 0; j <= k; ++j) {
            if (i < j) {
                c[i][j] = 0;
                continue;
            }
            if (i == 0 || j == 0) {
                c[i][j] = 1;
                continue;
            }
            c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod;
        }
    }
//    cerr << k << " " << k - 1 << " " << c[k][k - 1] << "\n\n";
    for (ll p = 1; p <= k; ++p) {
    	ll ans = 0;
        for (ll i = 0; i <= p; ++i) {
            // c[p][i];
            // i, p - i
            ll add = (pwsm[i] * pwsm[p - i]) % mod;
            add = (add - pwsm[p] + mod) % mod;
            add = (add * c[p][i]) % mod;
            ans += add;
            ans %= mod;
        }
        cout << (ans * rev(2)) % mod << "\n";
    }
}

signed main(){
    ll t = 1;
    ios_base::sync_with_stdio(0);
    srand(time(0));
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout << setprecision(20);
    #ifdef DEBUG
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #else
    #endif
    while(t--){
        solve();
    }
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define P pair<int, int>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T, class U>
inline bool chmax(T &a, U b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T, class U>
inline bool chmin(T &a, U b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
constexpr int INF = 1000000000;
constexpr ll llINF = 1000000000000000000;
constexpr int mod = 998244353;
constexpr double eps = 1e-10;
const double pi = acos(-1);
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int Random(int mi, int ma) {
    random_device rnd;
    mt19937 mt(rnd());  // 32bit
    //[mi,ma]
    uniform_int_distribution<int> engine(mi, ma);
    return engine(mt);
}
/*
vector<vector<ll>>C,sC;
void init_comb(int n,int m){
    C.resize(n+1,vector<ll>(m+1,0));
    sC.resize(n+1,vector<ll>(m+1,0));
    C[0][0]=1;
    for(int i=1;i<=n;i++){
        C[i][0]=1;
        for(int j=1;j<=m;j++){
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
        }
    }
    rep(i,n+1){
        rep(j,m){
            sC[i][j+1]=(sC[i][j]+C[i][j])%mod;
        }
    }
}*/
bool prime(int a) {
    if (a == 1) return false;
    for (int i = 2; i * i <= a; i++) {
        if (a % i == 0) return false;
    }
    return true;
}
vector<int> primes;
void init_prime(int n) {
    primes.push_back(2);
    for (int i = 3; i <= n; i += 2) {
        bool f = true;
        for (int j : primes) {
            if (j * j > i) break;
            if (i % j == 0) {
                f = false;
                break;
            }
        }
        if (f) primes.push_back(i);
    }
}
ll modpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) {
            res *= a;
            res %= mod;
        }
        a *= a;
        a %= mod;
        b >>= 1;
    }
    return res;
}
vector<ll> inv, fact, factinv;
void init_fact(int n) {
    inv.resize(n + 1);
    fact.resize(n + 1);
    factinv.resize(n + 1);
    inv[0] = 0;
    inv[1] = 1;
    fact[0] = 1;
    factinv[0] = 1;
    for (ll i = 1; i <= n; i++) {
        if (i >= 2) inv[i] = mod - ((mod / i) * inv[mod % i] % mod);
        fact[i] = (fact[i - 1] * i) % mod;
        factinv[i] = factinv[i - 1] * inv[i] % mod;
    }
}
ll _inv(ll a, ll m = mod) {
    // gcd(a,m) must be 1
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
ll comb(int a, int b) {
    if (a < b) return 0;
    if (a < 0) return 0;
    return fact[a] * factinv[a - b] % mod * factinv[b] % mod;
}
ll multicomb(int a, int b) { return comb(a + b - 1, b); }

void solve() {
    int n, a[200010];
    cin >> n;
    ll ma = 0, sum = 0, cur = 0;
    rep(i, n) {
        cin >> a[i];
        sum += a[i];
        chmax(ma, a[i]);
        cur += sum;
        cout << ma * (i + 1) + cur << endl;
    }
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // int t;cin >> t;while (t--)
    solve();
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define pre(i,a,b) for(int i=a;i>=b;i--)
#define N 1000005
using namespace std;
int n,a[N];
double calc(double x){
	double sum=0;
	rep(i,1,n)sum+=(a[i]-min(1.00*a[i],x*2))/(1.00*n);
	return sum+x;
}
int main(){
	scanf("%d",&n);
	rep(i,1,n)scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	if(n==1)printf("%.10lf\n",a[1]/2.00);
	else printf("%.10lf\n",min(calc(a[n/2]/2.00),calc(a[n/2+1]/2.00)));
	return 0;
}

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

const int INF=1e9;

bool LOCAL_TEST = false;
int h[30][30],v[30][30],u[30][30];
double priority[30][30];
vector<string> paths;
vector<int> dis,a(1000);
vector<double> e(1000),dis_ave;
map<int,char> mp;
map<char,int> mov;

inline void InitRand(){
    srand((unsigned int)time(NULL));
}

void move(int si,int sj,int ti,int tj,string& path){
    int y=si,x=sj;
    while(y!=ti||x!=tj){
        if(y==ti){
            rep(i,abs(tj-x)) path+=mp[1];
            break;
        }
        else if(x==tj){
            rep(i,abs(ti-y)) path+=mp[0];
            break;
        }
        if(LOCAL_TEST){

        }else{
            int next;
            if(u[y+mov[mp[0]]][x]==INF&&u[y][x+mov[mp[1]]]==INF) next=rand()%2;
            else if(u[y+mov[mp[0]]][x]==INF) next=0;
            else if(u[y][x+mov[mp[1]]]==INF) next=1;
            else{
                if(u[y+mov[mp[0]]][x]<=u[y][x+mov[mp[1]]]) next=0;
                else next=1;
            }
            path+=mp[next];
            if(next==0){
                if(y<ti) y++;
                else y--;
            }else{
                if(x<tj) x++;
                else x--;
            }
        }
    }
    if(LOCAL_TEST){
        y=si,x=sj;
        int d=0;
        rep(i,path.size()){
            if(path[i]=='U') d+=v[y-1][x],y--;
            if(path[i]=='D') d+=v[y][x],y++;
            if(path[i]=='L') d+=h[y][x-1],x--;
            if(path[i]=='R') d+=h[y][x],x++;
        }
        dis.push_back(d);
    }
}

void draw_cost(int si,int sj,int ti,int tj,int a,string path){
    int y=si,x=sj;
    u[y][x]=min(u[y][x],a);
    rep(i,path.size()){
        if(path[i]=='U') y--;
        if(path[i]=='D') y++;
        if(path[i]=='L') x--;
        if(path[i]=='R') x++;
        u[y][x]=min(u[y][x],a);
    }
}

void read_s_t_a_e(int k){
    int si,sj,ti,tj;
    cin>>si>>sj>>ti>>tj>>a[k]>>e[k];
    string path;
    if(si<=ti) mp[0]='D';
    else mp[0]='U';
    if(sj<=tj) mp[1]='R';
    else mp[1]='L';
    move(si,sj,ti,tj,path);
    paths.push_back(path);
    dis_ave.push_back((double)dis[k]/(abs(ti-si)+abs(tj-sj)));
}

void read_s_t(){
    int si,sj,ti,tj,a;
    cin>>si>>sj>>ti>>tj;
    string path;
    if(si<=ti) mp[0]='D';
    else mp[0]='U';
    if(sj<=tj) mp[1]='R';
    else mp[1]='L';
    move(si,sj,ti,tj,path);
    cout<<path<<endl;
    flush(cout);
    cin>>a;
    a/=(int)path.size();
    draw_cost(si,sj,ti,tj,a,path);
}

int main(){
    rep(i,30) fill(priority[i],priority[i]+30,-1);
    mov['U']=-1;
    mov['D']=1;
    mov['L']=-1;
    mov['R']=1;
    if(LOCAL_TEST){
        rep(i,30) rep(j,29) cin>>h[i][j];
        rep(i,29) rep(j,30) cin>>v[i][j];
    }else{
        rep(i,30) fill(u[i],u[i]+30,INF);
    }
    rep(k,1000){
        if(LOCAL_TEST) read_s_t_a_e(k);
        else read_s_t();
    }
    if(LOCAL_TEST){
        ofstream output("out.txt");
        rep(k,1000) output<<paths[k]<<'\n';
        rep(k,1000) cout<<k<<" "<<dis[k]<<" "
        <<(double)a[k]/dis[k]<<" "<<dis[k]*e[k]<<" "<<dis_ave[k]<<endl;
    }
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, st, n) for (int i = (st); i < (n); ++i)
#define rrep(i, n) for (int i > (n)-1; i >= 0; ++i)
#define RREP(i, st, n) for (int i = (st); i > (n); ++i)
using namespace std;
using ll = int64_t;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;

template <class T>
class Matrix {
 public:
  int h, w;
  V<V<T>> data;
  Matrix(int h_, int w_ = 0, T v = 0) {
    h = h_;
    w = (w != 0) ? w_ : h;
    data.resize(h);
    rep(i, h) data[i].resize(w, v);
  }
  Matrix(const V<V<T>>& data_)
      : h(data_.size()), w(data_[0].size), data(data_) {}
  Matrix(initializer_list<initializer_list<T>> list) {
    data.resize(list.size());
    int cnt = 0;
    for (auto row : list) data[cnt++] = V<T>(row);
    h = data.size(), w = data[0].size();
  }
  Matrix(const Matrix& a) : h(a.h), w(a.w), data(a.data) {}
  Matrix& operator=(const Matrix& a) {
    h = a.h, w = a.w, data = a.data;
    return *this;
  }
  V<T>& operator[](int n) { return data[n]; }
  Matrix& operator*=(Matrix& a) {
    V<V<T>> res(h, V<T>(a.w));
    rep(i, h) rep(j, a.w) {
      rep(k, w) res[i][j] += data[i][k] * a[k][j];
    }
    swap(data, res);
    w = a.w;
    return *this;
  }
  Matrix operator*(Matrix& a) { return Matrix(*this) *= a; }
  Matrix& operator+=(Matrix& a) {
    rep(i, h) rep(j, w) data[i][j] += a[i][j];
    return *this;
  }
  string str() {
    ostringstream oss;
    rep(i, h) {
      rep(j, w) oss << data[i][j] << " ";
      if (i != h - 1) oss << "\n";
    }
    return oss.str();
  }
};

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

  int n, m;
  cin >> n;

  V<P> xy(n);
  rep(i, n) cin >> xy[i].first >> xy[i].second;

  V<Matrix<ll>> ops;
  Matrix<ll> now = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
  ops.push_back(now);
  Matrix<ll> clk = {{0, 1, 0}, {-1, 0, 0}, {0, 0, 1}};
  Matrix<ll> clw = {{0, -1, 0}, {1, 0, 0}, {0, 0, 1}};
  Matrix<ll> f3 = {{-1, 0, 2}, {0, 1, 0}, {0, 0, 1}};
  Matrix<ll> f4 = {{1, 0, 0}, {0, -1, 2}, {0, 0, 1}};

  cin >> m;
  rep(i, m) {
    int op;
    ll p;
    cin >> op;
    if (op > 2) cin >> p;

    if (op == 1) {
      now = clk * now;
    } else if (op == 2) {
      now = clw * now;
    } else if (op == 3) {
      f3[0][2] = 2 * p;
      now = f3 * now;
    } else if (op == 4) {
      f4[1][2] = 2 * p;
      now = f4 * now;
    }
    ops.emplace_back(now);
  }

  int Q;
  cin >> Q;
  rep(i, Q) {
    int a, b;
    cin >> a >> b;
    b--;
    Matrix<ll> pos = {{xy[b].first}, {xy[b].second}, {1}};
    auto ans = ops[a] * pos;
    ll x = ans[0][0], y = ans[1][0];
    cout << x << " " << y << "\n";
  }
}

//#include <atcoder/maxflow.hpp>
#include <memory>
#include <iostream>
#include <map>
#include <list>
#include <set>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <fstream>
#include <iterator>
#include <random>
#include <chrono>
#include <complex>
#include <thread>

#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s

typedef long long ll;

using namespace std;
//using namespace atcoder;

ll modd = 1000 * 1000 * 1000 + 7;

int main()   {

    ios_base::sync_with_stdio(false);

    cout.precision(17);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no

    auto start = chrono::steady_clock::now();

//    readint(test_cases);
    int test_cases = 1;
    forr(t, 1, test_cases) {
        readint(x);
        cout << (x>=0 ? x : 0) << endl;
    }
//        auto stop = chrono::steady_clock::now();
//        auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
//        cout << "Duration: " << duration.count() << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll> pql;
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, k, n) for(ll i = ll(k); i <= ll(n); i++)
#define rep3(i, n, k) for(ll i = ll(n); i >= ll(k); i--)
#define all(v) (v).begin(), (v).end()
ll mod(ll a, ll b) {if(b == 0) return 0; return (a % b + b) % b;}
bool chmin(ll &a, ll b) {if(b < a) {a = b; return 1;} return 0;}
bool chmax(ll &a, ll b) {if(b > a) {a = b; return 1;} return 0;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll MAX = 2e5;
const ld eps = 1e-9;
const char newl = '\n';

ll modpow(ll n) {
  if(n==0) return 1;
  else if(n%2==0) return modpow(n/2)*modpow(n/2)%MOD;
  else return 2*modpow(n-1)%MOD;
}

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

  ll n;
  char caa, cab, cba, cbb;
  cin >> n >> caa >> cab >> cba >> cbb;

  if(n==2) cout << 1;

  else if((caa=='A' && cab=='A') || (cab=='B' && cbb=='B')) {
    cout << 1;
  }

  else if((cab=='B' && cba=='A' && cbb=='A') || (caa=='B' && cab=='A' && cba=='B')) {
    cout << modpow(n-3);
  }

  else if((cab=='B' && cba=='B' && cbb=='A') || (caa=='B' && cab=='A' && cba=='A')) {
    vvl bc(n-1, vl(n-1));
    rep(i, n-1) {
      bc[i][0] = 1;
      bc[i][i] = 1;
    }
    rep(i, n-1) rep2(j, 1, i-1) bc[i][j] = (bc[i-1][j-1] + bc[i-1][j]) % MOD;
    ll ans = 0;
    rep(k, n/2) (ans += bc[n-2-k][k]) %= MOD;
    cout << ans;
  }

  else return -1;

  cout << newl;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long
int max(int a,int b)
{
	if(a>b)
		return a;
	return b;
}
int min(int a,int b)
{
	if(a<b)
		return a;
	return b;
}
signed main() {
 ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

 string s;
 cin>>s;
 map<int,int> mp;
 int x=0;
 for(auto i:s){
 	mp[(i-'0')%3]++;
 	x+=(i-'0');
 }
 x%=3;
 int ans=s.size();
 if(x==1)
 {
 	if(mp[1]>=1)
 	ans=min(ans,1);
  	if(mp[2]>=2)
  		ans=min(ans,2);
 }
 else if(x==2)
 {
 	if(mp[1]>=2)
 	ans=min(ans,2);
 	if(mp[2]>=1)
 	ans=min(ans,1);
 }
 else
 	ans=0;
 if(ans==s.size())
 	ans=-1;

 cout<<ans;
}


//#include "bits/stdc++.h"

#define _USE_MATH_DEFINES

#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <functional>
#include <utility>
#include <tuple>
#include <vector>
#include <string>
#include <list>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <queue>
#include <deque>
#include <stack>
#include <iterator>
#include <bitset>
#include <complex>
#include <limits>
#include <random>
#include<fstream>
#include<array>

using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;

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

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;

const double EPS = 1e-9;

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

	int T;
	cin >> T;
	while (T--)
	{
		int N, A, B;
		cin >> N >> A >> B;

		int a = (N - A + 1)*(N - A + 1);
		int b = (N - B + 1)*(N - B + 1);
		a %= MOD;
		b %= MOD;

		int ans = a * b;
		ans %= MOD;

		int x = max(0LL, N - A - B + 1);
		x = x * (x + 1) / 2;
		x %= MOD;
		x *= 2;
		x %= MOD;

		int y = (N - A + 1)*(N - B + 1);
		y = (y - x + MOD) % MOD;
		y = y * y;
		y %= MOD;

		ans = (ans - y + MOD) % MOD;

		cout << ans << endl;
	}

	return 0;
}

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

int main(){
  int64_t a1,b1,c1,C,n,day;
  int64_t fee=0,feeall=0;
  vector<pair<int, int>> event;
  cin>>n>>C;
  for(int64_t i=0;i<2*n;i+=2){
    cin>>a1>>b1>>c1;
    event.push_back(make_pair(a1,c1));
    event.push_back(make_pair(b1+1,-1*c1));
  }
  sort(event.begin(),event.end());
  day=event[0].first;
  fee=event[0].second;
 // cout<<event[0].first<<"  "<<event[0].second<<endl;
  for(int i=1;i<2*n;i++){
    day=event[i].first-event[i-1].first;
    if(fee<C){
      feeall+=fee*day;
    }else{
      feeall+=C*day;
    }
    fee+=event[i].second;
   // cout<<event[i].first<<"  "<<event[i].second<<" "<<feeall<<endl;
  }
  cout<<feeall<<endl;
}


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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>  // Including tree_order_statistics_node_update

using namespace __gnu_pbds;
using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define minei(x) min_element(x.begin(), x.end()) - (x).begin()
#define maxei(x) max_element(x.begin(), x.end()) - (x).begin()
#define LSOne(S) (S & (-S))

#define uns(v) sort((v).begin(), (v).end()), v.erase(unique(v.begin(), v.end()), v.end())
#define acusum(x) accumulate(x.begin(), x.end(), 0)
#define acumul(x) accumulate(x.begin(), x.end(), 1, multiplies<int>());
#define bits(x) __builtin_popcount(x)
#define inf 1000000000
#define MAXN 1000007
#define MOD 1000000007
#define debug(x) cerr << (#x) << " = " << (x) << '\n';

const double pi = acos(-1.0);
const double eps = 1e-11;

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

typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

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

void pv(vector<ll> v) {
    if (v.empty()) return;
    cout << v[0];
    for (int i = 1; i < sz(v); i++) {
        cout << " " << v[i];
    }
    cout << '\n';
}
ll gtSum(ll n) {
    return n * (n + 1) / 2;
}
void _run_test() {
    int a, b;
    vector<ll> res;
    cin >> a >> b;
    ll cur = 1;
    if (a == b) {
        while (a--) {
            res.pb(cur);
            res.pb(-1 * cur);
            cur++;
        }

    } else if (a > b) {
        for (int i = 0; i < b - 1; i++) {
            res.pb(cur);
            res.pb(-1 * cur);
            cur++;
        }
        res.pb(-1 * (gtSum(a) - gtSum(b - 1)));
        for (int i = b - 1; i < a; i++) {
            res.pb(cur++);
        }

    } else {
        for (int i = 0; i < a - 1; i++) {
            res.pb(cur);
            res.pb(-1 * cur);
            cur++;
        }
        res.pb((gtSum(b) - gtSum(a - 1)));
        for (int i = a - 1; i < b; i++) {
            res.pb(-1 * cur);
            cur++;
        }
    }
    pv(res);
}
int main() {
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    // cin >> t;
    while (t--) {
        _run_test();
    }

    return 0;
}

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define replr(i, l, r) for (int i = (l); i < (r); ++i)
const int mod = 1e9 + 7;
int main() {
    int a, b;
    cin >> a >> b;
    int aa = 1, bb = -1;
    if (a < b)
        swap(a, b), swap(aa, bb);
    vector<int> A(a);
    A[0] = 1;
    rep(i, a - 1) { A[i + 1] = A[i] + 1; }
    int sum = 0;
    replr(i, b - 1, a) sum += A[i];

    rep(i, a) {
        if (i < b - 1)
            cout << bb * A[i] << " ";
        cout << aa * A[i] << " ";
    }
    cout << bb * sum << endl;

    return 0;
}


#include<bits/stdc++.h>

#define ld double long
#define ll long long
#define ull unsigned long long
#define all(A) A.begin(), A.end()
#define pb push_back
#define fri(start , end , step)  for(int i = start; i < end ; i = i + step)
#define frj(start , end , step)  for(int j = start; j < end ; j = j + step)
#define fast ios_base::sync_with_stdio(0); cin.tie(0)
#define debug(x) cerr<<#x<<": "<<x<<endl;

using namespace std;

void pv(vector<ll>& vec ){
    fri(0 , vec.size() , 1) cout << vec[i] << " ";
    cout << "\n";
}
multiset<string> A;
mt19937 rng(time(0));
char ans[20][20];

string ss = "ABCDEFGH";

pair<int  , multiset<string>> check(multiset<string> tec , int i1 , int j1){

	int cof = 0;
	fri(0 , 20 , 1){
		string s;
		frj(i , i + 12 , 1){
			if(ans[i1][j % 20] != '0' ) s += ans[i1][j % 20];
			else break;
			int c = tec.count(s) ;
			if(c) {
				cof += c;
				tec.erase(s);
				}
			}

		}
	fri(0 , 20 , 1){
		string s;
		frj(i , i + 12 , 1){
			if(ans[j % 20][j1] != '0' ) s += ans[j % 20][j1];
			else break;
			int c = tec.count(s) ;
			if(c) {
				cof += c;
				tec.erase(s);
				}
			}

		}
	return {cof , tec};

	}

bool vis[20][20];
//
signed main(){
	fast;
    int n , m;
    cin >> n >> m;
	map<char ,  int > M;
	fri(0 , 20   , 1){
		frj(0 , 20 , 1){
			ans[i][j] = '0';
			}
		}

    fri(0 , m , 1 ){
		string s;
		cin >> s;
		for(char x : s) M[x] ++;
		A.insert(s);
		}

    queue<pair<int , int>> q;
    q.push({0 , 0 });

    while(!q.empty()){
		int i = q.front().first ;
		int j = q.front().second;
		//debug(i);
		//debug(j);
		vis[i][j] = 1;
		q.pop();
		multiset<string> good;
		int mx = -1;
		char best;

		for(char x : ss){
			ans[i][j] = x;
			pair<int  , multiset<string>> f = check(A, i , j);
			int ls= mx;
			mx = max(mx , f.first);
			if(ls != mx) {
				good= f.second;
				best = x;
				}
			}

		if(mx == 0){
			int y = rng() % 8;
			best = ss[y];

			}
		ans[i][j] = best;
		/*for(int i1 = 0; i1 < 20 ; i1 ++){
			for(int j1 = 0; j1 < 20 ; j1 ++){
				cout << ans[i1][j1];
				}
			cout << endl;
			}
		cout << endl;
		cout << i << " " << j << endl;*/

		A = good;
		if(i < 19 && vis[i+  1][j] == 0) {
			q.push({i + 1 , j } );
			vis[i + 1][j ] = 1;
			}
		if(j < 19 && vis[i][j+ 1] == 0) {
			q.push({i , j + 1 } );
			vis[i][j + 1] = 1;
			}
		}

	fri(0 , 20 , 1){
		frj(0 , 20 , 1) cout << ans[i][j] ;
		cout << "\n";
		}
    return 0;
}

#pragma GCC optimize("Ofast")

#define _GLIBCXX_DEBUG

#include <bits/stdc++.h>

#include <sys/time.h>

using namespace std;

class XorShift128 {
private:
	uint32_t x, y, z, w;

public:
	XorShift128() : x(123456789), y(362436069), z(521288629), w(88675123) { }
	uint32_t rnd() {
		uint32_t t = x ^ (x << 11);
		x = y;
		y = z;
		z = w;
		return w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
	}

	uint32_t rnd(const int n) {
		return rnd() % n;
	}

	uint32_t rnd(const int l, const int r) { // [l, r]
		return rnd() % (r - l + 1) + l;
	}
};

long long get_time() {
	struct timeval tv;
	gettimeofday(&tv, NULL);
	long long result =  tv.tv_sec * 1000LL + tv.tv_usec / 1000LL;
	return result;
}

XorShift128 xor_shift_128;
long long start;

const int NIL = -1;

const int dh[2] = {0, 1};
const int dw[2] = {1, 0};

class Solver {
	int N, M;
	vector<string> strs;

	vector<vector<int>> datas;

	vector<vector<int>> ans;
public:
	Solver();
	void run();
private:
	int compute_score() const;

	void output_answer() const;
};

Solver::Solver() {
	cin >> N >> M;
	strs.resize(M);
	for(int i = 0; i < M; ++i) {
		cin >> strs[i];
	}

	datas.resize(M);
	for(int i = 0; i < M; ++i) {
		for(int j = 0; j < strs[i].size(); ++j) {
			int x = strs[i][j] - 'A';
			datas[i].emplace_back(x);
		}
	}

	ans.resize(N);
	for(int hi = 0; hi < N; ++hi) {
		for(int wi = 0; wi < N; ++wi) {
			ans[hi].emplace_back(xor_shift_128.rnd(8));
		}
	}
}

void Solver::run() {
	cerr << compute_score() << endl;

	output_answer();
}

int Solver::compute_score() const {
	int cnt = 0;

	for(const vector<int>& data : datas) {
		for(int i = 0; i < 2; ++i) {
			for(int hi = 0; hi < N; ++hi) {
				for(int wi = 0; wi < N; ++wi) {
					bool match = true;

					for(int j = 0; j < data.size(); ++j) {
						int h = (hi + j * dh[i]) % N;
						int w = (wi + j * dw[i]) % N;
						if(ans[h][w] != data[j]) {
							match = false;
							break;
						}
					}

					if(match) {
						++cnt;
						goto SEARCH_END;
					}
				}
			}
		}

		SEARCH_END:
		cnt;
	}

	if(cnt < M) {
		return round(1e8 * cnt / M);
	} else {
		int dots = 0;
		for(int hi = 0; hi < N; ++hi) {
			for(int wi = 0; wi < N; ++wi) {
				dots += ans[hi][wi] == -1;
			}
		}

		return round(1e8 * 2 * N * N / (2 * N * N - dots));
	}
}

void Solver::output_answer() const {
	for(int hi = 0; hi < N; ++hi) {
		for(int wi = 0; wi < N; ++wi) {
			if(ans[hi][wi] == NIL) {
				cout << '.';
			} else {
				cout << char(int('A') + ans[hi][wi]);
			}
		}
		cout << endl;
	}
}
int main() {
	Solver solver;
	solver.run();
}


//{{{
#include<algorithm>
#include<cmath>
#include<deque>
#include<fstream>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<random>
#include<set>
#include<sstream>
#include<sys/time.h>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(printde) FUNC(printdbe) FUNC(printdwe) FUNC(printdu) FUNC(printe) FUNC(printe0);
#endif
template <template <class T, class = std::allocator<T>> class V, class E>
istream& operator >> (istream& in, V<E>& v){ for(auto& e : v){ in >> e; } return in; }
//}}}

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    int n; cin >> n;
    set<int> st;
    for(int i = 0; i < n; i++){
        int a; cin >> a;
        st.insert(a);
    }
    while(st.size() > 1){
        int x = *st.begin();
        int X = *st.rbegin();
        st.erase(X);
        int X2 = X % x;
        st.insert(X2 ? X2 : x);
    }
    cout << *st.begin() << '\n';
}

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <assert.h>
#include <sys/time.h>
#include <fstream>

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  FOR(i,0,n)
#define REP(i,n)  FOR(i,0,n)
#define each(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define EACH(i,c) for(auto i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define deb(x) cerr << #x << " = " << (x) << " , ";
#define debl cerr << " (L" << __LINE__ << ")"<< endl;
#define sz(s) (int)((s).size())

#define clr(a) memset((a),0,sizeof(a))
#define nclr(a) memset((a),-1,sizeof(a))
#define pb push_back
#define INRANGE(x,s,e) ((s)<=(x) && (x)<(e))
#define MP(x,y) make_pair((x),(y))

double pi=3.14159265358979323846;

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template<typename T> std::ostream& operator<<(std::ostream& os, const vector<T>& z){
	os << "[ ";
	REP(i,z.size())os << z[i] << ", " ;
	return ( os << "]" << endl);
}

template<typename T> std::ostream& operator<<(std::ostream& os, const set<T>& z){
	os << "set( ";
	EACH(p,z)os << (*p) << ", " ;
	return ( os << ")" << endl);
}

template<typename T,typename U> std::ostream& operator<<(std::ostream& os, const map<T,U>& z){
	os << "{ ";
	EACH(p,z)os << (p->first) << ": " << (p->second) << ", " ;
	return ( os << "}" << endl);
}

template<typename T,typename U> std::ostream& operator<<(std::ostream& os, const pair<T,U>& z){
	return ( os << "(" << z.first << ", " << z.second << ",)" );
}

double get_time(){
	struct timeval tv;
	gettimeofday(&tv, NULL);
	return tv.tv_sec + tv.tv_usec*1e-6;
}

typedef unsigned int uint32_t;
struct RND{
	uint32_t x;
	uint32_t y;
	uint32_t z;
	uint32_t w;
	RND(){
		x=123456789;
		y=362436069;
		z=521288629;
		w=88675123;
	}
	void init(int seed){
		x=123456789;
		y=362436069;
		z=521288629;
		w=seed+100;
		REP(i,10)get();
	}
	uint32_t get(){
		uint32_t t;
		t=x^(x<<11);
		x=y;y=z;z=w;
		w=(w^(w>>19))^(t^(t>>8));
		return w;
	}
};
RND rnd;

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

void _main(istream &inp){
	ll N;
	ll ret = 0;
	inp  >> N;
	rep(j,N){
		ll a;
		inp >> a;
		ret = gcd(ret,a);
		//deb(a);deb(ret);debl;
	}
	cout <<  ret << endl;

}

int main(){
	if(0){
		ifstream ifs("test.txt");
		_main(ifs);
	}
	else{
		_main(cin);
	}
	return 0;
}


#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define PI 3.14159265359
#define rep(i, init, max) for (int i = (init); i < (int)(max); i++)
#define eqrep(i, init, max) for (int i = (init); i <= (int)(max); i++)
#define veccin(name) rep(i, 0, name.size()) cin >> name[i]
#define vecsort(name) sort(name.begin(), name.end())
#define vecsortg(name) sort(name.begin(), name.end(), greater<int>())
#define debug(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
template <typename T> T re() {
  T a;
  cin >> a;
  return a;
}
#pragma GCC diagnostic               ignored "-Wunused-value"
template <class T, class... Ts> void print(const T& a, const Ts&... b) {
  cout << a;
  (cout << ... << (cout << ' ', b));
  cout << endl;
}

int main() {
  vector<int> in(3);
  veccin(in);
  vecsortg(in);
  print(in[0] + in[1]);
  return 0;
}

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


/*---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---*/

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

#define rep(a, b)      for(int a = 0; a < (b); ++a)
#define rep1(a, b, c)  for(int a = (b); a < (c); ++a)
#define rrep(a, b, c)  for(int a = (b); a > c; --a)
#define each(a, b)     for(auto& a : b)

#define sz(x)       (int)(x).size()
#define all(a)      (a).begin(),(a).end()
#define rall(a)     (a).rbegin(), (a).rend()

#define lb lower_bound
#define ub upper_bound
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define dbg(x) cout << #x << " = " << x << endl;
#define vt vector
#define pii pair<int, int>
#define pll pair<ll, ll>
#define bitcnt(x) __builtin_popcount(x)
#define fbo(x) find_by_order(x)
#define ook(x) order_of_key(x)

template <typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T> bool umin(T& a, const T& b){
	return b<a?a=b, 1:0;
}
template <class T> bool umax(T& a, const T& b){
	return a<b?a=b, 1:0;
}

using ll = long long;
using ld = long double;
using str = string;

const int inf = (int)1e9 + 5;
const ll infl = (ll)1e18 + 5;
const int mod = 1e9 + 7;

const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};

/*---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---XXX---*/

struct UnionFindTree{
	vt<int> parent;
	vt<int> siz;

	UnionFindTree(int n){
		parent.resize(n);
		siz.resize(n, 1);
		rep(i, n) parent[i] = i;
	}

	int root(int x){
		return (x==parent[x]?x:parent[x]=root(parent[x]));
	}

	void unite(int x, int y){
		x = root(x); y = root(y);
		if(x==y) return;
		if(siz[x] < siz[y]) swap(x, y);
		siz[x] += siz[y];
		parent[x] = y;
	}

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

const int N = 100;
int n;
int x[N], y[N];

bool go(double m){
	UnionFindTree uft(n+2);
	rep(i, n){
		if(abs(100 - y[i]) <= 2*m) uft.unite(i, n);
		if(abs(100 + y[i]) <= 2*m) uft.unite(i, n+1);
		rep1(j, i+1, n){
			double d = sqrt(pow(x[i]-x[j], 2) + pow(y[i]-y[j], 2));
			if(d <= 2*m) uft.unite(i, j);
		}
	}
	return !(uft.same(n, n+1));
}

void solve(){
	cin >> n;
	rep(i, n) cin >> x[i] >> y[i];

	double l = 0, r = 100, m;
	while(r - l > 1e-6){
		m = (l+r)/2;
		if(go(m)) l = m;
		else r = m;
	}
	cout << fixed <<  setprecision(6) << l << '\n';
}

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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>

using namespace std;

const int maxn = 100 + 5;
const int modu = 1e9 + 7;
const double eps = 1e-5;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

typedef long long LL;

int n;
int nails[maxn][2];
bool g[205][205], vis[205];

bool dfs(int x) {
    vis[x] = true;
    if (x == n+1) return true;
    for (int i = 0; i <= n+1; ++i) {
        if (!vis[i] && g[x][i])
            if (dfs(i)) return true;
    }
    return false;
}

double dist2(int i, int j) {
    double x = nails[i][0] - nails[j][0];
    double y = nails[i][1] - nails[j][1];
    return x*x + y*y;
}

bool check(double radius) {
    memset(vis, 0, sizeof(vis));
    memset(g, 0, sizeof(g));
    for (int i = 0; i < n; ++i) {
        for (int j = i+1; j < n; ++j) {
            if (radius*radius*4 > dist2(i, j))
                g[i][j] = g[j][i] = true;
        }
        if (nails[i][1] < radius*2) g[i][n] = g[n][i] = true;
        if (200 - nails[i][1] < radius*2) g[i][n+1] = g[n+1][i] = true;
    }
    return !dfs(n);
}

double bs(double l, double r) {
    while (r-l >= eps) {
        double mid = (l+r) / 2;
        if (check(mid)) l = mid;
        else r = mid;
    }
    // for (int i = 0; i <= 200; ++i) {
    //     for (int j = 0; j <= 200; ++j)
    //         printf("%d", g[i][j]);
    //     printf("\n");
    // }
    return l;
}

int main() {
    // freopen("my.txt", "w", stdout);
	cin >> n;
    for (int i = 0; i < n; ++i) {
        scanf("%d%d", &nails[i][0], &nails[i][1]);
        nails[i][0] += 100;
        nails[i][1] += 100;
    }
    cout << bs(0, 100) << endl;
	return 0;
}


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

using ll = long long;
using vll = vector<ll>;
using vpl = vector<pair<ll, ll>>;
using pll = pair<ll, ll>;
#define rep(i, k, n) for(ll i = k; i < n; i++)
#define pb push_back
#define mp make_pair

ll n, m, s; //頂点の数, 辺の数, スタート地点
vll A(3010);  //辺の始点
vll B(3010);  //辺の終点
vll C(3010);  //辺のコスト
vll dist(3010);   //最短距離
vector<vpl> G(3010, vpl(0));  //隣接する頂点とコストをセットにした記憶したもの
priority_queue<pll, vpl, greater<pll>> Q;

int main(){
    cin >> n >> m;
    rep(i, 1, m+1){
        cin >> A[i] >> B[i] >> C[i];
        G[A[i]].pb(mp(C[i], B[i]));
    }

    rep(i, 1, n+1){
        //距離の初期化
        rep(j, 0, 3010){
            dist[j] = 1000000007;
        }

        //帰ってくる場所の追加
        rep(j, 1, n+1){
            rep(k, 0, G[j].size()){
                if(G[j][k].second == i){
                    G[j].pb(mp(G[j][k].first, 3000));
                }
            }
        }

        //優先度付きキューに(距離, 頂点番号)の組を入れる
        Q.push(mp(0, i));
        dist[i] = 0;

        while(!Q.empty()){
            ll pos = Q.top().second;
            Q.pop();
            rep(j, 0, G[pos].size()){
                ll to = G[pos][j].second;
                ll cost = G[pos][j].first;
                //posを経由してtoに行く距離がdist[to]より短ければ
                //dist[to]を更新してキューに入れる
                if(dist[to] > dist[pos] + cost){
                    dist[to] = dist[pos] + cost;
                    Q.push(mp(dist[to], to));
                }
            }
        }

        //帰ってくる場所の削除
        rep(j, 1, n+1){
            ll rec = 0;
            rep(k, 0, G[j].size()){
                if(G[j][k].second == 3000){
                    rec++;
                }
            }

            rep(k, 0, rec){
                G[j].pop_back();
            }
        }

        // cout << i << " : ";
        // rep(j, 0, 3010){
        //     if(dist[j] != 1000000007){
        //         cout << "{" << j << ", " << dist[j] << "}";
        //     }
        // }

        if(dist[3000] != 1000000007){
            cout << dist[3000] << endl;
        }
        else{
            cout << -1 << endl;
        }

    }

    // rep(i, 1, n+1){
    //     cout << i << " : ";
    //     rep(j, 0, G[i].size()){
    //         printf(" {%lld, %lld},", G[i][j].second, G[i][j].first);
    //     }
    //     cout << endl;
    // }

}

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <queue>
#include <limits.h>
#include <stack>
using namespace std;
typedef long long ll;
const int maxn = 110;
int H, W, A, B, ans = 0;
void solve(int s, int bit, int a, int b)
{
	if(s==H*W)
	{
		ans++;
		return ;
	}
	if(bit&(1<<s)) return solve(s+1, bit, a, b);
	if(a)
	{
		if(s%W!=W-1 && !(bit&(1<<(s+1)))) solve(s+1, bit|(1<<s)|(1<<(s+1)), a-1, b);
		if(s+W<H*W) solve(s+1, bit|(1<<s)|(1<<(s+W)), a-1, b);
	}
	if(b) solve(s+1, bit|(1<<s), a, b-1);
}
int main()
{
	//freopen("/Users/zhangkanqi/Desktop/11.txt", "r", stdin);
	cin >> H >> W >> A >> B;
	solve(0, 0, A, B);
	cout << ans << endl;
	return 0;
}


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

int main()
{
    int r,c;
    cin>>r>>c;
    vector<vector<char>>vec(r,vector<char>(c));
    for(int i=0;i<r;i++)
    {
        string s;
        cin>>s;
        for(int j=0;j<c;j++)
            vec[i][j]=s[j];
    }
    int ans=0;
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            if(vec[i][j]=='.' && j+1<c && vec[i][j+1]=='.')
                ans+=1;
           if(vec[i][j]=='.' && i+1<r && vec[i+1][j]=='.')
                ans+=1;
       }
    }
    cout<<ans<<endl;
}

#include "iostream"
#include "algorithm"
#include "cstring"
#include "cstdio"
#include "cmath"
#include "vector"
#include "map"
#include "set"
#include "queue"
#include "string"
#include "unordered_map"
#include "assert.h"
using namespace std;
#define MAXN 200006
//#define int long long
#define rep( i , a , b ) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per( i , a , b ) for (int i = (a), i##end = (b); i >= i##end; --i)
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define all( x ) (x).begin() , (x).end()
#define mem( a ) memset( a , 0 , sizeof a )
typedef long long ll;
#define P 998244353
int n;
char ch[MAXN];
int A[MAXN];
map<int,int> M;
void solve( ) {
	cin >> n;scanf("%s",ch + 1);
	int res = 0;
	M[0] ++;
	rep( i , 1 , n ) {
		if( ch[i] == 'A' ) A[i] = A[i - 1] + 10000;
		if( ch[i] == 'T' ) A[i] = A[i - 1] - 10000;
		if( ch[i] == 'G' ) A[i] = A[i - 1] + 1;
		if( ch[i] == 'C' ) A[i] = A[i - 1] - 1;
		res += M[A[i]];
		M[A[i]] ++;
	}
	cout << res << endl;
}

signed main( ) {
	//   freopen("input","r",stdin);
//    freopen("fuckout","w",stdout);
//	int T;cin >> T;while( T-- ) solve();
	solve( );
}


#include <bits/stdc++.h>
using namespace std;
int main(){
	long n;
	cin >> n;
	set<long> ans;
	for(long d=1;d*d<=n;d++){
		if(n%d==0){
			ans.insert(d);
			ans.insert(n/d);
		}
	}

	for(auto x:ans)cout << x << endl;
}

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using pi = pair<int, int>;
using vpi = vector<pair<int, int>>;
using pl = pair<ll, ll>;
using vl = vector<ll>;

#define all(v) (v).begin(), (v).end()
#define ar array
#define PB push_back
#define sz(x) (int)(x).size()

template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
    return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
    os << '{';
    string sep;
    for (const T &x : v)
        os << sep << x, sep = ", ";
    return os << '}';
}
void dbg_out()
{
    cerr << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
    cerr << ' ' << H;
    dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

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

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7; //998244353

void solve()
{
	ll n;
	cin >> n;
	ll l = 1, r = 1e6, ans = 0;
	while(l<=r){
		ll m = l+(r-l)/2;
		if (m*(m+1)/2>=n){
			ans = m;
			r = m-1;
		}else l = m+1;
	}
	cout << ans << "\n";
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int testcase=1;
    //cin >> testcase;
    while (testcase--)
    {
        solve();
    }
}


//g++  7.4.0

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MAXN 100005
ll C[MAXN];
ll D[MAXN];
vector<pair<ll,ll>> adj[MAXN];
ll dis[MAXN];

ll getDis(ll t, ll c, ll d)
{
    return(t + c + d/(t + 1LL));
}

void dijkstra(ll source)
{
    set<pair<ll,ll>> s;
    s.insert({dis[source], source});

    while(!s.empty())
    {
        pair<ll,ll> p = *s.begin();
        s.erase(p);
        ll u = p.second;
        //cout<<u<<endl;

        for(auto it: adj[u])
        {
            ll v = it.first;
            ll index = it.second;
            ll c = C[index];
            ll d = D[index];

            ll wait = max(0LL, (ll)sqrt(d) - dis[u]);
            ll ans = LLONG_MAX;
            for(ll i=0;i<=3;++i)
            {
                ll w = max(0LL, wait + i);
                ans = min(ans, getDis(w + dis[u], c, d));
                w = max(0LL, wait - i);
                ans = min(ans, getDis(w + dis[u], c, d));
            }

            //cout<<u<<" "<<v<<" "<<ans<<endl;

            if(dis[v] > ans)
            {
                s.erase({dis[v], v});
                dis[v] = ans;
                s.insert({dis[v], v});
            }
        }
    }
}

//Shit Man: There can be multiple edges as well
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

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

    for(ll i=1;i<=N;++i)
        dis[i] = LLONG_MAX;

    for(ll i=0;i<M;++i)
    {
        ll u,v; cin>>u>>v;
        adj[u].push_back({v, i});
        adj[v].push_back({u, i});
        cin>>C[i];
        cin>>D[i];
    }

    dis[1] = 0;
    dijkstra(1);

    if(dis[N] == LLONG_MAX)
        cout<<-1<<endl;
    else
        cout<<dis[N]<<endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)
#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);}
ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);}
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=ll(1e18)+ll(7);
const ll MOD=1000000007LL;
#define out(a) cout<<fixed<<setprecision((a))
//tie(a,b,c) = make_tuple(10,9,87);
#define pop_(a) __builtin_popcount((a))
ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;}
#define num_l(a,v) POSL(a,v) //v未満の要素数
#define num_eql(a,v) POSU(a,v) //v以下の要素数
#define num_h(a,v) (a.size() - POSU(a,v)) //vより大きい要素数
#define num_eqh(a,v) (a.size() - POSL(a,v)) //v以上の要素数
// static_cast< long long ll >

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

	vector<ll> G[N+1];
	vector<ll> A(M),B(M),C(M),D(M);

	FOR(i,0,M){
		cin >> A[i] >> B[i] >> C[i] >> D[i];
		G[A[i]].push_back(i);
		G[B[i]].push_back(i);
	}

	priority_queue<P, vector<P>, greater<P> > Q; //小さいものから出る

	Q.push({0,1});//{Time,vart}
	vector<int> did(N+1,0);
	vector<ll> T(N+1,INF);
	T[1] = 0;

	while(Q.size()){
		int u = Q.top().second;
		Q.pop();
		if(did[u])continue;
		did[u] = true;
		FOR(ind,0,G[u].size()){
			int v = B[G[u][ind]];
			if(v==u) v = A[G[u][ind]];
			if(did[v])continue;
			if(u==v)continue;

			ll t = sqrt(D[G[u][ind]]);
			ll at = INF;
			FOR(j,max(t-100,T[u]),max(t+100,T[u])+1){//出る時刻
				at = min(at, j + D[G[u][ind]]/(j+1) + C[G[u][ind]]);
			}
			//cout << at << " atat" << endl;
			if(T[v] > at){
				T[v] = at;
				Q.push({at,v});
			}

		}
	}

	if(T[N] == INF)T[N] = -1;
	cout << T[N] << endl;
	//FOR(i,0,N+1) cout << T[i] << " ";
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

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

    vector<ll> A(2*N);
    rep(i, 2*N) cin >> A[i];

    vector<ll> B(2*N);
    rep(i, N){
        B[2*i] = A[N-1-i], B[2*i+1] = A[N+i];
    }

    priority_queue<ll, vector<ll>, greater<ll>> que;
    ll S = accumulate(all(A), 0LL);

    rep(i, N){
        que.emplace(B[2*i]), que.emplace(B[2*i+1]);
        S -= que.top(); que.pop();
    }

    cout << S << '\n';
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;typedef double db;
typedef pair<int, int> pii;typedef pair<ll, ll> pll;
typedef pair<int,ll> pil;typedef pair<ll,int> pli;
#define Fi first
#define Se second
#define _Out(a)	cerr<<#a<<" = "<<(a)<<endl
const int INF = 0x3f3f3f3f, MAXN = 1e6 + 50;
const ll LINF = 0x3f3f3f3f3f3f3f3f, MOD = 998244353;
const db Pi = acos(-1), EPS = 1e-6;
void test(){cerr << "\n";}template<typename T,typename...Args>void test(T x,Args...args){cerr<<x<<" ";test(args...);}
inline ll qpow(ll a, ll b){return b?((b&1)?a*qpow(a*a%MOD,b>>1)%MOD:qpow(a*a%MOD,b>>1))%MOD:1;}
inline ll qpow(ll a, ll b,ll c){return b?((b&1)?a*qpow(a*a%c,b>>1,c)%c:qpow(a*a%c,b>>1,c)) %c:1;}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll cede(ll a,ll b){if(b<0)return cede(-a,-b);if(a<0)return a/b;return (a+b-1)/b;}
inline ll flde(ll a,ll b){if(b<0)return flde(-a,-b);if(a<0)return (a-b+1)/b;return a/b;}
inline int sign(db x){return x<-EPS ? -1:x>EPS;}
inline int dbcmp(db l,db r){return sign(l - r);}
namespace Fast_IO{ //orz laofu
    const int MAXL((1 << 18) + 1);int iof, iotp;
    char ioif[MAXL], *ioiS, *ioiT, ioof[MAXL],*iooS=ioof,*iooT=ioof+MAXL-1,ioc,iost[55];
    char Getchar(){
        if (ioiS == ioiT){
            ioiS=ioif;ioiT=ioiS+fread(ioif,1,MAXL,stdin);return (ioiS == ioiT ? EOF : *ioiS++);
        }else return (*ioiS++);
    }
    void Write(){fwrite(ioof,1,iooS-ioof,stdout);iooS=ioof;}
    void Putchar(char x){*iooS++ = x;if (iooS == iooT)Write();}
    inline int read(){
        int x=0;for(iof=1,ioc=Getchar();(ioc<'0'||ioc>'9')&&ioc!=EOF;)iof=ioc=='-'?-1:1,ioc=Getchar();
        if(ioc==EOF)Write(),exit(0);
        for(x=0;ioc<='9'&&ioc>='0';ioc=Getchar())x=(x<<3)+(x<<1)+(ioc^48);return x*iof;
    }
    inline long long read_ll(){
        long long x=0;for(iof=1,ioc=Getchar();(ioc<'0'||ioc>'9')&&ioc!=EOF;)iof=ioc=='-'?-1:1,ioc=Getchar();
        if(ioc==EOF)Write(),exit(0);
        for(x=0;ioc<='9'&&ioc>='0';ioc=Getchar())x=(x<<3)+(x<<1)+(ioc^48);return x*iof;
    }
    void Getstr(char *s, int &l){
        for(ioc=Getchar();ioc==' '||ioc=='\n'||ioc=='\t';)ioc=Getchar();
        if(ioc==EOF)Write(),exit(0);
        for(l=0;!(ioc==' '||ioc=='\n'||ioc=='\t'||ioc==EOF);ioc=Getchar())s[l++]=ioc;s[l] = 0;
    }
    template <class Int>void Print(Int x, char ch = '\0'){
        if(!x)Putchar('0');if(x<0)Putchar('-'),x=-x;while(x)iost[++iotp]=x%10+'0',x/=10;
        while(iotp)Putchar(iost[iotp--]);if (ch)Putchar(ch);
    }
    void Putstr(const char *s){for(int i=0,n=strlen(s);i<n;++i)Putchar(s[i]);}
} // namespace Fast_IO
using namespace Fast_IO;
ll fac[MAXN],invfac[MAXN];

void init()
{
    fac[0]=1;for(int i=1;i<MAXN;i++)fac[i]=fac[i-1]*i%MOD;
    invfac[MAXN-1]=qpow(fac[MAXN-1],MOD-2);
    for(int i=MAXN-2;i>=0;i--)invfac[i]=invfac[i+1]*(i+1)%MOD;

}
inline ll C(int n,int m)
{
    if(m<0||m>n)return 0;
    return fac[n]*invfac[m]%MOD*invfac[n-m]%MOD;
}
ll tot[333];
void work()
{
    int n=read(),k=read();
    for(int i=1;i<=n;i++)
    {
        ll x=read();
        ll now=1;
        for(int j=0;j<=k;j++)
        {
            tot[j]=(tot[j]+now)%MOD;
            now=now*x%MOD;
        }
    }
    ll mi2=1;
    for(int i=1;i<=k;i++)
    {
        mi2=mi2*2%MOD;
        ll ans=0;
        for(int j=0;j<=i;j++)
        {
            ans=(ans+tot[j]*tot[i-j]%MOD*C(i,j)%MOD)%MOD;
        }
        ans=(ans-tot[i]*mi2%MOD+MOD)*qpow(2,MOD-2)%MOD;
        printf("%lld\n",ans);
    }
}
int main()
{
    init();
    //std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //int T=read();for(int cas=1;cas<=T;cas++)
    //int T;scanf("%d",&T);for(int i=1;i<=T;i++)
    work();
    // Write();
}

/**
2
5 3 6
3 1 2 4 5

*/


//
//  main.cpp
//

#include <algorithm>
#include <array>
#include <assert.h>
#include <complex>
#include <iomanip>
#include <iostream>
#include <limits>
#include <inttypes.h>
#include <map>
#include <math.h>
#include <memory>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <atcoder/dsu>
// using namespace atcoder;
// using mint = modint1000000007;
// using mint = modint998244353;
// using mint = modint;

using namespace std;

using ll = int64_t;
using ull = uint64_t;

[[maybe_unused]] constexpr ll LL_MAX = numeric_limits<ll>::max();
[[maybe_unused]] constexpr ll LL_MIN = numeric_limits<ll>::min();
[[maybe_unused]] constexpr ull ULL_MAX = numeric_limits<ull>::max();

#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrep(i, a, b) for (ll i = (a)-1; i >= (b); i--)

template<typename T>
void chmin(T& x, T y) {
	x = min(x, y);
}

template<typename T>
void chmax(T& x, T y) {
	x = max(x, y);
}

ll csum(ll x, ll y) {
	if (x > 0 && y > 0 && x > LL_MAX - y) {
		return LL_MAX;
	}
	if (x < 0 && y < 0 && x < LL_MIN - y) {
		return LL_MIN;
	}
	return x + y;
}

ll readi() {
	ll ret;
	scanf("%" PRId64 "", &ret);
	return ret;
}

vector<ll> readvi(ll n) {
	vector<ll> ret(n);
	rep(i, 0, n) { ret[i] = readi(); }
	return ret;
}

double readf() {
	double ret;
	scanf("%lf", &ret);
	return ret;
}

string reads() {
	string s;
	cin >> s;
	return s;
}

void writei(ll x) {
	printf("%" PRId64 "\n", x);
}

void writevi(const vector<ll>& xs) {
    rep(i,0,xs.size()) {
        if (i < xs.size() - 1) {
            printf("%" PRId64 " ", xs[i]);
        } else {
            printf("%" PRId64 "\n", xs[i]);
        }
    }
}

void writes(const string& s) {
	cout << s.c_str() << endl;
}

template<typename T>
vector<T> make_vec_nd(T init, ll size) {
	return vector<T>(size, init);
}
template<typename T, typename... Args>
auto make_vec_nd(T init, ll size, Args... rest) {
	auto inner = make_vec_nd(init, rest...);
	return vector<decltype(inner)>(size, inner);
}

struct P {
	ll x;
	ll y;
};

int main() {
	ll N = readi();
	ll M = readi();
	vector<ll> hs = readvi(N);
	vector<ll> ws = readvi(M);
	sort(hs.begin(), hs.end());
	sort(ws.begin(), ws.end());
	struct HW {
		bool isH;
		ll idx;
		ll height;
		bool operator<(const HW& rhs) const {
			return height < rhs.height || (height == rhs.height && isH < rhs.isH);
		}
	};
	vector<HW> hws;
	rep(i,0,N) {
		hws.push_back({true, i, hs[i]});
	}
	rep(i,0,M) {
		hws.push_back({false, i, ws[i]});
	}
	sort(hws.begin(), hws.end());
	vector<ll> leftCnt(M);
	ll cnt = 0;
	for (HW& hw : hws) {
		if (hw.isH) {
			cnt++;
		} else {
			leftCnt[hw.idx] = cnt;
		}
	}
	auto makePairs = [N, &hs]() {
		vector<ll> leftPairs(N+1);
		rep(n,1,N+1) {
			if (n%2 == 0) {
				leftPairs[n] = leftPairs[n-1] + abs(hs[n-1] - hs[n-2]);
			} else {
				leftPairs[n] = leftPairs[n-1];
			}
		}
		return leftPairs;
	};
	vector<ll> leftPairs = makePairs();
	reverse(hs.begin(), hs.end());
	vector<ll> rightPairs = makePairs();
	reverse(hs.begin(), hs.end());
	// writevi(leftPairs);
	// writevi(rightPairs);
	ll ans = LL_MAX;
	rep(i,0,M) {
		ll ansLoc = 0;
		if (leftCnt[i] % 2 == 1) {
			ansLoc += leftPairs[leftCnt[i]];
			ansLoc += ws[i] - hs[leftCnt[i]-1];
			ansLoc += rightPairs[N-leftCnt[i]];
		} else {
			ansLoc += leftPairs[leftCnt[i]];
			ansLoc += hs[leftCnt[i]] - ws[i];
			ansLoc += rightPairs[N-leftCnt[i]-1];
		}
		chmin(ans, ansLoc);
	}
	writei(ans);
}

#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <algorithm>
#include <iostream>
#include <vector>
#include <array>
using namespace std;
#define f(x,y,z) for(int x=y;x<=z;++x)
vector<int> v[200001];
array<int, 200001> jr, dkt, tdk, at, uj;
int n, k, a, b, l, r, m, hit;
void jrk(int x) {
    for(int &i:v[x]) if(i != at[x]) {
        at[i] = x, jr[i] = jr[x]+1, jrk(i);
    }
}
bool cek(int &x) {
    // cerr << x;
    fill(dkt.begin(), dkt.end(), 0x3f3f3f3f);
    fill(tdk.begin(), tdk.end(), 0);
    hit = 0;
    f(i,1,n) {
        int &z = uj[i];
        // cerr << ' ' << z;
        if(dkt[z]+tdk[z] <= x) {
            tdk[z] = -1;
        } else if(tdk[z] == x || (z == 1 && tdk[z] >= 0)) {
            ++hit, tdk[z] = -1, dkt[z] = 0;
        }
        tdk[at[z]] = max(tdk[at[z]], tdk[z]+1);
        dkt[at[z]] = min(dkt[at[z]], dkt[z]+1);
    }
    // cerr << '\n';
    return hit <= k;
}
int main() {
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    cin >> n >> k;
    f(i,2,n) {
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    jrk(1);
    f(i,1,n) uj[i] = i;
    sort(uj.begin()+1, uj.begin()+n+1, [](int &i, int &j){
        return jr[i] > jr[j];
    });
    l = 1, r = n-1;
    while(l <= r) {
        m = (l+r)>>1;
        cek(m) ? r = m-1 : l = m+1;
    }
    cout << l << '\n';
}


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

inline long long read(){
    long long s = 0, w = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9'){
        if (ch == '-') w = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9'){
        s = s * 10 + ch - '0';
        ch = getchar();
    }
    return s * w;
}

int main(){
    int a = read(), b = read(), c = read();
    bool f = c;
    while(1){
        if(!f){
            if (a == 0){
                puts("Aoki");
                return 0;
            }
            a--;
        }
        else{
            if (b == 0){
                puts("Takahashi");
                return 0;
            }
            b--;
        }
        f ^= 1;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using VI = vector<int>;
using VL = vector<ll>;
using VS = vector<string>;
template<class T> using PQ = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i,a,n) for(int i=(a);i<(n);++i)
#define eFOR(i,a,n) for(int i=(a);i<=(n);++i)
#define rFOR(i,a,n) for(int i=(n)-1;i>=(a);--i)
#define erFOR(i,a,n) for(int i=(n);i>=(a);--i)
#define SORT(a) sort(a.begin(),a.end())
#define rSORT(a) sort(a.rbegin(),a.rend())
#define fSORT(a,f) sort(a.begin(),a.end(),f)
#define all(a) a.begin(),a.end()
#define out(y,x) ((y)<0||h<=(y)||(x)<0||w<=(x))
#define tp(a,i) get<i>(a)
#define line cout << "-----------------------------\n"
#define stop system("pause")
constexpr ll INF = 1000000000;
constexpr ll LLINF = 1LL << 60;
constexpr ll mod = 1000000007;
constexpr ll MOD = 998244353;
constexpr ld eps = 1e-10;
constexpr ld pi = 3.1415926535897932;
template<class T>inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; }return false; }
template<class T>inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; }return false; }
inline void init() { cin.tie(nullptr); cout.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); }
template<class T>inline istream& operator>>(istream& is, vector<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T>inline istream& operator>>(istream& is, deque<T>& v) { for (auto& a : v)is >> a; return is; }
template<class T, class U>inline istream& operator>>(istream& is, pair<T, U>& p) { is >> p.first >> p.second; return is; }
template<class T>inline vector<T> vec(size_t a) { return vector<T>(a); }
template<class T>inline vector<T> defvec(T def, size_t a) { return vector<T>(a, def); }
template<class T, class... Ts>inline auto vec(size_t a, Ts... ts) { return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...)); }
template<class T, class... Ts>inline auto defvec(T def, size_t a, Ts... ts) { return vector<decltype(defvec<T>(def, ts...))>(a, defvec<T>(def, ts...)); }
template<class T>inline void print(const T& a) { cout << a << "\n"; }
template<class T, class... Ts>inline void print(const T& a, const Ts&... ts) { cout << a << " "; print(ts...); }
template<class T>inline void print(const vector<T>& v) { for (int i = 0; i < v.size(); ++i)cout << v[i] << (i == v.size() - 1 ? "\n" : " "); }
template<class T>inline void print(const vector<vector<T>>& v) { for (auto& a : v)print(a); }
inline string reversed(const string& s) { string t = s; reverse(all(t)); return t; }
template<class T>inline T sum(const vector<T>& a, int l, int r) { return a[r] - (l == 0 ? 0 : a[l - 1]); }

int main() {
    init();

    int n, m; cin >> n >> m;
    VI w(n); cin >> w;
    SORT(w);
    vector<pair<int, int>> lv(m); cin >> lv;
    fSORT(lv, [](auto x, auto y) {
        if (x.second == y.second)return x.first < y.first;
        return x.second < y.second;});
    if (lv[0].second < w.back()) {
        print(-1);
        return 0;
    }
    FOR(i, 1, m)chmax(lv[i].first, lv[i - 1].first);

    int ans = INF;
    do {
        VI s(n);
        FOR(i, 0, n)s[i] = w[i] + (i == 0 ? 0 : s[i - 1]);
        VI dp(n);
        FOR(i, 0, n)FOR(j, i + 1, n) {
            int ok = m, ng = -1;
            while (ok - ng > 1) {
                int mid = ok + ng >> 1;
                if (sum(s, i, j) <= lv[mid].second)ok = mid;
                else ng = mid;
            }
            int d = (ok == 0 ? 0 : lv[ok - 1].first);
            chmax(dp[j], dp[i] + d);
        }
        chmin(ans, dp[n - 1]);
    } while (next_permutation(all(w)));
    print(ans);

    return 0;
}