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

int dx[] = { 0, 1, -1, 0, 1, -1, 1, -1 };  // i<4:4way i<8:8way
int dy[] = { 1, 0, 0, -1, 1, -1, -1, 1 };

constexpr ll MOD = 1000000007;
//constexpr ll MOD = 998244353;

constexpr ll INF = 1050000000;
constexpr ll LONGINF = 1050000000000000000;
struct all_init {
    all_init() {
        cout.tie(nullptr);
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(11);
    };
} ALL_INIT;
struct edge {
    int from, to;
    ll cost;
    ll capa;

    edge(int s, int d) : from(s), to(d) {cost = 0;capa = 0;}
    edge(int s, int d, ll w) : from(s), to(d), cost(w) { capa = 0; }
    edge(int s, int d, ll x, ll y) : from(s), to(d), cost(x), capa(y) {}

    bool operator<(const edge& x) const {
        if(cost<x.cost){
            return true;
        }
        return false;
    }
};
using graph = vector<vector<edge>>;
void add_edge(vector<vector<edge>>& g, int a, int b, ll cost, ll cap) {
    g[a].emplace_back(a, b, cost, cap);
    g[b].emplace_back(b, a, cost, cap);
}

#define CIN(vector_array_etc, n)         \
  for (int loop = 0; loop < n; loop++) { \
    cin >> vector_array_etc[loop];       \
  }
#define COUT(vector_array_etc, n)                                   \
  for (int LOOP = 0; LOOP < n; LOOP++) {                            \
    cout << vector_array_etc[LOOP] << (LOOP == n - 1 ? '\n' : ' '); \
  }
#define VC(Type_name) vector<Type_name>
#define SORT(vector_etc) sort(vector_etc.begin(), vector_etc.end())
#define ALL(vec_etc) vec_etc.begin(), vec_etc.end()
#define VCVC(Type_name) vector<vector<Type_name>>
#define WARSHALL vector<vector<ll>> g(n, vector<ll>(n, LONGINF))
#define endl '\n'

