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

#include<cstdio>
#include<cstring>

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

int N, M;
int to[400005], ne[400005], c[200005], he[100001];
int kotae[100001];

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += 72 - dig >> 3;
	return tmp;
}
const int MAX = 1000;
class shuturyoku_unko {
public:
	char C[MAX * 4];
	constexpr shuturyoku_unko() : C() {
		rep(i, MAX) {
			int X = i;
			rep(j, 3) {
				C[i * 4 + 2 - j] = '0' + X % 10;
				X /= 10;
			}
			C[i * 4 + 3] = ' ';
		}
	}
};
constexpr shuturyoku_unko f;
const int dm = 1 << 17;
char* dn = cn, * di = dn, * owad = dn + dm - 20;
void putint(int A) {
	if (owad < di) {
		fwrite(dn, 1, di - dn, stdout);
		di = dn;
	}
	if (A >= 1000) {
		int dig = 1;
		if (A >= 100000) dig = 3;
		else if (A >= 10000) dig = 2;
		memcpy(di, f.C + A / 1000 * 4 + 3 - dig, dig);
		memcpy(di + dig, f.C + A % 1000 * 4, 4);
		di += dig + 4;
	}
	else {
		int dig = 1;
		if (A >= 100) dig = 3;
		else if (A >= 10) dig = 2;
		memcpy(di, f.C + A * 4 + 3 - dig, dig + 1);
		di += dig + 1;
	}
}

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

	N = getint();
	M = getint();

	int k = 2;
	rep1(i, M) {
		int u = getint(), v = getint();
		c[i] = getint();
		to[k] = v;
		ne[k] = he[u];
		he[u] = k++;
		to[k] = u;
		ne[k] = he[v];
		he[v] = k++;
	}

	int Q[100001], q = 0;
	Q[q++] = 1;
	kotae[1] = 1;
	while (q) {
		int tmp = Q[--q];
		int oya = kotae[tmp];
		for (int k = he[tmp]; k; k = ne[k]) {
			int t = to[k];
			if (!kotae[t]) {
				int e = c[k >> 1];
				kotae[t] = e + (oya == e);
				Q[q++] = t;
			}
		}
	}

	rep1(i, N) putint(kotae[i] - ((kotae[i] > N) << 1));
	fwrite(dn, 1, di - dn, stdout);

	Would you please return 0;
}

#include <bits/stdc++.h>
#define watch(x) std::cout << (#x) << " is " << (x) << std::endl
using LL = long long;

int main() {
	// freopen("C:\\Users\\dna049\\cf\\in", "r", stdin);
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int n, m, k;
	std::cin >> n >> m >> k;
	std::vector<int> a(k);
	for (auto &x: a) std::cin >> x;
	for (int i = m - 1; i < k; ++i) if (a[i] - a[i - m + 1] == m - 1) {
		std::cout << "-1\n";
		return 0;
	}
	std::vector<double> b(n + m), c(n + m);
	double sb = 0, sc = 0;
	for (int i = n - 1; i >= 0; --i) {
		if (a.size() && i == a.back()) {
			b[i] = 0;
			c[i] = 1;
			a.pop_back();
		} else {
			b[i] = sb / m + 1;
			c[i] = sc / m;
		}
		sb += b[i] - b[i + m];
		sc += c[i] - c[i + m];
	}
	std::cout.precision(8);
	std::cout << std::fixed << b[0] / (1 - c[0]) << std::endl;
	return 0;
}


#include <bits/stdc++.h>
#include <unistd.h>
#define FAST_IO ios::sync_with_stdio(0);cin.tie(NULL)
#define all(v) (v).begin(),(v).end()
using namespace std;

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

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

typedef long long Long;
typedef long double Double;
typedef unsigned long long ULong;
typedef pair<Double, Double> Pair;

const int N = 1e6;
const Double EPS = 1e-9;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
Long random(Long a, Long b) { return uniform_int_distribution<Long> (a, b) (rng); }

Pair operator + (const Pair& a, const Pair& b) {
  return {a.first + b.first, a.second + b.second};
}

Pair operator - (const Pair& a, const Pair& b) {
  return {a.first - b.first, a.second - b.second};
}

Pair operator / (const Pair& a, Double k) {
  return {a.first / k, a.second / k};
}

void Solve(void) {
  Long n, m, k;
  cin >> n >> m >> k;
  if (k == 0) {
    vector<Double> dp(n + m + 2, 0.0);
    vector<Double> suffix(n + m + 2, 0.0);
    for (int i = n - 1; i >= 0; i--) {
      Double sum = suffix[i + 1] - suffix[i + m + 1];
      dp[i] = sum / m + 1;
      suffix[i] = dp[i] + suffix[i + 1];
    }
    cout << dp[0] << '\n';
  } else {
    vector<bool> is_hole(n + m + 2, false);
    for (int i = 0; i < k; i++) {
      int a;
      cin >> a;
      is_hole[a] = true;
    }
    vector<int> prefix(n + m + 2, 0);
    for (int i = 1; i <= n; i++) {
      prefix[i] = prefix[i - 1];
      if (is_hole[i]) prefix[i]++;
    }
    for (int i = 1; i < n - m + 1; i++) {
      if (prefix[i + m - 1] - prefix[i - 1] == m) {
        cout << -1 << '\n';
        return;
      }
    }
    vector<Pair> dp(n + m + 2, {0.0, 0.0});
    vector<Pair> suffix(n + m + 2, {0.0, 0.0});
    Pair one = {0.0, 1.0};
    for (int i = n - 1; i >= 0; i--) {
      if (is_hole[i]) {
        dp[i] = {1.0, 0};
      } else {
        Pair sum = suffix[i + 1] - suffix[i + m + 1];
        dp[i] = sum / m + one;
      }
      suffix[i] = dp[i] + suffix[i + 1];
    }
    Double ans = dp[0].second / (1.0 - dp[0].first);
    cout << fixed << setprecision(10) << ans << '\n';
  }
}

int main(void) {
  FAST_IO;
  int t = 1;
  //cin >> t;
  while (t--) Solve();
  return 0;
}

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
using namespace std;
// using namespace __gnu_pbds;
#define int ll
#define rep(i, a, b) for (int (i) = (a); (i) < (b); ++(i))
#define per(i, a, b) for (int (i) = (a); (i) >= (b); --(i))
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define ff first
#define ss second
#define sz(x) ((int) x.size())
#define rd(type, ...) type __VA_ARGS__; sc(__VA_ARGS__)
#define rda(a, n) rep(i, 0, n) cin >> a[i];
#define rdaa(a, n, m) rep(i, 0, n) rep(j, 0, m) cin >> a[i][j];
#define rdv(type, v, n) vector<type> v(n); rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w) vector<vector<type>> v(h, vector<type>(w)); rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
#define rdv2(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_rdv2_Init(n, __VA_ARGS__); for(int w_=0; w_<n; ++w_){MACRO_rdv2_Scan(w_, __VA_ARGS__);}
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : ", debug_out(__VA_ARGS__)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define err2(x) {wr(x); return;}
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// typedef tree<int, null_type, less, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
void rset();
void init_test();
void solve();
signed main() {ios::sync_with_stdio(false); cin.tie(0);
  cout << fixed; cout.precision(20); init_test(); return 0;}
template<typename T> void chmin(T &a, T b) {if (a > b) a = b;}
template<typename T> void chmax(T &a, T b) {if (a < b) a = b;}
template<typename T> void MACRO_rdv2_Init(int n, T& t) { t.resize(n); }
template<typename First, typename... Rest> void MACRO_rdv2_Init(int n, First& first, Rest& ...rest) { first.resize(n); MACRO_rdv2_Init(n, rest...); }
template<typename T> void MACRO_rdv2_Scan(int p, T& t) { std::cin >> t[p]; }
template<typename First, typename... Rest> void MACRO_rdv2_Scan(int p, First& first, Rest& ...rest) { std::cin >> first[p]; MACRO_rdv2_Scan(p, rest...); }
template<typename T> void wrv(const vector<T> &v) {rep(__ii, 0, sz(v)) {if (__ii) cout << ' '; cout << v[__ii];} cout << '\n';}
template<typename T> void wrm(const vector<vector<T>> &v) {rep(__ii, 0, sz(v)) {rep(__jj, 0, v[__ii].size()) {if (__jj) cout << ' '; cout << v[__ii][__jj];} cout << '\n';}}
template<typename T> void sc(T& x) {cin >> x;}
template<typename Head, typename... Tail> void sc(Head& head, Tail&... tail) {cin >> head; sc(tail...);}
template<typename T> void wr(const T& x) {cout << x << '\n';}
template<typename Head, typename... Tail> void wr(const Head& head, const Tail&... tail) {cout << head << ' '; wr(tail...);}
template<typename T> void wrf(const T& x) {cout << x << endl;}
template<typename Head, typename... Tail> void wrf(const Head& head, const Tail&... tail) {cout << head << ' '; wrf(tail...);}
template<typename T> void debug_out(const T& x) {cerr << x << '\n';}
template<typename Head, typename... Tail> void debug_out(const Head& head, const Tail&... tail) {cerr << head << ' '; debug_out(tail...);}
template<typename... T> void err(const T&... cod) {wr(cod...); exit(0);}

bool cmp(pii a, pii b) {
  return 2 * a.ff + a.ss > 2 * b.ff + b.ss;
}

void solve() {
  rd(int, n);
  int score = 0;
  vpii a(n);
  rep(i, 0, n) {
    cin >> a[i].ff >> a[i].ss;
    score -= a[i].ff;
  }
  sort(all(a), cmp);
  rep(i, 0, n) {
    score += 2 * a[i].ff + a[i].ss;
    if (score > 0) err2(i + 1);
  }
  assert(false);
}

void init_test() {
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int qq = 1;
  // cin >> qq;
  while (qq--) solve();
}


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

// template {{{
#define  range(i, l, r) for (int i = (int)(l); i < (int)(r); (i) += 1)
#define rrange(i, l, r) for (int i = (int)(r) - 1; i >= (int)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

constexpr i32 mod   = 1e9 + 7;
// constexpr i32 mod   = 998244353;

constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

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

struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

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

void solve() {
  int n = input();
  vector< int > as(n), bs(n), ps(n);
  cin >> as >> bs >> ps;

  for (auto &p : ps) p--;

  vector< int > used(n, false);

  bool valid = true;
  vector< pair< int, int > > ans;

  for (int v = 0; v < n; v++) {
    if (used[v]) continue;

    vector< int > us;
    us.emplace_back(v);
    used[v] = true;

    for (int u = ps[v]; u != v; u = ps[u]) {
      us.emplace_back(u);
      used[u] = true;
    }

    if ((int)us.size() == 1) continue;

    int min_b = inf;
    int idx_b = -1;

    for (int i = 0; i < (int)us.size(); i++) {
      int u = us[i];
      if (as[u] <= bs[ps[u]]) {
        valid = false;
      }

      if (bs[ps[u]] < min_b) {
        min_b = bs[ps[u]];
        idx_b = i;
      }
    }

    int sz = us.size();
    for (int i = (idx_b - 1 + sz) % sz; i != idx_b; i = (i - 1 + sz) % sz) {
      ans.emplace_back(us[i] + 1, us[(i + 1) % sz] + 1);
    }
  }

  if (not valid) {
    cout << -1 << endl;
    return;
  }

  cout << (int)ans.size() << endl;
  for (auto &t : ans) {
    cout << t.first << " " << t.second << endl;
  }
}

