//@Author: KeinYukiyoshi

// clang-format off
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
#define int long long
using namespace std;
#define stoi stoll
#define fi first
#define se second
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; i++)
#define rep2(i, a, b) for (int i = (int)(a), i##_len=(b); i < i##_len; i++)
#define rep3(i, a, b) for (int i = (int)(a), i##_len=(b); i >= i##_len; i--)
#define FOR(i, a) for (auto &i: a)
#define ALL(obj) begin(obj), end(obj)
#define _max(x) *max_element(ALL(x))
#define _min(x) *min_element(ALL(x))
#define _sum(x) accumulate(ALL(x), 0LL)

const int MOD    = 998244353;
const int INF    = (int)(1e13 + 7);
const double EPS = 1e-8;
const double PI  = 3.14159265358979;
template <class T> using V = vector<T>;
template <class T> using VV = vector<vector<T>>;
template <class T> using VVV = vector<vector<vector<T>>>;
template <class T, class S>  using P = pair<T, S>;
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;}
int _ceil(int a, int b) { return (a >= 0 ? (a + (b - 1)) / b : (a - (b - 1)) / b); }
int _mod(int a) {return a >= 0 ? a % MOD : a - (MOD * _ceil(a, MOD));}
int _pow(int a, int b) {int res = 1;for (a %= MOD; b; a = a * a % MOD, b >>= 1)if (b & 1) res = res * a % MOD;return res;}
struct mint {long long x;mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}mint operator-() const { return mint(-x); }mint &operator+=(const mint a) {if ((x += a.x) >= MOD) x -= MOD;return *this;}mint &operator-=(const mint a) {if ((x += MOD - a.x) >= MOD) x -= MOD;return *this;}mint &operator*=(const mint a) { (x *= a.x) %= MOD;return *this; }mint operator+(const mint a) const { return mint(*this) += a; }mint operator-(const mint a) const { return mint(*this) -= a; } mint operator*(const mint a) const { return mint(*this) *= a; }mint pow(long long t) const {if (!t) return 1;mint a = pow(t >> 1);a *= a;if (t & 1) a *= *this;return a;}mint inv() const { return pow(MOD - 2); }mint &operator/=(const mint a) { return *this *= a.inv(); }mint operator/(const mint a) const { return mint(*this) /= a; }};istream &operator>>(istream &is, mint &a) { return is >> a.x; }ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
// clang-format on

class DSkyReflector {
public:
  static void solve(istream &cin, ostream &cout) {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    int N, M, K;
    cin >> N >> M >> K;
    mint ans = 0;
    if (N == 1 and M == 1) {
      cout << K << endl;
      return;
    } else if (N == 1) {
      ans += _pow(K, M);
      cout << ans << endl;
      return;
    } else if (M == 1) {
      ans += _pow(K, N);
      cout << ans << endl;
      return;
    }
    rep2(i, 1, K + 1) {
      mint now = 1;
      now *= _pow(i, N) - _pow(i - 1, N);
      now *= _pow(K - i + 1, M);
      ans += now;
    }
    cout << ans << endl;
  }
};

signed main() {
  DSkyReflector solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <bits/stdc++.h>
using namespace std;

#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define ll long long
#define MAX 1000005
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}

int main () {
    int n;
    cin >> n;
    int x0, y0, x2, y2;
    cin >> x0 >> y0; cin >> x2 >> y2;
    double deg = acos(-1.0) * 2 / (float)n;
    double r = sqrt(pow(x0-x2, 2) + pow(y0-y2, 2))/2.0;
    double x = (float)(x0 + x2) / 2.0, y = (float)(y0+y2) / 2.0;
    double deg2 = acos((x0-x) / (float) r);
    if (y0 - y < 0) deg2 = 2 * acos(-1.0) - deg2;
    double deg3 =  deg2+deg;
    // cout << deg2 / acos(-1.0)  * 180 << endl;
    double retx = x + cos(deg3) * r, rety = y + sin(deg3) * r;
    printf("%.12lf %.12lf", retx, rety);
    return 0;
}


#include <bits/stdc++.h>

#define loop(s, e, i) for (int i = s; i < e; ++i)
#define print(s) cout << s << endl;
#define DIV 1000000007
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
const int INF = 1e9+7;

ll ceildiv(ll a, ll b) { return (a+b-1)/b; } // 切り上げ
ll floordiv(ll a, ll b) { return a/b; } // 切り下げ

int show_matrix(vector<vector<int>> &dp) {
  loop(0, dp.size(), i) {
    loop(0, dp[i].size(), j) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}

/*
浮動小数点の入力
cout << fixed << setprecision(9) << endl;
*/

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

  ll H, W, X, Y;
  cin >> H >> W >> X >> Y;
  X--; Y--;
  vector<vector<ll>> M(H, vector<ll>(W));

  loop(0, H, i) {
    string s;
    cin >> s;
    loop(0, W, j) {
      if (s[j] == '#') {
        M[i][j] = 1;
      }
    }
  }

  ll count = 0;
    for (int i=Y; i >= 0; i--) {
      if (M[X][i] == 1) {
        break;
      } else {
        count++;
      }
    }

    for (int i=Y; i < W; i++) {
      if (M[X][i] == 1) {
        break;
      } else {
        count++;
      }
    }

    for (int i=X; i >= 0; i--) {
      if (M[i][Y] == 1) {
        break;
      } else {
        count++;
      }
    }

    for (int i=X; i < H; i++) {
      if (M[i][Y] == 1) {
        break;
      } else {
        count++;
      }
    }
  print(count - 3);
}

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <queue>
#include <string>
#include <math.h>
#include <functional>
#include <iomanip>
#include <set>
#include <map>
#include <numeric>
#include <cstdint>

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

using ll = long long;

template <ll ModVal>
struct ModInt {
    ll x;

    ModInt(ll _x = 0) : x((_x% ModVal + ModVal) % ModVal) {
    }

    ModInt operator-() const {
        return ModInt(-x);
    }
    ModInt& operator+=(const ModInt a) {
        x += a.x;
        if (x >= ModVal)
            x -= ModVal;
        return *this;
    }
    ModInt& operator-=(const ModInt a) {
        x = x + ModVal - a.x;
        if (x >= ModVal)
            x -= ModVal;
        return *this;
    }
    ModInt& operator*=(const ModInt a) {
        x *= a.x;
        x %= ModVal;
        return *this;
    }

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

    // 逆元
    ModInt inv(const ModInt a) {
        ll u, v;
        ext_gcd(a.x, ModVal, u, v);
        return ModInt(u);
    }

    ModInt& operator/=(const ModInt a) {
        return (*this) *= inv(a);
    }

    ModInt operator+(const ModInt a) const {
        ModInt retval(*this);
        return retval += a;
    }
    ModInt operator-(const ModInt a) const {
        ModInt retval(*this);
        return retval -= a;
    }
    ModInt operator*(const ModInt a) const {
        ModInt retval(*this);
        return retval *= a;
    }
    ModInt operator/(const ModInt a) const {
        ModInt retval(*this);
        return retval /= a;
    }

    ModInt pow(ll n) {
        ModInt ans(1);
        while (n) {
            if (n & 1)
                ans = ans * x;
            *this = (*this) * (*this);
            n = n >> 1;
        }
        return ans;
    }

    constexpr const ll& value() {
        return this->x;
    }
};

template <ll ModVal>
ostream& operator<<(ostream & os, const ModInt<ModVal>&a) {
    os << a.x;
    return os;
}

#define mod (ll)(1e9 + 7)
using mint = ModInt<mod>;

int main(){
    int H,W;cin>>H>>W;
    int X,Y;cin>>X>>Y;
    vector<string> m(H);
    for (int i = 0; i < H; i++) {
        cin>>m[i];
    }
    X-=1;Y-=1;
    int ans = 0;
    for (int i = 1; i+X < H; i++) {
        if (m[i + X][Y] == '#') {
            break;
        }
        if (m[i + X][Y] == '.') {
            ans++;
        }
    }
    for (int i = 1; -i+X >=0; i++) {
        if (m[-i + X][Y] == '#') {
            break;
        }
        if (m[-i + X][Y]=='.') {
            ans++;
        }
    }
    for (int i = 1; i+Y < W; i++) {
        if (m[X][Y + i] == '#') {
            break;
        }
        if (m[X][Y + i] == '.') {
            ans++;
        }
    }
    for (int i = 1; -i+Y >=0; i++) {
        if (m[X][Y - i] == '#') {
            break;
        }
        if (m[X][Y - i] == '.') {
            ans++;
        }
    }
    ans++;
    cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<(int)(n); i++)
#define rep1(i, n) for(int i=1; i<=(int)(n); i++)
#define rep2(i, n, m) for(int i=(int)n; i<=(int)m; i++)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> wi;
typedef vector<ll> vl;
const ll inf=1LL<<60;

template<ll m> struct ModInt{
  ll val;

  constexpr ModInt(ll v=0) noexcept : val(v%m){
    if(val<0)val+=m;
  }

  constexpr ll getmod(){return m;}

  constexpr ModInt operator - () const noexcept{
    return val ? m-val : 0;
  }

  constexpr ModInt operator + (const ModInt& r)const noexcept{return ModInt(*this) += r;}

  constexpr ModInt operator - (const ModInt& r)const noexcept{return ModInt(*this) -= r;}

  constexpr ModInt operator * (const ModInt& r)const noexcept{return ModInt(*this) *= r;}

  constexpr ModInt operator / (const ModInt& r)const noexcept{return ModInt(*this) /= r;}

  constexpr ModInt& operator += (const ModInt& r)noexcept{
    val+=r.val;
    if(val>=m)val-=m;
    return *this;
  }

  constexpr ModInt& operator -= (const ModInt& r)noexcept{
    val-=r.val;
    if(val<0)val+=m;
    return *this;
  }

  constexpr ModInt& operator *= (const ModInt& r)noexcept{
    val=val*r.val%m;
    return *this;
  }

  constexpr ModInt& operator /= (const ModInt& r)noexcept{
    ll a=r.val, b=m, u=1, v=0;
    while(b){
      ll t=a/b;
      a-=t*b;swap(a, b);
      u-=t*v;swap(u, v);
    }
    val=val*u%m;
    if(val<0)val+=m;
    return *this;
  }

  constexpr bool operator == (const ModInt& r)const noexcept{
    return this->val == r.val;
  }

  constexpr bool operator != (const ModInt& r)const noexcept{
    return this->val != r.val;
  }

  friend constexpr ostream& operator << (ostream &os, const ModInt<m>& x)noexcept{
    return os << x.val;
  }

  friend constexpr istream& operator >> (istream &is, ModInt &x){
    is >> x.val;
    x=ModInt(x.val);
    return is;
  }

  friend constexpr ModInt<m> modpow(const ModInt<m> &a, ll n)noexcept{
    if(n==0)return 1;
    auto t=modpow(a, n/2);
    t=t*t;
    if(n&1)t=t*a;
    return t;
  }

  friend constexpr ModInt<m> modinv(const ModInt<m> &a)noexcept{
    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;
  }
};

const ll mod1=1000000007;
const ll mod9=998244353;

using mint = ModInt<mod9>;

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

  mint a, b, c;
  cin >> a >> b >> c;

  cout << (a*(a+1)*b*(b+1)*c*(c+1))/8 << endl;

  return 0;
}

#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<int, int> pii;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;

#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b); i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)

#define sz(x) (int)(x).size()
#define popcnt(x) __builtin_popcount(x)
#define low_bo(a, x) (lower_bound(a.begin(), a.end(), x) - a.begin())
#define up_bo(a, x) (upper_bound(a.begin(), a.end(), x) - a.begin())
#define unique(a) a.resize(unique(a.begin(), a.end()) - a.begin())
#define shuffle(a) shuffle(a.begin(), a.end(), rnd)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

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