template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
istream& operator>>(istream& is, vector<T>& Vec) {
    for (T& x : Vec) {
        is >> x;
    }
    return is;
}
template <typename V, typename H>
void resize(vector<V>& vec, const H head) {
    vec.resize(head);
}
template <typename V, typename H, typename... T>
void resize(vector<V>& vec, const H& head, const T... tail) {
    vec.resize(head);
    for (auto& v : vec) {
        resize(v, tail...);
    }
}
template <ll mod>
struct ModInt {
    long long val;
    constexpr ModInt(long long v = 0) noexcept : val(v% mod) {
        if (val < 0) val += mod;
    }
    constexpr int getmod() { return mod; }
    constexpr ModInt operator-() const noexcept { return val ? mod - val : 0; }
    constexpr ModInt operator+(const ModInt& r) const noexcept {
        return ModInt(*this) += r;
    }
    constexpr ModInt operator-(const ModInt& r) const noexcept {
        return ModInt(*this) -= r;
    }
    constexpr ModInt operator*(const ModInt& r) const noexcept {
        return ModInt(*this) *= r;
    }
    constexpr ModInt operator/(const ModInt& r) const noexcept {
        return ModInt(*this) /= r;
    }
    constexpr ModInt& operator+=(const ModInt& r) noexcept {
        val += r.val;
        if (val >= mod) val -= mod;
        return *this;
    }
    constexpr ModInt& operator-=(const ModInt& r) noexcept {
        val -= r.val;
        if (val < 0) val += mod;
        return *this;
    }
    constexpr ModInt& operator*=(const ModInt& r) noexcept {
        val = val * r.val % mod;
        return *this;
    }
    constexpr ModInt& operator/=(const ModInt& r) noexcept {
        long long a = r.val, b = mod, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b;
            swap(a, b);
            u -= t * v;
            swap(u, v);
        }
        val = val * u % mod;
        if (val < 0) val += mod;
        return *this;
    }
    constexpr bool operator==(const ModInt& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator!=(const ModInt& r) const noexcept {
        return this->val != r.val;
    }
    friend ostream& operator<<(ostream& os, const ModInt<mod>& x) noexcept {
        return os << x.val;
    }
    friend istream& operator>>(istream& is, ModInt<mod>& x) noexcept {
        return is >> x.val;
    }
    friend constexpr ModInt<mod> modpow(const ModInt<mod>& a,
        long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};
template <class T>
struct nCk {
    vector<T> fact_, inv_, finv_;
    constexpr nCk() {}
    constexpr nCk(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
        init(n);
    }
    constexpr void init(int n) noexcept {
        fact_.assign(n, 1), inv_.assign(n, 1), finv_.assign(n, 1);
        ll MOD = 1000000007;
        for (ll i = 2; i < n; i++) {
            fact_[i] = fact_[i - 1] * i;
            inv_[i] = -inv_[MOD % i] * (MOD / i);
            finv_[i] = finv_[i - 1] * inv_[i];
        }
    }
    constexpr T com(ll n, ll k) const noexcept {
        if (n < k || n < 0 || k < 0) return 0;
        return fact_[n] * finv_[k] * finv_[n - k];
    }
    constexpr T fact(int n) const noexcept {
        if (n < 0) return 0;
        return fact_[n];
    }
    constexpr T inv(int n) const noexcept {
        if (n < 0) return 0;
        return inv_[n];
    }
    constexpr T finv(int n) const noexcept {
        if (n < 0) return 0;
        return finv_[n];
    }
};

ll PowMod(ll n, ll k, ll mod) {
    ll r = 1;

    for (; k > 0; k >>= 1) {
        if (k & 1) {
            r = (r * n) % mod;
        }
        n = (n * n) % mod;
    }
    return r;
}
ll Gcd(ll a, ll b) {
    return b != 0 ? Gcd(b, a % b) : a;
}
ll Lcm(ll a, ll b) {
    return a / Gcd(a, b) * b;
}
vector<string> Split(string s, string t) {
    vector<string> v;
    int p = s.find(t);
    while (p != s.npos) {
        v.emplace_back(s.substr(0, p));
        s = s.substr(p + (int)t.size());
        p = s.find(t);
    }
    v.emplace_back(s);
    return v;
}
vector<int> Lis(const vector<int>& a) {
    #define Index_of(as, x) distance(as.begin(), upper_bound(as.begin(), as.end(),x))
//#define Index_of(as, x) \
  distance(as.begin(), upper_bound(as.begin(), as.end(), x))
    const int n = a.size();
    vector<int> A(n, INF);
    vector<int> id(n);
    for (int i = 0; i < n; ++i) {
        id[i] = Index_of(A, a[i]);
        A[id[i]] = a[i];
    }
    int m = *max_element(id.begin(), id.end());
    vector<int> b(m + 1);
    for (int i = n - 1; i >= 0; --i)
        if (id[i] == m) b[m--] = a[i];
    return b;
}
string LcsAlphabeticalMinOrder(string a, string b) {
    if (a.size() < b.size()) {
        swap(a, b);
    }

    int n = a.size(), m = b.size();

    vector<string> dp(m + 1);

    for (int i = 0; i < n; i++) {
        vector<string> to(m + 1);
        for (int j = 0; j < m; j++) {
            if (a[i] == b[j]) {
                to[j + 1] = dp[j] + a[i];
            }
            else {
                if (to[j].size() > dp[j + 1].size()) {
                    to[j + 1] = to[j];
                }
                else if (to[j].size() < dp[j + 1].size()) {
                    to[j + 1] = dp[j + 1];
                }
                else if (to[j] < dp[j + 1]) {
                    to[j + 1] = to[j];
                }
                else {
                    to[j + 1] = dp[j + 1];
                }
            }
        }
        dp.swap(to);
    }
    return dp[m];
}
string Lcs(const string& s, const string& t) {
    int n = s.size();
    int m = t.size();
    int dp[n+1][m+1];
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            if (s[i - 1] == t[j - 1]) {
                dp[i][j] = dp[i - 1][j - 1] + 1;
            }
            else {
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
        }
    }
    string ans = "";
    int i = s.size(), j = t.size();
    while (i > 0 && j > 0) {
        if (s[i - 1] == t[j - 1]) {
            ans += s[i - 1];
            i--;
            j--;
        }
        else if (dp[i - 1][j] >= dp[i][j - 1])
            i--;
        else
            j--;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}
vector<int> LcsInteger(const vector<int> &a, const vector<int> &b) {
#define index_of(as, x) \
  distance(as.begin(), lower_bound(as.begin(), as.end(), x))
    struct node {
        int value;
        node* next;
        node(int value, node* next) : value(value), next(next) {}
    };
    const int n = a.size(), m = b.size();
    map<int, vector<int>> M;
    for (int j = m - 1; j >= 0; --j) M[b[j]].push_back(j);
    vector<int> xs(n + 1, INF);
    xs[0] = -INF;
    vector<node*> link(n + 1);
    for (int i = 0; i < n; ++i) {
        if (M.count(a[i])) {
            vector<int> ys = M[a[i]];
            for (int j = 0; j < (int)ys.size(); ++j) {
                int k = index_of(xs, ys[j]);
                xs[k] = ys[j];
                link[k] = new node(b[ys[j]], link[k - 1]);
            }
        }
    }
    vector<int> c;
    int l = index_of(xs, INF - 1) - 1;
    for (node* p = link[l]; p; p = p->next) c.push_back(p->value);
    reverse(c.begin(), c.end());
    return c;
}
bool IsPrime(ll n) {
    if (n < 2) return false;
    for (ll i = 2; i * i <= n; i++)
        if (!(n % i)) return false;
    return true;
}
vector<ll> Eratosthenes(int n) {
    vector<ll> res;
    vector<bool> Prime(n + 1, true);
    Prime[0] = Prime[1] = false;
    for (int i = 2; i * i <= n; i++) {
        if (Prime[i]) {
            for (int j = 2; i * j <= n; j++) {
                Prime[i * j] = false;
            }
        }
    }
    for (int i = 2; i <= n; i++) {
        if (Prime[i]) {
            res.emplace_back(i);
        }
    }
    return res;
}
ll maxSubArraySum(vector<ll> a,int left,int right) {
    //[left,right)
    int size=a.size();
    ll max_so_far = -LONGINF, max_ending_here = 0;

    for (int i = left; i < right; i++) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here) {
            max_so_far = max_ending_here;
        }

        if (max_ending_here < 0) {
            max_ending_here = 0;
        }
    }
    return max_so_far;
}
ll MergeCount(vector<int>& a) {
    ll count = 0;
    int n = a.size();
    if (n > 1) {
        vector<int> b(a.begin(), a.begin() + n / 2);
        vector<int> c(a.begin() + n / 2, a.end());
        count += MergeCount(b);
        count += MergeCount(c);
        for (int i = 0, j = 0, k = 0; i < n; ++i)
            if (k == (int)c.size())
                a[i] = b[j++];
            else if (j == (int)b.size())
                a[i] = c[k++];
            else if (b[j] <= c[k])
                a[i] = b[j++];
            else {
                a[i] = c[k++];
                count += n / 2 - j;
            }
    }
    return count;
}
bool WarshallFloyd(vector<vector<ll>>& c) {
    int V = c.size();
    for (int i = 0; i < V; i++) {
        c[i][i] = 0;
    }

    for (int i = 0; i < V; i++) {
        for (int j = 0; j < V; j++) {
            for (int k = 0; k < V; k++) {
                if (c[j][k] > c[j][i] + c[i][k]) {
                    if(c[j][i] != LONGINF && c[i][k] != LONGINF){
                        c[j][k] = c[j][i] + c[i][k];
                    }
                }
            }
        }
    }

    for (int i = 0; i < V; i++) {
        if (c[i][i] < 0) {
            return false;
        }
    }

    return true;
}
vector<ll> Dijkstra(int i, vector<vector<edge>> graph) {
    int n = graph.size();
    vector<ll> d(n, LONGINF);
    d[i] = 0;
    priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
        q;
    q.push(make_pair(0, i));
    while (!q.empty()) {
        pair<ll, int> p = q.top();
        q.pop();
        int v = p.second;
        if (d[v] < p.first) {
            continue;
        }
        for (auto x : graph[v]) {
            if (d[x.to] > d[v] + x.cost) {
                d[x.to] = d[v] + x.cost;
                q.push(make_pair(d[x.to], x.to));
            }
        }
    }
    return d;
}
vector<vector<ll>> gridDijkstra(const int &start_h,const int &start_w,const vector<vector<ll>> &a){
    const int h=a.size();
    const int w=a[0].size();
    vector<vector<ll>> dp(h,vector<ll>(w,LONGINF));

    struct status{
        int x,y;
        ll cost;
        status(int x_,int y_):status(x_,y_,1LL){};
        status(int x_,int y_,ll cost_):x(x_),y(y_),cost(cost_){};
    };
    class Compare {
    public:
	    bool operator()(const status&from, const status&to) {
		    return from.cost>to.cost;
	    }
    };

    priority_queue<status,vector<status>,Compare> q;
    status tmp_start(start_h,start_w,0LL);
    q.push(tmp_start);

    //from,to cost
    auto costFunction=[](const ll a,const ll b){return max(b-a,0LL);};
    //auto costFinction=[](const ll a,const ll b){return a+b};

    #define COST costFunction(a[x][y],a[nx][ny])

    while(q.size()){
        status tmp1(q.top());
        int x=tmp1.x;
        int y=tmp1.y;
        ll sum=tmp1.cost;
        q.pop();
        if(chmin(dp[x][y],sum)){
            for(int i=0;i<4;i++){
                int nx=x+dx[i];
                int ny=y+dy[i];
                if(nx<0||nx>=h||ny<0||ny>=w){
                    continue;
                }
                if(dp[nx][ny]!=LONGINF&&dp[nx][ny]<=sum+COST){
                    continue;
                }
                status tmp2(nx,ny,sum+COST);
                q.push(tmp2);
            }
        }
    }

    return dp;
}
bool BellmanFord(int start, int V, int E, vector<edge> Edge, vector<ll>& d) {
    resize(d, V);
    fill(d.begin(), d.end(), LONGINF);
    d[start] = 0;
    vector<bool> t(V, false);
    for (int i = 0; i < V - 1; i++) {
        for (int j = 0; j < E; j++) {
            edge e = Edge[j];
            if (d[e.from] == LONGINF) {
                continue;
            }
            if (d[e.to] > d[e.from] + e.cost) {
                d[e.to] = d[e.from] + e.cost;
            }
        }
    }
    for (int i = 0; i < V; i++) {
        for (int j = 0; j < E; j++) {
            edge e = Edge[j];
            if (d[e.from] == LONGINF) {
                continue;
            }
            if (d[e.to] > d[e.from] + e.cost) {
                d[e.to] = d[e.from] + e.cost;
                t[e.to] = true;
                /*
                if (i == V - 1) {
                        return false;
                }
                */
            }
            if (t[e.from]) {
                t[e.to] = true;
            }
        }
    }

    if (t[V - 1]) {
        return false;
    }

    return true;
}
bool TopologicalSort(const vector<vector<edge>>& g, vector<int>& ans) {
    int n = g.size(), k = 0;
    vector<int> ord(n), in(n);
    for (auto& es : g) {
        for (auto& e : es) {
            in[e.to]++;
        }
    }
    queue<int> q;
    for (int i = 0; i < n; ++i) {
        if (in[i] == 0) q.push(i);
    }
    while (!q.empty()) {
        int v = q.front();
        q.pop();
        ord[k++] = v;
        for (auto& e : g[v]) {
            if (--in[e.to] == 0) q.push(e.to);
        }
    }
    ans = ord;
    if (*max_element(in.begin(), in.end()) == 0) {
        return true;
    }
    return false;
}
vector<int> ArticulationNode(const vector<vector<edge>>& g) {
    int n = g.size(), idx;
    vector<int> low(n), ord(n), art;
    function<void(int)> DFS = [&](int v) {
        low[v] = ord[v] = ++idx;
        for (auto& e : g[v]) {
            int w = e.to;
            if (ord[w] == 0) {
                DFS(w);
                low[v] = min(low[v], low[w]);
                if ((ord[v] == 1 && ord[w] != 2) || (ord[v] != 1 && low[w] >= ord[v])) {
                    art.push_back(v);
                }
            }
            else {
                low[v] = min(low[v], ord[w]);
            }
        }
    };
    for (int u = 0; u < n; u++) {
        if (ord[u] == 0) {
            idx = 0;
            DFS(u);
        }
    }

    sort(art.begin(), art.end());
    art.erase(unique(art.begin(), art.end()),
        art.end());

    return art;
}
edge TreeDiameter(const vector<vector<edge>>& g) {
    int start = 0;
    static const auto bfs = [](const vector<vector<edge>>& g, int s,
        queue<int>& q, vector<ll>& dist) {
            while (!q.empty()) {
                q.pop();
            }
            q.push(s);
            int n = g.size();
            dist.assign(n, LONGINF);
            dist[s] = 0;
            while (q.size()) {
                int u = q.front();
                q.pop();
                for (auto& e : g[u]) {
                    int v = e.to;
                    if (dist[v] == LONGINF) {
                        dist[v] = dist[u] + e.cost;
                        q.push(v);
                    }
                }
            }
            return dist;
    };
    vector<ll> dist;
    queue<int> q;
    bfs(g, start, q, dist);
    int n = g.size(), u = -1, v = -1;
    for (int i = 0; i < n; i++)
        if (dist[i] != LONGINF && (u == -1 || dist[i] > dist[u])) u = i;
    bfs(g, u, q, dist);
    for (int i = 0; i < n; i++)
        if (dist[i] != LONGINF && (v == -1 || dist[i] > dist[v])) v = i;
    ll d = dist[v];
    if (u > v) swap(u, v);
    return edge(u, v, d);
}
class UnionFind {
private:
    std::vector<int> parent;
    std::vector<int> height;
    std::vector<int> m_size;
    int forest_num;

public:
    UnionFind(int size_) : parent(size_), height(size_, 0), m_size(size_, 1) {
        forest_num = size_;
        for (int i = 0; i < size_; ++i) parent[i] = i;
    }
    void init(int size_) {
        parent.resize(size_);
        height.resize(size_, 0);
        m_size.resize(size_, 1);
        forest_num = size_;
        for (int i = 0; i < size_; ++i) parent[i] = i;
    }
    int find(int x) {
        if (parent[x] == x) return x;
        return parent[x] = find(parent[x]);
    }
    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return false;
        int t = size(x) + size(y);
        m_size[x] = m_size[y] = t;
        if (height[x] < height[y])
            parent[x] = y;
        else
            parent[y] = x;
        if (height[x] == height[y]) ++height[x];
        forest_num--;
        return true;
    }
    bool same(int x, int y) { return find(x) == find(y); }
    int size(int x) {
        if (parent[x] == x) return m_size[x];
        return size(parent[x] = find(parent[x]));
    }
    int forest() { return forest_num; }
    vector<int> get_parent(){
        return parent;
    }
};
class Dinic {
private:
    int n, s, t;
    vector<int> level, prog, que;
    vector<vector<ll>> cap, flow;
    vector<vector<int>> g;
    ll inf;

public:
    Dinic(const vector<vector<edge>>& graph)
        : n(graph.size()),
        cap(n, vector<ll>(n)),
        flow(n, vector<ll>(n)),
        g(n, vector<int>()),
        inf(LONGINF) {
        for (int i = 0; i < n; i++) {
            for (auto& e : graph[i]) {
                int u = e.from, v = e.to;
                ll c = e.capa;
                cap[u][v] += c;
                cap[v][u] += c;
                flow[v][u] += c;
                g[u].push_back(v);
                g[v].push_back(u);
            }
        }
    }
    inline ll residue(int u, int v) { return cap[u][v] - flow[u][v]; }
    ll solve(int s_, int t_) {
        this->t = t_, this->s = s_;
        que.resize(n + 1);
        ll res = 0;
        while (levelize()) {
            prog.assign(n, 0);
            res += augment(s, inf);
        }
        return res;
    }
    bool levelize() {
        int l = 0, r = 0;
        level.assign(n, -1);
        level[s] = 0;
        que[r++] = s;
        while (l != r) {
            int v = que[l++];
            if (v == t) break;
            for (const int& d : g[v])
                if (level[d] == -1 && residue(v, d) != 0) {
                    level[d] = level[v] + 1;
                    que[r++] = d;
                }
        }
        return level[t] != -1;
    }
    ll augment(int v, ll lim) {
        ll res = 0;
        if (v == t) return lim;
        for (int& i = prog[v]; i < (int)g[v].size(); i++) {
            const int& d = g[v][i];
            if (residue(v, d) == 0 || level[v] >= level[d]) continue;
            const ll aug = augment(d, min(lim, residue(v, d)));
            flow[v][d] += aug;
            flow[d][v] -= aug;
            res += aug;
            lim -= aug;
            if (lim == 0) break;
        }
        return res;
    }
};
class MinimumCostFlow {
private:
    using Flow = ll;
    using Cost = ll;
    struct Edge {
        int d;
        Flow c, f;
        Cost w;
        int r, is_r;
        Edge(int d_, Flow c_, Flow f_, Cost w_, int r_, bool is_r_)
            : d(d_), c(c_), f(f_), w(w_), r(r_), is_r(is_r_) {}
    };
    int n;
    vector<vector<Edge>> g;

public:
    MinimumCostFlow(int n_) : n(n_), g(vector<vector<Edge>>(n_)) {}

    void add_edge(int src, int dst, Cost cost,Flow cap) {
        int rsrc = g[dst].size();
        int rdst = g[src].size();
        g[src].emplace_back(dst, cap, 0, cost, rsrc, false);
        g[dst].emplace_back(src, cap, cap, -cost, rdst, true);
    }