signed main() {
  solve();
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0,endrep=(n); i<endrep; ++i)
#define rep1(i,n) for(ll i=1,endrep=(n); i<=endrep; ++i)
#define revrep(i,n) for(ll i=(n)-1; i>=0; --i)
inline constexpr ll Inf = (1ULL << 60) -123456789;
#define fastio cin.tie(0); ios_base::sync_with_stdio(false); cout<<fixed<<setprecision(10);
#define newl '\n'
#define YN(e) ((e)?"Yes":"No")
#define all(a) begin(a),end(a)
#define rall(a) rbegin(a),rend(a)
template <class T,class U> bool updmax(T& a, U b) { if(b>a){ a=b; return true;} return false;}
template <class T,class U> bool updmin(T& a, U b) { if(b<a){ a=b; return true;} return false;}
inline constexpr int Mod = 1000000007;
//inline constexpr int Mod = 998244353;
#define dbg(a) cout << #a << ": " << a << endl;
#define dbg1(a,n) cout<<#a<<": "; rep(i,n) cout<<a[i]<<" "; cout<<endl;
#define dbg2(m,h,w) cout<<#m<<":"<<endl; rep(i,h){ rep(j,w)cout<<m[i][j]<<" "; cout<<endl; }
int rng(int n) { return rand()/(RAND_MAX+1.0)*n; }

int main() {
    fastio;
    ll ans{Inf};
    ll N,M;
    cin >> N >> M;
    vector<ll> a(N);
    rep(i,N) cin >> a[i];
    vector<vector<int>> pos(1500005);
    rep(i,N) pos[a[i]].push_back(i);
    rep(i,1500005) {
        int p = -1;
        pos[i].push_back(N);
        for (int j : pos[i]) {
            if (j-p > M) updmin(ans, i);
            p = j;
        }
    }
    cout << ans << endl;
}


//#include <atcoder/all>
//using namespace atcoder;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#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 ALL(v) v.begin(), v.end()
#define bit(n) (1LL<<(n))
#define FIX(d) fixed << setprecision(d)
using P = pair<int, int>;
using LP = pair<ll, ll>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = 1e9;
const ll LINF = (1LL<<60);

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int x,y,z;
  cin >> x >> y >> z;
  REPR(i, 1000000){
    double p1 = (double)y / x;
    double p2 = (double)i / z;
    if(p1>p2){
      cout << i << endl;
      return 0;
    }
  }

  return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define vec vector<int>
#define vecp vector<pair<int,int>>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define fi first
#define se second
#define fr1(i,a,b) for(int i=a;i<b;i++)
#define fr2(i,a,b) for(int i=a;i>=b;i--)
#define fr3(i,a,b) for(int i=a;i<=b;i++)
#define umap unordered_map<int,int>
#define omap map<int,int>
#define uset unordered set<int>
#define oset set<int>
#define pr pair<int,int>
#define endl "\n"

void solve()
{
    int a,b;
    cin>>a>>b;
    a=(2*a)+100;
    cout<<a-b<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t=1;
    //cin>>t;
    fr3(i,1,t){
        solve();
    }
}


#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 {
#ifndef ONLINE_JUDGE
~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 imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// debug & operator << (debug & dd, P p) { dd << ", " << p.x << ", " << p.y << ")"; return dd; }

#define ll long long
#define ld long double

void test_case() {
    int n;
    cin >> n;
    set<int> a;
    for(int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        if(x <= 0 || x > n) {

        } else {
            a.insert(x);
        }
    }
    if(a.size() == n) {
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
}

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

    int T = 1;
    // cin >> T;
    while(T--) {
        test_case();
    }
    return 0;
}

#include <iostream>
#include<string>
#include <math.h>
#include<vector>
#include<algorithm>
#include <iomanip>
#include<map>
using ll = long long;
using namespace std;

int main() {
	int n; cin >> n;
	vector<int>a(n + 1);
	for (int i = 1; i <= n; i++) {
		int x; cin >> x;
		a[x]++;
	}
	bool ok = true;
	for (int i = 1; i <= n; i++) {
		if (a[i] > 1)ok = false;
	}
	if (ok)cout << "Yes" << endl;
	else cout << "No" << endl;
}


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

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

  vector<int> A(N,1), P(N,-1);
  P[0] = 0;

  for (int i = 0; i < N; i++) {
    if (P[i] == 1) continue;
    else if (P[i] == -1) {
      for (int j = i; j < N; j += i+1) {
        P[j] = 1;
        A[j]++;
      }
      if (i <= 1000) {
        int i2 = (i+1)*(i+1);
        while(i2 <= N) {
          for (int j = i2; j <= N; j += i2) {
            A[j-1]++;
          }
          i2 *= (i+1);
        }
      }
    }
  }

  for (int i = 0; i < N; i++) {
    cout << A[i];
    if (i == N-1) cout << endl;
    else cout << " ";
  }

return 0;
}

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

int main(){
  int x;
  cin  >> x;
  if(x>=0) cout << x<< endl;
  else cout << 0<< endl;;
}


//Date 16/06/2021
//practice
#include <bits/stdc++.h>
using namespace std;

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

#define   all(x)        (x).begin(),(x).end()
#define   rep(i,a,b)    for(ll i = a; i<b ; i++)
#define   sz(x)         (int)((x).size())
#define   PI             3.141592653589
#define   mod            1000000007
#define   MAX            1000005 //1e6+5
#define   ppc             __builtin_popcount
#define   ppcll           __builtin_popcountll
long long const INF = 1e13;

ll power(ll a, ll b)
{   ll result =1;
    while(b)
    {   if(b%2) result=(result*a);
        a=(a*a);
        b/=2;}
    return result;
}
void sol()
{
    int n; cin>>n;
    vector<int> arr(n);
    int sum = 0;
    rep(i,0,n) {cin>>arr[i];sum+=arr[i];}

    //SUBSET SUM PROBLEM
    bool dp[n+1][sum+1];
    for(int i=0;i<=n;i++)
    {
        dp[i][0] = true;
    }
    for(int i=1;i<=sum;i++)
    {
        dp[0][i] = false;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=sum;j++)
        {
            if(dp[i-1][j] == true) dp[i][j] = true; //1st condition
            else if(j-arr[i-1]>=0 && dp[i-1][j-arr[i-1]]) dp[i][j] = true; //2nd condition
            else dp[i][j] = false;
        }
    }
    int str = ceil(sum*1.0/2);
    for(int i = str;i<=sum;i++)
    {
        if(dp[n][i]) {cout<<i<<" "; return;}

    }

}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int T=1; //cin>>T;
    while(T--) sol();
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
  bitset<100100> b;
  b.set(0);
  int n;
  cin >> n;
  int s = 0;
  for(int i = 0; i < n; i++){
    int x;
    cin >> x;
    b |= b<<x;
    s += x;
  }
  int ans = s;
  for(int i = 0; i <= s; i++)
    if(b[i])
      ans = min(ans, max(i, s - i));
  cout << ans << '\n';
  return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <functional>
#include <bitset>
#include <cstddef>
#include <type_traits>
#include <vector>

using namespace std;
using lint = long long int;
long long int INF = 1001001001001001LL;
int inf = 1000000007;
long long int MOD = 1000000007LL;
double PI = 3.1415926535897932;

template<typename T1,typename T2>inline void chmin(T1 &a,const T2 &b){if(a>b) a=b;}
template<typename T1,typename T2>inline void chmax(T1 &a,const T2 &b){if(a<b) a=b;}

#define ALL(a) a.begin(),a.end()
#define RALL(a) a.rbegin(),a.rend()
#define rep(i, n) for(int i=0;i<(int)(n);i++)

int main() {

  lint b, c; cin >> b >> c;
  map<lint, lint> imos;

  // -1 しまくる
  {
    lint l = min(b - c / 2, b);
    lint r = max(b - c / 2, b);
    imos[l]++;
    imos[r + 1]--;

    // cerr << l << " " << r << endl;
  }
  // 最後に -1 倍する
  {
    lint l = min(-(b - (c - 1) / 2), -b);
    lint r = max(-(b - (c - 1) / 2), -b);
    imos[l]++;
    imos[r + 1]--;
    // cerr << l << " " << r << endl;
  }
  // -1 倍して、-1 しまくる
  {
    lint l = min(-b - (c - 1) / 2, -b);
    lint r = max(-b - (c - 1) / 2, -b);
    imos[l]++;
    imos[r + 1]--;
    // cerr << l << " " << r << endl;
  }
  // 最初と最後に -1 倍する
  {
    lint l = min(b + (c - 2) / 2, b);
    lint r = max(b + (c - 2) / 2, b);
    imos[l]++;
    imos[r + 1]--;
  }

  lint pre = -1e18 * 3;
  lint num = 0;
  lint ans = 0;
  for (auto e : imos) {
    lint cur = e.first;
    // cerr << "cur = " << cur << " num = " << num << endl;
    if (num > 0) {
      // cerr << "add: " << cur - pre << endl;
      ans += (cur - pre);
    }
    pre = cur;
    num += e.second;
  }

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

#include <set>
#include <unordered_set>
#include <iostream>
#include <stack>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <cmath>
#include <numeric>
#include <algorithm>
#include <queue>
#include <bitset>

#define INFL 100100100100100100
#define INF 1100100100
using namespace std;

int main() {
	long long b;
	cin >> b;
	long long c;
	cin >> c;
	long long br, bl, brr, bll;
	br = b + (c-2)/2;
	bl = b - c/2;
	brr = -b + (c-1)/2;
	bll = -b - (c-1)/2;
	if (br < brr || bl < bll) {
		swap(br, brr);
		swap(bl, bll);
	}
	cout << br - bl + 1 + brr - bll + 1 - (bl <= brr ? brr - bl + 1: 0) << endl;
	return 0;
}


#include <bits/stdc++.h>
// #include <atcoder/all>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define repm(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define all(v) v.begin(), v.end()
#define rall(x) (x).rbegin(), (x).rend()
#define pll pair<long long, long long>
#define pb emplace_back
#define mp make_pair
#define mt make_tuple
#define vc vector<char>
#define vvc vector<vc>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vvb = vector<vb>;
template <class T>bool chmax(T &a, const T &b){if (a < b){a = b;return 1;}return 0;}
template <class T>bool chmin(T &a, const T &b){if (b < a){a = b;return 1;}return 0;}

int main(){
  ll r,x,y;
  cin>>r>>x>>y;
  ll euc = x*x+y*y;
  ll r2 = r*r;
  if (euc >= r2)cout<<ceil(sqrt(euc/(long double)r2))<<endl;
  else cout<<2<<endl;
}

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define rep2(i, j, n) for(int i=j;i<(int)(n);i++)
#define all(v) v.begin(), v.end()
using ll = long long;
using P = pair<int, int>;

int main(){
    char a;
    int res1 =0 ,res2 = 0;
    rep(i, 3){
        cin >> a;
        res1 += (int)(a - '0');
    }
    rep(i, 3){
        cin >> a;
        res2 += (int)(a - '0');
    }
    cout << max(res1, res2) << endl;
}


#include <algorithm>
#include <numeric>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <numeric>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define DEBUG(x) std::cout << '>' << #x << ':' << x << '\n';
#define PRINTVEC(x) for (int i = 0; i < (int)x.size(); i++) std::cout << x[i] << ' '; std::cout << '\n';
#define PRINTPAIR(x) for (int i = 0; i < (int)x.size(); i++) std::cout << x[i].first << ' ' << x[i].second << '\n'; std::cout << '\n';
#define PRINTSET(x) for (auto it = x.begin(); it != x.end(); it++) { std::cout << *it << ' '; } std::cout << '\n';
#define io std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
const int INF = 1<<29;
const double PI = acos(-1.0);
typedef long long ll;
const std::vector<int> GRID = {0,1,0,-1,0}; // for(k < 4) r=x+GRID[k],c=y+GRID[k+1]; if(0<=r&&r<m&&0<=c&&c<n) do smthg;
using namespace std;

// 3, 7, 13, 61, 61, 421, 841, 2521

ll lcm(ll a, ll b) {
    //return (a * b) / gcd(a,b);
    return (a / __gcd(a,b)) * b;
}

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

    ll n; cin >> n;
    ll ans = 1;
    for (ll i = 2; i <= n; i++) {
        ans = lcm(ans,i);
    }
    cout << ans + 1 << endl;

    #ifndef ONLINE_JUDGE
    std::cout << '\n' << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
    #endif
    return 0;
}