const int MOD = 998244353;
const char nl = '\n';
const int MX = 200001; //check the limits, dummy
ll p[MX];
void init() {
  p[0]=1;
  for(int i=1; i<MX; i++) {
    p[i]=(p[i-1]*2)%MOD;
  }
}
void solve(int t) {
  int n;
  cin>>n;
  vl arr(n);
  F0R(i,n) {
    cin>>arr[i];
  }
  sort(all(arr));
  ll ret=0;
  ll sm[2][n];
  sm[0][0]=0;
  sm[1][0]=0;
  for(int i=1; i<n; i++) {
    sm[0][i]=((sm[0][i-1]*2+arr[i-1])%MOD)%MOD;
    // sm[1][i]=(sm[1][i-1]+(arr[i]*pow[i-1])%MOD)%MOD;
  }
  // F0R(i,n) {
  //   cout << sm[0][i]<<nl;
  // }
  F0R(i,n) {
    ret=(ret+(arr[i]*(sm[0][i]+arr[i]))%MOD)%MOD;
    // cout << ret <<nl;
  }
  cout << ret <<nl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(20);
  cout << fixed;
  init();
  int T = 1;
  F0R(i,T) {
    solve(i+1);
  }
}

// read the question correctly (ll vs int)
// template by bqi343


#include "bits/stdc++.h"
#define int          long long int
#define mp           make_pair
#define pb           emplace_back
#define F            first
#define S            second
using vi       =     std::vector<int>;
using vvi      =     std::vector<vi>;
using pii      =     std::pair<int, int>;
using vpii     =     std::vector<pii>;
using vvpii    =     std::vector<vpii>;
using namespace std;
const int inf  =     1e18 + 10;
const int N    =     2e6 + 10;
int n, taka;
long double a[N], l, r, ans = inf, m1, m2;
long double f(long double x) {
	long double cost = 0;
	for (int i = 1; i <= n; i++) {
		cost += max(x, a[i] - x);
	}
	ans = min(ans, cost);
	return cost;
}
void solve() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	r = inf;
	while (l / (long double) n <= r / (long double) n - 1e-6) {
		m1 = (r - l) / 3.0 + l;
		m2 = r - (r - l) / 3.0;
		if (f(m1) < f(m2))
			r = m2;
		else
			l = m1;
	}
	cout << fixed << setprecision(6) << ans / (long double) n;
}
int32_t main() {
	ios_base::sync_with_stdio(false); cin.tie(0);
	solve(); return 0;
	return 0;
}

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

#include <iostream>
#include <fstream>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <vector>

#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define mp make_pair

using namespace std;

typedef long long int64;
typedef vector<int> vi;
typedef pair<int, int> ii;

class TaskC {
 public:
  void solveOne(istream &in, ostream &out) {
    int n;
    int l;
    in >> n;
    in >> l;
    vector<int> a(n + 2);
    vector<int> b(n + 2);
    for (int i = 1; i <= n; ++i) {
      in >> a[i];
      a[i] -= i;
    }
    for (int i = 1; i <= n; ++i) {
      in >> b[i];
      b[i] -= i;
    }
    a[n + 1] = b[n + 1] = l + 1 - (n + 1);
    n += 2;

    int ai = 0;
    int64 res = 0;

    for (int i = 0; i < n;) {
      int j = i;
      int val = b[i];
      while (j < n && b[j] == val) ++j;
      while (ai < n && a[ai] < val) ++ai;
      if (ai >= n || a[ai] != val) {
        out << -1 << "\n";
        return;
      }
      int aj = ai;
      while (aj < n && a[aj] == val) ++aj;
      res += max(0, ai - i);
      res += max(0, j - aj);
      i = j;
    }
    out << res << "\n";
  }