    Cost solve(int s, int t, Flow f) {
        Cost res = 0;

        vector<Cost> h(n + 10), dist(n);
        vector<int> prevv(n + 10), preve(n + 10);

        using pcv = pair<Cost, int>;
        priority_queue<pcv, vector<pcv>, greater<pcv>> q;
        fill(h.begin(), h.end(), 0);
        while (f > 0) {
            fill(dist.begin(), dist.end(), LONGINF);
            dist[s] = 0;
            q.emplace(0, s);
            while (q.size()) {
                Cost cd;
                int v;
                tie(cd, v) = q.top();
                q.pop();
                if (dist[v] < cd) continue;
                for (int i = 0; i < (int)(g[v].size()); ++i) {
                    Edge& e = g[v][i];
                    if (residue(e) == 0) continue;
                    if (dist[e.d] + h[e.d] > cd + h[v] + e.w) {
                        dist[e.d] = dist[v] + e.w + h[v] - h[e.d];
                        prevv[e.d] = v;
                        preve[e.d] = i;
                        q.emplace(dist[e.d], e.d);
                    }
                }
            }

            if (dist[t] == LONGINF) return -1;

            for (int i = 0; i < n; ++i) h[i] += dist[i];
            Flow d = f;
            for (int v = t; v != s; v = prevv[v]) {
                chmin(d, residue(g[prevv[v]][preve[v]]));
            }
            f -= d;
            res += d * h[t];
            for (int v = t; v != s; v = prevv[v]) {
                Edge& e = g[prevv[v]][preve[v]];
                e.f += d;
                g[v][e.r].f -= d;
            }
        }
        return res;
    }

    Flow residue(const Edge& e) { return e.c - e.f; }

    void show() {
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < (int)(g[i].size()); ++j) {
                Edge& e = g[i][j];
                if (e.is_r) continue;
                cout << i << "->" << e.d << "(flow:" << e.f << ")" << endl;
            }
        }
    }
};
class BipartiteMatching {
private:
    int V;
    vector<int> match;
    vector<bool> used;
    vector<vector<int>> g;
    vector<pair<int, int>> match_pair;

    bool dfs(int v) {
        used[v] = true;
        for (int i = 0; i < (int)g[v].size(); i++) {
            int u = g[v][i];
            int w = match[u];
            if (w < 0 || !used[w] && dfs(w)) {
                match[v] = u;
                match[u] = v;
                match_pair.emplace_back(make_pair(u, v));
                return true;
            }
        }
        return false;
    }

public:
    BipartiteMatching(int n) {
        V = n;
        resize(match, n);
        resize(used, n);
        resize(g, n);
    }

    void add_edge(int u, int v) {
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    }

    int MatchingSolve() {
        int res = 0;
        fill(match.begin(), match.end(), -1);

        for (int v = 0; v < V; v++) {
            if (match[v] < 0) {
                fill(used.begin(), used.end(), false);
                if (dfs(v)) {
                    res++;
                }
            }
        }
        return res;
    }

    vector<pair<int, int>> get_pair() {
        for (auto x : match_pair) {
            cout << x.first << "  " << x.second << endl;
        }
        return match_pair;
    }
};
class Lca {
private:
    int n;
    int log2_n;
    vector<vector<int>> parent;
    vector<int> depth;

    void dfs(const vector<vector<edge>>& g, int v, int p, int d) {
        parent[0][v] = p;
        depth[v] = d;
        for (auto& e : g[v]) {
            if (e.to != p) {
                dfs(g, e.to, v, d + 1);
            }
        }
    }

public:
    Lca(const vector<vector<edge>>& g, int root) {
        n = g.size();
        log2_n = (int)log2(n) + 1;
        resize(parent, log2_n, n);
        resize(depth, n);

        dfs(g, root, -1, 0);

        for (int k = 0; k + 1 < log2_n; k++) {
            for (int v = 0; v < (int)g.size(); v++) {
                if (parent[k][v] < 0) {
                    parent[k + 1][v] = -1;
                }
                else {
                    parent[k + 1][v] = parent[k][parent[k][v]];
                }
            }
        }
    }

    int get_lca(int u, int v) {
        if (depth[u] > depth[v]) {
            swap(u, v);
        }

        for (int k = 0; k < log2_n; k++) {
            if ((depth[v] - depth[u]) >> k & 1) {
                v = parent[k][v];
            }
        }
        if (u == v) {
            return u;
        }

        for (int k = log2_n - 1; k >= 0; k--) {
            if (parent[k][u] != parent[k][v]) {
                u = parent[k][u];
                v = parent[k][v];
            }
        }
        return parent[0][u];
    }

    int get_depth(int v) { return depth[v]; }
};
class DAG {
private:
    int n;
    vector<vector<edge>> g;
    vector<int> visited;
    vector<int> dp;
    vector<int> topological;

    int dfs(int s) {
        if ((int)g[s].size() == 0) {
            return 1;
        }
        if (dp[s] > 0) {
            return dp[s];
        }

        int mx = 1;
        for (auto j : g[s]) {
            if (visited[j.to] == 0) {
                visited[j.to] = 1;
                int l = 0;
                l = dfs(j.to);
                chmax(mx, l);
            }
            else {
                chmax(mx, dp[j.to]);
            }
        }
        return dp[s] = mx + 1;
    }

public:
    DAG(const vector<vector<edge>>& f) {
        g = f;
        n = f.size();
        resize(visited, n + 1);
        fill(visited.begin(), visited.end(), 0);
        resize(dp, n + 1);
        fill(dp.begin(), dp.end(), -1);
        resize(topological, n);
    }
    DAG(int x) {
        n = x;
        resize(g, n);
        resize(visited, n + 1);
        fill(visited.begin(), visited.end(), 0);
        resize(dp, n + 1);
        fill(dp.begin(), dp.end(), -1);
    }

    void add_edge(int a, int b) { g[a].emplace_back(a, b); }
    void add_edge(int a, int b, ll c) { g[a].emplace_back(a, b, c); }
    void add_edge(int a, int b, ll c, ll d) { g[a].emplace_back(a, b, c, d); }

    int longest_path() {
        int mx = -1;
        int res=0;
        for (int i = 0; i < n; i++) {
            int h = 0;
            if (visited[i] == 0) {
                h = dfs(i);
                if(mx<h){
                    mx=h;
                    res=i;
                }
            }
        }
        return res;
    }

    bool TopologicalSort() {
        int k = g.size();
        vector<int> ord(n), in(n);
        for (int i=0;i<n;i++) {
            for (auto& e : g[i]) {
                in[e.to]++;
            }
        }
        priority_queue<int> q;
        for (int i = 0; i < n; ++i) {
            if (in[i] == 0) q.push(i);
        }
        while (!q.empty()) {
            int v = q.top();
            q.pop();
            ord[v] = k;
            k--;
            for (auto& e : g[v]) {
                in[e.to]-=1;
                if (in[e.to] == 0) {
                    q.push(e.to);
                }
            }
        }
        topological = ord;
        if (*max_element(in.begin(), in.end()) == 0) {
            return true;
        }
        return false;
    }

    vector<int> getTopologicalArray(){
        return topological;
    }
};

//↑ここまでGitHubに突っ込んだ

class RangeMinimumUpdateQuerySegmentTree {
private:
    int n;
    ll inf = (1LL << 31) - 1;  // 2^31-1
    vector<ll> dat, lazy;

    void eval(int len, int k) {
        if (lazy[k] == inf) return;
        if (k * 2 + 1 < n * 2 - 1) {
            lazy[2 * k + 1] = lazy[k];
            lazy[2 * k + 2] = lazy[k];
        }
        dat[k] = lazy[k];
        lazy[k] = inf;
    }

public:
    RangeMinimumUpdateQuerySegmentTree() {}
    RangeMinimumUpdateQuerySegmentTree(int n_) {
        n = 1;
        while (n < n_) n *= 2;
        dat.assign(n * 2, inf);
        lazy.assign(n * 2, inf);
    }

    // [a,b)
    ll update(int a, int b, ll x, int k, int l, int r) {
        eval(r - l, k);
        if (b <= l || r <= a) return dat[k];
        if (a <= l && r <= b) {
            lazy[k] = x;
            return lazy[k];
        }
        return dat[k] = min(update(a, b, x, 2 * k + 1, l, (l + r) / 2),
            update(a, b, x, 2 * k + 2, (l + r) / 2, r));
    }
    ll update(int a, int b, ll x) { return update(a, b, x, 0, 0, n); }

    // [a, b)
    ll query(int a, int b, int k, int l, int r) {
        eval(r - l, k);
        if (b <= l || r <= a) return inf;
        if (a <= l && r <= b) return dat[k];
        ll vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
        ll vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
        return min(vl, vr);
    }
    ll query(int a, int b) { return query(a, b, 0, 0, n); }
};
class RangeSumQuerySegmentTree {
private:
    struct Node {
        Node* left, * right;
        ll v;

        Node() : left(nullptr), right(nullptr), v(0) {}
    };
    Node* root;
    ll n, n0;
    ll query(ll a, ll b, Node* n, ll l, ll r) {
        if (a <= l && r <= b) {
            return n->v;
        }
        if (r <= a || b <= l) {
            return 0;
        }

        ll lv = n->left ? query(a, b, n->left, l, (l + r) >> 1) : 0;
        ll rv = n->right ? query(a, b, n->right, (l + r) >> 1, r) : 0;
        return (lv + rv) % MOD;
    }

public:
    RangeSumQuerySegmentTree(ll n) : n(n) {
        n0 = 1;
        while (n0 < n) n0 <<= 1;
        root = new Node();
    }
    ~RangeSumQuerySegmentTree() {
        delete root;
        root = nullptr;
    }

    void update(ll k, ll x) {
        Node* n = root;
        ll l = 0, r = n0;
        n->v = (n->v + x) % MOD;
        while (r - l > 1) {
            ll m = (l + r) >> 1;
            if (k < m) {
                if (!n->left) n->left = new Node();
                n = n->left;

                r = m;
            }
            else {
                if (!n->right) n->right = new Node();
                n = n->right;

                l = m;
            }
            n->v = (n->v + x) % MOD;
        }
    }

    ll query(ll a, ll b) { return query(a, b, root, 0, n0); }

    ll lquery(ll b) { return query(0, b, root, 0, n0); }

    ll rquery(ll a) { return query(a, n0, root, 0, n0); }
};
class KDimensionalTree {
public:
    struct Node {
        int location;
        int p, l, r;
        Node() {}
    };
    struct Point {
        int id, x, y;
        Point() {}
        Point(int i, int a, int b) {
            id = i;
            x = a;
            y = b;
        }
        bool operator<(const Point& p) const { return id < p.id; }
        void print() { cout << id << endl; }
    };
    static const ll NIL = -1;
    static bool lessX(const Point& p1, const Point& p2) { return p1.x < p2.x; }
    static bool lessY(const Point& p1, const Point& p2) { return p1.y < p2.y; }