// #pragma GCC target ("avx2")
// #pragma GCC optimization ("O2")
// #pragma GCC optimization ("unroll-loops")
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define int ll
#define pb push_back
#define mp make_pair
#define pob pop_back
#define __bp __builtin_popcount
using pii = pair<int,int>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;
using pll = pair<ll,ll>;
using pdd = pair<ld,ld>;
using vint = vector<int>;
using vll = vector<ll>;
using vpii = vector<pii>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpll = vector<pll>;
#define F first
#define S second
const int N = 1e5+5;
const int Inf = 1e9+7;
// const int Inf = 998244353;
const ll INF = 1e18+7;
const int Mod = 998244353;
const ld eps = 1e-9;
#ifndef M_PI
#define M_PI 3.1415926535
#endif
// const int M = 1e7+5;

void io()
{
	ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	// freopen("sparse.in", "r", stdin);
	// freopen("sparse.out", "w", stdout);
	// cout << setprecision(9) << fixed;
}

int n;

ll gc(ll x, ll y)
{
	if (y == 0)
		return x;
	return gc(y, x % y);
}

void solve()
{
	ll ans = 1;
	cin >> n;
	for (int i = 2; i <= n; ++i)
	{
		ll x = ans * i / gc(ans, i);
		ans = x;
	}
	cout << ans + 1;
}

signed main()
{
	io();
	int tt = 1;
	// cin >> tt;
	while (tt--) {
		solve();
		// cerr << '\n';

	}
}


#include <bits/stdc++.h>
//#pragma once
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define F first
#define S second
#define vi vector<int>
#define pii pair<int,int>
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORN(i, a, b) for(int i = (a); i < (b); ++i)
#define FORD(i, a, b) for(int i = a; i >=b; --i)
#define pb push_back
#define mp make_pair
#define yes cout <<"YES"<<"\n"
#define no  cout << "NO"<<"\n"
#define MOD 1000000007
#define maxn 1000005
#define MIN(x,y) if (x > (y)) x = (y)
#define MAX(x,y) if (x < (y)) x = (y)
#define reset(x) memset(x, 0, sizeof(x))
#define all(x) (x.begin(),x.end())
#define ll long long
#define db double
#define ull unsigned long long
#define udb unsigned double
#define ui unsigned int
#define ldb long double
#define ulli unsigned long long int
#define lli long long int
#define str string
//#define fl Team Flash a nham float
#define fl float
#define TASK "A"
const int INF = 1e9+5;
using namespace std;
// ATTEMPED 1
int n, a[1001];
int c = 0;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    if(fopen(TASK".inp", "r")) {
        freopen(TASK".inp","r",stdin);
        freopen(TASK".out","w",stdout);
    }
    scanf("%d", &n);
    FOR(i,1,n) scanf("%d", &a[i]);
    FOR(i,1,n){
        if(a[i] > 10) c += a[i] - 10;
    }
    printf("%d", c);
}
// Designed and Coded by YasashiSensei.

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

int main() {
    short n; cin >> n;
    short a;
    int sum = 0;

    for(short i = 0; i < n; i++) {
        cin >> a;
        if(a > 10) {
            sum += a-10;
        }
    }

    cout << sum;

return 0;
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <utility>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <limits>
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i)
using namespace std;
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(a > b){ a = b; return 1; } return 0; }
template<class T> inline int  sz(T &a) { return a.size(); }
using ll = long long; using ld = long double;
using pi = pair<int,int>; using pl = pair<ll,ll>;
using vi = vector<int>; using vvi = vector<vi>;
using vl = vector<ll>; using vvl = vector<vl>;
const int inf = numeric_limits<int>::max();
const ll infll = numeric_limits<ll>::max();
int main()
{
  string s; cin >> s;
  ll m; cin >> m;
  int n = sz(s);
  ll mx = 0;
  vl arr(n);
  rep(i,n) {
    ll tmp = s[i] - '0';
    arr[i] = tmp;
    chmax(mx, tmp);
  }
  if(n == 1) {
    if(mx <= m) cout << 1 << "\n";
    else cout << 0 << "\n";
    return 0;
  }
  ll lb = mx, ub = 1000000000000000001;
    ll an = s[0] - '0';
  while(ub - lb > 1) {
    ll B = (ub + lb) / 2;
//    cout << B << "\n";
    ll x = an;
    int cnt = 0;
    while(x <= m/B) {
      x *= B;
//      cout << x << "\n";
      cnt++;
      if(cnt == n-1) break;
    }

    if(cnt==n-1) {
      ll sum = 0;
      ll buf = 1;
      for(int j = n-1; j >= 1; --j) {
        sum += arr[j] * buf;
        buf *= B;
      }
      if(an*buf <= m - sum) {
        lb = B;
      }
      else ub = B;
    }
    else ub = B;
  }
  cout << lb-mx << "\n";

  return 0;
}

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define ch() getchar()
#define pc(x) putchar(x)
template<typename T>inline void read(T&x){
	int f;char c;
	for(f=1,c=ch();c<'0'||c>'9';c=ch())if(c=='-')f=-f;
	for(x=0;c<='9'&&c>='0';c=ch())x=x*10+(c&15);x*=f;
}
template<typename T>inline void write(T x){
	static char q[64];int cnt=0;
	if(!x)pc('0');if(x<0)pc('-'),x=-x;
	while(x)q[cnt++]=x%10+'0',x/=10;
	while(cnt--)pc(q[cnt]);
}
const int maxn=1005,mod=1000000007;
int mo(const int x){
	return x>=mod?x-mod:x;
}
int fac[maxn],iac[maxn];
int binom(int n,int m){
	return 1ll*iac[m]*iac[n-m]%mod*fac[n]%mod;
}
char caa[5],cab[5],cba[5],cbb[5];
int main(){
	int n;read(n);if(n==2)return puts("1"),0;fac[0]=fac[1]=iac[0]=iac[1]=1;
	for(int i=2;i<=n;++i)iac[i]=1ll*(mod-mod/i)*iac[mod%i]%mod;
	for(int i=2;i<=n;++i)iac[i]=1ll*iac[i-1]*iac[i]%mod,fac[i]=1ll*fac[i-1]*i%mod;
	scanf("%s%s%s%s",caa,cab,cba,cbb);int ans=0;
	if(cab[0]=='A'){
		if(caa[0]=='A')ans=1;
		else{
			if(cba[0]=='A'){
				ans=1;
				for(int i=1;n-3-(i-1)>=i;++i)
					ans=mo(ans+binom(n-3-(i-1),i));
			}
			else{
				for(int i=0;i<=n-3;++i)
					ans=mo(ans+binom(n-3,i));
			}
		}
	}
	else{
		if(cbb[0]=='B')ans=1;
		else{
			if(cba[0]=='B'){
				ans=1;
				for(int i=1;n-3-(i-1)>=i;++i)
					ans=mo(ans+binom(n-3-(i-1),i));
			}
			else{
				for(int i=0;i<=n-3;++i)
					ans=mo(ans+binom(n-3,i));
			}
		}
	}
	write(ans),pc('\n');
	return 0;
}


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

using namespace std;
using ll=long long;

bool chmin(int& a, int b){ if(a > b){ a = b; return 1; } return 0; }
bool chmax(int& a, int b){ if(a < b){ a = b; return 1; } return 0; }
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
const ll MOD = 1000000007LL;

int main() {
    int N; ll C;
    cin >> N >> C;
    vector<ll> as(N),bs(N),cs(N);
    set<int> abs;
    for (int i = 0; i < N; ++i) {
        cin >> as[i] >> bs[i] >> cs[i];
        bs[i]++;
        abs.insert(as[i]);
        abs.insert(bs[i]);
    }
    unordered_map<ll,int> inds;
    vector<ll> inds2(abs.size()+1);
    int cnt = 0;
    for (ll x : abs) {
        inds[x] = ++cnt;
        inds2[cnt] = x;
    }
    vector<ll> dps(cnt+1,0);
    for (int i = 0; i < N; ++i) {
        int ia = inds[as[i]];
        int ib = inds[bs[i]];
        dps[ia] += cs[i];
        dps[ib] -= cs[i];
    }
    ll ans = 0;
    for (int i = 1; i < cnt; ++i) {
        dps[i] += dps[i-1];
        ans += min(dps[i],C) * (inds2[i+1]-inds2[i]);
    }
    cout << ans << endl;

    return 0;
}

#include "bits/stdc++.h"
#define int long long
#define endl '\n'
using namespace std;

typedef long long ll;
typedef long double ld;
#define db(x) cerr << #x << ": " << x << '\n';
#define read(a) int a; cin >> a;
#define reads(s) string s; cin >> s;
#define readb(a, b) int a, b; cin >> a >> b;
#define readc(a, b, c) int a, b, c; cin >> a >> b >> c;
#define readarr(a, n) int a[(n) + 1] = {}; FOR(i, 1, (n)) {cin >> a[i];}
#define readmat(a, n, m) int a[n + 1][m + 1] = {}; FOR(i, 1, n) {FOR(j, 1, m) cin >> a[i][j];}

#define print(a) cout << a << endl;
#define printarr(a, n) FOR (i, 1, n) cout << a[i] << " "; cout << endl;
#define printv(v) for (int i: v) cout << i << " "; cout << endl;
#define printmat(a, n, m) FOR (i, 1, n) {FOR (j, 1, m) cout << a[i][j] << " "; cout << endl;}
#define all(v) v.begin(), v.end()
#define sz(v) (int)(v.size())
#define rz(v, n) v.resize((n) + 1);
#define pb push_back
#define fi first
#define se second
#define vi vector <int>
#define pi pair <int, int>
#define vpi vector <pi>
#define vvi vector <vi>
#define setprec cout << fixed << showpoint << setprecision(20);
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll N = 2e5 + 1;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power (int a, int b = mod - 2)
{
    int res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

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

	readb(n, fix);
	vpi v;
	FOR (i, 1, n)
	{
	    readc(a, b, c);
	    v.pb({a, c});
	    v.pb({b + 1, -c});
	}

	sort(all(v));
	int cost = 0, sum = 0, prev = 0;
	for (pi i: v)
	{
	    cost += min(fix, sum)*(i.fi - prev);
	    sum += i.se;
	    prev = i.fi;
	}

	print(cost);
}


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