  void solve(std::istream &in, std::ostream &out) {
    int nt = 1;
    for (int it = 0; it < nt; ++it) {
      solveOne(in, out);
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  TaskC solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}


/**
  *  Author: dhruv_gheewala
  *  Problem: __________
  *  Date:   19.12.2020
  *  Time:   17:37:15
**/

#if __has_include("debug.h")
    #include "debug.h"
#else
    #include<bits/stdc++.h>
    using namespace std;

    #define fundri 108
    #define debug(...) 1729
#endif

#define endl '\n'
#define int int64_t

typedef pair<int, int> pii;
typedef vector<int> vi;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int rnd(int l = 0, int r = INT_MAX) {return uniform_int_distribution<int>(l, r)(rng);}

bool in_range(int x, int l, int r) {return l <= x && x <= r;}
template<typename H, typename ...T>void inp(H &head) {cin >> head;}
template<typename H, typename ...T>void inp(H &head, T &...tail) {cin >> head;inp(tail...);}
template<typename T>inline istream &operator >>(istream &in, vector<T> &a) {for(T &x : a)in >> x; return in;}
template<typename T, typename U>inline istream &operator >>(istream &in, pair<T, U> &a) {in >> a.first >> a.second; return in;}

// Multi-Dimension Vector
// Usage: vec<n, data-type> dp(dimention-1, dimention-2, ..., dimention-n, default = data-type())
template<int D, typename T> struct vec : public vector<vec<D - 1, T>>
{
    static_assert(D >= 1, "Vector dimensions 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){}};

const int inf = 1e15;
const bool testcases = false;

void init_main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

#ifdef DHRUV_GHEEWALA
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    freopen("debug.txt", "w", stderr);
#endif
}

void solve(int tc)
{
    int h, w;
    cin >> h >> w;

    vec<2, int> a(h, w);
    cin >> a;

    int mini = inf;
    for(int i = 0; i < h; i++) {
        for(int j = 0; j < w; j++)
            mini = min(mini, a[i][j]);
    }

    int res = 0;
    for(int i = 0; i < h; i++) {
        for(int j = 0; j < w; j++)
            res += a[i][j] - mini;
    }
    cout << res << endl;
}

int32_t main(int32_t argc, char **argv)
{
    init_main();

    int TC = 1;
    if(testcases)
        cin >> TC;

    for(int tc = 1; tc <= TC; ++tc) {
        solve(tc);
        fundri;
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for (int i = 0; i < (n); ++i)
#define cinb(a, b) cin >> a >> b;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

using ll = long long;
#define p(ans) cout << ans << endl;

ll n, m, k;
ll ab[110][2], cd[110][2];
ll memo[110];
//
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  cinb(n, m);
  for0(i, m) cin >> ab[i][0] >> ab[i][1];
  cin >> k;
  for0(i, k) cin >> cd[i][0] >> cd[i][1];
  ll ans = 0;

  for0(i, 1 << k) {
    for0(j, n) memo[j] = 0;
    for0(j, k) memo[cd[j][(i >> j) & 1] - 1] = 1;
    ll cnt = 0;
    for0(j, m) cnt += (memo[ab[j][0] - 1] & memo[ab[j][1] - 1]);
    chmax(ans, cnt);
  }
  p(ans);
}


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

long long n;
int m;

long long add(long long a, long long b, long long mod){
	return (a + b) % mod;
}

long long mul(long long a, long long b, long long mod){
	long long r = 0;
	while(b > 0){
		if(b & 1) r = add(r, a, mod);
		a = add(a, a, mod);
		b >>= 1;
	}
	return r;
}

long long pow_mod(long long a, long long b, long long mod){
	long long r = 1 % mod;
	while(b > 0){
		if(b & 1) r = mul(a, r, mod);
		a = mul(a, a, mod);
		b >>= 1;
	}
	return r;
}

int main(){
	cin >> n >> m;
	long long R = pow_mod(10, n, 1LL * m * m);
	long long r = R % m;
	printf("%lld\n", (R - r) / m);
	return 0;
}

#include <iostream>
using namespace std;

typedef long long ll;

#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)<(y)?(y):(x))

ll comb(ll n, ll k){
    if (k == 0 || k == n) return 1;
    if (k == 1 || k == n-1) return n;
    return comb(n-1,k)+comb(n-1,k-1);
}

int main(){
    ll n,k;cin >> n >> k;--k;
    for(ll xyz=3;xyz<=3*n;++xyz){
        if(xyz <= n+1){
            ll xmax = xyz-2;
            ll s = (2*xyz-3-xmax)*xmax/2;
            if(s <= k){
                k -= s;
                continue;
            }
            for(ll x=1;x<=xmax;++x){
                ll c = xyz-1-x;
                if(c <= k){
                    k -= c;
                    continue;
                }
                ll y=1+k;
                cout << x << " " << y << " " << (xyz-x-y) << "\n";
                return 0;
            }
        }else if(xyz < 2*n+1){
            ll xmax = xyz-n-1;
            ll s = (4*n-2*xyz+xmax+3)*xmax/2;
            if(s <= k){
                k -= s;
            }else{
                for(ll x=1;x<=xmax;++x){
                    ll c = 2*n-xyz+1+x;
                    if(c <= k){
                        k -= c;
                        continue;
                    }
                    ll y=xyz-n-x+k;
                    cout << x << " " << y << " " << (xyz-x-y) << "\n";
                    return 0;
                }
            }
            ll xmin = xyz-n;
            s = (2*xyz-2-n-xmin)*(n-xmin+1)/2;
            if(s <= k){
                k -= s;
                continue;
            }
            for(ll x=xmin;x<=n;++x){
                ll c = xyz-1-x;
                if(c <= k){
                    k -= c;
                    continue;
                }
                ll y=1+k;
                cout << x << " " << y << " " << (xyz-x-y) << "\n";
                return 0;
            }
        }else{
            ll s = (3*n-xyz+2)*(3*n-xyz+1)/2;
            if(s <= k){
                k -= s;
                continue;
            }
            for(ll x=xyz-2*n;x<=n;++x){
                ll c = 2*n-xyz+1+x;
                if(c <= k){
                    k -= c;
                    continue;
                }
                ll y=xyz-n-x+k;
                cout << x << " " << y << " " << (xyz-x-y) << "\n";
                return 0;
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (ll i=(ll)from; i<(ll)to; i++)
#define all(vec) vec.begin(), vec.end()
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (ll i=(ll)N-1; i>=0; i--)
#define popcount __builtin_popcount

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const ll INF = pow(2,30)-1;

vector<ll> fac;
void c_fac(ll x=pow(10,7)+10) { fac.resize(x,true); rep(i,x) fac[i] = i ? (fac[i-1]*i)%MOD : 1; }
ll inv(ll a, ll m=MOD) { ll b = m, x = 1, y = 0; while (b!=0) { ll d = a/b; a -= b*d; swap(a,b); x -= y*d; swap(x,y); } return (x+m)%m; }
ll nck(ll n, ll k) { return fac[n]*inv(fac[k]*fac[n-k]%MOD)%MOD; }
ll modpow(ll x, ll p) { ll result = 1, now = 1, pm = x; while (now<=p) { if (p&now) { result = result * pm % MOD; } now*=2; pm = pm*pm % MOD; } return result; }
ll gcd(ll a, ll b) { if (a<b) swap(a,b); return b==0 ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a/gcd(a,b)*b; }

struct edge  { ll i, a, b, rev; };

vector<vector<edge>> adj;
vector<ll> toa;

void dfs(ll n, ll p=-1) {
  for (auto x: adj[n]) {
    if (toa[x.i]==-1) {
      toa[x.i] = x.rev;
      dfs(x.a,n);
    }
  }
}

int main() {
  ll N, M; cin >> N >> M;
  edge e[M];
  rep(i,M) {
    e[i].rev = false;
    e[i].i = i;
    cin >> e[i].a >> e[i].b; e[i].a--; e[i].b--;
  }
  ll c[N]; rep(i,N) cin >> c[i];

  adj.resize(N);
  toa.resize(M,-1);
  rep(i,M) {
    if      (c[e[i].a]>c[e[i].b]) toa[e[i].i] = false;
    else if (c[e[i].a]<c[e[i].b]) toa[e[i].i] = true;
    else {
      // toはaを使う
      adj[e[i].a].push_back({e[i].i,e[i].b,e[i].a,false});
      adj[e[i].b].push_back({e[i].i,e[i].a,e[i].b,true});
    }
  }

  rep(i,N) dfs(i);

  rep(i,M) {
    string result = toa[i] ? "<-" : "->";
    cout << result << endl;
  }
  return 0;
}

#include <bits/stdc++.h>
using namespace std;
template <typename T> void read(T &t) {
	t=0; char ch=getchar(); int f=1;
	while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
	do { (t*=10)+=ch-'0'; ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;
}
int n,m,X[100010],Y[100010],c[110];
vector<int> g[110];
int ans[110][110];
bool vis[110];
int st[110],tot,flag,inst[110];
vector<int> hi,cyc;
int sz,on[110],ban[110];
void dfs(int u,int p) {
	//printf("%d\n",u);
	vis[u]=1; st[++tot]=u; inst[u]=1;
	hi.push_back(u);
	for (int i=0,v;i<g[u].size();i++) {
		v=g[u][i];
		if (v==p) continue;
		if (!vis[v]) {
			dfs(v,u);
		} else if (inst[v]&&!flag) {
			flag=1;
			for (int j=tot;j>=1;j--) {
				cyc.push_back(st[j]);
				if (st[j]==v) break;
			}
		}
	}
	inst[u]=0,tot--;
}
int tim,ora[110],ts,fx;
void sc(int u) {
	if (ora[u]==tim) return; ora[u]=tim;
	st[++tot]=u,inst[u]=1;
	if (on[u]) {
		if (!fx) {
			for (int i=1;i<tot;i++) ans[st[i]][st[i+1]]=1,ans[st[i+1]][st[i]]=0;
		} else {
			for (int i=1;i<tot;i++) ans[st[i+1]][st[i]]=1,ans[st[i]][st[i+1]]=0;
		}
	//	for (int i=1;i<=tot;i++) printf("%d ",st[i]); printf("\n");
		for (int i=2;i<=tot;i++) ban[st[i]]=ts;
		flag=1; return;
	}
	for (int i=0,v;i<g[u].size();i++) {
		v=g[u][i];
		if (inst[v]) continue;
		if (ans[v][u]!=-1&&ans[v][u]!=fx) continue;
		sc(v); if (flag) return;
	}
	tot--,inst[u]=0;
}
int ttt[110],tttim;
bool fd(int u,bool f) {
	if (on[u]) return 1;
	if (ttt[u]==tttim) return 0;
	ttt[u]=tttim;
	for (int i=0,v;i<g[u].size();i++) {
		v=g[u][i];
		if (ans[u][v]==f) {
			if (fd(v,f)) return 1;
		}
	}
	return 0;
}
vector<int> G[110];
int t[110],cnt;
void ddfs(int u) {
	if (t[u]) return;
	t[u]=1,cnt++;
	for (int i=0,v;i<G[u].size();i++) {
		v=G[u][i];
		ddfs(v);
	}
}
int main() {
	read(n),read(m); int x,y;
	for (int i=1;i<=m;i++) {
		read(x),read(y);
		X[i]=x,Y[i]=y;
	}
	memset(ans,-1,sizeof(ans));
	for (int i=1;i<=n;i++) read(c[i]);
	for (int i=1;i<=m;i++) {
		x=X[i],y=Y[i];
		if (c[x]==c[y]) {
			g[x].push_back(y),g[y].push_back(x);
			//printf("add %d %d\n",x,y);
		} else {
			if (c[x]>c[y]) ans[x][y]=1,ans[y][x]=0; else ans[y][x]=1,ans[x][y]=0;
		}
	}
	for (int i=1;i<=n;i++) if (!vis[i]) {
		flag=0,cyc.clear(),hi.clear(),dfs(i,0);
		sz=(int)cyc.size();
		for (int j=0;j<sz;j++) ans[cyc[j]][cyc[(j+1)%sz]]=1,ans[cyc[(j+1)%sz]][cyc[j]]=0,on[cyc[j]]=1;
	//	for (int j=0;j<sz;j++) printf("%d ",cyc[j]); printf("\n");
		if ((int)hi.size()==1) continue;
		for (int j=0;j<hi.size();j++) {
			x=hi[j];
			ts++;
			if (on[x]) continue;
			tttim++;
			if (!fd(x,1)) {
				//printf("x1=%d\n",x);
				tim++,flag=0,fx=0,sc(x);
				assert(flag);
				while (tot) inst[st[tot]]=0,tot--;
			}
			tttim++;
			if (!fd(x,0)) {
				//printf("x2=%d\n",x);
				tim++,flag=0,fx=1,sc(x);
				assert(flag);
				while (tot) inst[st[tot]]=0,tot--;
			}
			on[x]=1;
		}
	}
	for (int i=1;i<=m;i++) {
		x=X[i],y=Y[i];
		if (ans[x][y]) puts("->");
		else puts("<-");

		//if (ans[x][y]) G[x].push_back(y);//,printf("%d %d\n",x,y);
		//else G[y].push_back(x);//,printf("%d %d\n",y,x);
	}
/*	for (int i=1;i<=n;i++) {
		for (int j=1;j<=n;j++) t[j]=0; cnt=0;
		ddfs(i);
		if (cnt!=c[i]) printf("%d %d %d\n",i,cnt,c[i]);
	}
	//puts("");
	puts("AC");*/
	return 0;
}
/*
  0. Enough array size? Enough array size? Enough array size? Interger overflow?

  1. Think TWICE, Code ONCE!
  Are there any counterexamples to your algo?

  2. Be careful about the BOUNDARIES!
  N=1? P=1? Something about 0?

  3. Do not make STUPID MISTAKES!
  Time complexity? Memory usage? Precision error?
*/


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

using pii = pair<int, int>; using vpii = vector<pii>;
using vi = vector<int>; using vvi = vector<vi>;
using ll = long long;
using db = long double;
using pl = pair<ll, ll>; using vpl = vector<pl>;
using vl = vector<ll>; using vvl = vector<vl>;
using vc = vector<char>; using vvc = vector<vc>;
using vb = vector<bool>; using vvb = vector<vb>;
#define FOR(i, a, b) for(ll i=(a); i<(b); ++i)
#define FORE(i, a, b) for(ll i=(a); i<=(b); ++i)
#define ROF(i, a, b) for(ll i=(a); i>(b); --i)
#define ROFE(i, a, b) for(ll i=(a); i>=(b); --i)
#define all(x) begin(x), end(x)
#define rall(x) x.rbegin(), x.rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pb push_back
#define eb emplace_back
#define pf push_front
#define fi first
#define se second
#define _ ios_base::sync_with_stdio(0);cin.tie(0);

#define tpl template
#define tn typename

tpl <tn T>
istream& operator>> (istream& in, vector<T>&v) {
    for(auto &x:v) in>>x;
    return in;
}
tpl <tn T1, tn T2>
ostream& operator<< (ostream& out, const pair<T1,T2>& p) {
    out<<p.fi<<' '<<p.se;
    return out;
}
tpl <tn T>
ostream& operator<< (ostream& out, const vector<T>& v) {
    for(auto x:v) out<<x<<' ';
    return out;
}
tpl <tn T>
ostream& operator<< (ostream& out, const set<T>& st) {
    for(auto x:st) out<<x<<' ';
    return out;
}

tpl <tn T>
T mx(vector<T> A) {
    T ans = 0;
    for(auto x : A) ans = max(ans, x);
    return ans;
}
tpl <tn T>
T mn(vector<T> A) {
    T ans = 0;
    for(auto x : A) ans = min(ans, x);
    return ans;
}
tpl <tn T>
void ps(vector<T> &A){
    partial_sum(all(A), A.begin());
    return;
}
tpl <tn T>
void ad(vector<T> &A){
    adjacent_difference(all(A), A.begin());
    return;
}

ll n, k;
vvl vv;
vl rt;
vl sz;
vl fc;
ll fr(ll x) {return (rt[x]==x ? x : rt[x] = fr(rt[x]));}
void un(ll x, ll y){
    x = fr(x), y = fr(y);
    if(x == y) return;
    if(sz[x] < sz[y]) swap(x, y);
    rt[y] = x;
    sz[x] += sz[y];
}

#define MOD 998244353

bool chv(ll x, ll y){
    FOR(i, 0, n){
        if(vv[i][x] + vv[i][y] > k) return 0;
    }return 1;
}
bool chh(ll x, ll y){
    FOR(i, 0, n){
        if(vv[x][i] + vv[y][i] > k) return 0;
    }return 1;
}

int main(){
    cin >> n >> k;
    vv.rsz(n, vl(n));
    fc.rsz(n+1);
    cin >> vv;
    fc[0] = 1;
    FORE(i, 1, n) fc[i] = fc[i-1] * i % MOD;
    rt.rsz(n + n), sz.rsz(n + n, 1);
    iota(all(rt), 0);
    ll ans = 1;
    FOR(i, 0, n-1) FOR(j, i+1, n){
        if(chv(i, j)) un(i, j);
        if(chh(i, j)) un(i + n, j + n);
    }
    FOR(i, 0, n + n) if(rt[i] == i) ans = ans * fc[sz[i]] % MOD;
    cout << ans;
}

#include <bits/stdc++.h>

#define ll long long
#define ii pair<int, int>
#define pll pair<ll, ll>
#define dd pair<double, double>
#define vi vector<int>
#define vl vector<ll>
#define vd vector<double>
#define vii vector<ii>
#define vll vector<pll>
#define vdd vector<dd>
#define vvi vector<vi>
#define vvl vector<vl>
#define vvd vector<vd>
#define vvii vector<vii>
#define vvll vector<vll>
#define vvdd vector<vdd>
#define fi first
#define se second
#define uni(v) v.erase(unique(v.begin(), v.end()), v.end())
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) (ll) a / __gcd(a, b) * b
#define prt(v) \
    for (auto& i : v) cout << i << " \n"[&i == &v.back()]

using namespace std;

void print();
template <typename T, typename... Args>
void print(T x, Args... args);

void solve(int T) {
    int n;
    cin >> n;
    vi a(n);
    for (int i = 0; i < n; ++i) {
        string s;
        cin >> s;
        a[i] = s == "AND";
    }
    vvl dp(n + 1, vl(2, 0));
    dp[0][0] = dp[0][1] = 1;

    for (int i = 0; i < n; ++i) {
        if (a[i]) {
            dp[i + 1][0] = dp[i][0] * 2 + dp[i][1];
            dp[i + 1][1] = dp[i][1];
        } else {
            dp[i + 1][0] = dp[i][0];
            dp[i + 1][1] = dp[i][0] + dp[i][1] * 2;
        }
    }

    cout << dp[n][1] << '\n';
}
int main() {
    ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);

    int t = 1;
    // cin >> t;
    for (int i = 0; i++ < t;) solve(i);

    return 0;
}

void print() { cout << "\n"; }
template <typename T, typename... Args>
void print(T x, Args... args) {
    if (sizeof...(args)) {
        cout << x << ' ';
        print(args...);
    } else {
        cout << x << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false); cin.tie(0);
#define FOR(i,s,n) for(int i = (s); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) for(int i = (n); i >= 0; i--)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
#define ATYN(n) cout << ( (n) ? "Yes":"No") << '\n';
#define CFYN(n) cout << ( (n) ? "YES":"NO") << '\n';
#define OUT(n) cout << (n) << '\n';
using ll = long long;
using ull = unsigned long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;

int main(void)
{
    IOS
    int n; cin >> n;
    vector<ll> amax(n);
    ll cmax = 0;
    REP(i,n) {
        ll a; cin >> a;
        if (i != 0) amax[i] = max(a,amax[i-1]);
        else amax[i] = a;
    }
    REP(i,n) {
        ll b;
        cin >> b;
        cmax = max(cmax,amax[i] * b);
        cout << cmax << '\n';
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz = 2e5+5;

ll a[sz], b[sz], ans[sz];

int main() {
   int n;
   cin >> n;
   for(int i=1; i<=n; i++) scanf("%lld", &a[i]), a[i] = max(a[i-1], a[i]);
   for(int i=1; i<=n; i++) scanf("%lld", &b[i]);
   for(int i=1; i<=n; i++) {
      ans[i] = max(ans[i-1], a[i] * b[i]);
      printf("%lld\n", ans[i]);
   }
}


#include<bits/stdc++.h>
#define forn(i,s,t) for(register int i=(s);i<=(t);++i)
#define form(i,s,t) for(register int i=(s);i>=(t);--i)
using namespace std;
typedef long long LL;
template<typename T>inline T Max(T A, T B) {return A>B?A:B;}
template<typename T>inline T Min(T A, T B) {return A<B?A:B;}
inline bool pd(char *s) {
	return (s[0] == 'A');
}
const int N = 1e4+3, M = 1e5+3;
int n; char s[N][100]; LL t[N], f[N];
int main() {
	scanf("%d", &n);
	forn(i,1,n) scanf("%s", s[i]);
	t[0] = f[0] = 1;
	forn(i,1,n) {
		if(pd(s[i])) {
			t[i] = t[i-1];
			f[i] = f[i-1]*2 + t[i-1];
		} else {
			t[i] = t[i-1]*2 + f[i-1];
			f[i] = f[i-1];
		}
	}
	printf("%lld\n", t[n]);
	return 0;
}
// and -> T & T  c
// or  -> T | F
// 		  T | T
// 		  F | T

#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(false), cin.tie(0);
#define et cout<<endl;
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
    while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
const int N = 2e5+5;
const int M = 2e5+5;
const int mod = 1e9+7;
int t, n, k;
int g[N];
bool check(vector<ll> v, vector<ll> q)
{
    if(v.size() > q.size()) return false;
    if(v.size() == q.size())
    {
        for(int i = 0 ; i < v.size(); i ++)
        {
            if(v[i] > q[i]) return false;
            if(v[i] == q[i]) continue;
            if(v[i] < q[i]) return true;
        }
        return true;
    }
    return true;
}
void solve()
{
    string X; ll n;
    cin >> X >> n;
    int d = -1;
    vector<ll> v;
    for(int i = 0 ; i < X.length(); i ++) v.push_back(X[i] - '0'), d = max(d, X[i] - '0');
    ll l = d + 1, r = n;
    while(l < r)
    {
        ll mid = l + r + 1 >> 1;
        ll p = n;
        vector<ll> q;
        while(p)
        {
            q.push_back(p % mid);
            p /= mid;
        }
        reverse(q.begin(), q.end());
        if(check(v, q)) l = mid;
        else r = mid - 1;
    }
    if(v.size() == 1)
    {
        if(d <= n) cout << 1 << endl;
        else cout << 0 << endl;
        return;
    }
    if(l == d + 1)
    {
        ll p = n;
        vector<ll> q;
        while(p)
        {
            q.push_back(p % l);
            p /= l;
        }
        reverse(q.begin(), q.end());
        if(check(v, q)) cout << 1 << endl;
        else cout << 0 << endl;
    }
    else cout << l - d << endl;
}
int main()
{
    ios
    bool multi = 0;   // 0 for single and 1 for multi
    if(multi) cin >> t;
    else t = 1;
    while(t--)
    {
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
char s[2010][2010];
long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
int main(){
  ll mod=1000000007;
  ll h,w,i,j;
  scanf("%lld %lld\n",&h,&w);
  ll x[2010][2010];
  for(i=0;i<=h+1;i++){
    for(j=0;j<=w+1;j++){
      x[i][j]=0,s[i][j]='#';
    }
  }
  ll ans1=0;
  for(i=1;i<=h;i++){
    for(j=1;j<=w;j++){
      scanf("%c",&s[i][j]);
      if(s[i][j]=='.'){
        ans1++;
      }
    }
    scanf("\n");
  }
  for(i=1;i<=h;i++){
    int count=1;
    for(j=1;j<=w;j++){
      if(s[i][j]=='.'){
        x[i][j]+=count;
        count++;
      }
      else{
        count=1;
        x[i][j]+=count;
      }
    }
  }
  for(i=1;i<=h;i++){
    int count=1;
    for(j=w;j>=1;j--){
      if(s[i][j]=='.'){
        x[i][j]+=count;
        count++;
      }
      else{
        count=1;
        x[i][j]+=count;
      }
    }
  }
  for(i=1;i<=w;i++){
    int count=1;
    for(j=1;j<=h;j++){
      if(s[j][i]=='.'){
        x[j][i]+=count;
        count++;
      }
      else{
        count=1;
        x[j][i]+=count;
      }
    }
  }
  for(i=1;i<=w;i++){
    int count=1;
    for(j=h;j>=1;j--){
      if(s[j][i]=='.'){
        x[j][i]+=count;
        count++;
      }
      else{
        count=1;
        x[j][i]+=count;
      }
    }
  }
  for(i=1;i<=h;i++){
    for(j=1;j<=w;j++){
      x[i][j]-=3;
    }
  }
  ll ans=0;
  for(i=1;i<=h;i++){
    for(j=1;j<=w;j++){
      if(s[i][j]=='.'){
        ans+=modpow(2,ans1,mod)-modpow(2,ans1-x[i][j],mod);
        ans+=mod+mod+mod;
        ans=ans%mod;
      }
    }
  }
  ans=ans%mod;
  cout << ans << endl;
}

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

const int64_t MOD = 1e9+7;
void add(int64_t& a, int64_t b){
    a = (a+b) % MOD;
}
void mul(int64_t& a, int64_t b){
    a = a*b % MOD;
}

int64_t power_mod(int64_t num, int64_t power){
    int64_t prod = 1;
    num %= MOD;
    while(power > 0){
        if(power&1) prod = prod * num % MOD;
        num = num * num % MOD;
        power >>= 1;
    }
    return prod;
}

void solve(){
    int64_t N, A, B;
    cin >> N >> A >> B;
    int64_t all = (N-A+1)*(N-B+1);
    int64_t mx = max(int64_t(0), N-A-B+1);
    int64_t ans = power_mod(all, 2);
    add(ans, MOD - power_mod(all - mx*(mx+1), 2));
    cout << ans << endl;
}

int main(){
    int H, W;
    cin >> H >> W;
    string S[2000];
    for(int i=0; i<H; i++) cin >> S[i];
    static int U[2000][2000], D[2000][2000], L[2000][2000], R[2000][2000];
    int K = 0;
    int64_t ans = 0;
    for(int i=0; i<H; i++) for(int j=0; j<W; j++) if(S[i][j] == '.'){
        K++;
        if(i && S[i-1][j] == '.') U[i][j] = U[i-1][j] + 1;
        if(j && S[i][j-1] == '.') L[i][j] = L[i][j-1] + 1;
    }
    for(int i=H-1; i>=0; i--) for(int j=W-1; j>=0; j--) if(S[i][j] == '.'){
        if(i<H-1 && S[i+1][j] == '.') D[i][j] = D[i+1][j] + 1;
        if(j<W-1 && S[i][j+1] == '.') R[i][j] = R[i][j+1] + 1;
        int ok = U[i][j] + L[i][j] + D[i][j] + R[i][j] + 1;
        add(ans, power_mod(2, K) - power_mod(2, K-ok) + MOD);
    }
    cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll a,b,c,ans,n;
    cin >> n;

    b = 0LL;
    ll two = 1LL;
    ans = 1e18;
    while(two <= n)
    {
        c = n % two;
        a = n / two;
        ans = min(ans, a+b+c);
        b++;
        two *= 2LL;

    }
    cout << ans << endl;

}

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
using namespace std;
using ll = long long;
const int INF = 1e9;
const ll LINF = 1e18;
template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
    for (int i = 0; i < int(v.size()); i++) {
        os << v[i];
        if (i < sz(v) - 1) os << ' ';
    }
    return os;
}
int main() {
    ll n;
    cin >> n;
    ll ans = LINF;
    for (ll b = 0; b < 60; b++) {
        ll x = 1ll << b;
        for (ll a = max(0ll, n / x - 100); n - a * x >= 0 && a <= n / x + 100;
             a++) {
            ll c = n - a * x;
            chmin(ans, a + b + c);
        }
    }
    cout << ans << '\n';
}


#include "bits/stdc++.h"
#ifdef FFS
#include "debug.h"
#else
#define debug(args...)
#endif
using namespace std;
const int N = 6 + 9 + 4e5;
vector <int> gr[N];
signed main()
{
    ios_base::sync_with_stdio(false), cin.tie(nullptr);
    int n; cin >> n;
    int x = INT_MAX, y = INT_MIN;
    for(int i = 0, a, b; i < n; ++i)
    {
        cin >> a >> b;
        x = min({x, a, b}), y = max({y, a, b});
        gr[a].emplace_back(b);
        gr[b].emplace_back(a);
    }
    bool vis[N], tree;
    int node, ans = 0;
    memset(vis, false, sizeof vis);
    function<void(int, int)> dfs = [&] (int child, int parent)
    {
        if(vis[child])
        {
            tree = false;
            return;
        }
        vis[child] = true, ++node;
        for(auto c : gr[child]) if(c ^ parent) dfs(c, child);
    };
    for(auto i = x; i <= y; ++i)
    {
        if(gr[i].empty() or vis[i]) continue;
        tree = true, node = 0;
        dfs(i, -1);
        ans += tree ? (node - 1) : node;
    }
    cout << ans << '\n';
}

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

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i,n) for (ll i = 0; i < n; ++i)
#define REPR(i,n) for (ll i = n-1; i >= 0; --i)
#define FOR(i,m,n) for (ll i = m; i < n; ++i)
#define FORR(i,m,n) for (ll i = n-1; i >= m; --i)
#define FORE(x,xs) for (const auto& x : xs)
#define FORI(i,v) for (auto i = v.begin(); i != v.end(); i++)
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REV(v) reverse(ALL(v))
#define UNIQ(v) sort(ALL(v)); v.erase(unique(ALL(v)),end(v))
#define CHMIN(x,y) x = min(x, y)
#define CHMAX(x,y) x = max(x, y)
#define YES(b) cout << ((b) ? "YES" : "NO") << endl
#define Yes(b) cout << ((b) ? "Yes" : "No") << endl

struct UnionFind {
private:
    int N;
    vi parent;
    vi rank_;

    void init(int n) {
        N = n;
        parent.resize(0); parent.clear(); parent.resize(n);
        REP (i, N) parent[i] = i;
        rank_.resize(0); rank_.clear(); rank_.resize(n);
    }

public:
    UnionFind() { init(0); }
    UnionFind(int n) { init(n); }

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

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

    void merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;

        if (rank_[x] < rank_[y])
            parent[x] = y;
        else {
            parent[y] = x;
            if (rank_[x] == rank_[y]) rank_[x]++;
        }
    }
};

const int MAX = 21;

int N, M;

vi adj[MAX];
vector<bool> visited;
vector<char> colors;

char next(char c, bool s) {
    if (c == 'R') {
        if (s) return 'B';
        else return 'G';
    } else if (c == 'G') {
        if (s) return 'R';
        else return 'B';
    } else {
        if (s) return 'R';
        return 'G';
    }
}

bool check(int state, int root) {
    queue<int> q;
    q.push(root);
    while (q.size()) {
        int x = q.front(); q.pop();
        if (x == root) {
            colors[x] = 'R';
        }
        FORE (a, adj[x]) {
            if (colors[a] == 'W') {
                colors[a] = next(colors[x], (state>>a)&1);
                if (!visited[a]) q.push(a);
                visited[a] = true;
            } else {
                if (colors[a] == colors[x]) return false;
            }
        }
    }
    return true;
}

int main() {
    cout << fixed << setprecision(15);
    cin >> N >> M;
    UnionFind uf(N);
    REP (i, M) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
        uf.merge(a, b);
    }
    vi roots;
    REP (i, N) if (uf.root(i) == i) roots.push_back(i);

    ll ans = 0;
    REP (state, 1<<N) {
        visited = vector<bool>(N, false);
        colors = vector<char>(N, 'W');
        bool f = true;
        FORE (r, roots) {
            if ((state & (1<<r)) || !check(state, r)) {
                f = false;
                break;
            }
        }
        if (f) ans++;
    }
    FORE (r, roots) ans *= 3;
    cout << ans << endl;
}


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

int main(){
    int N, count = 0;
    double tmp;
    cin >> N;
    vector<int> x(N), y(N);
    for(int i = 0; i < N; i++)
        cin >> x.at(i) >> y.at(i);
    for(int i = 0; i < N; i++){
        for(int j = i+1; j < N; j++){
            tmp = (double)y.at(i) - y.at(j);
            tmp /= (double)x.at(i) - x.at(j);
            if(-1 <= tmp && tmp <= 1) count++;
        }
    }
    cout << count << endl;
}

#include <iostream>
#include <vector>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std;

int main()
{
	int n;
	cin >> n;
	vector<int> x(n), y(n);
	rep(i, 0, n) {
		cin >> x[i] >> y[i];
	}

	int dx, dy, cnt = 0;
	double t;
	rep(i, 0, n - 1) rep(j, 0, n) {
		if (i < j) {
			dx = x[j] - x[i];
			dy = y[j] - y[i];
			t = (double)dy / (double)dx;
			if (abs(t) <= 1) {
				cnt++;
			}
		}
	}

	cout << cnt;

	return 0;
}


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

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

int main() {
  double ans;
  int N;
  double D, H;
  double delta;
  double dN, hN;

  cin >> N >> D >> H;
  ans = 0;
  rep(i, N){
      cin >> dN >> hN;
      delta = (H - hN) / (D - dN);
      ans = max(ans, (H - delta * D) );
  }
   cout << fixed << setprecision(13) << ans << endl;
}

#include <bits/stdc++.h>
using namespace std;
int d[105],h[105];
int main()
{
	int N,D,H;
	double max=-1;
	cin>>N>>D>>H;
	for(int i=0;i<N;i++)
	{
		scanf("%d%d",&d[i],&h[i]);
		double x1=D,y1=H,x2=d[i],y2=h[i];
		double b=y1-((x1*y1-x1*y2)*1.0/(x1-x2));
		if(b>=0)
		{
			if(b>max)
			{
				max=b;
			}
		}
	}
	if(max==-1)//b都是小于0的
	{
		printf("0.0");
	}
	else{
		printf("%.8f",max);
	}

}


#ifdef _LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;

ld solve(ll n) {
    ld res = 0;
    for(int i = 1; i < n; i++) {
        res += (ld)n / (ld)i;
    }
    return res;
}

#ifndef _LOCAL
int main() {
    ll n;
    cin >> n;
    cout << fixed << setprecision(12) << solve(n) << endl;
    return 0;
}
#endif

#include <iostream>

#include <cmath>
#include <cstring>

#include <map>
#include <set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <complex>
#include <numeric>
#include <string>
#include <utility>

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

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

const int MaxA = 200;

int main()
{
	int n;
	cin >> n;
	double ans = 0;
	for (int i = 1; i < n; ++i) {
		ans += (double)n / (n - i);
	}
	printf("%.10f\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll =long long;
typedef pair<ll,ll> P;
#define SORT(a) sort((a).begin(),(a).end())
#define REV(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

void coY() {cout <<"Yes"<<endl;}
void coN(){cout <<"No"<<endl;}
void mswap(ll &a, ll &b){ if(a >= b) swap(a,b); }
void rswap(ll &a, ll &b){ if(a <= b) swap(a,b); }

const int dy[] = {0,0,1,-1};
const int dx[] = {1,-1,0,0};
const ll mod = 1e9+7;
const ll MOD = 998244353;
const double PI=3.14159265358979323846;
const int inf = 1001001001;
const ll INF = 1'000'000'000'000'000'000;
const ll minf = -inf;

ll dp[200005][2];
//Write From this Line
int main()
{
	int n;
	cin >> n;
	vector<ll> x(n), c(n);
	rep(i,n) cin >> x[i] >> c[i];
	vector<ll> l(n+1,1'000'000'001);
    vector<ll> r(n+1,-1'000'000'001);
	rep(i,n){
		int color = c[i];

		chmin(l[color], x[i]);
		chmax(r[color], x[i]);
	}
	rep(i,200005) rep(j,2) dp[i][j] = INF;
	dp[0][0] = 0;
	dp[0][1] = 0;
	// 0:l側にいる 1:R側にいる
	ll tmpL = 0; // 座標
	ll tmpR = 0;
	rep(i,n+1){
		if(i == 0) continue;
		if(l[i] == 1'000'000'001 || r[i] == - 1'000'000'001){
			dp[i][0] = dp[i-1][0];
			dp[i][1] = dp[i-1][1];
		   	continue;
		}
		rep(j,2){
			if(j == 0){
				//取り終わったらL側l[i]に行く というか、R側から取り始めて、最後がL側
				// tmpL -> r[i] -> l[i]
				chmin(dp[i][j], dp[i-1][0] + abs(r[i] - tmpL) + abs(r[i] - l[i]));
				// tmpR -> r[i] -> l[i]
				chmin(dp[i][j], dp[i-1][1] + abs(r[i] - tmpR) + abs(r[i] - l[i]));
			} else {
				// 取り終わったらR側r[i]に行く L側Start最後がR
				// tmpL -> l[i] -> r[i]
				chmin(dp[i][j], dp[i-1][0] + abs(l[i] - tmpL) + abs(r[i] - l[i]));
				// tmpR -> l[i] -> r[i]
				chmin(dp[i][j], dp[i-1][1] + abs(l[i] - tmpR) + abs(r[i] - l[i]));
			}
		}
		tmpL = l[i];
		tmpR = r[i];
	}
	ll ans = min(dp[n][0] + abs(tmpL), dp[n][1] + abs(tmpR));
	cout << ans << endl;
}

/*
    author : Aryan Agarwal, IIT KGP
    created : 06-February-2021 18:04:32 IST
*/

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

const int mxn = 2000;
const long long INF = 2e18;
const int32_t M = 1000000007; /*more than 1e9 */ /*7 + 1e9*/
// const int32_t M = 998244353; /*less than 1e9 */   /*1 + 7*17*(1<<23) */
const long double pie = acos(-1);

#define X first
#define Y second
#define pb push_back
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define F(i, a, b) for (int i = a; i <= b; i++)
#define RF(i, a, b) for (int i = a; i >= b; i--)

#define dbg(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}

int n,m;
vector< vector<pair <int,int> > >  adj(mxn+1);
vector <int> ans(mxn+1,INF);
vector <bool> vis(mxn+1,false);

void dijkstra(int s, vector<int> & d) {
    int n = adj.size();
    d.assign(n, INF);
    // p.assign(n, -1);

    d[s] = 0;
    using pii = pair<int, int>;
    priority_queue<pii, vector<pii>, greater<pii>> q;
    q.push({0, s});
    while (!q.empty()) {
        int v = q.top().second;
        int d_v = q.top().first;
        q.pop();
        if (d_v != d[v])
            continue;

        for (auto edge : adj[v]) {
            int to = edge.first;
            int len = edge.second;

            if (d[v] + len < d[to]) {
                d[to] = d[v] + len;
                // p[to] = v;
                q.push({d[to], to});
            }
        }
    }
}

void solve_LOG()
{
    cin>>n>>m;
    F(i,1,m)
    {
        int a,b,c;
        cin>>a>>b>>c;
        if(a==b)ans[a]=min(ans[a],c);
        else adj[a].pb({b,c});

    }

    vector< vector<int> > dis(n+1);

    F(i,1,n)
    {
        dijkstra(i,dis[i]);
    }

    F(i,1,n)
    {
        F(j,i+1,n)
        {
            if(dis[i][j]!=INF && dis[j][i]!=INF)
            {
                ans[i]=min(ans[i],dis[i][j]+dis[j][i]);
                ans[j]=min(ans[j],dis[j][i]+dis[i][j]);
            }
        }
    }

    F(i,1,n)
    {
        if(ans[i]==INF)ans[i]=-1;
        cout<<ans[i];
        cout<<"\n";
    }

}

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
#endif
#ifdef ARYAN_SIEVE
    // defualt mxn_sieve = 1e5
    sieve();
#endif
#ifdef ARYAN_SEG_SIEVE
    // default [L,R] = [1,1e5]
    segmented_sieve();
#endif
#ifdef ARYAN_FACT
    // default mxn_fact = 1e5
    fact_init();
#endif
    // cout<<fixed<<setprecision(10);
    int _t=1;
    // cin>>_t;
    for (int i=1;i<=_t;i++)
    {
        // cout<<"Case #"<<i<<": ";
        solve_LOG();
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int T,n,m,p,k;
char s[100010];
int main(){
	scanf("%d%s",&n,s+1);
	if(s[1]!=s[n]) return puts("1"),0;
	for(int i=2;i<n-1;i++) if(s[i]!=s[1]&&s[i+1]!=s[n]) return puts("2"),0;
	puts("-1");
}

/*{{{*/
#include "bits/stdc++.h"
using namespace std;

#define MT make_tuple
#define all(x) begin(x), end(x)
#define putchar(x) cout << (x)
static int fastio = [](){ ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(17); return 0; }();
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template<typename ...T> string format(const string& fmt, T&&... args) {
    size_t sz = snprintf(nullptr, 0, fmt.c_str(), args...) + 1;
    unique_ptr<char[]> buf(new char[sz]);
    snprintf(buf.get(), sz, fmt.c_str(), args...);
    return string(buf.get(), buf.get() + sz - 1);
}

template<class T> struct rge { T b, e; auto begin() const { return b; } auto end() const { return e; } };
template<class T> rge<T> range(T i, T j) { return rge<T>{i, j}; }
template<class T> auto dud(T* x) -> decltype(cerr << *x, 0);
template<class T> char dud(...);

struct debug {
#if DEBUG == 2
    debug(int line) {
        if(line) cerr << "LINE(" << line << ") -> ";
    }
    template<class T> typename enable_if<sizeof dud<T>(0) != 1, debug&>::type operator<<(T i) {
        cerr << boolalpha << i; return * this;
    }
    template<class T> typename enable_if<sizeof dud<T>(0) == 1, debug&>::type operator<<(T i) {
        return *this << range(begin(i), end(i));
    }
    template<class T, class U> debug& operator<<(pair<T, U> d) {
        return *this << "(" << d.first << ", " << d.second << ")";
    }
    debug& operator<<(tuple<>&) { return *this << "()"; };
    template<class ...T> debug& operator<<(tuple<T...> d) {
        *this << "("; debug_tuple<sizeof...(T), 0>(d);
        return *this << ")";
    }
    template<size_t L, size_t I, class T> void debug_tuple(const T& t) {
        *this << (I == 0 ? "" : ", ") << get<I>(t);
        if(I + 1 < L) debug_tuple<L, (I + 1) % L>(t);
    }
    template<class T> debug & operator <<(rge<T> d) {
        *this << "[";
        for(auto it = d.b; it != d.e; ++it)
            *this << (it != d.b ?  ", " : "") << *it;
        return *this << "]";
    }
    debug& operator<<(ostream&(*pf)(std::ostream&)) {
        cerr << pf; return *this;
    }
#else
    debug(int) {}
    template<class T> debug& operator<<(T&&) { return *this; }
    debug& operator<<(ostream&(*)(std::ostream&)) { return *this; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ << ": " << (__VA_ARGS__) << "] "

#define debug0()  debug(__LINE__)
#define debug1(x) debug(0)
#define GET_MACRO(_0, _1, NAME, ...) NAME
#define debug(...) GET_MACRO(_0, ##__VA_ARGS__, debug1, debug0)(__VA_ARGS__)
/*}}}*/
using LL = long long;

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const LL LLINF = 0x3f3f3f3f3f3f3f3f;
const int MAX_N = 200005;

void solve(int) {
    string s;
    cin >> s;
    if(s[0] == s[1] and s[1] == s[2]) {
        cout << "Won" << endl;
    } else {
        cout << "Lost" << endl;
    }
}

int main() {
    int T = 1;
    //cin >> T;
    for(int i = 1; i <= T; ++i) {
        solve(i);
    }
    return 0;
}


 /*
    Stay motivated and keep working hard
*/
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define int long long
#define pb push_back
#define cntbit __builtin_popcount
#define fr first
#define mem(arr, val) memset(arr, val, sizeof(arr))
#define sc second
#define sz(v) ((int)(v).size())
#define mp make_pair
#define all(a) a.begin(),a.end()
#define rep(i,a,n) for(int i=a;i<n;i++)
#define repd(i,a,p) for(int i=a-1;i>=p;i--)
#define md 998244353
#define vi vector<int>
#define vp vector<pair<int,int> >
#define ml map<int,int>
#define MPI 3.1415926536
#define in insert
#define endl "\n"
#define ub  upper_bound
#define lb  lower_bound
#define gcd __gcd
#define setval(a,b) memset(a,b,sizeof(a))
#define lwr lower_bound
#define pii pair<int,int>
#define upr upper_bound
#define ordered_set     tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//CANDIDATE MASTER TOH BNKE RHUNGA
//dsu is pel concept
//KMP is pel concept
int k,n,t,i,j=0,m;
const int N=1e6+5;
int md2;
inline void add(int &a, int b) {
  a += b;
  if (a >= md) a -= md;
}

inline void sub(int &a, int b) {
  a -= b;
  if (a < 0) a += md;
}

inline int mul(int a, int b) {
  return (int) ((long long) a * b % md);
}

inline int power(int a, long long b) {
  int res = 1;
  while (b > 0) {
    if (b & 1) {
      res = mul(res, a);
    }
    a = mul(a, a);
    b >>= 1;
  }
  return res;
}

inline int inv(int a) {
  a %= md;
  if (a < 0) a += md;
  int b = md, u = 0, v = 1;
  while (a) {
    int t = b / a;
    b -= t * a; swap(a, b);
    u -= t * v; swap(u, v);
  }
  if (u < 0) u += md;
  return u;
}
void extgcd(int a,int b, int& x,int& y)
{
    if(b != 0){
        extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    }else{
        x = 1;
        y = 0;
    }
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n;
    vi a(n),b(n),c(n);
    for(int i=0;i<n;i++)
    cin>>a[i];
    for(int i=0;i<n;i++)
    cin>>b[i];
    int mx[n];
    mx[0]=a[0];
    for(int i=1;i<n;i++)
    mx[i]=max(mx[i-1],a[i]);
    c[0]=a[0]*b[0];
	for(int i=1;i<n;i++)
    c[i]=max(c[i-1],mx[i]*b[i]);
    for(int i=0;i<n;i++)
    cout<<c[i]<<endl;

}

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i,e,j;
 //e=1;;
 //while(e--)
 {

	 cin>>n;
	 long a[n],b[n],c[n],max,d;
	 for(i=0;i<n;i++)
	 cin>>a[i];
	 for(i=0;i<n;i++)
	 cin>>b[i];
	 c[0]=0;
	  max=a[0];
	 for(i=1;i<n;i++)
	 {
		 if(a[i]>max)
		 {
			 max=a[i];
			 c[i]=i;
		 }
		 else
		 c[i]=c[i-1];
	 }
	// for(i=0;i<n;i++)
	// cout<<c[i]<<" ";
	  long long dp[n];
	 dp[0]=a[0]*b[0];
	 for(i=1;i<n;i++)
	 {
		 dp[i]=dp[i-1];
		 long long t=a[c[i]]*b[i];
	     if(dp[i]<t)
		 dp[i]=t;

	 }
	 for(i=0;i<n;i++)
	 cout<<dp[i]<<"\n";
 }
}


// ----- In the name of ALLAH, the Most Gracious, the Most Merciful -----

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

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define deb(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define int long long
#define endl "\n"
#define mod 1000000007
#define inf (int)1e18

const int mxn = 2e3+5;
vector<pair<int,int>> g[mxn];
bool vis[mxn];
int d[mxn];

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

    int n,m; cin >> n >> m;
    for(int i=0;i<m;i++){
        int u,v,c; cin >> u >> v >> c;
        g[v].push_back({u,c});
        // g[u].push_back({v,c});
    }

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            d[j] = inf;
            vis[j] = false;
        }
        d[i] = inf;
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
        q.push({0,i});

        while(!q.empty()){
            int c = q.top().first;
            int v = q.top().second;
            q.pop();

            for(auto it : g[v]){
                int u = it.first;
                int cost = it.second;

                if(d[u]> cost+c){
                    d[u] = cost + c;
                    vis[u] = 1;
                    q.push({d[u],u});
                }
            }
        }

        if(d[i]==inf) cout << -1 << endl;
        else cout << d[i] << endl;
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (ll i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define pb push_back
using ll = long long;
using ld=long double;
using vin=vector<int>;
using vvin=vector<vin>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vst=vector<string>;
using P = pair<ll,ll>;
using vp=vector<P>;
using vvp=vector<vp>;
const int inf=1e9+7;
const ll INF=9e18/2;
const long double PI = acos(-1.0);
template <typename T> bool chmin(T &a, const T& b){if(a > b){a = b;return true;}return false;}
template <typename T> bool chmax(T &a, const T& b){if(a < b){a = b;return true;}return false;}
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 0, 0, 1, 1 };
const int dy[4] = { 0, 1, 0, 1 };

template<class T>
struct dijkstra{
  struct edge{
    int to;
    T cost;
  };

  vector<T> dist;
  vector<int> prev;
  vector<vector<edge>> g;
  T Inf;

  dijkstra(int n,T _Inf):Inf(_Inf){
   init(n);
   g.resize(n);
  };

  void init(int n){
   prev.assign(n,-1);
   dist.assign(n,Inf);
  }

  void add_edge(int s,int t,T cost){
   g[s].push_back((edge){t,cost});
  }

  void run(int s){
    priority_queue<pair<T,int>,vector<pair<T,int>>,greater<pair<T,int>>> pq;
    dist[s]=0;
    pq.push(pair<T,int>(0,s));
    while(!pq.empty()){
      int now=pq.top().second;
      T now_cost=pq.top().first;
      pq.pop();
      if(dist[now]<now_cost)continue;
      for(auto v:g[now]){
        if(dist[v.to]>dist[now]+v.cost){
          dist[v.to]=dist[now]+v.cost;
          prev[v.to]=now;
          pq.push(pair<T,int>(dist[v.to],v.to));
        }
      }
    }
  }
};

int main(){//cout<<fixed<<setprecision(20);
  int n,m;
  cin>>n>>m;
  dijkstra<ll> dijk(2*n,INF);
  rep(i,0,m){
    ll a,b,c;
    cin>>a>>b>>c;
    a--;b--;
    b+=n;
    dijk.add_edge(a,b,c);
    a+=n;
    dijk.add_edge(a,b,c);
  }
  rep(i,0,n){
   dijk.run(i);
   ll ans=INF;
   chmin(ans,dijk.dist[i+n]);
   dijk.init(n*2);
    if(ans==INF)ans=-1;
   cout<<ans<<endl;
  }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m; i >= n; --i)
#define ALL(v) (v).begin(),(v).end()
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; }
const ll INF=1LL<<60;
const int inf=(1<<30)-1;
const int mod=1e9;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
const int nmax=200005;
vector<vector<int>> g(nmax);
vector<ll> c(nmax),d(nmax);
void dfs(int cur,int pre=-1,ll res=0){
    c[cur]=res+d[cur];
    for(int to:g[cur]){
        if(to==pre) continue;
        dfs(to,cur,res+d[cur]);
    }
}
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;cin >> n;
    vector<int> a(n-1),b(n-1);
    REP(i,n-1){
        cin >> a[i] >> b[i];
        a[i]--,b[i]--;
        g[a[i]].push_back(b[i]);
        g[b[i]].push_back(a[i]);
    }
    vector<int> dis(n,inf);
    queue<int> qu;
    dis[0]=0;
    qu.push(0);
    while(!qu.empty()){
        int p=qu.front();
        qu.pop();
        for(int x:g[p]){
            if(dis[x]!=inf) continue;
            dis[x]=dis[p]+1;
            qu.push(x);
        }
    }
    int q;cin >> q;
    while(q--){
        int t,e,x;cin >> t >> e >> x;
        e--;
        if(t==1){
            if(dis[a[e]]<dis[b[e]]){
                d[0]+=x;
                d[b[e]]-=x;
            }
            else{
                d[a[e]]+=x;
            }
        }
        else{
            if(dis[a[e]]<dis[b[e]]){
                d[b[e]]+=x;
            }
            else{
                d[0]+=x;
                d[a[e]]-=x;
            }
        }
    }
    dfs(0);
    REP(i,n){
        cout << c[i] << endl;
    }
}

#include <bits/stdc++.h>
#define endl "\n"
#define int long long
#define ll long long

using namespace std;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int,int> pii;

template<typename T>
void printv(vector<T> v) {
  for (auto e : v) {
    cout << e << " ";
  } cout << endl;
}

template<typename T>
void printvv(vector<T> vv) {
  for (int i=0; i<vv.size(); i++) {
    cout << i << ": ";
    for (auto e : vv[i]) {
      cout << e << " ";
    } cout << endl;
  }
}

///////////////////////////////////////////////////////////////////////

const int p = 1e9 + 7;

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

  int t, n, a, b;
  cin >> t;
  while (t--) {
    cin >> n >> a >> b;

    if (n < a+b) {
      cout << 0 << endl;
      continue;
    }

    int x5 = (((n-a-b+1) * (n-a-b+2)) / 2 %p);
    // cout << "x5: " << x5 << endl;
    int x4 = ((((n-a+1) * (n-b+1))%p - (2 * x5)) + p) % p;
    int x3 = (x4 * x4) % p;

    int x1 = (n-a+1)*(n-a+1) % p;
    int x2 = (n-b+1)*(n-b+1) % p;
    int ans = ((((x1*x2) % p) - x3) % p + p) % p;

    cout << ans << endl;

  }

  return 0;
}


#include <bits/stdc++.h>
typedef long long ll ;
#define int ll
const int MAX=2e5+10;
const int modn=1e9+7;
const int INF=0x3f3f3f3f3f3f;
#define endl '\n'
#define cwk     freopen("D:\\workplace\\CLion\\in.in","r",stdin),freopen("D:\\workplace\\CLion\\out.out","w",stdout)
using namespace std;
int n,m,k,x;
bool vis[MAX];
double a[MAX],b[MAX];
bool check(){
    int cnt=0;
    for(int i=1;i<=n;i++){
        if(vis[i]) cnt++;
        else{
            if(cnt>=m) return false;
            cnt=0;
        }
    }
    return true;
}
void solve(){
    cin>>n>>m>>k;
    while(k--){
        cin>>x;
        vis[x]=true;
    }
    if(!check()){
        cout<<"-1\n";
        return;
    }
    double sumb=0;
    double suma=0;
    for(int i=n-1;i>=0;i--){
        if(vis[i]){
            a[i]=1;
            b[i]=0;
        }else{
            a[i]=suma*1.0/m;
            b[i]=sumb*1.0/m+1;
        }
        sumb+=b[i]-b[i+m];
        suma+=a[i]-a[i+m];
    }
    printf("%.4f",b[0]/(1-a[0]));
}
signed main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    //cwk;
    int _=1;
    //cin>>_;
    while(_--){
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
#define FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++)
#define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--)
#define rep(i, n) FOR(i, 0, n)
#define ALL(x) begin(x), end(x)
using namespace std;
using ll = long long;
constexpr ll Mod = 998244353;
constexpr ll mod = 1e9 + 7;
constexpr ll inf = 1LL << 60;
const double PI = acos(-1);
template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
    return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
    return a > b && (a = b, true);
}
/*-------------------------------------------*/

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

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

    bool a[100009]{};
    rep(i, k) {
        int x;
        cin >> x;
        a[x] = true;
    }

    vector<complex<double>> dp(n + 1);
    dp[n] = 0;
    complex<double> sum = 0;

    FORR(i, n) {
        if(a[i])
            dp[i] = complex<double>(0, 1);
        else
            dp[i] = sum / double(m) + 1.;
        sum += dp[i];
        if(i + m <= n) sum -= dp[i + m];
    }

    if(dp[0].imag() + 1e-8 > 1) {
        cout << -1 << endl;
        return 0;
    }

    cout << fixed << setprecision(10);

    cout << dp[0].real() / (1 - dp[0].imag()) << endl;

    return 0;
}


#include<iostream>
using namespace std;
int main(){
    long long a,b;
    cin>>a>>b;
    if(a+b>=15&&b>=8)cout<<1<<endl;
    else if(a+b>=10&&b>=3)cout<<2<<endl;
    else if(a+b>=3)cout<<3<<endl;
    else cout<<4<<endl;

}

#include <bits/stdc++.h>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define cerr if(1) cerr
#define ll long long
#define fp cout<<fixed<<setprecision(9);
#define loop(var,s,n) for(ll var=s ;var<n;var++)
#define rloop(var,s,n) for(ll var=s ;var>=n;var--)
#define pb push_back
#define endl '\n'
#define mk make_pair
#define all(x) x.begin(),x.end()
#define mod 1000000007
#define ipair pair<ll,pair<ll,ll>>
using namespace std;
int main() {

#ifdef KEVIN
	freopen("ingoing3.txt", "r", stdin);
	freopen("outgoing3.txt", "w", stdout);
	freopen("err3.txt", "w", stderr);
#endif
	FAST

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

	while (t--)
	{
		int fat, nonfat, milk;
		cin >> nonfat >> milk;
		fat = nonfat + milk;
		if (fat >= 15 && milk >= 8)cout << 1;
		else if (fat >= 10 && milk >= 3)cout << 2;
		else if (fat >= 3)cout << 3;
		else cout << 4;
	}
	//cerr << "Time elapsed: " << clock() / (double)CLOCKS_PER_SEC << endl;
}


#include <algorithm>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, n, s) for (int i = (s); i < (n); i++)
#define rrep(i, n) for (int i = (n - 1); i >= 0; i--)
#define rreps(i, n, s) for (int i = s; i >= n; i--)

using ll = long long;
using namespace std;
constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int MOD = 1000000007;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, C;
    cin >> n >> C;

    vector<pair<ll, ll>> event;

    rep(i, n) {
        ll a, b, c;
        cin >> a >> b >> c;
        event.pb({a, c});
        event.pb({b + 1, -c});
    }
    sort(event.begin(), event.end());

    ll res = 0, t = 0, fee = 0;
    for (auto v : event) {
        if (v.first != t) {
            res += min(C, fee) * (v.first - t);
            t = v.first;
        }
        fee += v.second;
    }

    cout << res << endl;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define d(x) cout<<x<<" "
#define nl cout<<endl
#define rep(var,init_val,final_val) for(int var = init_val; var < final_val; ++var)
#define tt ll t;cin>>t;while(t--)
#define show(a,b) rep(i,0,b) d(a[i]);nl;
#define mod 1000000007
#define vi vector<int>
#define pii pair<int,int>
const int d4i[] = {-1,0,1,0};
const int d4j[] = {0,1,0,-1};

const int d8i[] = {-1,-1,-1,0,1,1,1,0};
const int d8j[] = {-1,0,1,1,1,0,-1,-1};
// #define ONLINE_JUDGE true

int ti[8][8];
int n,k;

int cal_time(vi val){
	int tii = 0;
	rep(i,0,n){
		int a = val[i]-1;
		int b = val[i+1]-1;
		tii+=ti[a][b];
		if(b==0) return tii;
	}
	return tii;
}

void solve(){
	cin>>n>>k;

	rep(i,0,n){
		rep(j,0,n) cin>>ti[i][j];
	}
	vector<int> ans;
	ans.pb(1);
	rep(i,1,n+1) ans.pb(i);
	int v = 0;
	do{
		v += (cal_time(ans)==k)?1:0;
		//cout<<cal_time(ans)<<" : ";
		//show(ans,ans.size());
	}while(next_permutation(ans.begin(),ans.end()));
	cout<<v<<endl;
}

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


#include <bits/stdc++.h>
using namespace std;
#define ll           long long
#define all(v)       (v).begin() , (v).end()
#define vi           vector <ll>
#define vii          vector <pair<ll,ll>>
#define ii           pair<ll,ll>
#define sz(v)	     ((int)((v).size()))
#define Forr(i,s)       for(auto &i:s)
#define lp(i,a,b)    for(int i=a;i<b;i++)
#define pb           push_back
#define pf           push_front
#define F            first
#define S            second
#define endl         "\n"
#define IOS          ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const double PI = 3.14159265358979323846;
const ll inf=1e9+7,MOD=1e9+7,MAX=1e3+2;

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    IOS
    ll n,m,t;
    cin>>n>>m>>t;
    ll a[m],b[m],cur=0,ok=1,mx=n;
    lp(i,0,m){
        cin>>a[i]>>b[i];
        n-=a[i]-cur;
        ok&=n>0;
        n+=b[i]-a[i];
        n=min(n,mx);
        cur=b[i];
    }
    n-=t-cur;
    ok&=n>0;
    cout<<(ok?"Yes":"No");
}

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

typedef long long int ll;
ll MOD = 1000000007;
ll INFL = 1ll << 60;
ll INF = 1 << 28;

// ====================================================================

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

  vector<int> a(m), b(m);
  for (int i = 0; i < m; i++) cin >> a[i] >> b[i];

  int bat = n, time = 0;
  bool can = true;
  for (int i = 0; i < m; i++) {
    bat -= a[i] - time;
    // cout << "now battery ... " << bat << endl;
    if (bat <= 0) {
      cout << "No" << endl;
      return 0;
    }
    bat = min(n, bat + b[i] - a[i]);
    time = b[i];
  }

  // last bun
  bat -= t - time;
  // cout << "now battery ... " << bat << endl;
  if (bat <= 0) {
    cout << "No" << endl;
  } else {
    cout << "Yes" << endl;
  }
}


#include<iostream>
#include <cassert>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <cstring>
using namespace std;

const int N = 3e5 + 123;

char a[200][200];
int n, m, x, y;
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n >> m >> x >> y;

	for(int i = 1; i <= n; i++)
	for(int j = 1; j <= m; j++)
		cin >> a[i][j];

	int ux = x;
	while(ux - 1 > 0 && a[ux - 1][y] == '.') ux--;
	int dx = x;
	while(dx + 1 > 0 && a[dx + 1][y] == '.') dx++;
	int ly = y;
	while(ly - 1 > 0 && a[x][ly - 1] == '.') ly--;
	int ry = y;
	while(ry + 1 > 0 && a[x][ry + 1] == '.') ry++;
	cout << ry - ly + 1 + dx - ux + 1 - 1;
	return 0;
}

#include<bits/stdc++.h>
#define inf 1e18
#define endl "\n"
#define mp make_pair
#define pb push_back
#define loop(i,x,y) for(int i = x; i < y ; i++ )
#define all(x) (x).begin(),(x).end()
#define in(n)       int n; cin>>n;
#define inarr(arr,n) vector<int>arr(n); for(int i = 0; i < n ; i++){cin>>arr[i];}
#define maploop(x) for(auto itr = x.begin(); itr != x.end();itr++)
#define int long long
using namespace std;
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x<<" "; _print(x); cerr << endl;
#else
#define debug(x);
#endif

void _print(int t) {cerr << t;}
void _print(string t) {cerr << t;}
void _print(char t) {cerr << t;}
void _print(double t) {cerr << t;}

template <class T, class V> void _print(pair <T, V> p);
template <class T> void _print(vector <T> v);
template <class T> void _print(set <T> v);
template <class T, class V> void _print(map <T, V> v);
template <class T> void _print(multiset <T> v);
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
/*-------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

const int N = 10001;
int mod = 1000000007;

void Main()
{
	in(h)
	in(w)
	in(x)
	in(y)
	char arr[h][w];
	loop(i, 0, h)
	{
		loop(j, 0, w)
		{

			cin >> arr[i][j];
			//cout << arr[i][j];
		}
		//cout << endl;
	}
	int cnt = 1;
	loop(i, x, h)
	{
		if (arr[i][y - 1] != '.')
			break;
		cnt++;

	}
	//cout << cnt << endl;
	for (int i = x - 2; i >= 0 ; i--)
	{
		if (arr[i][y - 1] != '.')
			break;
		cnt++;

	}
	// loop(i, 0, h)
	// {
	// 	loop(j, 0, w)
	// 	{

	// 		//cin >> arr[i][j];
	// 		cout << arr[i][j];
	// 	}
	// 	cout << endl;
	// }
	// cout << cnt << endl;
	loop(i, y, w)
	{
		//cout << x - 1 << " " << i << endl;
		//cout << arr[x - 1][i] << endl;
		if (arr[x - 1][i] != '.')
			break;
		//cout << "wd" << endl;
		cnt++;
	}
	//cout << cnt << endl;
	for (int i = y - 2; i >= 0 ; i--)
	{

		if (arr[x - 1][i] != '.')
			break;
		cnt++;

	}
	cout << cnt << endl;
}

int32_t main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int t = 1;
	//cin >> t;
	while (t--)
	{
		Main();
	}
}


//#undef DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll llinf = (1ll<<61)-1;
#define sz(a) int(a.size())
#define all(x) begin(x), end(x)
#ifdef DEBUG
const int DEBUG_END = 26;
#define DOS cout
#include <debug.h>
#else
#define bug(args...) void()
#define cbug(a, args...)
#endif
#define ASSERT(a, o, b, args...) if (!((a)o(b))) bug(a, b, ##args), assert((a)o(b));
#define fi first
#define se second
const int inf = 1000000007, N=1005;
int TC = 1, CN, n;
pair<int, int> a[N];

signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); cout.precision(11), cout.setf(ios::fixed);
    //cin >> TC;// inputAll + resetAll -> solution
    auto kase = [&]()->void {
        cin >> n;
        int ans = 0;
        for (int i = 0; i < n; i++) {
            cin >> a[i].fi >> a[i].se;
            for (int j = 0; j < i; j++) {
                ans += abs(a[i].se-a[j].se) <= abs(a[i].fi-a[j].fi);
            }
        }
        cout << ans << '\n';
    };
    while (CN++!=TC) kase();
}

#include <bits/stdc++.h>

using namespace std;

int main(){
    int N;
    cin >> N;
    vector<pair<int, int>> A(N);
    for(auto& [x, y] : A) cin >> x >> y;
    int ans = 0;
    for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) {
            auto [x1, y1] = A[i];
            auto [x2, y2] = A[j];
            if(abs(y1-y2) <= abs(x1-x2)) ans++;
        }
    cout << ans << endl;
}


#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
#define ll long long
#define scn(n) scanf("%d",&n)
#define lscn(n) scanf("%lld",&n)
#define lpri(n) printf("%lld",n)
#define pri(n) printf("%d",n)
#define pln() printf("\n")
#define priln(n) printf("%d\n",n)
#define lpriln(n) printf("%lld\n",n)
#define rep(i,init,n) for(int i=init;i<n;i++)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define gcd __gcd
#define inf INT_MAX
#define ninf INT_MIN
#define linf LLONG_MAX
#define lninf LLONG_MIN
const ll mod = 1e9 + 7;
const int N = 1e6 + 4;

int solve()
{
	int n; scn(n);

	string s; cin>>s;

	rep(i, 2, n)
	if(s[i] == '1' and s[i - 1] == '1' and s[i - 2] == '1')
	{
		pri(0); return 0;
	}

	vector<int> v;

	int f = -1, l = -1, cnt = 0, prev = -1;

	rep(i, 0, n)
	if(s[i] == '0')
	{
		cnt++; l = i;

		if(f == -1)
		f = i;

		if(prev != -1 and i - prev - 1 != 2)
		{
			pri(0); return 0;
		}

		prev = i;
	}

	ll val = 10000000000;

	if(n == 1 and s[0] == '1')
	{
		lpri(2 * val); return 0;
	}

	if(l != n - 1)
	cnt++;

	// cout<<cnt<<endl;

	lpri(val - cnt + 1);

	return 0;
}

int main()
{
	int t = 1; //scn(t);

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

    return 0;
}

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

bool solve(string s){
	int n = s.size();
  	if(n == 1) return s == "8";
  	if(n == 2){
    	if(stoi(s)%8 == 0) return 1;
      	swap(s[0],s[1]);
      	return stoi(s)%8 == 0;
    }

  	vector<int> cnt(10);
    for(char x : s) cnt[x-'0']++;
  	for(int i = 0; i < 1000; i += 8){
		int x = i;
      	vector<int> nc(10);
      	rep(j, 3){
        	nc[x%10]++;
          	x /= 10;
        }
      	bool ok = true;
      	rep(j, 10) if(nc[j] > cnt[j]) ok = false;
      	if(ok) return true;
    }
  	return 0;
}

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

  	puts(solve(s) ? "Yes" : "No");
}


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

int n, m, k;
vector<long long> a, nxt;

long double solve();
bool check(long double e);

int main() {
  cin >> n >> m >> k;
  a.resize(k);
  for (auto &p : a) cin >> p;
  cout << fixed << setprecision(10);
  auto res = solve();
  if (res < 0)
    cout << "-1" << endl;
  else
    cout << solve() << endl;
  return 0;
}

long double solve() {
  nxt.resize(n + m + 1);
  for (int i = 0; i < n + m + 1; ++i) {
    nxt[i] = i + m;
    for (int j = 0; j < k; ++j)
      if (i <= a[j]) nxt[i] = min(nxt[i], a[j]);
  }
  for (int i = 0; i < k;) {
    int now = a[i], cnt = 0;
    while (i < k && now == a[i]) ++i, ++now, ++cnt;
    if (cnt >= m) return -1;
  }
  long double l = 0, r = 1e18;
  for (int t = 0; t < 200; ++t) {
    long double mid = (l + r) / 2;
    if (check(mid))
      r = mid;
    else
      l = mid;
  }
  return r;
}

bool check(long double e) {
  vector<long double> dp(n + m, 0);
  long double now = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (nxt[i] == i)
      dp[i] = e;
    else
      dp[i] = 1 + now / m;
    now += dp[i];
    now -= dp[i + m];
  }
  return dp[0] <= e;
}

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

#define rep2(i,k,n) for(i64 i=(i64)(k);i<(i64)(n);i++)
#define rep(i,n) rep2(i,0,n)
#define all(x) begin(x),end(x)
#ifdef ENV_LOCAL
#define dump if (1) cerr
#else
#define dump if (0) cerr
#endif

using namespace std;
using namespace std::string_literals;

using i32 = int32_t;
using i64 = int64_t;
using f64 = double;
using f80 = long double;
using vi32 = vector<i32>;
using vi64 = vector<i64>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  i64 n,m,k;
  cin>>n>>m>>k;
  vi64 a(k);
  rep(i,k) cin>>a[i];
  vector<f64> c0(n+1);
  vector<f64> c1(n+1);
  rep(i,k) c1[a[i]] = 1.0;
  f64 s0 = 0;
  f64 s1 = 0;
  rep(ri,n) {
    i64 i = n - 1 - ri;
    s0 += c0[i+1];
    s1 += c1[i+1];
    if (ri >= m) {
      s0 -= c0[i+m+1];
      s1 -= c1[i+m+1];
    }
    if (c1[i] == 1.0) continue;
    else {
      c0[i] = s0 / m + 1.0;
      c1[i] = s1 / m;
    }
  }
  if (abs(1.0 - c1[0]) < 1e-8) {
    cout<<-1<<endl;
  } else {
    cout<<fixed<<setprecision(10)<<c0[0] / (1.0 - c1[0])<<endl;
  }
  return 0;
}


#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define F first
#define S second
#define PB push_back
#define MP make_pair

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

void setIO()
{
    FAST_IO;
}

void setIO (string s) {
    FAST_IO;
 	freopen((s+".in").c_str(),"r",stdin);
 	freopen((s+".out").c_str(),"w",stdout);
}

int main()
{
    setIO();
    int x, y;
    cin >> x >> y;
    if(x<y && x+3>y)
    {
        cout << "Yes";
    }
    else if(x<y)
    {
        cout << "No";
    }
    else if(x>y && y+3>x)
    {
        cout << "Yes";
    }
    else if(x>y)
    {
        cout << "No";
    }
}

#include<bits/stdc++.h>
#define dbug printf("I am here\n");
#define Fast ios_base::sync_with_stdio(false); cin.tie(0);
#define vs           v.size()
#define ss           s.size()
#define sot(v)       sort(v.begin(),v.end())
#define all(v)       (v).begin(), (v).end()
#define rev(v)       reverse(v.begin(),v.end())
#define revsot(v)    sort(v.rbegin(),v.rend())
#define yes          cout<<"Yes"<<endl
#define no           cout<<"No"<<endl
#define ii           pair<int, int>
#define int          long long
#define ll           long long
#define ull          unsigned long long
#define pb           push_back
#define mpp          make_pair
#define Okay         0
#define pi           3.14159

const int mx = 2e5+100;
const int cont = 1e18;
const int mod = 1e9+7;
using namespace std;

void solve(){
    int a, b;
    cin>>a>>b;
    if(abs(a-b)<3)yes;
    else no;

}

int32_t main(){
    Fast;
    int tst;
//    cin>>tst;
//    while(tst--)
    solve();
    return Okay;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	int N;
	cin >> N;
	vector<int> A(N);
	for (auto& x : A) cin >> x;
	ll res = 0;
	map<ll, int> a;
	ll s = 0;
	++ a[0];
	for (int i = 0; i < N; ++ i) {
		if (i % 2) {
			s += A[i];
			res += a[s];
			++ a[s];
		} else {
			s -= A[i];
			res += a[s];
			++ a[s];
		}
	}
	cout << res << endl;
}

# include <bits/stdc++.h>
# define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
# define reps(i, n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
# define rrep(i, n) for(int i=((int)(n)-1); i>=0; --i)
# define rreps(i, n) for(int i=((int)(n)); i>0; --i)
# define ALL(x) (x).begin(), (x).end()
# define SZ(x) ((int)(x).size())
# define pb push_back
# define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)
using namespace std;
using lint = long long;
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; }

void solve() {
    int N;
    cin >> N;
    double x[N];
    rep(i,N) cin >> x[i];

    double ans1=0, ans2=0, ans3=-1;

    cout << fixed << setprecision(20);
    rep(i,N) ans1+=abs(x[i]);
    cout << ans1 << endl;
    lint temp = 0;
    rep(i,N) temp += x[i]*x[i];
    cout << sqrt(temp) << endl;
    rep(i,N) ans3 = max(abs(x[i]), ans3);
    cout << ans3 << endl;

    // cout << ans << endl;
}
signed main() {

    solve();

    return 0;
}


#include<iostream>
#include<string>
using namespace std;
int main() {
  int T,N;
  cin >> T;
  string s1,s2,s3;
  int i,j;
  for(i=0;i<T;i++) {
    cin >> N >> s1 >> s2 >> s3;
    cout << "0";
    for(j=0;j<N;j++)cout << "1";
    for(j=0;j<N;j++)cout << "0";
    cout << endl;
  }
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
	int n;
	cin>>n;
	vector<int>a(n),b(n),c(n);
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		if(i)a[i]=max(a[i],a[i-1]);
	}
	for(int i=0;i<n;i++)
		cin>>b[i];
	for(int i=0;i<n;i++)
	{
		c[i]=a[i]*b[i];
		if(i)c[i]=max(c[i],c[i-1]);
	}
	for(int i=0;i<n;i++)
	cout<<c[i]<<endl;
	return 0;
}


#include <iostream>
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <tuple>
#include <math.h>
#include <set>
#include <cassert>
#include <stack>
#include <bitset>
#include <map>
#include <queue>
#include <random>
#include <unordered_set>
#include <unordered_map>

#define pqueue priority_queue
#define pb(x) push_back(x)
#define endl '\n'
#define all(x) x.begin(), x.end()
#define int long  long

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ull> vull;
typedef vector<ll> vll;
// typedef tuple<ll, ll, ll> tiii;
typedef pair<int, int> pii;
typedef vector<pair<int, int> > vpii;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<vector<ll> > vvll;
typedef vector<char> vc;

const int inf = 1e9 + 228;
//const int mod = 1e9 + 7;

void fast_io() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    freopen("river.in", "r", stdin);
//    freopen("river.out", "w", stdout);
}

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

int phi (int n) {
    int result = n;
    for (int i=2; i*i<=n; ++i)
        if (n % i == 0) {
            while (n % i == 0)
                n /= i;
            result -= result / i;
        }
    if (n > 1)
        result -= result / n;
    return result;
}

int binpow(int n, int k, int mod){
    if(!k)
        return 1;
    if(k&1)
        return binpow(n, k-1, mod) * n % mod;
    int b = binpow(n, k/2, mod);
    return b*b%mod;
}

void solve(){
    int n, s, k;
    cin >> n >> s >> k;
    int a = k;
    int b = n-s;
//    cout << a << " " << b << endl;
//    cout << b * binpow(a, n-2, n) % n << endl;
    if(b % gcd(a, n)){
        cout << -1 << endl;
    } else{
        int lol = gcd(a, n);
        a /= lol;
        b /= lol;
        int st_n = n;
        n /= lol;
        int x = b * binpow(a, phi(n)-1,  n) % n;
        int kek = (st_n - x) / n;
        x += kek * n;
        x += n;
        x %= st_n;
        cout << x << endl;
    }

}

signed main() {
    fast_io();
    srand(time(NULL));
    cout << fixed << setprecision(10);
    int q = 1;
    cin >> q;
    while (q--)
        solve();
}

#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
using namespace std;

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

int main(){
    long T;
    long long N,S,K,d,ans,x0,y0;
    cin>>T;
    for(int i=0;i<T;++i){
        cin>>N>>S>>K;
        if(S%__gcd(K,N)!=0){
            ans=-1;
        }
        else{
            d=__gcd(K,N);
            K=K/d;
            N=N/d;
            S=S/d;
            extGCD(K,N,x0,y0);
            x0%=N;
            x0=(-x0*S)%N;
            if(x0<0){
                x0+=N;
            }
            ans=x0;
        }
        cout<<ans<<endl;
    }
    return 0;
}


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

ll dp[5050][5050];

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int h, w, n;
    cin>>h>>w>>n;
    vector<string> a(h, string(w, '.'));
    rep(i,n){
        int x,y;
        cin>>x>>y;
        cin>>a[--x][--y];
    }
    dp[0][0]=1;
    rep(i,h*w-n)dp[0][0]=3*dp[0][0]%mod;
    ll inv3 = (mod+1)/3;
    rep(i,h)rep(j,w){
        dp[i][j] %= mod;
        if(a[i][j]=='X'){
            dp[i+1][j]+=dp[i][j];
            dp[i][j+1]+=dp[i][j];
        } else if(a[i][j]=='R'){
            dp[i][j+1]+=dp[i][j];
        } else if(a[i][j]=='D'){
            dp[i+1][j]+=dp[i][j];
        } else {
            dp[i+1][j]+=2*inv3*dp[i][j];
            dp[i][j+1]+=2*inv3*dp[i][j];
        }
    }
    cout<<dp[h-1][w-1]<<endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1000000010
#define EPS 1e-9
#define F first
#define S second

#define debug(x) cout<<x<<endl;
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define lp(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
#define int long long
#define endl "\n"

typedef pair<int,int> PII;
typedef pair<int,string> PIS;
typedef pair<string,int> PSI;

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

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

char calc(char c,char d){
  if(c==d) return c;
  else{
    if(c=='B'){
      if(d=='W') return 'R';
      else return 'W';
    }else if(c=='W'){
      if(d=='B') return 'R';
      else return 'B';
    }else{
      if(d=='W') return 'B';
      else return 'W';
    }
  }
}

signed main(){
  //cin.tie(0);
  //ios::sync_with_stdio(false);
  int n;
  cin>>n;
  string s;
  cin>>s;
  vector<int> trg;
  int num=1;
  while(num<400000){
    trg.push_back(num);
    num*=3;
  }
  trg.push_back(num*3);
  int now=0;
  string nxt="";
  while(s.size() != 1){
    now=0;
    while( (s.size()-1) % trg[now+1] == 0) now++;
    //cout<<s<<" "<<now<<endl;

      nxt="";
      //cout<<trg[now-1]<<endl;
      for(int i=0;i<s.size()-trg[now];i+=trg[now]){
	nxt+=calc(s[i],s[i+trg[now]]);

      }
      s=nxt;
    //cout<<s<<endl;
  }
  cout<<s<<endl;

  return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=400010;
int a[N];
const int mod=3;
int n;
ll qmi(ll a, ll k){
	ll res = 1;
	while(k){
		if(k & 1) res = res * a % mod;
		a = a * a % mod;
		k >>= 1;
	}
	return res;
}
ll C(ll a, ll b, ll p){
	if(b > a) return 0;
	ll res = 1;
	for(int i = 1, j =a;i<=b;i++,j--){
		res = res * j % p;
		res = res * qmi(i, p - 2) % p;
	}
	return res;
}
ll lucas(ll a, ll b, ll p){
	if(a < p && b < p) return C(a, b, p);
	return C(a % p, b % p, p) * lucas(a / p, b / p, p) % p;
}
int main(){
    scanf("%d",&n);
    getchar();
    char ch;
    for(int i=1;i<=n;i++){
        scanf("%c",&ch);
        if(ch=='B')a[i]=0;
        else if(ch=='W')a[i]=1;
        else a[i]=2;
    }
    ll ans=0;
    for(int i=1;i<=n;i++){
        ans=(ans+lucas(n-1,i-1,mod)*a[i])%mod;
    }
    if(n%2==0)ans=-ans;
    ans=(ans+mod)%mod;
    if(ans==0)printf("B\n");
    else if(ans==1)printf("W\n");
    else printf("R\n");
}


#include<bits/stdc++.h>

using namespace std;

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

    vector<int> A(n);
    for(auto& x : A)
        cin >> x;

    int sum = 0;
    for(auto x : A)
        sum += x;

    int need = m * (n+1) - sum;
    if(need > k)
        cout << -1 << endl;
    else if(need < 0)
        cout << 0 << endl;
    else
        cout << need << endl;
}

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

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

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

using namespace std;
using ll = long long;

int main (){
   int N,K,M;
   cin >> N >> K >>M;
   int A[N];
   int sum=0;
   for (int i=0;i<N-1;i++){
       cin >> A[i];
        sum += A[i];
   }
   int g = M*N - sum;
   if (g>K)cout << "-1";
   else cout <<max(0, g);

}