    int N;
    vector<Point> P;
    vector<Node> T;
    int np;

    KDimensionalTree() {}
    KDimensionalTree(int N) { init(N); }

    void init(int n) {
        N = n;
        P.clear();
        T.clear();
        resize(P, N);
        resize(T, N);
        np = 0;
    }

    int makeKDTree(int l, int r, int depth) {
        if (l >= r) {
            return NIL;
        }
        int mid = (l + r) / 2;
        int t = np++;
        if (depth & 1) {
            sort(P.begin() + l, P.begin() + r, lessY);
        }
        else {
            sort(P.begin() + l, P.begin() + r, lessX);
        }
        T[t].location = mid;
        T[t].l = makeKDTree(l, mid, depth + 1);
        T[t].r = makeKDTree(mid + 1, r, depth + 1);
        return t;
    }
    void find(int v, int sx, int tx, int sy, int ty, int depth,
        vector<Point>& ans) {
        int x = P[T[v].location].x;
        int y = P[T[v].location].y;
        if (sx <= x && x <= tx && sy <= y && y <= ty) {
            ans.push_back(P[T[v].location]);
        }
        if (depth % 2 == 0) {
            if (T[v].l != NIL) {
                if (sx <= x) find(T[v].l, sx, tx, sy, ty, depth + 1, ans);
            }
            if (T[v].r != NIL) {
                if (x <= tx) find(T[v].r, sx, tx, sy, ty, depth + 1, ans);
            }
        }
        else {
            if (T[v].l != NIL) {
                if (sy <= y) find(T[v].l, sx, tx, sy, ty, depth + 1, ans);
            }
            if (T[v].r != NIL) {
                if (y <= ty) find(T[v].r, sx, tx, sy, ty, depth + 1, ans);
            }
        }
    }
    void add_point(int i, int x, int y) {
        P[i].id = i;
        P[i].x = x;
        P[i].y = y;
        T[i].l = T[i].r = T[i].p = NIL;
    }
};
class RangeAddQuerySegmentTree {
private:
    int n;
    vector<ll> data;

public:
    RangeAddQuerySegmentTree() {}
    RangeAddQuerySegmentTree(int N) {
        n = N;
        resize(data, n + 1);
        fill(data.begin(), data.end(), 0);
    }

    void add(int i, ll x) {
        while (i) {
            data[i] += x;
            i -= (i & -i);
        }
    }
    void add(int i, int j, ll x) {
        add(j, x);
        add(i - 1, -x);
    }

    ll get(int i) {
        ll res = 0;
        while (i <= n) {
            res += data[i];
            i += (i & -i);
        }
        return res;
    }
};
class RangeSumAddQuerySegmentTree {
private:
    vector<ll> bit0, bit1;
    int n;

    ll sum(const vector<ll>& b, int i) {
        ll s = 0;
        while (i > 0) {
            s += b[i];
            i -= (i & -i);
        }
        return s;
    }

    void add(vector<ll>& b, int i, ll v) {
        while (i <= n) {
            b[i] += v;
            i += (i & -i);
        }
    }

public:
    RangeSumAddQuerySegmentTree() {}
    RangeSumAddQuerySegmentTree(int N) {
        n = N;
        resize(bit0, n + 1);
        resize(bit1, n + 1);
        fill(bit0.begin(), bit0.end(), 0);
        fill(bit1.begin(), bit1.end(), 0);
    }

    void update(int l, int r, ll x) {
        add(bit0, l, -x * (l - 1));
        add(bit1, l, x);
        add(bit0, r + 1, x * r);
        add(bit1, r + 1, -x);
    }

    ll query(int l, int r) {
        ll res = 0;
        res += sum(bit0, r) + sum(bit1, r) * r;
        res -= sum(bit0, l - 1) + sum(bit1, l - 1) * (l - 1);
        return res;
    }
};

long double cross(const complex<long double> &a, const complex<long double> &b) { return a.real() * b.imag() - a.imag() * b.real(); }
long double dot(const complex<long double> &a, const complex<long double> &b) { return a.real() * b.real() + a.imag() * b.imag(); }

pair<long double,complex<long double>> smallest_enclosing_disc(vector<complex<long double>> ps){
    const long double pi = 3.1415926535897932384626;
    static const long double eps = 1e-10;
    static const long double inf = 1e12;

    using circle = pair<complex<long double>, long double>;

    auto c3 = [](const complex<long double> &a, const complex<long double> &b, const complex<long double> &c){
        long double A = norm(b - c);
        long double B = norm(c - a);
        long double C = norm(a - b);
        long double S = abs(cross(b - a, c - a));
        complex<long double> p = (A*(B+C-A)*a + B*(C+A-B)*b + C*(A+B-C)*c) / (4*S*S);
        long double r = abs(p - a);
        return make_pair(r, p);
    };

    auto c2 = [](const complex<long double> &a, const complex<long double> &b){
        complex<long double> c = (a + b) / (long double)2;
        long double r = abs(a - c);
        return make_pair(r, c);
    };

    auto in_circle = [](const complex<long double> &a, const pair<long double, complex<long double>> &c){
        return norm(a - c.second) <= c.first*c.first + eps;
    };

    int n = ps.size();
    random_shuffle(ps.begin(), ps.end());
    pair<long double, complex<long double>> c = c2(ps[0], ps[1]);
    for(int i = 2; i < n; ++i){
        if(!in_circle(ps[i], c)){
            c = c2(ps[0], ps[i]);
            for(int j = 1; j < i; ++j){
                if(!in_circle(ps[j], c)){
                    c = c2(ps[j], ps[i]);
                    for(int k = 0; k < j; ++k){
                        if(!in_circle(ps[k], c)){
                            c = c3(ps[i], ps[j], ps[k]);
                        }
                    }
                }
            }
        }
    }
    return c;
}

pair<ll, vector<edge>> Prim_minimumSpanningTree(const vector<vector<edge>> &g, int r,int K) {
  int n = g.size();
  vector<edge> T;
  ll total = 0;

  vector<bool> visited(n);
  priority_queue<edge> Q;
  Q.push( edge(-1, r, 0,0));
  ll ans=0;
  int k=0;
  while (!Q.empty()) {
    edge e = Q.top(); Q.pop();
    if (visited[e.to]) continue;
    T.push_back(e);
    total += e.cost;
    ans=max(ans,-e.cost);
    k++;
    if(k==K){
        break;
    }
    visited[e.to] = true;
    for(auto f:g[e.to]) if (!visited[f.to]) Q.push(f);
  }
  return pair<ll, vector<edge>>(ans, T);
}

class NodeTwoColoring{
    private:
        vector<vector<edge>> G;
        vector<int> visited;
        vector<int> white;
        vector<int> black;

        void dfs(const vector<vector<edge>> &G,int v=0, ll d=0) {
            if(d & 1){white.push_back(v+1);}
            else{black.push_back(v+1);}
            visited[v]=1+(d&1);

	        for (auto nv : G[v]) {
		        if (visited[nv.to]==-1){}
                else{
                    continue;
                }
		        dfs(G,nv.to,d+nv.cost);
	        }
        }

    public:
    NodeTwoColoring(){};
    NodeTwoColoring(const vector<vector<edge>> &g):G(g){
        int n=G.size();
        visited.resize(n,-1);
        dfs(G);
    };

    vector<int> get_white(){
        return white;
    }
    vector<int> get_black(){
        return black;
    }

    vector<int> get_color(){
        return visited;
    }

};