int main() {
  int A1 = 0, A2 = 0, A3 = 0,tm;
  cin >> A1 >> A2 >> A3;
  if(A1 > A2) {
    tm = A1;
    A1 = A2;
    A2 = tm;
  }
  if (A2 > A3) {
	tm = A2;
	A2 = A3;
	A3 = tm;
	}
if (A1 > A2)
{
tm = A1;
A1 = A2;
A2 = tm;
}

  if(A3 - A2 == A2 - A1) {
    cout << "Yes" << endl;
  }
  else cout << "No" << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i, a, b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)

int main() {
    vector<int> A(3);
    REP(i, 3) cin >> A[i];
    sort(A.begin(), A.end());
    if (A[1] - A[0] == A[2] - A[1]) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }

    return 0;
}


#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<stack>
#include<queue>
#include<deque>
#include<algorithm>
#include<cmath>
#include<functional>
#include<bitset>
#include<iomanip>
#define _GLIBCXX_DEBUG
#define fi first
#define se second
#define pb push_back
#define all(x) begin(x), end(x)
#define rep(i,n) for(int i=0;i<n;++i)
#define rep2(i,l,r) for(int i=l;i<=r;++i)
#define rep3(i,l,r) for(int i=l;i>=r;--i)
#define pii pair<int,int>
#define pll pair<ll,ll>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

const long long inf = 1ll << 60;
const long long mod=1000000007;
const double PI = acos(-1);

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

    ll proc = 0,start = 0;
    ll mx = 0, ans = 0;
    for(int i=0;i<n;++i){
        proc += a[i];
        mx = max(mx, proc);
        ans = max(ans, start+mx);
        start += proc;
    }
    cout << ans << endl;
    return 0;
}

/*in the name of Aintah the most beneficent the most merciful.*/
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define MAX 1e18
#define MIN -1e18
// #define MOD 1000000007
//#define mod2 20071027
#define MOD 998244353
// #define base 100001
//#define mod 1000074259
#define endl "\n"
#define base2 31
#define memz(a) memset(a, 0, sizeof(a))
#define memn(a) memset(a, -1, sizeof(a))
#define in1(a) scanf("%lld", &a)
#define in2(a, b) scanf("%lld%lld", &a, &b)
#define TC(c) printf("Case #%lld: ", ++c)
#define out(x) cout << #x << " -> " << x << endl;
#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define FAST ios_base::sync_with_stdio(false); cin.tie(NULL);
#define FILE   freopen("input.txt", "r", stdin);  freopen("out.txt", "w", stdout);
using namespace std;
const int N = 300010;
ll t, n, a[N], res=0, sum;
ll pre[N], fp[N], nxt;
int solve() {
	in1(n);
	pre[0]=MIN;
	for (int i = 1; i <= n; i++) {
		in1(a[i]);
		sum += a[i];
		pre[i] = max(pre[i - 1], sum);
		fp[i] = sum;
		res=max(res, nxt+pre[i]);
		nxt += fp[i];
	}
	printf("%lld\n", res);

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


#include<bits/stdc++.h>
using namespace std;
#define _ 0
const int maxn=2e5+5;
const int inf=0x3f3f3f3f;
long long a[maxn],b[maxn],ans;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    	cin>>a[i];
    	b[a[i]%200]++;
    }
    for(int i=0;i<=199;i++)
    	ans+=b[i]*(b[i]-1)/2;
    cout<<ans<<endl;
	return ~~(0^_^0);
}

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

#define pb push_back
#define FOR(i,f,n) for(int i=f;i<n;i++)
#define FORI(i,f,n) for(int i=f;i>=n;i--)
#define sz(a) ((int)(a).size())
#define ff first
#define ss second
#define all(a) (a).begin(),(a).end()
#define alli(a) (a).rbegin(),(a).rend()
#define approx(a) fixed<<setprecision(a)
#define trav(a,x) for(auto& a : x)
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<int,bool> pib;
typedef pair<ll,bool> plb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vld;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
template <class T> using pq = priority_queue<T>;
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T> void ckmin(T &a, const T &b) { a = min(a, b); }
template <class T> void ckmax(T &a, const T &b) { a = max(a, b); }

template <class T> void read(vector<T> &v);
template <class F, class S> void read(pair<F, S> &p);
template <class T> void read(T &x) {cin>>x;}
template <class R, class... T> void read(R& r, T&... t){read(r); read(t...);};
template <class T> void read(vector<T> &v) {trav(x, v) read(x);}
template <class F, class S> void read(pair<F, S> &p) {read(p.ff, p.ss);}

template <class F, class S> void pr(const pair<F, S> &x);
template <class T> void pr(const T &x) {cout<<x;}
template <class R, class... T> void pr(const R& r, const T&... t) {pr(r); pr(t...);}
template <class F, class S> void pr(const pair<F, S> &x) {pr("{", x.ff, ", ", x.ss, "}");}
void ps() {pr("\n");}
template <class T> void ps(const T &x) {pr(x); ps();}
template <class T> void ps(vector<T> &v) {trav(x, v) pr(x, ' '); ps();}
template <class F, class S> void ps(const pair<F, S> &x) {pr(x.ff, ' ', x.ss); ps();}
template <class R, class... T> void ps(const R& r, const T &...t) {pr(r, ' '); ps(t...);}

void solve(){
    ll n; read(n);
    map<ll, ll> m;
    vl a(n), b(n), c(n);
    read(a);
    read(b);
    read(c);
    FOR(i,0,n){
        m[b[c[i]-1]]++;
    }
    ll ans = 0;
    FOR(i,0,n){
        ans+=m[a[i]];
    }
    ps(ans);
}

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


#include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
using namespace std;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<long>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvb = vector<vector<bool>>;
using vvc = vector<vector<char>>;
using vvl = vector<vector<long>>;
using pii = pair<int, int>;
using pil = pair<int, long>;
using pll = pair<long, long>;
using ll = long long;
#define fix20 cout << fixed << setprecision(20)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define rep(i,n) for(long i=0; i<(long)(n);i++)
#define REP(i,s,t) for(long i=s; i<t; i++)
#define RNG(i,s,t,u) for(long i=s; i<t; i+=u)
#define MOD 1000000007
#define all(vec) vec.begin(), vec.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main(){
  ll b,c;
  cin >> b >> c;
  ll h = c/2;
  ll odd = c % 2;
  ll sum = 0;
  if(b > 0){
    if(b - h > 0){
      //sum += (h+1)*2 - (1 - odd);
      //ll r = max((ll)0,c-1);
      //sum += r-1;
      sum += c+1;
      sum += max((ll)0, c-2);
    }else{
      // sum += 2*b + 1;
      // ll r = max((ll)0,c-1);
      // sum += r-1;
      sum += b*2 + 1;
      sum += max((ll)0, c-2);
    }
  }else if(b < 0){
    ll rb = -1*b;
    if(rb*2 + 1 > c){
      sum += c;
      sum += max((ll)1, c);
    }else{
      sum += 2*rb;
      sum += max((ll)1, c);
    }
  }else{
    sum += max((ll)1, c);
  }
  cout << sum << endl;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
static long long INF = (1LL<<62);

// b>0が前提
LL g(LL b, LL c){
	if(c == 1) return 2;
	if(c == 2) return 3;
	LL l = -b - ((c-1)/2);
	LL r = -l;
	if((c-1)%2 == 0){ r--; }
	LL e = b - (c/2);
	LL ret;
	if(e > 0){
		if(c%2 == 0){ e++; }
		// l～rのうち、-e～eを除く
		ret = r - l + 1 - (2*(e-1) + 1);
		if(c%2 == 0){ ret++; } //eの分をカウントする
	}
	else{
		// l～r全てが対象
		ret = r - l + 1;
	}
	return ret;
}

// b<0が前提
LL h(LL b, LL c){
	if(b != 0 && c == 1){ return 2; }
	if(b == 0 && c == 1){ return 1; }
	LL l = b - (c/2);
	LL r = -l;
	if(c%2 == 0){ r--; }
	LL e = -b - ((c-1)/2);
	LL ret;
	if(e > 0){
		if((c-1)%2 == 0){ e++; }
		// l～rのうち、-e～eを除く
		ret = r - l + 1 - (2*(e-1) + 1);
		if((c-1)%2 == 0){ ret++; } //eの分をカウントする
	}
	else{
		// l～r全てが対象
		ret = r - l + 1;
	}
	return ret;
}

LL f(LL b, LL c){
	if(c == 0){ return 1; }
	if(b < 0){ return h(b, c); }
	else if(b == 0){
		if(c == 1) return 1;
		if(c == 2) return 2;
		LL ret = h(-1, c-2);
		if((1-(c-2))/2 >= 0) ret++; //hの結果が0を含まない場合、0を加算しておく
		return ret;
	}
	else{
		return g(b, c);
	}
}

int main(int argc, char* argv[]){
	cin.tie(0); ios::sync_with_stdio(false);
	LL B, C; cin >> B >> C;
	LL ans = f(B, C);
	printf("%lld\n", ans);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FORN(i, n) for (int i = 1; i <= n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
    for (T& x : data)
        input >> x;
    return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
    output << "(" << data.x << "," << data.y << ")";
    return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
    for (const T& x : data)
        output << x << " ";
    return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>;
tcT> void re(V<T>& x) {
    trav(a, x)
        cin >> a;
}
tcT> bool ckmin(T& a, const T& b) {
    return b < a ? a = b, 1 : 0;
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
    return a < b ? a = b, 1 : 0;
}
ll gcd(ll a, ll b) {
    while (b) {
        tie(a, b) = mp(b, a % b);
    }
    return a;
}
signed main() {
    #ifdef LOCAL
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    int n;
    cin >> n;
    string s;
    cin >> s;

    vi a(n + 1);
    cin >> a;

    auto check = [&] (vi a) {
    	trav (e, a) {
    		if (e < 0)
    			return 0;
    	}
    	FOR (i, n) {
    		if (s[i] == '<') {
    			if (a[i] >= a[i + 1])
    				return 0;
    		}
    		else {
    			if (a[i] <= a[i + 1])
    				return 0;
    		}
    	}
    	return 1;
    };

    const int INF = 1e4+7;
    int l = 0, r = INF;
    V <vi> ans;
    while (l < r - 1) {
    	int m = (l + r) / 2;

    	V <vi> d(m, vi (n + 1));
    	FOR (i, n + 1) {
    		FOR (j, m) {
    			d[j][i] = a[i]/m;
    		}
    		FOR (j, a[i]%m) {
    			d[j][i]++;
    		}
    	}

    	bool ok = 1;
    	FOR (i, m) {
    		ok &= check(d[i]);
    	}
    	if (ok) {
    		l = m;
    		ans = d;
    	}
    	else {
    		r = m;
    	}
    }

    cout << l << endl;
    trav (e, ans) {
    	cout << e << endl;
    }
}

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

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

    int n;
    cin >> n;

    string s;
    cin >> s;

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

    int k = 1 << 30;
    for (int i = 0; i < n; i++) {
        k = min(k, abs(a[i + 1] - a[i]));
    }

    cout << k << '\n';
    for (int l = 0; l < k; l++) {
        for (int i = 0; i <= n; i++) {
            cout << a[i] / k + (l < a[i] % k) << " \n"[i == n];
        }
    }

    //差の絶対値の最小がkになりそうだけど 微分する？

    return 0;
}


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

typedef long long ll;
typedef pair<ll, ll> PII;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 7;
/*const*/ ll mod = 1e9+7;

#define mst(x, a) memset( x,a,sizeof(x) )
#define rep(i, a, b) for(int i=(a);i<=(b);++i)
#define dep(i, a, b) for(int i=(a);i>=(b);--i)

ll read() {
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=x*10+ch-'0';
		ch=getchar();
	}
	return x*f;
}
void out(ll x) {
	int stackk[40];
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (!x) {
		putchar('0');
		return;
	}
	int top = 0;
	while (x) stackk[++top] = x % 10, x /= 10;
	while (top) putchar(stackk[top--] + '0');
}

ll qpow(ll a,ll b) {
	ll ans=1;
	while(b) {
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}

ll n,a[maxn],m,f[121][121][121],ans;
int main() {
	n=read(),m=read();
	rep(i,1,n) a[i] = read();
	ans = m;
	for(int len =1; len<=n ; len++) {
		mst(f,0xcf);
		f[0][0][0] =0 ;
		for(int i=1 ; i<=n ; i++) {
			f[i][0][0] = 0;
			for(int j=1 ; j<=len ; j++) {
				for(int k=0 ; k<len ; k++) {
					f[i][j][k] = f[i-1][j][k];
					f[i][j][k] = max(f[i][j][k],f[i-1][j-1][(((k-a[i])%len)+len)%len]+a[i]);
				}
			}
		}
		if(f[n][len][m%len]>=0) ans = min(ans,(m-f[n][len][m%len])/len);
	}
	out(ans);
	return 0;
}
/*

*/


#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <bitset>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
using namespace std;
typedef long long ll;

//infの設定
#define INF32 2147483647
#define INF64 9223372036854775807
//repマクロ
#define rep(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))

int main(){
    ll H,W;
    vector<string> S(1010),x(1010);

    cin>>H>>W;
    rep(i,0,H) cin>>S.at(i);

    ll line=H+W-1;
    int hantei=0;

    rep(i,0,H+W-1){
        hantei=0;
        rep(j,0,W){
            if(0<=i-j&&i-j<H){
                if(S.at(i-j).at(j)=='R'){
                    if(hantei==2){
                        cout<<"0"<<endl;
                        return 0;
                    }else{
                        hantei=1;
                    }
                }else if(S.at(i-j).at(j)=='B'){
                    if(hantei==1){
                        cout<<"0"<<endl;
                        return 0;
                    }else{
                        hantei=2;
                    }
                }
            }
        }
        if(hantei!=0) line--;
    }
    ll ans=1;
    rep(i,0,line) ans=(ans*2)%998244353;

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

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

#define FAST ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define mp make_pair
#define pb push_back
#define lp(i,s,f) for(ll i = s; i < ll(f); i++)
#define inF freopen("input.in", "r", stdin);
#define outF freopen("output.in", "w", stdout);
#define endl '\n'
#define MOD 1000000007
#define mm(arr) memset(arr, 0, sizeof(arr))
#define F first
#define S second
#define int ll

int dp[105][105][105];
int n, k, x;
int a[105];
int calc(int ind, int taken, int sum){
    if(ind == n){
        if(taken == k && sum == x%k){
            return 0;
        }
        else{
            return -1e18;
        }
    }
    if(taken == k){
        if(sum == x%k){
            return 0;
        }
        else{
            return -1e18;
        }
    }
    if(dp[ind][taken][sum] != -1){
        return dp[ind][taken][sum];
    }
    int ch1 = calc(ind + 1, taken + 1, (sum + a[ind])%k) + a[ind];
    int ch2 = calc(ind + 1, taken, sum);
    return dp[ind][taken][sum] = max(ch1, ch2);
}
int32_t main(){
    FAST
    cin >> n >> x;
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }
    sort(a, a + n);
    int ans = 1e18;
    for(int i = 1; i <= n; i++){
        memset(dp, -1, sizeof(dp));
        k = i;
        int x2 = x - calc(0, 0, 0);
        if(x2 > x)continue;
        if(x2%i == 0){
            ans = min(ans, x2/i);
        }
    }
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T> using vc = vector<T>;
template<class T> using vvc = vc<vc<T>>;
template<class T> using vvvc = vc<vvc<T>>;
template<class T> using vvvvc = vvc<vvc<T>>;
template<class T> using PQ = priority_queue<T>;
template<class T> using invPQ = priority_queue<T, vector<T>, greater<T>>;
using IP = pair<int, int>;
using LP = pair<ll, ll>;
#define mp make_pair
#define pb push_back
#define all(x) begin(x), end(x)
#define rep(i, n) for (int i = 0; (i) < (int)(n); i++)
#define rep3(i, m, n) for (int i = (m); (i) < (int)(n); i++)
#define repr(i, n) for (int i = n; (i) >= 0; i--)
#define rep3r(i, m, n) for (int i = (n); (i) >= (int)(m); i--)
template<class T> inline bool chmax(T & a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T & a, T b) { if (a > b) { a = b; return 1; } return 0; }
constexpr int INF = 1080000000;
constexpr long long LINF = 4611686015206162431;

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

    int N;
    cin >> N;
    vc<int> X(N), C(N);
    rep (i, N) cin >> X[i] >> C[i];

    vc<int> cmin(N, INF), cmax(N, -INF);
    rep (i, N) {
        chmin(cmin[C[i]-1], X[i]);
        chmax(cmax[C[i]-1], X[i]);
    }

    int l = 0, r = 0;
    ll lt = 0, rt = 0;
    rep (i, N) {
        if (cmin[i] == INF) continue;
        ll tlt = min(abs(l - cmax[i]) + lt, abs(r - cmax[i]) + rt) + cmax[i] - cmin[i];
        ll trt = min(abs(l - cmin[i]) + lt, abs(r - cmin[i]) + rt) + cmax[i] - cmin[i];
        lt = tlt, rt = trt;
        l = cmin[i];
        r = cmax[i];
    }

    cout << min(lt + abs(l), rt + abs(r)) << endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[2][2];
int main(){
	int n;
	scanf("%d",&n);
	ll a,b;
	map<int,vector<ll> > mapa;
	for(int i = 0; i < n; i++){
		scanf("%lld %lld",&a,&b);
		mapa[b].push_back(a);
	}
	a = b = 0;
	ll izq,der; izq = 0; der = 0;
	int op = 1;
	for(auto x : mapa){
		sort(x.second.begin(), x.second.end());
		int y = x.second.size();
		dp[op][0] = min(dp[1-op][0]+abs(x.second[0]-a),dp[1-op][1] + abs(x.second[0]-b)) + abs(x.second[y-1]-x.second[0]);
		dp[op][1] = min(dp[1-op][0]+abs(x.second[y-1]-a),dp[1-op][1] + abs(x.second[y-1]-b)) + abs(x.second[y-1]-x.second[0]);

		a = x.second[y-1];
		b = x.second[0];
		op = 1-op;
	}
	printf("%lld\n", min(dp[1-op][0] + abs(a), dp[1-op][1] + abs(b)));
	return 0;
}


#include <bits/stdc++.h>
#define endl '\n'

#define SZ(x) ((int)x.size())
#define ALL(V) V.begin(), V.end()
#define L_B lower_bound
#define U_B upper_bound
#define pb push_back

using namespace std;
template<class T, class T1> int chkmin(T &x, const T1 &y) { return x > y ? x = y, 1 : 0; }
template<class T, class T1> int chkmax(T &x, const T1 &y) { return x < y ? x = y, 1 : 0; }
const int MAXN = (1 << 20);

int n;
int a[MAXN];
int64_t sum = 0;

void read() {
	cin >> n;
	for(int i = 0; i < 2 * n; i++) {
		cin >> a[i];
		sum += a[i];
	}
}

void solve() {
	int r = 2 * n - 1;
	int64_t ans = 0;
	priority_queue<int> q;
	for(int i = n; i < 2 * n; i++) {
		ans += a[i];
	}

	for(int i = 0; i < n; i++) {
		q.push(a[r--]);
		if(q.top() > a[i]) {
			ans -= q.top();
			ans += a[i];
			q.pop();
			q.push(a[i]);
		}
	}

	cout << sum - ans << endl;
}

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

	read();
	solve();
	return 0;
}

#include<bits/stdc++.h>
#define pb push_back
template <typename _Tp>void read(_Tp &x){
	char ch(getchar());bool f(false);while(!isdigit(ch))f|=ch==45,ch=getchar();
	x=ch&15,ch=getchar();while(isdigit(ch))x=x*10+(ch&15),ch=getchar();
	if(f)x=-x;
}
template <typename _Tp,typename... Args>void read(_Tp &t,Args &...args){read(t);read(args...);}
const int N=21;
std::vector<int> e[N];
int n,col[N];
long long ans;
void dfs(int dep,long long coef){
	if(dep==n){ans+=coef;
	return;}
	if(e[dep].empty())dfs(dep+1,coef*3LL);
	else{
		for(int i=0;i<3;++i){
			bool flag=true;
			for(auto v:e[dep]){
				if(col[v]==i){flag=false;break;}
			}
			if(flag)col[dep]=i,dfs(dep+1,coef),col[dep]=-1;
		}
	}
}
int main(){
	memset(col,-1,sizeof(col));
	int m;read(n,m);
	for(int i=1,x,y;i<=m;++i)read(x,y),--x,--y,e[x].pb(y),e[y].pb(x);
	dfs(0,1);
	printf("%lld\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define ALL(v) v.begin(),v.end()
constexpr ll MOD=1000000007;
constexpr ll INF=2e18;

struct BIT{
    int N;
    VI dat;
    BIT(int N):N(N){
        dat.resize(N+1,0);
    }
    ll sum(int k){
        k++;
        if(k==0)
            return 0;
        ll res=0;
        for(;k>0;k-=k&-k)
            res+=dat[k];
        return res;
    }
    ll sum(int l, int r){
        return sum(r)-sum(l-1);
    }
    void add(int k, ll x){
        k++;
        for(;k<=N;k+=k&-k)
            dat[k]+=x;
    }
    int lowerBound(int w){
        if (w<=0) return 0;
        int x=0;
        int k=1;
        while(k*2<=N) k*=2;
        for(;k>0;k/=2){
            if(x+k<=N&&dat[x+k]<w){
                w-=dat[x+k];
                x+=k;
            }
        }
        return x;
    }
};

int main(){
    ll n, m, q; cin >> n >> m >> q;
    VI t(q), x(q), y(q);
    REP(i,q){
        cin >> t[i] >> x[i] >> y[i];
    }
    VI sy=y; sy.push_back(0); sort(ALL(sy));
    map<int,int> mp; int cnt=1;
    REP(i,q+1){
        if(mp[sy[i]]==0){
            mp[sy[i]]=cnt;
            cnt++;
        }
    }
    ll ans=0;
    VI a(n,0), b(m,0);
    BIT bta(q+10), btb(q+10);
    BIT btax(q+10), btbx(q+10);
    btax.add(0,n);
    btbx.add(0,m);
    REP(i,q){
        if(t[i]==1){
            int j=x[i]-1;
            ll pre=btbx.sum(mp[a[j]]-1)*a[j]+btb.sum(mp[a[j]],q+3);
            ll ne =btbx.sum(mp[y[i]]-1)*y[i]+btb.sum(mp[y[i]],q+3);
            ans+=ne-pre;
            cout << ans << endl;
            bta.add(mp[a[j]],-a[j]);
            bta.add(mp[y[i]],y[i]);
            btax.add(mp[a[j]],-1);
            btax.add(mp[y[i]],1);
            a[j]=y[i];
        }
        else{
            int j=x[i]-1;
            ll pre=btax.sum(mp[b[j]]-1)*b[j]+bta.sum(mp[b[j]],q+3);
            ll ne =btax.sum(mp[y[i]]-1)*y[i]+bta.sum(mp[y[i]],q+3);
            ans+=ne-pre;
            cout << ans << endl;
            btb.add(mp[b[j]],-b[j]);
            btb.add(mp[y[i]],y[i]);
            btbx.add(mp[b[j]],-1);
            btbx.add(mp[y[i]],1);
            b[j]=y[i];
        }
    }
    return 0;
}

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x);
#define mp(x, y) make_pair(x, y)
#define mem(dp, a) memset(dp, a, sizeof dp);
#define all(a) a.begin(), a.end()
#define sall(a) sort(all(a))
#define X first
#define Y second
typedef long long int ll;
typedef pair<ll, ll> pp;

#define debug(x) cout << #x << " :: " << x << "\n";
#define debug2(x, y) cout << #x << " :: " << x << "\t" << #y << " :: " << y << "\n";
#define debug3(x, y, z) cout << #x << " :: " << x << "\t" << #y << " :: " << y << "\t" << #z << " :: " << z << "\n";
#define boost                         \
    ios_base::sync_with_stdio(false); \
    cin.tie(0);                       \
    cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<pp, null_type, less<pp>, rb_tree_tag, tree_order_statistics_node_update>

ll power(ll b, ll exp)
{
    ll res = 1;
    while (exp > 0)
    {
        if (exp % 2)
        {
            res = 1LL * res * b % mod;
        }
        b = 1LL * b * b % mod;
        exp /= 2;
    }
    return res;
}

map<ll, ll> ma1,ma2;    // ma2->reverse
ll n, m;

const ll maxN = 1000002;
ll bit[maxN][2][2];

void update(ll i, ll val,ll j,ll k)
{
    while (i < maxN )
    {
        bit[i][j][k] += val;
        i += (i & (-i));
    }
}

ll query(ll i,ll j,ll k)
{
    ll ans = 0;
    while (i > 0)
    {
        ans += bit[i][j][k];
        i -= (i & (-i));
    }
    return ans;
}

ll query_range(ll l, ll r,ll j,ll k)
{
    return query(r, j, k) - query(l - 1, j, k);
}

int main()
{
    boost
    ll q;
    cin >> n >> m >> q;
    pair<ll, pp> Q[q];
    ma1[0] = 0;
    for (ll i = 0; i < q; i++)
    {
        ll t, x, y;
        cin >> t >> x >> y;
        ma1[y] = 0;
        Q[i] = {t, {x, y}};
    }
    ll cnt = 1;
    vector<ll> a(n + 1), b(m + 1);
    for (auto &&temp : ma1) {
        temp.second = cnt++;
        ma2[cnt - 1] = temp.first;
    }
    ll ans = 0;
    update(ma1[0], n, 0, 0);    // [x][y] -> {a or b, freq or sum}
    update(ma1[0], m, 1, 0);
    for (ll i = 0; i < q; i++)  {
        ll t = Q[i].first, x = Q[i].second.first, y = Q[i].second.second;
        if (t==1)   {
            ll prev = (query(ma1[a[x]] - 1, 1, 0) * a[x]) + query_range(ma1[a[x]], maxN - 1, 1, 1);
            ans -= prev;
            update(ma1[a[x]], -1, 0, 0);
            update(ma1[a[x]], -a[x], 0, 1);
            a[x] = y;
            update(ma1[a[x]], 1, 0, 0);
            update(ma1[a[x]], a[x], 0, 1);
            ll cur = (query(ma1[a[x]] - 1, 1, 0) * a[x]) + query_range(ma1[a[x]], maxN - 1, 1, 1);
            ans += cur;
        }
        else    {
            ll prev = (query(ma1[b[x]] - 1, 0, 0) * b[x]) + query_range(ma1[b[x]], maxN - 1, 0, 1);
            ans -= prev;
            update(ma1[b[x]], -1, 1, 0);
            update(ma1[b[x]], -b[x], 1, 1);
            b[x] = y;
            update(ma1[b[x]], 1, 1, 0);
            update(ma1[b[x]], b[x], 1, 1);
            ll cur = (query(ma1[b[x]] - 1, 0, 0) * b[x]) + query_range(ma1[b[x]], maxN - 1, 0, 1);
            ans += cur;
        }
        cout << ans << "\n";
    }
    return 0;
}


//abc187_f.cpp
//Sat Jan  9 15:11:53 2021

#include <bits/stdc++.h>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007
#define rep(i,n) for (int i=0;i<(n);++i)

using namespace std;
using ll=long long;
typedef pair<int,int> P;

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

	vector<int> e(n,0);
	rep(i,m){
		int a,b;
		cin >> a >> b;
		a--;b--;
		e[a] += 1<<b;
		e[b] += 1<<a;
	}

	vector<int> dp(1<<n,99999);
	dp[0] = 0;
	rep(i,1<<n){
		if (i==0)continue;
		rep(j,n){
			if (1<<j&i){
				int target = i-(1<<j);
				if (dp[target]<=1 && (e[j]&target)==target){
					dp[i] = 1;
				}
				break;
			}
		}
	}

	rep(i,1<<n){
		for (int j=i;j>0;j=(j-1)&i){
			int k = i-j;
			dp[i] = min(dp[i],dp[j]+dp[k]);
		}
	}

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

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Vec = vector<ll>;
using VecP = vector<P>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
template <class T1, class T2>
bool chmax(T1 &a, const T2 b) { return a < b ? a = b, true : false; }
template <class T1, class T2>
bool chmin(T1 &a, const T2 b) { return a > b ? a = b, true : false; }
template <class T>
istream &operator>>(istream &is, vector<T> &v) { for (T &i : v) is >> i; return is; }
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) { for (const T &i : v) os << i << ' '; return os; }
void _co() { cout << '\n'; }
template <class Head, class... Tail>
void _co(Head&& head, Tail&&... tail) { cout << ' ' << head; _co(forward<Tail>(tail)...); }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) { cout << head; _co(forward<Tail>(tail)...); }
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) { cerr << head << ' '; ce(forward<Tail>(tail)...); }
void sonic() { ios::sync_with_stdio(false); cin.tie(nullptr); }
void setp(const int n) { cout << fixed << setprecision(n); }
constexpr int64_t INF = 1000000000000000001;
constexpr int64_t MOD = 1000000007;
constexpr int64_t MOD_N = 998244353;
constexpr long double EPS = 1e-11;
const double PI = acos(-1);