/*
const int MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];

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

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

int main() {
    ll x;cin>>x;

    for(ll i=-5000;i<=5000;i++){
        for(ll j=-5000;j<=5000;j++){
            if(i*i*i*i*i-(j*j*j*j*j)==x){
                cout<<i<<" "<<j<<endl;
                return 0;
            }
        }
    }

    ;
    return 0;
}


#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i < (n); i++)
#define RREP(i, s, n) for (int i = s; i < (n); i++)
#define ALL(a) a.begin(), a.end()
#define RALL(a) a.rbegin(), a.rend()
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; }
using namespace std;
using ll = long long;
typedef pair<int, int> pint;
typedef pair<ll, ll> pll;
const ll MOD = 1000000007;
const ll INF = MOD * MOD;
const int inf = (1<<29);

int main() {

    int n;
    cin >> n;
    vector<int> a(n);
    REP(i, n) cin >> a[i];

    int res = 0;

    for(int i = 0; i < n; i++) {
        for(int j = i+1; j < n; j++) {
            if (abs(i-j) == a[i] + a[j]) res++;
        }
    }

    cout << res << endl;

    return 0;
}

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score: 500 points

Problem Statement

You are the top spy of AtCoder Kingdom. To prevent the stolen secret from being handed to AlDebaran Kingdom, you have sneaked into the party where the transaction happens.

There are N attendees in the party, and they are given attendee numbers from 1 through N. The height of Attendee i is A_i.

According to an examination beforehand, you know that a pair of attendees satisfying the condition below will make the transaction.

The absolute difference of their attendee numbers is equal to the sum of their heights.

There are \frac{N(N-1)}{2} ways to choose two from the N attendees and make a pair. Among them, how many satisfy the condition above?

P.S.: We cannot let you know the secret.

Constraints

All values in input are integers.

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9\ (1 \leq i \leq N)

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the number of pairs satisfying the condition.

Sample Input 1

6
2 3 3 1 3 1

Sample Output 1

3

A_1 + A_4 = 3, so the pair of Attendee 1 and 4 satisfy the condition.

A_2 + A_6 = 4, so the pair of Attendee 2 and 6 satisfy the condition.

A_4 + A_6 = 2, so the pair of Attendee 4 and 6 satisfy the condition.

No other pair satisfies the condition, so you should print 3.

Sample Input 2

6
5 2 4 2 8 8

Sample Output 2

0

No pair satisfies the condition, so you should print 0.

Sample Input 3

32
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5

Sample Output 3

22

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


Score : 600 points

Problem Statement

There is a game that involves three variables, denoted A, B, and C.

As the game progresses, there will be N events where you are asked to make a choice.
Each of these choices is represented by a string s_i. If s_i is AB, you must add 1 to A or B then subtract 1 from the other; if s_i is AC, you must add 1 to A or C then subtract 1 from the other; if s_i is BC, you must add 1 to B or C then subtract 1 from the other.

After each choice, none of A, B, and C should be negative.

Determine whether it is possible to make N choices under this condition. If it is possible, also give one such way to make the choices.

Constraints

1 \leq N \leq 10^5

0 \leq A,B,C \leq 10^9

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

If it is possible to make N choices under the condition, print Yes; otherwise, print No.

Also, in the former case, show one such way to make the choices in the subsequent N lines. The (i+1)-th line should contain the name of the variable (A, B, or C) to which you add 1 in the i-th choice.

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

In the first choice, add 1 to A and subtract 1 from B. A becomes 2, and B becomes 2.

In the second choice, add 1 to C and subtract 1 from A. C becomes 1, and A becomes 1.

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

8 6 9 1
AC
BC
AB
BC
AC
BC
AB
AB

Sample Output 4

Yes
C
B
B
C
C
B
A
A


#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <sstream>
#include <bitset>
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)

using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

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

    ll i,j,h;
    ll Q,M,K,P,A,B,C,N,X;

    long double x;

    string S;

    ll ans=0;
    ll count=0;
    ll flag=0;
    ll temp=0;
    vector<char> kotae;
    vector<string> ss;
    ll a,b;
    cin >>N>>A>>B>>C;

rep(i,N){
  cin>>S;
  ss.push_back(S);
}
//cout<<"aa"<<endl;
map <char,ll> mm;
mm['B']=B;
mm['A']=A;
mm['C']=C;
//cout<<0;
if(A+B+C==0){
  cout<<"No"<<endl;
  return 0;
}else if(A+B+C==1){
  rep(i,N){
  	if(mm[ss[i][0]]==0 && mm[ss[i][1]]==0){
      cout << "No"<<endl;
      return 0;
    }else if (mm[ss[i][0]]==0){
      mm[ss[i][0]]++;
      mm[ss[i][1]]--;
      kotae.push_back(ss[i][0]);
    }else{
      mm[ss[i][0]]--;
      mm[ss[i][1]]++;
      kotae.push_back(ss[i][1]);
    }
  }
}else{
    //cout<<"ee"<<endl;
    //cout<<ss[i][0<<ss[i][1]<<endl;

	if(mm[ss[0][0]]==0 && mm[ss[0][1]]==0){
        cout<<"No"<<endl;
        return 0;
    }else{

        rep(i,N){

            if(mm[ss[i][0]]==0){
                mm[ss[i][0]]++;
                mm[ss[i][1]]--;
                kotae.push_back(ss[i][0]);
            }else if(mm[ss[i][1]]==0){
                mm[ss[i][1]]++;
                mm[ss[i][0]]--;
                kotae.push_back(ss[i][1]);
            }else if(i!=N-1 && (mm[ss[i][0]]==1 || mm[ss[i][1]]==1)){
                if(ss[i+1][0]==ss[i][0] || ss[i+1][1]==ss[i][0] ){
                    mm[ss[i][1]]--;
                    mm[ss[i][0]]++;
                    kotae.push_back(ss[i][0]);

                }else{
                    mm[ss[i][1]]++;
                    mm[ss[i][0]]--;
                    kotae.push_back(ss[i][1]);
                }

            }else{
                mm[ss[i][0]]--;
                mm[ss[i][1]]++;
                kotae.push_back(ss[i][1]);
            }
        }

    }
}

cout<<"Yes"<<endl;
rep(i,N){
    cout<<kotae[i]<<endl;
}

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define INF (1LL<<50)
#define MOD 1000000007

int main(){
    int N,A,B,C,D;
    cin >> N >> A >> B >> C;
    vector<string> S(N);
    rep(i,N){
        cin >> S[i];
    }
    bool flg = true;
    rep(i,N){
        if(S[i]=="AB"){
            if(A<B){
                A++;
                B--;
            }else{
                A--;B++;
            }
        }else if(S[i]=="AC"){
            if(A<C){
                A++;C--;
            }else{
                A--;C++;
            }
        }else{
            if(B<C){
                B++;C--;
            }else{
                B--;C++;
            }
        }
        if(A*B*C<0) {
            flg=false;
            break;
        }
    }
    if(flg){
        cout << "Yes" << endl;
        rep(i,N){
        if(S[i]=="AB"){
            if(A<B){
                cout << "A" << endl;
            }else{
                cout << "B" << endl;
            }
        }else if(S[i]=="AC"){
            if(A<C){
                cout << "A" << endl;
            }else{
                cout << "C" << endl;
            }
        }else{
            if(B<C){
                cout << "B" << endl;
            }else{
                cout << "C" << endl;
            }
        }}
    }else{
        cout << "No" << endl;
    }
    return 0;
}


#include <iostream>
using namespace std;

int main() {
    int K, A, B;
    bool flag;
    cin >> K >> A >> B;

    flag = false;
    for (int i=1; i<=1000; i++) {
        if (A<=i*K and i*K<=B) {
            flag = true;
            break;
        }
    }

    if (flag) {
        cout << "OK" << endl;
    } else {
        cout << "NG" << endl;
    }
    return 0;
}

Score: 100 points

Problem Statement

Takahashi the Jumbo will practice golf.

His objective is to get a carry distance that is a multiple of K, while he can only make a carry distance of between A and B (inclusive).

If he can achieve the objective, print OK; if he cannot, print NG.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 1000

1 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

K
A B

Output

If he can achieve the objective, print OK; if he cannot, print NG.

Sample Input 1

7
500 600

Sample Output 1

OK

Among the multiples of 7, for example, 567 lies between 500 and 600.

Sample Input 2

4
5 7

Sample Output 2

NG

No multiple of 4 lies between 5 and 7.

Sample Input 3

1
11 11

Sample Output 3

OK


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define dup(x,y) (((x)+(y)-1)/(y))
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef pair<int, int> pii;
const double EPS = 1e-10;
const int INF = 1e9;
const ll LINF = 1e15;
const int MOD = 1000000007;
const double PI = acos(-1);
int dx[4] = {0,1,0,-1};
int dy[4] = {1,0,-1,0};

int main() {
	ll x;
	cin >> x;
	ll cur = 100;
	int ans = 0;
	if (x == 100) {
		cout << 0 << endl;
		return 0;
	}
	while (cur < x / 1.01) {
		ans++;
		cur *= 1.01;
	}
	cout << ans + 1 << endl;
}

Score : 200 points

Problem Statement

Takahashi has a deposit of 100 yen (the currency of Japan) in AtCoder Bank.

The bank pays an annual interest rate of 1 % compounded annually. (A fraction of less than one yen is discarded.)

Assuming that nothing other than the interest affects Takahashi's balance, in how many years does the balance reach X yen or above for the first time?

Constraints

101 \le X \le 10^{18}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X

Output

Print the number of years it takes for Takahashi's balance to reach X yen or above for the first time.

Sample Input 1

103

Sample Output 1

3

The balance after one year is 101 yen.

The balance after two years is 102 yen.

The balance after three years is 103 yen.

Thus, it takes three years for the balance to reach 103 yen or above.

Sample Input 2

1000000000000000000

Sample Output 2

3760

Sample Input 3

1333333333

Sample Output 3

1706


#define ll long long
#include "bits/stdc++.h"
using namespace std;
vector<pair<ll ,ll >>vc;
const int N = 2e5 + 2;
ll arr[N];
int main() {
ll n;
cin>>n;
ll res=100;
ll cnt=0;
while(res <= n)
{
    res+=(.01 * res );
    cnt++;
    if(res==n)break;

}
cout<<cnt;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ll,int> pli;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define F first
#define S second
typedef vector<pli> vpli;
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define all(v) v.begin(),v.end()
#define tests int t; cin>>t; while(t--)
#define take(a,b,c) for(b=0;b<c;b++) cin>>a[b];
#define INF 1e18
// https://codeforces.com/problemset/page/3?tags=1500-1800
map <int,stack<int> > m;
int main()
{
  ll x;
  cin>>x;
  ll y=100;
  ll j=1;
  while(1){
    y+=y/100;
    if(y>=x){
      cout<<j;
      break;
    }
    else j++;
  }
}


#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

#define REPR(i,n) for(int i=(n); i >= 0; --i)
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ALL(a)  (a).begin(),(a).end()
#define endl "\n"

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

void solve() {
    ll X;
    cin >> X;
    int nen = 0;
    ll now = 100;
    while (now < X) {
        nen++;
        now*=1.01;
    }
    cout << nen << endl;
}

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


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(var, begin, end) for (int var = (begin); var <= (end); var++)
#define RFOR(var, begin, end) for (int var = (begin); var >= (end); var--)
#define REP(var, length) FOR(var, 0, length - 1)
#define RREP(var, length) RFOR(var, length - 1, 0)
#define EACH(value, var) for (auto value : var)
#define SORT(var) sort(var.begin(), var.end())
#define REVERSE(var) reverse(var.begin(), var.end())
#define RSORT(var) \
  SORT(var);       \
  REVERSE(var)
#define OPTIMIZE_STDIO         \
  ios::sync_with_stdio(false); \
  cin.tie(0);                  \
  cout.precision(10);          \
  cout << fixed
#define endl '\n'

const int INF = 1e9;
const ll MOD = 1e9 + 7;
const ll LINF = 1e18;
struct mint {
  ll x;
  mint() : x(0) {
  }
  mint(ll x) : x((x % MOD + MOD) % MOD) {
  }
  mint& operator+=(const mint& r) {
    if ((x += r.x) >= MOD) {
      x -= MOD;
    }
    return *this;
  }
  mint& operator-=(const mint& r) {
    if ((x -= r.x) < 0) {
      x += MOD;
    }
    return *this;
  }
  mint& operator*=(const mint& r) {
    if ((x *= r.x) > MOD) {
      x %= MOD;
    }
    return *this;
  }
  mint& operator++() {
    if ((++x) >= MOD) {
      x -= MOD;
    }
    return *this;
  }
  mint operator++(int) {
    mint ret = x;
    if ((++x) >= MOD) {
      x -= MOD;
    }
    return ret;
  }
  mint& operator--() {
    if ((--x) < 0) {
      x += MOD;
    }
    return *this;
  }
  mint operator--(int) {
    mint ret = x;
    if ((--x) < 0) {
      x += MOD;
    }
    return ret;
  }
  mint operator+(const mint& r) {
    mint ret;
    return ret = x + r.x;
  }
  mint operator-(const mint& r) {
    mint ret;
    return ret = x - r.x;
  }
  mint operator*(const mint& r) {
    mint ret;
    return ret = x * r.x;
  }
  mint operator-() {
    return mint() - *this;
  }
  bool operator<(const mint& a) {
    return x < a.x;
  }
  bool operator>(const mint& a) {
    return x > a.x;
  }
  bool operator<=(const mint& a) {
    return x <= a.x;
  }
  bool operator>=(const mint& a) {
    return x >= a.x;
  }
  bool operator==(const mint& a) {
    return x == a.x;
  }
  bool operator!=(const mint& a) {
    return x != a.x;
  }
};
ostream& operator<<(ostream& os, const mint& r) {
  os << r.x;
  return os;
}
ostream& operator>>(ostream& os, const mint& r) {
  os >> r.x;
  return os;
}

int n, m, q;
vector<int> a, b, c, d;
int ans;

void dfs(vector<int> buf) {
  if (buf.size() == n + 1) {
    int now = 0;
    REP(i, q) {
      if ((buf[b[i]] - buf[a[i]]) == c[i]) now += d[i];
    }
    ans = max(ans, now);
    return;
  }

  buf.push_back(buf.back());
  while (buf.back() <= m) {
    dfs(buf);
    buf.back()++;
  }
}

void solve(istream& cin, ostream& cout) {
  cin >> n >> m >> q;
  a = b = c = d = vector<int>(q);
  REP(i, q) {
    cin >> a[i] >> b[i] >> c[i] >> d[i];
  }
  dfs(vector<int>(1, 1));
  cout << ans << endl;
}

#ifndef TEST
int main() {
  OPTIMIZE_STDIO;
  solve(cin, cout);
  return 0;
}
#endif

Score : 300 points

Problem Statement

Given are positive integers N, M, Q, and Q quadruples of integers ( a_i , b_i , c_i , d_i ).

Consider a sequence A satisfying the following conditions:

A is a sequence of N positive integers.

1 \leq A_1 \leq A_2 \le \cdots \leq A_N \leq M.

Let us define a score of this sequence as follows:

The score is the sum of d_i over all indices i such that A_{b_i} - A_{a_i} = c_i. (If there is no such i, the score is 0.)

Find the maximum possible score of A.

Constraints

All values in input are integers.

2 ≤ N ≤ 10

1 \leq M \leq 10

1 \leq Q \leq 50

1 \leq a_i < b_i \leq N ( i = 1, 2, ..., Q )

0 \leq c_i \leq M - 1 ( i = 1, 2, ..., Q )

(a_i, b_i, c_i) \neq (a_j, b_j, c_j) (where i \neq j)

1 \leq d_i \leq 10^5 ( i = 1, 2, ..., Q )

Input

Input is given from Standard Input in the following format:

N M Q
a_1 b_1 c_1 d_1
:
a_Q b_Q c_Q d_Q

Output

Print the maximum possible score of A.

Sample Input 1

3 4 3
1 3 3 100
1 2 2 10
2 3 2 10

Sample Output 1

110

When A = \{1, 3, 4\}, its score is 110. Under these conditions, no sequence has a score greater than 110, so the answer is 110.

Sample Input 2

4 6 10
2 4 1 86568
1 4 0 90629
2 3 0 90310
3 4 1 29211
3 4 3 78537
3 4 2 8580
1 2 1 96263
1 4 2 2156
1 2 0 94325
1 4 3 94328

Sample Output 2

357500

Sample Input 3

10 10 1
1 10 9 1

Sample Output 3

1


#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define mod 1000000007
using ll = long long;
using namespace std;
int main(){
  ll a,b,n;
  cin >> a >> b >> n;
  ll ans = 0;
  for(ll x = n; x >= 0; x--){
    ll val = ll(a * x / b) - a * ll(x / b);
    ans = ans < val ? val : ans;
    if(ans > val + 10) break;
  }
  cout << ans << endl;
}

Score : 400 points

Problem Statement

Given are integers A, B, and N.

Find the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N.

Here floor(t) denotes the greatest integer not greater than the real number t.

Constraints

1 ≤ A ≤ 10^{6}

1 ≤ B ≤ 10^{12}

1 ≤ N ≤ 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B N

Output

Print the maximum possible value of floor(Ax/B) - A × floor(x/B) for a non-negative integer x not greater than N, as an integer.

Sample Input 1

5 7 4

Sample Output 1

2

When x=3, floor(Ax/B)-A×floor(x/B) = floor(15/7) - 5×floor(3/7) = 2. This is the maximum value possible.

Sample Input 2

11 10 9

Sample Output 2

9


//g++ -std=gnu++14 a.cpp
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <iostream>
#include <istream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <tuple>
#include <iomanip>
#include <random>
#include <math.h>

using namespace std;

#define ll long long
#define rep(i, n) for(ll i = 0; i < (n); i++)
ll MOD = 1e9 + 7;
int INF = 1 << 30;
ll INFL = 1LL << 60;

int main() {
  ll N,M;
  cin >> N >> M;
  ll a,b;
  a=3;
  b=5;
    if(M>=1)cout << 1 << " " << 2 << endl;
    if(M>=2)cout << 3 << " " << 5 << endl;
  if(M>=3){
    for(int i =3;i <= M;i++){
      a++;
      b=a+i;
      cout << a << " " << b << endl;
    }
  }

}

Score : 500 points

Problem Statement

You are going to hold a competition of one-to-one game called AtCoder Janken. (Janken is the Japanese name for Rock-paper-scissors.)
N players will participate in this competition, and they are given distinct integers from 1 through N.
The arena has M playing fields for two players. You need to assign each playing field two distinct integers between 1 and N (inclusive).
You cannot assign the same integer to multiple playing fields.
The competition consists of N rounds, each of which proceeds as follows:

For each player, if there is a playing field that is assigned the player's integer, the player goes to that field and fight the other player who comes there.

Then, each player adds 1 to its integer. If it becomes N+1, change it to 1.

You want to ensure that no player fights the same opponent more than once during the N rounds.
Print an assignment of integers to the playing fields satisfying this condition.
It can be proved that such an assignment always exists under the constraints given.

Constraints

1 \leq M

M \times 2 +1 \leq N \leq 200000

Input

Input is given from Standard Input in the following format:

N M

Output

Print M lines in the format below.
The i-th line should contain the two integers a_i and b_i assigned to the i-th playing field.

a_1 b_1
a_2 b_2
:
a_M b_M

Sample Input 1

4 1

Sample Output 1

2 3

Let us call the four players A, B, C, and D, and assume that they are initially given the integers 1, 2, 3, and 4, respectively.

The 1-st round is fought by B and C, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 2, 3, 4, and 1, respectively.

The 2-nd round is fought by A and B, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 3, 4, 1, and 2, respectively.

The 3-rd round is fought by D and A, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 4, 1, 2, and 3, respectively.

The 4-th round is fought by C and D, who has the integers 2 and 3, respectively. After this round, A, B, C, and D have the integers 1, 2, 3, and 4, respectively.

No player fights the same opponent more than once during the four rounds, so this solution will be accepted.

Sample Input 2

7 3

Sample Output 2

1 6
2 5
3 4


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
#define ALL(v) v.begin(), v.end()
#define vmax(v) *max_element(ALL(v))
#define vmin(v) *min_element(ALL(v))
#define chmax(x, a) x = max(x, a)
#define chmin(x, a) x = min(x, a)

int main() {
    int N, M; cin >> N >> M;
    if (M % 2 == 1) {
        int tmp = 1;
        int mx = M;
        while(tmp != mx) {
            cout << tmp << " " << mx << endl;
            tmp++;
            mx--;
        }
        tmp = M+1;
        mx = 2*M+1;
        while(tmp < mx) {
            cout << tmp << " " << mx << endl;
            tmp++;
            mx--;
        }
    }
    else {
        int tmp = 1;
        int mx = M+1;
        while(tmp != M-tmp+2) {
            cout << tmp << " " << mx << endl;
            tmp++;
            mx--;
        }
        tmp = M+2;
        mx = 2*M+1;
        while(tmp < mx) {
            cout << tmp << " " << mx << endl;
            tmp++;
            mx--;
        }
    }
}


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

int main(){
    int N, M;
    cin >> N >> M;
    int LL, LR, RL, RR;
    LL = 1;
    LR = N / 2;
    RL = LR + 1;
    RR = N;
    if(N % 2 == 0) LR--;
    int count = 0;
    while(true){
        cout << RL << " " << RR << endl;
        count++;
        if(count == M) break;
        RL++;
        RR--;
        cout << LL << " " << LR << endl;
        count++;
        if(count == M) break;
        LL++;
        LR++;
    }
}


#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho10
#define ll long long int
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define all(a) (a).begin(), (a).end()
#define sz size
#define f first
#define s second
#define pb push_back
#define er erase
#define in insert
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000000007
#define PI 3.14159265359
#define CODE_START  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,m,q,ans=0,a[55],b[55],c[55],d[55];
vector<ll>v;
int32_t main(){
CODE_START;
cin>>n>>m;
if(n%2==1){
    n--;
ll l=1,r=n;
for(ll i=0;i<m;i++)
{
    cout<<l<<' '<<r<<endl;
    l++;
    r--;
}
}else {
ll l=1,r=n;
for(ll i=0;i<m;i++)
{
    cout<<l<<' '<<r;
    l++;
    r=r-2;
}
}
}


#include<bits/stdc++.h>
#define ll long long
#define ALL(v) (v).begin(),(v).end()
#define REP(i,p,n) for(int i=p;i<(int)(n);++i)
#define rep(i,n) REP(i,0,n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list) cout << "{ "; for(auto nth : list){ cout << nth << " "; } cout << "}" << endl;
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; }

using namespace std;
typedef pair<ll, ll> pint;

const int MOD = 1000000007;
void yesno(bool b) {
    if (b) {
        cout << "Yes" << endl;
    }     else {
        cout << "No" << endl;
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    ll l = 1;
    ll r = N;
//    rep(i, M) {
//            cout << l << " " << r << endl;
//            l++;
//            r--;
//    }
    if (N % 2 == 0) {
        rep(i, M) {
            if (i != M- 1) {
                cout << l << " " << r << endl;
                l++;
                r--;
            } else {
                r--;
                cout << l << " " << r << endl;
            }
        }
    } else {
        rep(i, M) {
            cout << l << " " << r << endl;
            l++;
            r--;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define M1 					1000000007
#define M2 					998244353
#define ll 					int
#define pll 				pair<ll,ll>
#define mll 				map<ll,ll>
#define F 					first
#define S 					second
#define PB 					push_back
#define mp 					make_pair
#define lb 					lower_bound
#define ub 					upper_bound
#define V(a) 				vector<a>
#define endl 				'\n'
#define test(t) 			while(t--)
#define PI 					acos(-1.0)
#define rep(i,a,b) 			for(ll i=a;i<b;i++)
#define repp(i,b,a) 		for(ll i=b-1;i>=a;i--)
#define clr(ar, val)		memset(ar, val, sizeof(ar))
#define setbits(x) 			__builtin_popcountll(x)
#define zrobits(x) 			__builtin_ctzll(x)
#define ps(y) 				fixed << setprecision(y)
#define all(x) 				begin(x),end(x)
#define allr(x) 			rbegin(x),rend(x)
const int inf= 				0x3f3f3f3f;
// const ll INF= 				0x3f3f3f3f3f3f3f3f;
const int dx[4]= 			{ 0, -1, 0, 1 };
const int dy[4]= 			{ -1, 0, 1, 0 };

unsigned  int  randInt (){
    static  unsigned  int  tx  =  123456789 ,  ty = 362436069 ,  tz = 521288629 ,  tw = 88675123 ;
    unsigned  int  tt  =  ( tx ^ ( tx << 11 ));
    tx  =  ty ;  ty  =  tz ;  tz  =  tw ;
    return  (  tw = ( tw^ ( tw >> 19 )) ^ ( tt ^ ( tt >> 8 ))  );
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
#define o_set(T) tree<T, null_type,less<T>, rb_tree_tag,tree_order_statistics_node_update>
//member functions :
//1. order_of_key(k) : number of elements strictly lesser than k
//2. find_by_order(k) : k-th element in the set

ll floor_div(ll a, ll b) {
	return a / b - (((a ^ b) < 0) and a % b);
}
ll ceil_div(ll a, ll b){
    return a / b + (((a ^ b) >= 0) and a % b);
}

vector<int> suffix_array_cyclic(string s){
    int n = s.size(), N = n + 256;
    vector<int> sa(n), ra(n);
    for(int i = 0; i < n; i++) sa[i] = i, ra[i] = s[i];
    for(int k = 0; k < n; k ? k *= 2 : k++)
    {
        vector<int> nsa(sa), nra(n), cnt(N);
        for(int i = 0; i < n; i++) nsa[i] = (nsa[i] - k + n) % n;
        for(int i = 0; i < n; i++) cnt[ra[i]]++;
        for(int i = 1; i < N; i++) cnt[i] += cnt[i - 1];
        for(int i = n - 1; i >= 0; i--) sa[--cnt[ra[nsa[i]]]] = nsa[i];

        int r = 0;
        for(int i = 1; i < n; i++)
        {
            if(ra[sa[i]] != ra[sa[i - 1]]) r++;
            else if(ra[(sa[i] + k) % n] != ra[(sa[i - 1] + k) % n]) r++;
            nra[sa[i]] = r;
        }
        ra = nra;
    }
    return sa;
}

vector<int> kasai(string s, vector<int> sa)
{
    int n = s.size(), k = 0;
    vector<int> ra(n), lcp(n);
    for(int i = 0; i < n; i++) ra[sa[i]] = i;
    for(int i = 0; i < n; i++)
    {
        if(k) k--;
        if(ra[i] == n - 1) {k = 0; continue;}
        int j = sa[ra[i] + 1];
        while(k < n && s[(i + k) % n] == s[(j + k) % n]) k++;
        lcp[ra[i]] = k;
        if(ra[(sa[ra[i]] + 1) % n] > ra[(sa[ra[j]] + 1) % n]) k = 0;
    }
    return lcp;
}

inline void INP()
{
	#ifndef ONLINE_JUDGE
	    freopen("input.txt","r",stdin);
	    freopen("output.txt","w",stdout);
	#endif
}
V(ll) adj[200005];
ll n,k;
ll val[200005];
ll dp[200005];
o_set(int) v;

ll mytree[800005];

void build(ll ind, ll l, ll r){
    if (l == r){
        mytree[ind] = 0;
        return;
    }
    ll mid = (l+r)/2;
    build(2*ind,l,mid);
    build(2*ind+1,mid+1,r);
    mytree[ind] = max(mytree[2*ind],mytree[2*ind+1]);
}

void update(ll ind, ll l, ll r, ll t, ll val){
    if (t < l || t > r){
        return;
    }
    if (l == r){
        mytree[ind] = val;
        return;
    }
    ll mid = (l+r)/2;
    update(2*ind,l,mid,t,val);
    update(2*ind+1,mid+1,r,t,val);
    mytree[ind] = max(mytree[2*ind],mytree[2*ind+1]);

}

ll query(ll ind, ll l, ll r, ll qs, ll qe){
    if (qe < l || qs > r){
        return -1;
    }
    if (l >= qs && r <= qe){
        return mytree[ind];
    }
    ll mid = (l+r)/2;
    return max(query(2*ind,l,mid,qs,qe),query(2*ind+1,mid+1,r,qs,qe));
}
map<ll,bool> visited;
void dfs(ll node, ll depth){
    visited[node] = 1;
    ll qps = v.order_of_key(val[node]);
    v.insert(val[node]);
    ll qry = query(1,0,n-1,0,max(qps-1,0))+1;
    dp[node] =qry;
    update(1,0,n-1,depth,dp[node]);
    dp[node] = query(1,0,n-1,0,n-1);

    for(ll child:adj[node]){
        if (!visited[child]){
            dfs(child,depth+1);
        }
    }
    v.erase(val[node]);
    update(1,0,n-1,depth,0);
}

void solve()
{

	cin >> n;
    rep(i,1,n+1) cin >> val[i];
    rep(i,0,n-1){
        ll x,y;
        cin >> x >> y;
        adj[x].PB(y);
        adj[y].PB(x);
    }
    rep(i,0,200005) dp[i] = 1;
    build(1,0,n-1);
    dfs(1,0);
	rep(i,1,n+1) cout << dp[i] << endl;
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    // cin>>t;
    test(t){
        solve();
    }
    return 0;
}

Score : 600 points

Problem Statement

We have a tree with N vertices, whose i-th edge connects Vertex u_i and Vertex v_i.
Vertex i has an integer a_i written on it.
For every integer k from 1 through N, solve the following problem:

We will make a sequence by lining up the integers written on the vertices along the shortest path from Vertex 1 to Vertex k, in the order they appear. Find the length of the longest increasing subsequence of this sequence.

Here, the longest increasing subsequence of a sequence A of length L is the subsequence A_{i_1} , A_{i_2} , ... , A_{i_M} with the greatest possible value of M such that 1 \leq i_1 < i_2 < ... < i_M \leq L and A_{i_1} < A_{i_2} < ... < A_{i_M}.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq a_i \leq 10^9

1 \leq u_i , v_i \leq N

u_i \neq v_i

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N
u_1 v_1
u_2 v_2
:
u_{N-1} v_{N-1}

Output

Print N lines. The k-th line, print the length of the longest increasing subsequence of the sequence obtained from the shortest path from Vertex 1 to Vertex k.

Sample Input 1

10
1 2 5 3 4 6 7 3 2 4
1 2
2 3
3 4
4 5
3 6
6 7
1 8
8 9
9 10

Sample Output 1

1
2
3
3
4
4
5
2
2
3

For example, the sequence A obtained from the shortest path from Vertex 1 to Vertex 5 is 1,2,5,3,4. Its longest increasing subsequence is A_1, A_2, A_4, A_5, with the length of 4.


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

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

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

template<typename T> void _F(const char *name, T arg1){
    cerr << name << " = " << arg1 << endl;
}
template<typename T, typename... Args> void _F(const char *names, T arg1, Args... args){
    const char *name = strchr(names, ',');
    cerr.write(names, name-names) << " = " << arg1 << endl;
    _F(name+2, args...);
}
template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2> &q){
    in >> q.F >> q.S; return in;
}
template<typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2> &q){
    out << q.F << " " << q.S; return out;
}
template< typename T1, typename T2> pair<T1, T2> operator+(pair<T1, T2> p1, pair<T1, T2> p2){
    return {p1.F+p2.F, p1.S+p2.S};
}
template< typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> p1, pair<T1, T2> p2){
    return {p1.F-p2.F, p1.S-p2.S};
}

void solve(){
    int s, w;
    cin >> s >> w;
    if(w >= s){
        cout << "unsafe" << endl;
    }else{
        cout << "safe" << endl;
    }
}

signed main(){

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int t = 1;
    start = clock();

    cout << fixed << setprecision(12);

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

    //cin >> t;

    for(int i = 1; i <= t; ++i){
        //cout << "Case #" << i << ": ";
        solve();
       // cout << endl;
    }
    //cerr << time(start);
    return 0;
}

Score : 100 points

Problem Statement

There are S sheep and W wolves.

If the number of wolves is greater than or equal to that of sheep, the wolves will attack the sheep.

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Constraints

1 \leq S \leq 100

1 \leq W \leq 100

Input

Input is given from Standard Input in the following format:

S W

Output

If the wolves will attack the sheep, print unsafe; otherwise, print safe.

Sample Input 1

4 5

Sample Output 1

unsafe

There are four sheep and five wolves. The number of wolves is not less than that of sheep, so they will attack them.

Sample Input 2

100 2

Sample Output 2

safe

Many a sheep drive away two wolves.

Sample Input 3

10 10

Sample Output 3

unsafe


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

bool isKaibun(string s);

int main()
{
   int num;
   cin >> num;
   //景品の種類が、すでに持っている景品の種類と一致しているかみて、一致していなければインクリメントする

   string temp;
   int ans = 0;

   vector<string> list(num+1, "111");

   //くじ引き1つ1つについて確認
   for ( int i = 1; i <= num; i++)
   {
       cin >> temp;

       for(int k = 1; k <= num; k++)
       {
           if(list[k] == "111")
           {
               list[k] = temp;
               ans++;
               break;
           }

           if(list[k] == temp)
           {
               //重複している
               break;
           }
       }
   }

   cout << ans << endl;

}

Score : 300 points

Problem Statement

You drew lottery N times. In the i-th draw, you got an item of the kind represented by a string S_i.

How many kinds of items did you get?

Constraints

1 \leq N \leq 2\times 10^5

S_i consists of lowercase English letters and has a length between 1 and 10 (inclusive).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print the number of kinds of items you got.

Sample Input 1

3
apple
orange
apple

Sample Output 1

2

You got two kinds of items: apple and orange.

Sample Input 2

5
grape
grape
grape
grape
grape

Sample Output 2

1

Sample Input 3

4
aaaa
a
aaa
aa

Sample Output 3

4


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

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

	vector<string> S(N);
	for (int i = 0; i < N; i++) {
		cin >> S.at(i);
	}

	int cnt = 0;
	for (int i = 0; i < N; i++) {
		int flag = 0;
		for (int j = 0; j < i; j++) {
			if (i != j && S.at(i).at(0) == S.at(j).at(0)) {
				if (S.at(i) == S.at(j)){
					flag++;
					break;
				}
			}
		}
		if (flag == 0 || i == 0) cnt++;
		flag = 0;
	}

	cout << cnt << endl;
	return 0;
}


#pragma region
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <list>
#include <iomanip>
#include <cstdint>
#include <cstdlib>

using namespace std;
typedef long long ll;
//#define rep(i, s, e) for (int(i) = (s); (i) < (e); ++(i))
#define rep(i, e) for (int(i) = 0; (i) < (e); ++(i))
#define rrep(i, s) for (int(i) = (s) - 1; (i) >= 0; --(i))
#define all(x) x.begin(),x.end()
#pragma endregion

int main()
{
	int n; cin >> n;
	set<string> s;
	rep(i, n)
	{
		string t; cin >> t;
		s.insert(t);
	}
	cout << s.size() << endl;
}


#include<iostream>
using namespace std;
int main(){
  int n,m,a[10005],sum = 0;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>a[i];
    sum += a[i];
  }
  if(n>=sum){
    cout<<n-sum<<'\n';
  }
  else if(n<sum){
    cout<<-1<<'\n';
  }
  return 0;
}

Score : 200 points

Problem Statement

Takahashi has N days of summer vacation.

His teacher gave him M summer assignments. It will take A_i days for him to do the i-th assignment.

He cannot do multiple assignments on the same day, or hang out on a day he does an assignment.

What is the maximum number of days Takahashi can hang out during the vacation if he finishes all the assignments during this vacation?

If Takahashi cannot finish all the assignments during the vacation, print -1 instead.

Constraints

1 \leq N \leq 10^6

1 \leq M \leq 10^4

1 \leq A_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_M

Output

Print the maximum number of days Takahashi can hang out during the vacation, or -1.

Sample Input 1

41 2
5 6

Sample Output 1

30

For example, he can do the first assignment on the first 5 days, hang out on the next 30 days, and do the second assignment on the last 6 days of the vacation. In this way, he can safely spend 30 days hanging out.

Sample Input 2

10 2
5 6

Sample Output 2

-1

He cannot finish his assignments.

Sample Input 3

11 2
5 6

Sample Output 3

0

He can finish his assignments, but he will have no time to hang out.

Sample Input 4

314 15
9 26 5 35 8 9 79 3 23 8 46 2 6 43 3

Sample Output 4

9


#include <bits/stdc++.h>
using namespace std;
const double PI = 3.141592653589793238463;
int main()
{
    long long int n, sum = 0;
    int m;
    cin >> n >> m;
    int a[m];
    for (int i = 0; i < m; i++)
    {
        cin >> a[i];
        sum += a[i];
    }
    if (sum <= n)
    {
        cout << n - sum;
    }
    else
    {
        cout << "-1";
    }

    return 0;
}


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

int main()
{
    int n, m;
    cin >> n >> m;
    int sum = 0, a;
    for(int i = 0; i < m; i++){
        cin >> a;
        sum += a;
    }

    if(n - sum >= 0) cout << n - sum << endl;
    else cout << -1 << endl;

}


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

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define ALL(v) (v.begin(),v.end())
#define COUT(x) cout<<(x)<<'\n'

int main(){
    int n;
    cin >> n;
    int a[200010];
    int t;
    REP(i,n-1){
        cin >> t;
        a[t]++;
    }
    for(int i=1;i<n+1;i++){
        COUT(a[i]);
    }
    return 0;
}

Score : 300 points

Problem Statement

A company has N members, who are assigned ID numbers 1, ..., N.

Every member, except the member numbered 1, has exactly one immediate boss with a smaller ID number.

When a person X is the immediate boss of a person Y, the person Y is said to be an immediate subordinate of the person X.

You are given the information that the immediate boss of the member numbered i is the member numbered A_i. For each member, find how many immediate subordinates it has.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq A_i < i

Input

Input is given from Standard Input in the following format:

N
A_2 ... A_N

Output

For each of the members numbered 1, 2, ..., N, print the number of immediate subordinates it has, in its own line.

Sample Input 1

5
1 1 2 2

Sample Output 1

2
2
0
0
0

The member numbered 1 has two immediate subordinates: the members numbered 2 and 3.

The member numbered 2 has two immediate subordinates: the members numbered 4 and 5.

The members numbered 3, 4, and 5 do not have immediate subordinates.

Sample Input 2

10
1 1 1 1 1 1 1 1 1

Sample Output 2

9
0
0
0
0
0
0
0
0
0

Sample Input 3

7
1 2 3 4 5 6

Sample Output 3

1
1
1
1
1
1
0


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
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;
const int mod=1e9+7;
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;cin >> n;
    vector<int> c(n);
    for(int i=0;i<n-1;i++){
        int a;cin >> a;
        a--;
        c[a]++;
    }
    for(int i=0;i<n;i++){
        cout << c[i] << endl;
    }
}


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

const ll maxs = 1e5+5;
const ll lmaxs = 20;
ll mod = 1e9+7;
ll oo = 1e15;

#define IOS ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define I insert
#define endl '\n'

void pre(){

}

ll get(ll x){
    return x*(x-1)/2;
}

void solve(){
   ll n , k;
   cin >> n >> k;
   ll ans=0;

   for(ll i=k;i<=n+1;i++){
       ll MIN = get(i);
       ll MAX = n*i - get(i);
       ans += MAX - MIN + 1;
       ans %= mod;
   }

   cout << ans << endl;
}

int main(){
    // #ifndef ONLINE_JUDGE
    // freopen("input00.txt","r",stdin);
    // #endif
    IOS;
    pre();
    ll T = 1;
    //cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define forr(i,a,n)	for(long long int i=a; i<n; i++)
#define loop(i,a,n)	for(long long int i=a; i>=n; i--)
const long long int m=1e9+7;
int main()
{
	ios_base :: sync_with_stdio(false),cin.tie(NULL),cout.tie(0);

	long long int a,b,ct=0;
	cin>>a>>b;
	forr(i,b,a+2)
	{
		long long int x=(i*(i-1)/2);
		long long int y=(a*(a+1)/2)-((a-i)*(a-i+1)/2);
		ct+=(y-x+1);
		ct%=m;
	}
	cout<<ct;

	return 0;
}


#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <iterator>
#include <numeric>
#include <cstdint>
#include <cmath>
#include <bitset>
#include <string>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>

#define N_TIMES(i, n)     for ( uint64_t i = 0;     i <  n; ++i )
#define N_TIMES_REV(i, n) for (  int64_t i = n - 1; i >= 0; --i )

using namespace std;

struct VectorItem
{
  int64_t key, val;
  VectorItem(int64_t key = 0, int64_t val = 0) : key(key), val(val) {}
  bool operator>(const VectorItem &item) const {
    return val > item.val;
  }
};

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

  vector<VectorItem> A(N);
  N_TIMES(n, N) {
    int64_t a;
    cin >> a;

    A[n] = VectorItem(n, a);
  }
  sort(A.begin(), A.end(), greater<VectorItem>());

  vector<vector<int64_t>> dp(N + 1, vector<int64_t>(N + 1, 0));
  for (int64_t n = 0; n < N; ++n) {
    for (int64_t x = 0; x <= n; ++x) {
      int64_t y = n - x;
      dp[x + 1][y] = max(dp[x + 1][y], dp[x][y] + A[n].val * (((N - 1) - x) - A[n].key));
      dp[x][y + 1] = max(dp[x][y + 1], dp[x][y] + A[n].val * (A[n].key - y));
    }
  }

  int64_t ans = 0;
  for (int64_t x = 0; x <= N; ++x) {
    int64_t y = N - x;
    ans = max(ans, dp[x][y]);
  }
  cout << ans << endl;

  return 0;
}

Score : 500 points

Problem Statement

There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.

You can rearrange these children just one time in any order you like.

When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.

Find the maximum total happiness points the children can earn.

Constraints

2 \leq N \leq 2000

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum total happiness points the children can earn.

Sample Input 1

4
1 3 4 2

Sample Output 1

20

If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.

Sample Input 2

6
5 5 6 1 1 1

Sample Output 2

58

Sample Input 3

6
8 6 9 1 2 1

Sample Output 3

85


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector< vector<int> > VVI;
typedef vector< vector<ll> > VVL;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

int main(){
  int N;
  cin>>N;
  VI X(N);
  rep(i,N) if((i+1)%3 and (i+1)%5) X[i]=i+1;
  ll ans=0;
  rep(i,N) ans+=X[i];
  cout<<ans<<endl;
}

Score : 200 points

Problem Statement

Let us define the FizzBuzz sequence a_1,a_2,... as follows:

If both 3 and 5 divides i, a_i=\mbox{FizzBuzz}.

If the above does not hold but 3 divides i, a_i=\mbox{Fizz}.

If none of the above holds but 5 divides i, a_i=\mbox{Buzz}.

If none of the above holds, a_i=i.

Find the sum of all numbers among the first N terms of the FizzBuzz sequence.

Constraints

1 \leq N \leq 10^6

Input

Input is given from Standard Input in the following format:

N

Output

Print the sum of all numbers among the first N terms of the FizzBuzz sequence.

Sample Input 1

15

Sample Output 1

60

The first 15 terms of the FizzBuzz sequence are:

1,2,\mbox{Fizz},4,\mbox{Buzz},\mbox{Fizz},7,8,\mbox{Fizz},\mbox{Buzz},11,\mbox{Fizz},13,14,\mbox{FizzBuzz}

Among them, numbers are 1,2,4,7,8,11,13,14, and the sum of them is 60.

Sample Input 2

1000000

Sample Output 2

266666333332

Watch out for overflow.


#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
int main()
{
    lint num;
    lint sum = 0;
    cin >> num;
    for (lint i = 1; i < num;i++)
    {
        if (i%3 != 0 && i%5 !=0)
            sum = sum + i;
    }
    cout << sum << endl;
    return 0;
}


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

typedef long long ll;
#define vec vector<int>
#define vecll vector<ll>
#define vpair vector<pair<int, int>>
#define vpairll vector<pair<ll, ll>>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define rep(i,n)   for(int i=(int)0; i<(int)n; i++)
#define llrep(i,n)   for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n) for(int i=(int)m; i<(int)n; i++)
#define all(var)     (var).begin(), (var).end()
#define prt(var)     cout << var << endl
#define prt_dbl(i,var) cout<<fixed<<setprecision(i)<<var<<endl;
template <typename Val>
Val gcd(Val a, Val b) {
//use ll or int for gcd
    if (b==0) return a;
    else return gcd(b, a%b);
}
template <typename Val>
Val vec_max(vector<Val> v){
  return *max_element(v.begin(),v.end());
}
template <typename Val>
Val vec_min(vector<Val> v){
  return *min_element(v.begin(),v.end());
}

//---------------------------------------------------------------

int main(){
  ll N;
  cin>>N;
  ll count = 1;
  ll c = 0;
  while(1){
    if(count%3==0||count%5==0){
      c+=0;

    }else {
      c+=count;
    }
    if(count==N){
      break;
    }
      count++;
  }

  cout<<c<<endl;

}


#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
const lint MOD = 1e9+7;

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

signed main(){
	lint N, K; cin >> N >> K;
	lint ans = 0;
	vector<lint> data(K + 1, 0);
	for(lint i = K; i > 0; i--){
		lint num = modpow(K/i, N);
		for(lint j = 2 * i; j <= K; j += i){
			num = num - data[j] + MOD;
			num %= MOD;
		}
		data[i] = num;
		ans += num * i;
		ans %= MOD;
	}
	cout << ans << endl;
}

Score : 500 points

Problem Statement

Consider sequences \{A_1,...,A_N\} of length N consisting of integers between 1 and K (inclusive).

There are K^N such sequences. Find the sum of \gcd(A_1, ..., A_N) over all of them.

Since this sum can be enormous, print the value modulo (10^9+7).

Here \gcd(A_1, ..., A_N) denotes the greatest common divisor of A_1, ..., A_N.

Constraints

2 \leq N \leq 10^5

1 \leq K \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the sum of \gcd(A_1, ..., A_N) over all K^N sequences, modulo (10^9+7).

Sample Input 1

3 2

Sample Output 1

9

\gcd(1,1,1)+\gcd(1,1,2)+\gcd(1,2,1)+\gcd(1,2,2)
+\gcd(2,1,1)+\gcd(2,1,2)+\gcd(2,2,1)+\gcd(2,2,2)
=1+1+1+1+1+1+1+2=9

Thus, the answer is 9.

Sample Input 2

3 200

Sample Output 2

10813692

Sample Input 3

100000 100000

Sample Output 3

742202979

Be sure to print the sum modulo (10^9+7).