int main(void) {
    ll n, m;
    cin >> n >> m;
    const int N = 1 << n;

    vector<bitset<18>> g(n);
    rep(i, m) {
        ll a, b;
        cin >> a >> b;
        --a, --b;
        g[a][b] = true;
        g[b][a] = true;
    }
    rep(i, n) g[i][i] = true;

    Vec dp(1 << n, INF);
    dp[0] = 0;

    rep(bit, N) {
        bool flg = true;
        rep(i, n) {
            rep(j, n) {
                if ((bit >> i) & (bit >> j) & 1)
                    flg &= g[i][j];
            }
        }
        if (flg)
            dp[bit] = 1;
    }

    rep(bit, N) {
        for (int64_t i = bit; --i &= bit; ) {
            chmin(dp[bit], dp[i] + dp[bit ^ i]);
        }
    }
    co(dp.back());

    return 0;
}


#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
#pragma GCC optimize("Ofast")
using namespace         std;
#define                 int long long
#define                 double long double
#define                 fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ArrayFill(arr,a)       memset(arr,a,sizeof arr)
#define                 endl '\n'
#define countBit(x)     __builtin_popcountll(x)
#define all(x)          x.begin() , x.end()
#define ln              cout<<'\n';
using namespace std;
using namespace __gnu_pbds;
typedef tree<pair<int,int>, null_type, less<pair<int,int>>, rb_tree_tag, tree_order_statistics_node_update> oset;

int ceil(int a,int b){return (a+b-1)/b;}
#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_ {~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 << "]";}};
#define db(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define debug(x) debug_() << db(x)

const int MOD = 1000000007ll;
const int N = 1e5+100;
const int inf = 1e18;

vector<array<int,3>> graph[N];

signed main(){
    fast;
    int n,m,x,y;
    cin >> n >> m >> x >> y;

    for(int i=0;i<m;i++){
        int a,b,c,d;
        cin >> a >> b >> c >> d;
        graph[a].push_back({b,c,d});
        graph[b].push_back({a,c,d});
    }
    set<array<int,2>> q;

    q.insert({0,x});
    vector<int> dist(n+1,inf);
    dist[x] = 0;
    while(!q.empty()){
        auto cur = *q.begin();
        q.erase(cur);
        int cost = cur[0];

        for(auto u: graph[cur[1]]){
            int nx = u[0];
            int t = u[1];
            int md = u[2];
            int cst = (md - (dist[cur[1]]%md))%md;
            t+=cst;

            if(dist[cur[1]] + t < dist[nx]){
                q.insert({dist[cur[1]] + t, nx});
                dist[nx] = dist[cur[1]] + t;

            }
        }
    }
    if(dist[y]>1e17)dist[y]=-1;
    cout << dist[y];ln;

    return 0;
}

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

#define FORI(i, a, b) for (int i = (int)a; i <= (int)b; i++)
#define FORD(i, a, b) for (int i = (int)a; i >= (int)b; i--)
#define PB push_back
#define ALL(v) v.begin(),v.end()
#define OJ \
    freopen("input.txt", "r", stdin); \
    freopen("output.txt", "w", stdout);
#define FIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<pii> vpii;

const ll MAX = 1e16;

struct Edge{
    ll to, t, k;
};

ll dijkstra(vector<vector<Edge>>& graph, int x, int y) {
    vll distTo(graph.size(), MAX);
    set<pair<ll, ll>> st;
    distTo[x] = 0;
    st.insert({0, x});
    while (st.size() > 0) {
        // cout << "in side\n";
        pair<ll,ll> cur = *st.begin();
        st.erase(st.begin());
        if (cur.second == y) break;
        int curVertex = cur.second;
        ll curDis = cur.first;
        // cout << "ver: " << curVertex << endl;
        // cout << "dis: " << curDis << endl;
        for (auto adj : graph[curVertex]) {
            ll distToAdj = distTo[adj.to];
            ll waitTime = 0;
            if (curDis % adj.k != 0) {
                waitTime = adj.k - curDis % adj.k;
            }
            if (curDis + adj.t + waitTime <= distTo[adj.to]) {
                distTo[adj.to] = curDis + adj.t + waitTime;
                if (st.count({distToAdj, adj.to})) {
                    st.erase({distToAdj, adj.to});
                }
                st.insert({distTo[adj.to], adj.to});
            }
        }
    }
    // FORI(i, 0, distTo.size() - 1) {
    //     cout << "dist to " << i << " : " << distTo[i] << endl;
    // }
    // cout << "Here\n";
    return distTo[y];
}

int main() {
    int n, m, x, y; cin >> n >> m >> x >> y;
    x--, y--;
    vector<vector<Edge>> graph(n, vector<Edge>());
    FORI(i, 1, m) {
        int a, b, c, d; cin >> a >> b >> c >> d;
        a--, b--;
        graph[a].push_back({b, c, d});
        graph[b].push_back({a, c, d});
    }
    ll ans = dijkstra(graph, x, y);
    if (ans == MAX) cout << -1;
    else cout << ans;
    return 0;
}


//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int,int>;
using PL = pair<ll,ll>;
using PPL = pair<pair<ll,ll>,ll>;
#define INF 1000000000
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define repp(i, n, s) for (int i = (int)(n); i >= (int)(s); i--)
#define mp make_pair
#define tp make_tuple
#define ALL(vec) vec.begin(), vec.end()

long long modinv(long long a, long long m) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll INFLL = numeric_limits<ll>::max();
    ll t;cin>>t;
    rep(_,0,t){
        ll x,y,p,q;cin>>x>>y>>p>>q;
        ll mod1 = (2*x+2*y)%(p+q);
        ll ans = INFLL;

        rep(i,x,x+y){
            if (mod1==0 && p<=i%(p+q) && i%(p+q) <p+q){
                ans = min(ans, (ll)i);
                continue;
            }
            rep(j,p,p+q){
                ll num = i%(p+q);
                ll num2 = j%(p+q);
                ll num3 = (num2-num+p+q)%(p+q);

                ll d = __gcd(__gcd((ll)num3,mod1),p+q);
                ll mod3 = mod1 / d;
                num3/=d;
                ll mod2 = (p+q)/d;
                if (__gcd(mod3,mod2)==1){
                    ll a1 = modinv(mod3, mod2);
                    ans = min(ans, (a1*num3)%mod2*(2*x+2*y)+i);
                }
            }
        }
        if (ans==INFLL){
            cout<<"infinity"<<endl;
            continue;
        }
        cout<<ans<<endl;
    }
}

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MOD int(1e9+7)
#define INF int(1e9+7)
#define LINF ll(1e18+7)
#define PI acos(-1)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define P pair<ll,ll>
#define chmax(x,y) (x = max(x,y))
#define chmin(x,y) (x = min(x,y))

vector<int> dp[210000];

int main(){
  int n;
  string s;
  string x;
  cin>>n>>s>>x;
  dp[n].push_back(0);
  for(int i=n-1; i>=0; i--){
    if(x[i]=='A'){
      rep(r,7){
        bool a=0, b=0;
        for(auto p : dp[i+1]){
          if(10*r%7==p) a=1;
          if((10*r+s[i]-'0')%7==p) b=1;
        }
        if(a && b) dp[i].push_back(r);
      }
    }else{
      rep(r,7){
        for(auto p : dp[i+1]){
          if(10*r%7==p || (10*r+s[i]-'0')%7==p){
            dp[i].push_back(r);
            break;
          }
        }
      }
    }
  }
  for(auto x : dp[0]){
    if(x==0){
      cout<<"Takahashi"<<endl;
      return 0;
    }
  }
  cout<<"Aoki"<<endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i,n) for(int i=0;i<n;i++)
#define ALL(v) v.begin(),v.end()

ll GCD(ll a, ll b) {
	if(a<b) swap(a,b);
	ll r=a%b;
	while(r!=0){
		a=b;
		b=r;
		r=a%b;
	}
	return b;
}

int main(){
	int n; cin >> n;
	VI a(n); REP(i,n) cin >> a[i];
	ll ans=a[0];
	REP(i,n){
		ans=GCD(ans,a[i]);
	}
	cout << ans << endl;
	return 0;
}

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=1e5+100;
int a[maxn],n;
int num1=0x3f3f3f3f;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		num1=min(num1,a[i]);
	}
	while(1){
		int y=num1;
		for(int i=1;i<=n;i++){
			if(a[i]%num1==0) a[i]=num1;
			else{
				int x=a[i]%num1;
				a[i]=x;
				num1=x;
			}
		}
		if(y==num1)
		{
			printf("%d\n",y);
			break;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+100;
#define fr(i,a,b) for(int i = a;i <= b; ++i)
#define nfr(i,a,b) for(int i = a;i >= b; --i)

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

map<int,int> m1;

void solve(){
   m1.clear();
   scanf("%d",&n);
   fr(i,1,n)scanf("%d",a+i);

   int res1 = 0,res2 = 0;
   fr(i,1,n){
      res1 ^= a[i]; m1[a[i]] ^= 1;
   }

   fr(i,1,n)res2 |= m1[a[i]];
   if(res1){
      if(n%2 == 0)puts("First");
      else puts("Second");
   } else if(res2){
      if(n%2 == 0)puts("First");
      else puts("Second");
   } else {
      if(n%2 == 0)puts("Second");
      else puts("First");
   }
   return ;
}

int main(){
   scanf("%d",&t);
   while(t--)solve();
}

#include <bits/stdc++.h>
//#include <atcoder/all>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

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

const long long INF = 1e18;
//const ll mod = 1000000007;

void solve() {
    ll N;
    cin >> N;
    map<ll, ll> mp;
    for(int i = 0; i < N; i++) {
        ll a;
        cin >> a;
        mp[a]++;
    }
    if(N % 2 == 1) {
        cout << "Second" << endl;
        return;
    }
    for(auto tmp : mp) {
        if(tmp.second % 2 == 1) {
            cout << "First" << endl;
            return;
        }
    }
    cout << "Second" << endl;
}

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


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cstdint>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#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>

using namespace std;
#define ll long long
#define M 1000000007
#define pb push_back
#define bg begin
#define all(x) x.bg(),x.end()
#define pob pop_back
#define rep(x,y,j) for(ll j = x;j < y ;j++)
#define sz size
#define ss second
#define ff first
#define SUM accumulate
#define INF 2000000000000000000
#define SPACE " "
#define vr std::vector
#define que queue
#define dqu deque
#define stk stack
#define pqu priority_queue
#define mst multiset
#define pf push_front
#define pof pop_front
#define sqr(x) ((x)*(x))
#define cube(x) (x)*sqr(x)
// graph class

void solve(){
    // std::cout << std::setprecision(9) << std::showpoint << std::fixed;

ll a,b,c;
cin >> a>>b>>c;
cout << max(a+b,max(b+c,c+a))<<endl;

}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  int tt= 1;
  // cin >> tt;
  while (tt--) {
    solve();

  }
  return 0;
}

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

void printmat(const vector<vector<char>>& mat) {
    for (auto row : mat) {
        for (auto elem : row)
            cout << elem << " ";
        cout << endl;
    }
}
void printv(const vector<long long>& v) {
    for (auto elem : v)
        cout << elem << " ";
    cout << endl;
}
void printht(const unordered_map<long long, int>& ht) {
    for (auto elem : ht)
        cout << elem.first << " : " << elem.second << endl;
}
void printmp(const map<int, int>& ht) {
    for (auto elem : ht)
        cout << elem.first << " : " << elem.second << endl;
}
void printst(const set<int>& st) {
    for (auto elem : st)
        cout << elem << " ";
    cout << endl;
}

int main() {
    int T=1, caseIdx=0;
    //cin >> T;
    while (T--) {
        //caseIdx++;
        vector<int> v(3);
        cin >> v[0] >> v[1] >> v[2];
        cout << (accumulate(v.begin(), v.end(), 0) - *min_element(v.begin(), v.end())) << endl;
        //cout << "Case #" << caseIdx << ": " << ans << endl;
    }
}


#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 998244353;

const ll MAX_N = 5;

// a^n (mod.MOD)を求める。計算量はO(logn)
ll modpow(ll a, ll n, ll MOD = mod) {
    if(n == 0) {
        return 1;
    }
    if(n%2 == 1) {
        return (a * modpow(a, n-1, MOD))%MOD;
    }
    return (modpow(a, n/2, MOD) * modpow(a, n/2, MOD))%MOD;
}

ll inv[MAX_N];  // inv[i] : iの逆数のmod

void init() {
    inv[0] = inv[1] = 1;
    for(ll i = 1; i < MAX_N; i++) {
        if(i >= 2) {
            inv[i] = mod - inv[mod%i]*(mod/i)%mod;
        }
    }
}

//--- main関数でinit()を呼び出すのを忘れるな ---//
int main() {
    ll h, w, k;
    cin >> h >> w >> k;

    vvl board(h, vl(w, -1));
    rep(i, k) {
        ll x, y;
        char c;
        cin >> x >> y >> c;
        x--;  y--;
        if(c == 'R') {
            board[x][y] = 0;
        }
        else if(c == 'D') {
            board[x][y] = 1;
        }
        else if(c == 'X') {
            board[x][y] = 2;
        }
    }

    init();

    vvl dp(h, vl(w));  // dp[i][j] : (i, j)への移動経路数の総和
    dp[0][0] = modpow(3LL, h*w - k);
    exrep(j, 1, w-1) {
        if(board[0][j-1] == -1) {
            dp[0][j] = 2*inv[3]*dp[0][j-1]%mod;
        }
        else if(board[0][j-1] == 1) {
            dp[0][j] = 0;
        }
        else {
            dp[0][j] = dp[0][j-1];
        }
    }
    exrep(i, 1, h-1) {
        if(board[i-1][0] == -1) {
            dp[i][0] = 2*inv[3]*dp[i-1][0]%mod;
        }
        else if(board[i-1][0] == 0) {
            dp[i][0] = 0;
        }
        else {
            dp[i][0] = dp[i-1][0];
        }
    }

    exrep(i, 1, h-1) {
        exrep(j, 1, w-1) {
            if(board[i-1][j] == -1) {
                dp[i][j] += 2*inv[3]*dp[i-1][j];
            }
            else if(board[i-1][j] == 1 || board[i-1][j] == 2) {
                dp[i][j] += dp[i-1][j];
            }
            if(board[i][j-1] == -1) {
                dp[i][j] += 2*inv[3]*dp[i][j-1];
            }
            else if(board[i][j-1] == 0 || board[i][j-1] == 2) {
                dp[i][j] += dp[i][j-1];
            }
            dp[i][j] %= mod;
        }
    }

    out(dp[h-1][w-1]);
    re0;
}

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define INF 0x7f7f7f7f
#define LL_INF 0x7f7f7f7f7f7f7f7f
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define MOD 998244353

vector<ll> three(5000*5000+1);
void solve() {
    int n, m, k; cin >> n >> m >> k;
    vector<vector<int>> r(n+1, vector<int>(m+1));
    vector<vector<int>> d(n+1, vector<int>(m+1));
    vector<vector<int>> x(n+1, vector<int>(m+1));
    vector<vector<int>> a(n+1, vector<int>(m+1));
    vector<vector<int>> num(n+1, vector<int>(m+1));
    for(int i=1;i<=n*m;i++) three[i] = (three[i-1] * 3) % MOD;
    for(int i=0;i<k;i++) {
        int x, y;
        char c;
        cin >> x >> y;
        cin >> c;
        if(c == 'R') {
            a[x][y] = 1;
        }
        else if(c == 'D') {
            a[x][y] = 2;
        }
        else if(c == 'X') {
            a[x][y] = 3;
        }
    }
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            num[i][j] = num[i-1][j] + num[i][j-1] - num[i-1][j-1] + (a[i][j] == 0);
        }
    }

    if(a[1][1] == 0) {
        r[1][1] = d[1][1] = x[1][1] = 1;
    } else if(a[1][1] == 1) {
        r[1][1] = 1;
    } else if(a[1][1] == 2) {
        d[1][1] = 1;
    } else if(a[1][1] == 3) {
        x[1][1] = 1;
    }
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            if(i == 1 && j == 1) continue;
            r[i][j] = (((r[i][j-1] + x[i][j-1]) % MOD) * three[num[i][j] - num[i][j - 1] - (a[i][j] == 0)]) % MOD;
            r[i][j] += (((d[i-1][j] + x[i-1][j]) % MOD) * three[num[i][j] - num[i - 1][j] - (a[i][j] == 0)]) % MOD;
            r[i][j] %= MOD;

            x[i][j] = d[i][j] = r[i][j];

            if(a[i][j] == 1) {
                d[i][j] = x[i][j] = 0;
            }
            else if(a[i][j] == 2) {
                r[i][j] = x[i][j] = 0;
            }
            else if(a[i][j] == 3) {
                r[i][j] = d[i][j] = 0;
            }
        }
    }
    cout << ((ll)r[n][m] + (ll)d[n][m] + (ll)x[n][m]) % MOD << endl;

}

int main(void) {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    three[0] = 1;
    int t = 1;
    // cin >> t;
    while(t--) solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
  int N,T;
  cin >> N >> T;
  int x = N/2;
  int y = N-x;
  vector<int>a(x),b(y);
  for(int i=0; i<x; i++){
    cin >> a[i];
  }
  for(int i=0; i<y; i++){
    cin >> b[i];
  }
  vector<int>c;
  for(int bit=0; bit<(1<<x); bit++){
    int sum = 0;
    for(int j=0; j<x; j++){
      if(bit&(1<<j)){
        sum += a[j];
      }
    }
    c.push_back(sum);
  }
  vector<int>d;
  for(int bit=0; bit<(1<<y); bit++){
    int sum = 0;
    for(int j=0; j<y; j++){
      if(bit&(1<<j)){
        sum += b[j];
      }
    }
    d.push_back(sum);
  }
  sort(c.begin(),c.end());
  sort(d.begin(),d.end());
  int ans = 0;
  int point = 1;
  for(int i=0; i<(int)c.size(); i++){
    if(c[i] > T){
      continue;
    }
    int a = upper_bound(d.begin(),d.end(),T-c[i])-d.begin();
    a--;
    if(a>=0){
      ans = max(ans,c[i]+d[a]);
    }
  }
  cout << ans << endl;
}

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

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

int to[400001], ne[400001], he[200001];
int Q[200001], pl[200001], dl[200001];

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += 72 - dig >> 3;
	return tmp;
}

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

	int N = getint(), K = getint();
	int k = 1;
	rep(i, N - 1) {
		int u = getint(), v = getint();
		to[k] = v;
		ne[k] = he[u];
		he[u] = k++;
		to[k] = u;
		ne[k] = he[v];
		he[v] = k++;
	}

	int q = 0, p = 0;
	Q[q++] = 1;
	while (p < N) {
		int u = Q[p++];
		int tmp = 0;
		int ind = he[u];
		he[u] = 0;
		while (ind) {
			int v = to[ind];
			tmp = ne[ind];
			if (he[v] > 0) {
				Q[q++] = v;
				ne[ind] = he[u];
				he[u] = -ind;
			}
			ind = tmp;
		}
	}

	int are = 1000000, pd0 = are;
	int L = 0, R = (N + K - 1) / K;
	while (L + 1 < R) {
		int wj = (L + R) / 2;

		int num = 0;

		for (int i = N - 1; i >= 0; i--) {
			int po = pd0 - 1;
			int dd = pd0;
			int u = Q[i];
			for (int ind = -he[u]; ind; ind = -ne[ind]) {
				int v = to[ind];
				if (po < pl[v]) po = pl[v];
				if (dd < dl[v]) dd = dl[v];
			}
			if (po >= dd) dd = 0;
			if (dd >= pd0 + wj) {
				num++;
				po = pd0 + wj;
				dd = 0;
			}
			pl[u] = po - 1;
			dl[u] = dd + 1;
		}

		int are = num + (dl[1] > pd0);
		if (are > K) L = wj;
		else R = wj;

		pd0 += are;
	}

	printf("%d", R);

	Would you please return 0;
}


#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+7;
int dx[8]={1,0,-1,0,-1,-1,1,1};
int dy[8]={0,1,0,-1,-1,1,-1,1};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int 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;
}

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

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

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += 72 - dig >> 3;
	return tmp;
}

int kazu[1500001];
int A[1500001];

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

	int N = getint(), M = getint();

	auto a = A;
	rep(i, M) {
		kazu[(*a++ = getint())]++;
	}
	int kotae = 0;
	while (kazu[kotae]) kotae++;

	auto b = A;
	for (int i = M; i < N; i++) {
		kazu[(*a++ = getint())]++;
		kazu[*b]--;
		if (kazu[*b] == 0) {
			if (*b < kotae) kotae = *b;
		}
		b++;
	}

	printf("%d", kotae);

	Would you please return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// agr set ki tarah krna hai to less vrna agr multiset ki tarah to less_equal
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

long long int mod97 = 1000000007;
long long int modg = 998244353;
double pi = 3.1415926536;
bool comp(pair<long long int, long long int>a, pair<long long int, long long int>b)
{
    if (a.first == b.first)
    {
        if (a.second <= b.second)
            return true;
        else
            return false;
    }
    if (a.first > b.first)
        return true;
    else return false;
}
bool rev(long long int a, long long int b)
{
    if (a > b)return true;
    else return false;
}
bool compa(pair<string, pair<int, int>>a, pair<string, pair<int, int>>b)
{
    if (a.first == b.first)
    {
        if (a.second.first > b.second.first)
            return true;
        else
            return false;
    }
    if (a.first < b.first)
        return true;
    else
        return false;
}
long long int gcd(long long int bdda, long long int chota)
{
    if (chota == 0)
        return bdda;
    return gcd(chota, bdda % chota);
}
long long int lcm(long long int a, long long int b)
{
    return a * b / gcd(max(a, b), min(a, b));
}
long long int powerm(long long int a, long long int n, long long int modn)
{
    if (n == 0)return 1;
    if (n % 2 == 0)
    {
        long long int val = powerm(a, n / 2, modn);
        val = (val * val) % modn; return val;
    }
    else
    {
        long long int val = powerm(a, n / 2, modn);
        val = ((val * val) % modn * a) % modn; return val;
    }
}
long long int power(long long int a, long long int n)
{
    if (n == 0)return 1;
    if (n % 2 == 0)
    {
        long long int val = power(a, n / 2);
        val = (val * val); return val;
    }
    else
    {
        long long int val = power(a, n / 2);
        val = ((val * val) * a) ; return val;
    }
}
bool comppp(string a, string b)
{
    if (a.size() > b.size())return true;
    else return false;
}
bool compp(pair<long long int, long long int>a, pair<long long int, long long int>b)
{
    if (a.second < b.second)
        return true;
    else if (a.second > b.second)
        return false;
    else
    {
        if (a.first < b.first)return true;
        else return false;
    }
}
bool compar(pair<long long int, long long int>a, pair<long long int, long long int>b)
{
    if (a.second < b.second)return true;
    else if (a.second > b.second)return false;
    else
    {
        if (a.first >= b.first)return true; else return false;
    }
}

void solve()
{
    long long int n; cin >> n; pair<long long int, long long int>a[n];
    for (long long int i = 0; i < n; i++)cin >> a[i].first >> a[i].second;
    long long int ans = 0; sort(a, a + n);
    for (long long int i = 0; i < n; i++)
    {
        for (long long int j = i + 1; j < n; j++)
        {
            long long int dx = a[j].first - a[i].first, dy = a[j].second - a[i].second;
            if (abs(dx) >= abs(dy))ans++;
        }
    }
    cout << ans << endl;

}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r" , stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t;
    t = 1;
    //cin >> t;// long long int count = 1;
    while (t--)
    {
        //cout << "Case #" << count << ": ";
        solve();
        //count++;
    }
}

#include <bits/stdc++.h>
#define REP(i, s, n) for (int i = s; i < (int)(n); i++)
#define ALL(a) a.begin(), a.end()
#define MOD 1000000007
using namespace std;
using ll = long long;

int main() {
    int N; cin >> N;
    vector<int> A(N);
    REP(i, 0, N) cin >> A[i];

    int ans = 1 << 30;
    REP(i, 0, 1 << N) {
        int x = 0, d = i & 1, o = A[0];
        REP(j, 1, N) {
            if (i & (1 << j)) {
                if (d) {
                    o |= A[j];
                } else {
                    x ^= o;
                    o = A[j];
                    d = 1;
                }
            } else {
                if (d) {
                    x ^= o;
                    o = A[j];
                    d = 0;
                } else {
                    o |= A[j];
                }
            }
        }
        x ^= o;
        ans = min(ans, x);
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <iostream>
#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 DEBUG
#define fi first
#define se second
#define pqueue priority_queue
#define pb(x) push_back(x)
#define endl '\n'
#define all(x) x.begin(), x.end()
#define int long  long
#define mk(a, b) make_pair(a, b)

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 ll infll = 1e18;
const ll MOD = 1e9 + 7;
//static const int maxn = 1e6 + 228;
const ld eps = 1e-5;
const int K = 31;
const ld eps2 = 1e-9;
const ll MOD2 = 998244353;
const ll dosz = 5e5;
const ll SZ = (1 << 18);
const ld PI = 3.1415926535;

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

void solve() {
    string s;
    cin >> s;
    string temp = "atcoder";
    if(s>temp){
        cout << 0 << endl;
        return;
    }
    int n = s.size();
    for(int i=1; i<n; i++){
        if(s[i] > 't'){
            cout << max(i-1, 0ll) << endl;
            return;
        } if(s[i] > 'a'){
            cout << i << endl;
            return;
        }
    }
    cout << -1 << endl;
}

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()

using namespace std;
using ll = long long;
using P = pair<int, int>;

const ll INF_L = 1LL << 60;
const int INF_I = 1 << 30;
const int MOD = (int)1e9 + 7;
const double PI = acos(-1);
int dx[] = {1, 0, -1, 0};
int dy[] = {0, -1, 0, 1};

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

    rep(i, n)
    {
        x[i] -= a[i];
        x[i] = max(x[i], 0);
    }

    int ans = 1001001001;
    rep(i, n)
    {
        if (x[i])
            ans = min(p[i], ans);
    }
    cout << (ans == 1001001001 ? -1 : ans) << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
const int N = 2e5 + 7;

struct Node {
	LL a, b;
} in[N];

bool cmp(Node o1, Node o2) {
	return (2 * o1.a + o1.b) > (2 * o2.a + o2.b);
}

int main(void) {
	int n;
	LL sa = 0, sb = 0;
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> in[i].a >> in[i].b;
		sa += in[i].a;
	}

	sort(in + 1, in + n + 1, cmp);
	for (int i = 1; i <= n; ++i) {
		LL a = in[i].a, b = in[i].b;
		sa -= a;
		sb += a + b;
		if (sb > sa) {
			cout << i;
			return 0;
		}
	}

	return 0;
}

#include <bits/stdc++.h>
#define rep2(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) rep2(i,0,n)
#define rep1(i,n) rep2(i,1,n+1)
#define FOR(i) for(cin>>i;i>0;i--)
#define elif else if
#define pb push_back
#define ll long long
#define mp make_pair
#define all(x) x.begin(),x.end()
#define re(x) {cout<<x<<endl;return 0;}
//#define x first
//#define y second

using namespace std;
ll n,a[222222],b,ans=0,l=-1,r;
int main()
{
	cin>>n;
	r=n;
	a[0]=0;
	rep1(i,n)
	{
		cin>>b>>a[i];
		ans+=b;
		a[i]+=b*2;
	}
	sort(a+1,a+n+1);
	reverse(a+1,a+n+1);
	rep1(i,n) a[i]+=a[i-1];
	while(l<r-1)
	{
		ll mid=(l+r)/2;
		if(a[mid]>ans) r=mid;
		else l=mid;
	}
	re(r)
	return 0;}