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

#define FSCNd64 "%" SCNd64
#define FPRId64 "%" PRId64

using namespace std;

using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vpii=vector<pii>;

#define PI 3.1415926535897932384626433832795

template<typename X>
bool max_u(X&m, X v)
{
	if(m<v)
	{
		m=v;
		return true;
	}
	return false;
}

template<typename X>
bool min_u(X&m, X v)
{
	if(m>v)
	{
		m=v;
		return true;
	}
	return false;
}

using ll=long long;

class mod_ll
{
public:
	ll m_v;
	static const ll m_m=1000000007; // prime value ex. 1000000009
	mod_ll(ll v=0) : m_v(v%m_m) {}
	mod_ll operator = (ll rhs)
	{
		m_v=rhs%m_m;
		return *this;
	}
	ll operator ()(){return m_v;}
	mod_ll operator = (mod_ll rhs)
	{
		m_v=rhs.m_v;
		return *this;
	}
	mod_ll operator += (mod_ll rhs)
	{
		m_v=(m_v+rhs.m_v)%m_m;
		return *this;
	}
	mod_ll operator + (mod_ll rhs)
	{
		auto ret=*this;
		return ret+=rhs;
	}
	mod_ll operator -= (mod_ll rhs)
	{
		m_v=(m_v-rhs.m_v+m_m)%m_m;
		return *this;
	}
	mod_ll operator - (mod_ll rhs)
	{
		auto ret=*this;
		return ret-=rhs;
	}
	mod_ll operator *= (mod_ll rhs)
	{
		m_v=(m_v*rhs.m_v)%m_m;
		return *this;
	}
	mod_ll operator * (mod_ll rhs)
	{
		mod_ll ret=*this;
		return ret*=rhs;
	}
	mod_ll operator ^ (ll rhs)
	{
		mod_ll t=*this;
		mod_ll ret(1LL);

		for(;rhs>0;rhs>>=1)
		{
			if(rhs&1) ret*=t;
			t*=t;
		}
		return ret;
	}
	mod_ll operator /= (mod_ll rhs)
	{
		return *this*=(rhs^(m_m-2));
	}
	mod_ll operator / (mod_ll rhs)
	{
		mod_ll ret=*this;
		return ret/=rhs;
	}
};

mod_ll mod_fact(int n)
{
	static vector<mod_ll> memo(1);

	if((int)memo.size()<=n)
	{
		if(memo.size()==1)
		{
			memo[0]=1;
		}
		for(int i=memo.size();i<=n;i++)
		{
			memo.push_back(memo.back()*i);
		}
	}
	return memo[n];
}

mod_ll mod_nCr(int n, int r)
{
	if(n<=0) return 1;
	mod_ll bunsi=mod_fact(n);
	mod_ll bunbo=mod_fact(n-r)*mod_fact(r);
	return bunsi/bunbo;
}

mod_ll mod_nCr2(int n, int r)
{
	mod_ll bunsi(1);
	mod_ll bunbo(1);
	if(r*2>n) r=n-r;

	for(int i=0;i<r;i++)
	{
		bunsi*=(n-i);
		bunbo*=(i+1);
	}

	return bunsi/bunbo;
}

mod_ll mod_nPr(int n, int r)
{
	mod_ll ret(1);

	for(int i=0;i<r;i++)
	{
		ret*=(n-i);
	}
	return ret;
}

struct solve
{
	solve(){}
	ll operator()(vi&a, int k) {
		vi ap, an;
		int n=a.size();
		mod_ll ret(1);

		for(auto&m:a)
		{
			if(m>=0) ap.push_back(m);
			else an.push_back(-m);
		}
		sort(ap.rbegin(), ap.rend());
		sort(an.rbegin(), an.rend());

		if(ap.size()==0)
		{
			if(k&1)
			{
				for(int i=0;i<k;i++) ret*=an[n-1-i];
				ret=(ret.m_m-ret.m_v);
			}
			else
			{
				for(int i=0;i<k;i++) ret*=an[i];
			}
			return ret();
		}

		if(n-k==0)
		{
			for(int i=0;i<(int)ap.size();i++) ret*=ap[i];
			for(int i=0;i<(int)an.size();i++) ret*=an[i];
			if(an.size()&1) ret=(ret.m_m-ret.m_v);
			return ret();
		}

		vll an2;
		for(int i=0;i+1<(int)an.size();i+=2)
		{
			an2.push_back((ll)an[i]*an[i+1]);
		}

		int ip=0, in=0;
		for(;k>0;)
		{
			if(ip+1>=(int)ap.size()) break;
			if(in>=(int)an2.size()) break;
			if((ll)ap[ip]*ap[ip+1]>an2[in])
			{
				ret*=ap[ip++];
				k--;
			}
			else
			{
				ret*=an2[in++];
				k-=2;
			}
		}
		if(k&1)
		{
			ret*=ap[ip++];
			k--;
		}
		if(in>=(int)an2.size())
		{
			for(;k>0;k--)
			{
				ret*=ap[ip++];
			}
		}
		else
		{
			for(;k>0;k-=2)
			{
				ret*=an2[in++];
			}
		}

		return ret();
	}
};

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

	for(;;)
	{
		int n, k;
		cin >> n >> k;
		if(cin.fail()) break;
		vi a(n);
		for(auto&m:a) cin >> m;

		cout << solve()(a, k) << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll,ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll,ll>>>
#define PL pair<ll,ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main(){
    const ll mod = 1e9 + 7;
    ll n, k;
    cin >> n >> k;
    ll ek = (k / 2) * 2;
    VL a(n), neg, pos;
    ll zero = 0;
    rep(i, 0, n){
        cin >> a[i];
        if(a[i] == 0) zero++;
        else if(a[i] > 0) pos.push_back(a[i]);
        else neg.push_back(a[i]);
    }
    sort(ALL(pos), greater<ll>());
    sort(ALL(neg));
    priority_queue<ll> que;
    ll pn = pos.size(), nn = neg.size();

    if(k == n){
        ll ans = 1;
        rep(i, 0, k){
            ans *= a[i];
            ans %= mod;
        }
        if(ans < 0) ans += mod;
        cout << ans << endl;
        return 0;
    }

    if(n == nn && k % 2 == 1){
        ll ans = 1;
        sort(ALL(a), greater<ll>());
        rep(i, 0, k){
            // cerr << i << ": " << a[i] << endl;
            ans *= a[i];
            ans %= mod;
        }
        cerr << "flr: " << ans << endl;
        if(ans < 0) ans += mod;
        cout << ans << endl;
        return 0;
    }

    for(ll i = 0; 2*i + 1 < pn; i++){
        que.push((pos[2*i] * pos[2*i+1]) % mod);
    }
    for(ll i = 0; 2*i + 1 < nn; i++){
        que.push((neg[2*i] * neg[2*i+1]) % mod);
    }
    ll ans = 1;
    ll cnt = 0;
    while(cnt < ek && !que.empty()){
        auto q = que.top();
        ans *= q;
        ans %= mod;
        // cerr << q << endl;
        que.pop();
        cnt += 2;
    }
    ll rem = k - cnt;
    if(rem == 0){
        // cerr << "pek: " << cnt << endl;
        cout << ans << endl;
        return 0;
    }
    if(pn % 2 != 0){
        ans *= pos[pn-1];
        ans %= mod;
        rem--;
    }
    if(rem == 0){
        cout << ans << endl;
        return 0;
    }
    if(zero > 0){
        cout << 0 << endl;
        return 0;
    }
    ans *= neg[nn-1];
    ans %= mod;
    if(ans < 0) ans += mod;
    cout << ans << endl;
    return 0;
}

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

Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

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

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

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

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.


#include <iostream>
using namespace std;

int main(void){
    // Your code here!
    int D;
    cin >> D;

    int c[27];
    int i=0;
    int j=0;
    for (i = 0; i<26; i++){
        cin >> c[i];
    }

    int s[D][26];
    for (i = 0; i<D;i++){
        for (j=0; j<26; j++){
            cin >> s[i][j] ;
        }
    }

    int t[D];
    for (i = 0; i<D; i++){
        cin >> t[i];
        t[i]--;
    }

    int day=0;
    int last[365][26] = {};
    long long int sat = 0;

    day = 0;
    last[day][t[day]] = day + 1;
    sat += s[day][t[day]];
    for (i= 0; i < 26; i++){
        sat -= c[i] * (day + 1 - last[day][i]);
    }
    cout << sat << endl;

    for (day = 1; day<D; day++){
        for (i = 0; i<26; i++){
            last[day][i] = last[day-1][i];
        }
        last[day][t[day]] = day + 1;
        sat += s[day][t[day]];

        for (i= 0; i < 26; i++){
            sat -= c[i] * (day + 1 - last[day][i]);
        }

        cout << sat << endl;
    }

}

(Please read problem A first. The maximum score you can get by solving this problem B is 1, which will have almost no effect on your ranking.)

Beginner's Guide

Let's first write a program to calculate the score from a pair of input and output. You can know the total score by submitting your solution, or an official program to calculate a score is often provided for local evaluation as in this contest. Nevertheless, writing a score calculator by yourself is still useful to check your understanding of the problem specification. Moreover, the source code of the score calculator can often be reused for solving the problem or debugging your solution. So it is worthwhile to write a score calculator unless it is very complicated.

Problem Statement

You will be given a contest schedule for D days.
For each d=1,2,\ldots,D, calculate the satisfaction at the end of day d.

Input

Input is given from Standard Input in the form of the input of Problem A followed by the output of Problem A.

D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D

The constraints and generation methods for the input part are the same as those for Problem A.

For each d, t_d is an integer satisfying 1\leq t_d \leq 26, and your program is expected to work correctly for any value that meets the constraints.

Output

Let v_d be the satisfaction at the end of day d.
Print D integers v_d to Standard Output in the following format:

v_1
v_2
\vdots
v_D

Sample Input 1

5
86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82
19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424
6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570
6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256
8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452
19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192
1
17
13
14
13

Sample Output 1

18398
35037
51140
65837
79325

Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case.

Next Step

We can build a solution (schedule) for this problem in the order of day 1, day 2, and so on. And for every partial solution we have built, we can calculate the goodness (satisfaction) by using the above score calculator. So we can construct the following algorithm: for each d=1,2,\ldots,D, we select the contest type that maximizes the satisfaction at the end of day d. You may have already encountered this kind of "greedy algorithms" in algorithm contests such as ABC. Greedy algorithms can guarantee the optimality for several problems, but unfortunately, it doesn't ensure optimality for this problem. However, even if it does not ensure optimality, we can still obtain a reasonable solution in many cases. Let's go back to Problem A and implement the greedy algorithm by utilizing the score calculator you just implemented!

Greedy methods can be applied to a variety of problems, are easy to implement, and often run relatively fast compared to other methods. Greedy is often the most powerful method when we need to process huge inputs.
We can further improve the score by changing the greedy selection criteria (evaluation function), keeping multiple candidates instead of focusing on one best partial solution (beam search), or using the output of greedy algorithms as an initial solution of other methods.
For more information, please refer to the editorial that will be published after the contest.


#include <stdint.h>
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-12;
static const double PI = acos(-1.0);
static const ll MOD = 1000000007;

#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(a) (a).begin(), (a).end()
#define DEBUG(x) cout << #x << ": " << x << endl

ll calc_score(long long D, const std::vector<long long> &c, const std::vector<std::vector<long long>> &s,
              const std::vector<long long> &t, vector<set<ll>> &last) {
  ll score = 0;
  REP(d, D) {
    ll i = t[d] - 1;
    score += s[d][i];
    last[i].insert(d + 1);
    REP(j, 26) {
      score -= c[j] * (d + 1 - *last[j].rbegin());
    }
  }
  return score;
}

ll area(const set<ll> &last, ll d) {
  auto it = last.lower_bound(d);
  auto x = *it - d;
  it--;
  auto y = d - *it;
  return x * y;
}

ll calc_diff(const std::vector<long long> &c, const std::vector<std::vector<long long>> &s, ll d, vector<set<ll>> &last,
             ll pre, ll next) {
  ll diff = s[d - 1][next] - s[d - 1][pre];
  {
    auto it = last[next].insert(d).first;
    it++;
    auto n = *it;
    it--;
    it--;
    auto p = *it;
    ll x = n - d;
    ll y = d - p;
    diff += c[next] * x * y;
  }
  {
    last[pre].erase(d);
    diff -= c[pre] * area(last[pre], d);
  }
  return diff;
}

void solve(long long D, std::vector<long long> c, std::vector<std::vector<long long>> s, std::vector<long long> t,
           long long M, std::vector<long long> d, std::vector<long long> q) {
  vector<set<ll>> last(26);
  REP(j, 26) {
    last[j].insert(0);
  }
  ll score = calc_score(D, c, s, t, last);
  REP(j, 26) {
    last[j].insert(D + 1);
  }
  REP(i, M) {
    ll pre = t[d[i] - 1] - 1;
    ll next = q[i] - 1;
    t[d[i] - 1] = next + 1;

    score += calc_diff(c, s, d[i], last, pre, next);
    cout << score << endl;
  }
}

int main() {
  long long D;
  scanf("%lld", &D);
  std::vector<long long> c(26);
  for (int i = 0; i < 26; i++) {
    scanf("%lld", &c[i]);
  }
  std::vector<std::vector<long long>> s(D, std::vector<long long>(26));
  for (int i = 0; i < D; i++) {
    for (int j = 0; j < 26; j++) {
      scanf("%lld", &s[i][j]);
    }
  }
  std::vector<long long> t(D);
  for (int i = 0; i < D; i++) {
    scanf("%lld", &t[i]);
  }
  long long M;
  scanf("%lld", &M);
  std::vector<long long> d(M);
  std::vector<long long> q(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &d[i]);
    scanf("%lld", &q[i]);
  }
  solve(D, std::move(c), std::move(s), std::move(t), M, std::move(d), std::move(q));
  return 0;
}

(Please read problem A first. The maximum score you can get by solving this problem C is 1, which will have almost no effect on your ranking.)

Beginner's Guide

"Local search" is a powerful method for finding a high-quality solution.
In this method, instead of constructing a solution from scratch, we try to find a better solution by slightly modifying the already found solution.
If the solution gets better, update it, and if it gets worse, restore it.
By repeating this process, the quality of the solution is gradually improved over time.
The pseudo-code is as follows.

solution = compute an initial solution (by random generation, or by applying other methods such as greedy)
while the remaining time > 0:
slightly modify the solution (randomly)
if the solution gets worse:
restore the solution

For example, in this problem, we can use the following modification: pick the date d and contest type q at random and change the type of contest to be held on day d to q.
The pseudo-code is as follows.

t[1..D] = compute an initial solution (by random generation, or by applying other methods such as greedy)
while the remaining time > 0:
pick d and q at random
old = t[d] # Remember the original value so that we can restore it later
t[d] = q
if the solution gets worse:
t[d] = old

The most important thing when using the local search method is the design of how to modify solutions.

If the amount of modification is too small, we will soon fall into a dead-end (local optimum) and, conversely, if the amount of modification is too large, the probability of finding an improving move becomes extremely small.

In order to increase the number of iterations, it is desirable to be able to quickly calculate the score after applying a modification.

In this problem C, we focus on the second point.
The score after the modification can, of course, be obtained by calculating the score from scratch.
However, by focusing on only the parts that have been modified, it may be possible to quickly compute the difference between the scores before and after the modification.
From another viewpoint, the impossibility of such a fast incremental calculation implies that a small modification to the solution affects a majority of the score calculation.
In such a case, we may need to redesign how to modify solutions, or there is a high possibility that the problem is not suitable for local search.
Let's implement fast incremental score computation.
It's time to demonstrate the skills of algorithms and data structures you have developed in ABC and ARC!

In this kind of contest, where the objective is to find a better solution instead of the optimal one, a bug in a program does not result in a wrong answer, which may delay the discovery of the bug.
For early detection of bugs, it is a good idea to unit test functions you implemented complicated routines.
For example, if you implement fast incremental score calculation, it is a good idea to test that the scores computed by the fast implementation match the scores computed from scratch, as we will do in this problem C.

Problem Statement

You will be given a contest schedule for D days and M queries of schedule modification.
In the i-th query, given integers d_i and q_i, change the type of contest to be held on day d_i to q_i, and then output the final satisfaction at the end of day D on the updated schedule.
Note that we do not revert each query. That is, the i-th query is applied to the new schedule obtained by the (i-1)-th query.

Input

Input is given from Standard Input in the form of the input of Problem A followed by the output of Problem A and the queries.

D
c_1 c_2 \cdots c_{26}
s_{1,1} s_{1,2} \cdots s_{1,26}
\vdots
s_{D,1} s_{D,2} \cdots s_{D,26}
t_1
t_2
\vdots
t_D
M
d_1 q_1
d_2 q_2
\vdots
d_M q_M

The constraints and generation methods for the input part are the same as those for Problem A.

For each d=1,\ldots,D, t_d is an integer generated independently and uniformly at random from {1,2,\ldots,26}.

The number of queries M is an integer satisfying 1\leq M\leq 10^5.

For each i=1,\ldots,M, d_i is an integer generated independently and uniformly at random from {1,2,\ldots,D}.

For each i=1,\ldots,26, q_i is an integer satisfying 1\leq q_i\leq 26 generated uniformly at random from the 25 values that differ from the type of contest on day d_i.

Output

Let v_i be the final satisfaction at the end of day D on the schedule after applying the i-th query.
Print M integers v_i to Standard Output in the following format:

v_1
v_2
\vdots
v_M

Sample Input 1

5
86 90 69 51 2 96 71 47 88 34 45 46 89 34 31 38 97 84 41 80 14 4 50 83 7 82
19771 12979 18912 10432 10544 12928 13403 3047 10527 9740 8100 92 2856 14730 1396 15905 6534 4650 11469 3628 8433 2994 10899 16396 18355 11424
6674 17707 13855 16407 12232 2886 11908 1705 5000 1537 10440 10711 4917 10770 17272 15364 19277 18094 3929 3705 7169 6159 18683 15410 9092 4570
6878 4239 19925 1799 375 9563 3445 5658 19857 11401 6997 6498 19933 3848 2426 2146 19745 16880 17773 18359 3921 14172 16730 11157 5439 256
8633 15862 15303 10749 18499 7792 10317 5901 9395 11433 3514 3959 5202 19850 19469 9790 5653 784 18500 10552 17975 16615 7852 197 8471 7452
19855 17918 7990 10572 4333 438 9140 9104 12622 4985 12319 4028 19922 12132 16259 17476 2976 547 19195 19830 16285 4806 4471 9457 2864 2192
1
17
13
14
13
5
1 7
4 11
3 4
5 24
4 19

Sample Output 1

72882
56634
38425
27930
42884

Note that this example is a small one for checking the problem specification. It does not satisfy the constraint D=365 and is never actually given as a test case.

Next Step

Let's go back to Problem A and implement the local search algorithm by utilizing the incremental score calculator you just implemented!
For this problem, the current modification "pick the date d and contest type q at random and change the type of contest to be held on day d to q" is actually not so good. By considering why it is not good, let's improve the modification operation.
One of the most powerful and widely used variant of the local search method is "Simulated Annealing (SA)", which makes it easier to reach a better solution by stochastically accepting worsening moves.
For more information about SA and other local search techniques, please refer to the editorial that will be published after the contest.


/////bsjsns
#include<stdio.h>
int main()
{
    long long a,b;
    scanf("%lld",&a);
    b=a+(a*a)+(a*a*a);
    printf("%lld",b);

    return 0;
}

Score : 100 points

Problem Statement

Given an integer a as input, print the value a + a^2 + a^3.

Constraints

1 \leq a \leq 10

a is an integer.

Input

Input is given from Standard Input in the following format:

a

Output

Print the value a + a^2 + a^3 as an integer.

Sample Input 1

2

Sample Output 1

14

When a = 2, we have a + a^2 + a^3 = 2 + 2^2 + 2^3 = 2 + 4 + 8 = 14.

Print the answer as an input. Outputs such as 14.0 will be judged as incorrect.

Sample Input 2

10

Sample Output 2

1110


#include <bits/stdc++.h>
#define ll long long
#define all(x) begin(x), end(x)
#define range(x) begin(x), begin(x)
#define what(x) cout << #x << ": " << x << '\n'
#define print(x) for (auto &el: x) cout << el << ' '; cout << '\n'
using namespace std;

template<typename S, typename T>
ostream& operator <<(ostream &os, pair<S, T> &m) {
    os << '[' << m.first << " " << m.second << ']';
    return os;
}

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

    string a, b;
    cin >> a >> b;

    int n = a.size();
    int cnt = 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] != b[i]) cnt++;
    }
    cout << cnt << endl;

    return 0;
}

Score : 200 points

Problem Statement

Given are strings S and T. Consider changing S to T by repeating the operation below. Find the minimum number of operations required to do so.

Operation: Choose one character of S and replace it with a different character.

Constraints

S and T have lengths between 1 and 2\times 10^5 (inclusive).

S and T consists of lowercase English letters.

S and T have equal lengths.

Input

Input is given from Standard Input in the following format:

S
T

Output

Print the answer.

Sample Input 1

cupofcoffee
cupofhottea

Sample Output 1

4

We can achieve the objective in four operations, such as the following:

First, replace the sixth character c with h.

Second, replace the eighth character f with t.

Third, replace the ninth character f with t.

Fourth, replace the eleventh character e with a.

Sample Input 2

abcde
bcdea

Sample Output 2

5

Sample Input 3

apple
apple

Sample Output 3

0

No operations may be needed to achieve the objective.


/*The woods are lovely, dark and deep,
But I have promises to keep,
And miles to go before I sleep,
And miles to go before I sleep.*/

//PRABHJOT SINGH A.K.A. PRABHI
//~~~~~conquizztador~~~~~

#include<bits/stdc++.h>
using namespace std;
using lli = long long int;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
const int MOD = 1000000007;
const int MOD1 = 998244353;
const int maxn = 100010;
const int lim = (int)1e9;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int ct=0;string s,t;
	cin>>s;
	cin>>t;
	for(int i=0;i<s.length();++i)
		if(s[i]!=t[i])
			++ct;
	cout<<ct<<endl;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int dig=52;
inline ll read(){
    ll x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9'){
        if(c=='-')f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9'){
        x=(x<<3)+(x<<1)+c-'0';
        c=getchar();
    }
    return x*f;
}
int n;
ll x,a1,a2;
int main(){
	n=read();
	a1=read(),a2=read();
	for(int i=3;i<=n;++i){
		x^=read();
	}
	ll tmp=a1+a2-x;
	if(tmp&1){
		printf("-1\n");
		return 0;
	}
	tmp>>=1;
	if(a1<=tmp){
		printf("-1\n");
		return 0;
	}
	for(int i=dig;i>=0;--i){
		if((x>>i)&1&&(tmp>>i)&1){
			printf("-1\n");
			return 0;
		}
	}
	ll ans=a1-tmp;
	for(int i=dig;i>=0;--i){
		if((x>>i)&1&&ans>=(1LL<<i)){
			ans-=1LL<<i;
		}
	}
	cout<<(ans==a1?-1:ans)<<endl;
	return 0;
}

Score : 600 points

Problem Statement

There are N piles of stones. The i-th pile has A_i stones.

Aoki and Takahashi are about to use them to play the following game:

Starting with Aoki, the two players alternately do the following operation:

Operation: Choose one pile of stones, and remove one or more stones from it.

When a player is unable to do the operation, he loses, and the other player wins.

When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.

In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1-st pile to the 2-nd pile before the game begins.

If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.

Constraints

2 \leq N \leq 300

1 \leq A_i \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N
A_1 \ldots A_N

Output

Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.

Sample Input 1

2
5 3

Sample Output 1

1

Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.

If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.

Sample Input 2

2
3 5

Sample Output 2

-1

It is not allowed to move stones from the 2-nd pile to the 1-st.

Sample Input 3

3
1 1 2

Sample Output 3

-1

It is not allowed to move all stones from the 1-st pile.

Sample Input 4

8
10 9 8 7 6 5 4 3

Sample Output 4

3

Sample Input 5

3
4294967297 8589934593 12884901890

Sample Output 5

1

Watch out for overflows.


#include <bits/stdc++.h>

using namespace std;
int main()
{
    char c;
    cin >> c;
    if(97 <= c && c <= 122) cout << "a" << endl;
    else if(65 <= c && c <= 90) cout << "A" << endl;
    return 0;
}

Score : 100 points

Problem Statement

An uppercase or lowercase English letter \alpha will be given as input.
If \alpha is uppercase, print A; if it is lowercase, print a.

Constraints

\alpha is an uppercase (A - Z) or lowercase (a - z) English letter.

Input

Input is given from Standard Input in the following format:

α

Output

If \alpha is uppercase, print A; if it is lowercase, print a.

Sample Input 1

B

Sample Output 1

A

B is uppercase, so we should print A.

Sample Input 2

a

Sample Output 2

a

a is lowercase, so we should print a.


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define MOD  1000000007
#define ll long long
#define int long long
# define INF 1000005
#define ar array
# define fr first
# define sc second
#define wt(x) int x;cin>>x;while(x--)
#define watch(x) cerr << "\n" << (#x) << " is " << (x) << endl
//#define cerr if(false)cerr
typedef tree<pair<int,int>,null_type,less<pair<int,int>>,rb_tree_tag,tree_order_statistics_node_update> pbds;

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

#ifndef ONLINE_JUDGE
  freopen("input1.txt","r",stdin);
  freopen("output1.txt","w",stdout);
#endif
 }

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

int  MMI(int p){
    return power(p,MOD-2,MOD);
}

int32_t main()
{
    c_p_c();

       int n,k;
       cin>>n>>k;
       int s=0;
       vector<int>a(n);
       for(int i=0;i<n;i++)cin>>a[i];
       nth_element(a.begin(),a.begin()+k,a.end());
       for(int i=0;i<k;i++){
        s+=a[i];
       }
       cout<<s;

      return 0;
       }

Score : 200 points

Problem Statement

A shop sells N kinds of fruits, Fruit 1, \ldots, N, at prices of p_1, \ldots, p_N yen per item, respectively. (Yen is the currency of Japan.)

Here, we will choose K kinds of fruits and buy one of each chosen kind. Find the minimum possible total price of those fruits.

Constraints

1 \leq K \leq N \leq 1000

1 \leq p_i \leq 1000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
p_1 p_2 \ldots p_N

Output

Print an integer representing the minimum possible total price of fruits.

Sample Input 1

5 3
50 100 80 120 80

Sample Output 1

210

This shop sells Fruit 1, 2, 3, 4, and 5 for 50 yen, 100 yen, 80 yen, 120 yen, and 80 yen, respectively.

The minimum total price for three kinds of fruits is 50 + 80 + 80 = 210 yen when choosing Fruit 1, 3, and 5.

Sample Input 2

1 1
1000

Sample Output 2

1000


#include <iostream>

using namespace std;

typedef long long ll;

string Conv(int d, ll x)
{
    string w = "";
    while (x != 0)
    {
        w = (char) (x % 26 + 'a') + w;
        x /= 26;
    }
    while (w.size() < d)
        w = "a" + w;
    return w;
}

int main()
{
    ios_base::sync_with_stdio(0);
    ll n;
    cin >> n;
    ll d = 1, sp = 26;
    while (n > sp)
    {
        n -= sp;
        sp *= 26;
        ++d;
    }
    cout << Conv(d, n - 1);
    return 0;
}

Score : 300 points

Problem Statement

1000000000000001 dogs suddenly appeared under the roof of Roger's house, all of which he decided to keep. The dogs had been numbered 1 through 1000000000000001, but he gave them new names, as follows:

the dogs numbered 1,2,\cdots,26 were respectively given the names a, b, ..., z;

the dogs numbered 27,28,29,\cdots,701,702 were respectively given the names aa, ab, ac, ..., zy, zz;

the dogs numbered 703,704,705,\cdots,18277,18278 were respectively given the names aaa, aab, aac, ..., zzy, zzz;

the dogs numbered 18279,18280,18281,\cdots,475253,475254 were respectively given the names aaaa, aaab, aaac, ..., zzzy, zzzz;

the dogs numbered 475255,475256,\cdots were respectively given the names aaaaa, aaaab, ...;

and so on.

To sum it up, the dogs numbered 1, 2, \cdots were respectively given the following names:

a, b, ..., z, aa, ab, ..., az, ba, bb, ..., bz, ..., za, zb, ..., zz, aaa, aab, ..., aaz, aba, abb, ..., abz, ..., zzz, aaaa, ...

Now, Roger asks you:

"What is the name for the dog numbered N?"

Constraints

N is an integer.

1 \leq N \leq 1000000000000001

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer to Roger's question as a string consisting of lowercase English letters.

Sample Input 1

2

Sample Output 1

b

Sample Input 2

27

Sample Output 2

aa

Sample Input 3

123456789

Sample Output 3

jjddja


#include<vector>
#include<iostream>
#include<algorithm>
#include<chrono>
#include<set>
#include<cstdio>
#include<string>
#include<unordered_map>

using namespace std;

int main()
{
    int n, q, ans = 0;
    scanf("%d",&n);
    unordered_map<int, int> mp;
    vector<int> a(n);
    for(int i = 0; i < n; i++){
        scanf("%d", &a[i]);
        mp[a[i]]++;
        ans += a[i];
    }
    scanf("%d",&q);
    for(int i = 0; i < q; i++){
        int b, c;
        scanf("%d%d", &b, &c);
        mp[c] += mp[b];
        ans += (mp[b]*c - mp[b]*b);
        mp[b] = 0;
        cout << ans << endl;
    }
}

Score : 400 points

Problem Statement

You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.

You will now successively do the following Q operations:

In the i-th operation, you replace every element whose value is B_{i} with C_{i}.

For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Constraints

All values in input are integers.

1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5}

B_{i} \neq C_{i}

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

4
1 2 3 4
3
1 2
3 4
2 4

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

4
1 1 1 1
3
1 2
2 1
3 5

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000


#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using Int = long long;
const char newl = '\n';

template<typename T,T MOD = 1000000007>
struct Mint{
  static constexpr T mod = MOD;
  T v;
  Mint():v(0){}
  Mint(signed v):v(v){}
  Mint(long long t){v=t%MOD;if(v<0) v+=MOD;}

  Mint pow(long long k){
    Mint res(1),tmp(v);
    while(k){
      if(k&1) res*=tmp;
      tmp*=tmp;
      k>>=1;
    }
    return res;
  }

  static Mint add_identity(){return Mint(0);}
  static Mint mul_identity(){return Mint(1);}

  Mint inv(){return pow(MOD-2);}

  Mint& operator+=(Mint a){v+=a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator-=(Mint a){v+=MOD-a.v;if(v>=MOD)v-=MOD;return *this;}
  Mint& operator*=(Mint a){v=1LL*v*a.v%MOD;return *this;}
  Mint& operator/=(Mint a){return (*this)*=a.inv();}

  Mint operator+(Mint a) const{return Mint(v)+=a;}
  Mint operator-(Mint a) const{return Mint(v)-=a;}
  Mint operator*(Mint a) const{return Mint(v)*=a;}
  Mint operator/(Mint a) const{return Mint(v)/=a;}

  Mint operator-() const{return v?Mint(MOD-v):Mint(v);}

  bool operator==(const Mint a)const{return v==a.v;}
  bool operator!=(const Mint a)const{return v!=a.v;}
  bool operator <(const Mint a)const{return v <a.v;}

  static Mint comb(long long n,int k){
    Mint num(1),dom(1);
    for(int i=0;i<k;i++){
      num*=Mint(n-i);
      dom*=Mint(i+1);
    }
    return num/dom;
  }
};
template<typename T,T MOD> constexpr T Mint<T, MOD>::mod;
template<typename T,T MOD>
ostream& operator<<(ostream &os,Mint<T, MOD> m){os<<m.v;return os;}

template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}

//INSERT ABOVE HERE
const int MAX = 303;
using M = Mint<int, 998244353>;

M dp[MAX][MAX][MAX]={};
M ac[MAX][MAX]={};

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

  string s;
  cin>>s;
  int k;
  cin>>k;

  chmin(k,MAX-1);

  while(!s.empty() and s.front()=='1') s.erase(s.begin());
  while(!s.empty() and s.back() =='0') s.erase(--s.end());
  if(s.empty()) drop(1);

  vector<int> vs;
  for(char c:s){
    if(c=='0') vs.emplace_back(0);
    if(c=='1') vs.back()++;
  }

  int n=vs.size();
  vector<int> sm(n+1,0);
  for(int i=0;i<n;i++)
    sm[i+1]=sm[i]+vs[i];

  for(int x=0;x<MAX;x++) dp[0][x][0]=M(1);

  for(int i=0;i<n;i++){
    for(int j=sm[i];j<MAX;j++){
      int need=sm[i+1]-j;

      for(int d=0;d<vs[i];d++){
        for(int c=0;c<MAX;c++)
          if(need<=d and j+d<MAX and c+(vs[i]-d)<MAX)
            dp[i+1][j+d][c+(vs[i]-d)]+=dp[i][j][c];
      }
    }

    for(int j=0;j<MAX;j++)
      for(int c=0;c<MAX;c++)
        ac[j][c]=M(0);

    for(int j=sm[i];j+1<MAX;j++)
      for(int c=0;c<MAX;c++)
        ac[j+1][c]=ac[j][c]+dp[i][j][c];

    for(int j=sm[i];j+1<MAX;j++)
      for(int c=0;c<MAX;c++)
        dp[i+1][j][c]+=ac[max(j-vs[i]+1,0)][c];
  }

  M ans{0};
  for(int c=0;c<=k;c++)
    ans+=dp[n][sm[n]][c];
  cout<<ans<<newl;
  return 0;
}

Score : 800 points

Problem Statement

Given is a string S consisting of 0 and 1. Find the number of strings, modulo 998244353, that can result from applying the following operation on S between 0 and K times (inclusive):

Choose a pair of integers i, j (1\leq i < j\leq |S|) such that the i-th and j-th characters of S are 0 and 1, respectively. Remove the j-th character from S and insert it to the immediate left of the i-th character.

Constraints

1 \leq |S| \leq 300

0 \leq K \leq 10^9

S consists of 0 and 1.

Input

Input is given from Standard Input in the following format:

S K

Output

Find the number of strings, modulo 998244353, that can result from applying the operation on S between 0 and K times (inclusive).

Sample Input 1

0101 1

Sample Output 1

4

Four strings, 0101, 0110, 1001, and 1010, can result.

Sample Input 2

01100110 2

Sample Output 2

14

Sample Input 3

1101010010101101110111100011011111011000111101110101010010101010101 20

Sample Output 3

113434815


#include<bits/stdc++.h>
using namespace std;
int main()
{int x[6];
for(int i=1;i<=5;i++)
{cin>>x[i];
}
 for(int i=1;i<=5;i++)
 {if(x[i]==0)
  cout<<i;
}
return 0;
 }

Score : 100 points

Problem Statement

We have five variables x_1, x_2, x_3, x_4, and x_5.

The variable x_i was initially assigned a value of i.

Snuke chose one of these variables and assigned it 0.

You are given the values of the five variables after this assignment.

Find out which variable Snuke assigned 0.

Constraints

The values of x_1, x_2, x_3, x_4, and x_5 given as input are a possible outcome of the assignment by Snuke.

Input

Input is given from Standard Input in the following format:

x_1 x_2 x_3 x_4 x_5

Output

If the variable Snuke assigned 0 was x_i, print the integer i.

Sample Input 1

0 2 3 4 5

Sample Output 1

1

In this case, Snuke assigned 0 to x_1, so we should print 1.

Sample Input 2

1 2 0 4 5

Sample Output 2

3


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

int main() {
  int a,b,c,d,e;
  cin >> a >> b >> c >> d >>e;

  int S = a+b+c+d+e;

  if(S==14){
    cout << 1 <<endl;
  }
  else if(S==13){
    cout << 2 <<endl;
  }
  else if(S==12){
    cout << 3 <<endl;
  }
  else if(S==11){
    cout << 4 <<endl;
  }
  else{
    cout << 5 <<endl;
  }
}


#include <bits/stdc++.h>

#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define ll long long
#define ii pair<int,int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

using namespace std;

int32_t main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int x,y;
	cin>>x>>y;
	if(y&1)
		cout<<"No\n";
	else if(y%4==0 && y/4 == x)
		cout<<"Yes\n";
	else if(y%2==0 && y/2==x)
		cout<<"Yes\n";
	else if((y%2)%4==0 && (y/2+(y%2)/4)==x)
		cout<<"Yes\n";
	else if((y%4)%2==0 && (y/4+(y%4)/2)==x)
		cout<<"Yes\n";
	else
		cout<<"No\n";
	return 0;
}

Score : 200 points

Problem Statement

There are some animals in a garden. Each of them is a crane with two legs or a turtle with four legs.

Takahashi says: "there are X animals in total in the garden, and they have Y legs in total." Determine whether there is a combination of numbers of cranes and turtles in which this statement is correct.

Constraints

1 \leq X \leq 100

1 \leq Y \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If there is a combination of numbers of cranes and turtles in which the statement is correct, print Yes; otherwise, print No.

Sample Input 1

3 8

Sample Output 1

Yes

The statement "there are 3 animals in total in the garden, and they have 8 legs in total" is correct if there are two cranes and one turtle. Thus, there is a combination of numbers of cranes and turtles in which the statement is correct.

Sample Input 2

2 100

Sample Output 2

No

There is no combination of numbers of cranes and turtles in which this statement is correct.

Sample Input 3

1 2

Sample Output 3

Yes

We also consider the case in which there are only cranes or only turtles.


#include<bits/stdc++.h>

using namespace std;

typedef long long int li;
typedef long double ld;
typedef vector<li> vi;
typedef pair<li,li> pi;
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define B begin()
#define E end()
#define f(i,l,r) for(li i=l;i<=r;++i)
#define fr(i,l,r) for(li i=l;i>=r;--i)
#define u_map unordered_map
#define endl "\n"
#define debug(x) cout<<#x<<" = "<<x<<endl;

li fastpow(li base,li exp,li M)
{
    li res=1;
    while(exp>0) {
        if(exp&1)
            res=(res*base)%M;

        base=(base*base)%M;
        exp>>=1;
    }
    return res;
}

void solve()
{
    li x,n;
    cin>>x>>n;

    li a[n];

    f(i,0,n-1)
    {
        cin>>a[i];
    }

    li mn=1e18;

    li ans=100;

    f(i,0,n-1)
    {
        if(abs(a[i]-x)<=mn)
        {
            mn=abs(a[i]-x);
            ans=min(ans,a[i]);
        }
    }

    cout<<ans;
}

int main()
{

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

    li t=1;

    // cin>>t;

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

    return 0;
}

Score : 300 points

Problem Statement

Given are an integer X and an integer sequence of length N: p_1, \ldots, p_N.

Among the integers not contained in the sequence p_1, \ldots, p_N (not necessarily positive), find the integer nearest to X, that is, find the integer whose absolute difference with X is the minimum. If there are multiple such integers, report the smallest such integer.

Constraints

1 \leq X \leq 100

0 \leq N \leq 100

1 \leq p_i \leq 100

p_1, \ldots, p_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.

Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the one nearest to 6 is 8.

Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

Among the integers not contained in the sequence 4, 7, 10, 6, 5, the ones nearest to 10 are 9 and 11. We should print the smaller one, 9.

Sample Input 3

100 0

Sample Output 3

100

When N = 0, the second line in the input will be empty. Also, as seen here, X itself can be the answer.


#include<iostream>
#include<vector>
#include<queue>
#include<climits>
#include<cmath>

typedef std::pair<int,int> index;
typedef std::pair<index,char> indexWithDirect;
typedef std::pair<indexWithDirect,double> priorityNode;

class grid{
public:
    bool isBarier;              // lotus or not
    bool isSearched;            // once searched or not
    long double cost[4];
    grid(){
        isBarier=0;
        isSearched=0;
        for(int i=0;i<4;++i){
            cost[i]=INT_MAX;
        }
    }
};

enum DIRECT{
    EAST,
    WEST,
    SOUTH,
    NORTH,
    NUM_DIRECT,
};

index moveV(index pos,int direct,int distance){
    index movedPos=pos;
    switch (direct){
    case EAST:
        movedPos.second+=distance;
        break;
    case WEST:
        movedPos.second-=distance;
        break;
    case SOUTH:
        movedPos.first+=distance;
        break;
    case NORTH:
        movedPos.first-=distance;
        break;
    default:
        break;
    }
    return movedPos;
}

bool isEnableMove(index movedPos,std::vector<std::vector<grid> > pond){
    if(movedPos.first>=0 && movedPos.second>=0){
        if(movedPos.first<pond.size() && movedPos.second<pond[0].size()){
            if(pond[movedPos.first][movedPos.second].isBarier==0){
                return 1;
            }
        }
    }
    return 0;
}
/*

*/

double calcCost(double preCost,char movingDirect,char movedDirect,int distance,int K){
    double cost=preCost+distance/(double)K;
    if(movingDirect!=movedDirect){
        cost=ceil(cost);
    }
    return cost;
}

void print(index id,char end='\n'){
    std::cout<<"("<<id.first<<","<<id.second<<")"<<end;
}

void print(indexWithDirect iwd,char end='\n'){
    print(iwd.first,' ');
    std::cout<<"direct:"<<iwd.second<<end;
}

void print(priorityNode n,char end='\n'){
    print(n.first,' ');
    std::cout<<"cost:"<<n.second<<end;
}

void print(std::vector<std::vector<grid> >pond){
    for(auto r:pond){
        for(auto g:r){
            double cost=g.cost[0];
            for(int i=1;i<4;++i)
                if(cost>g.cost[i])
                    cost=g.cost[i];
            std::cout<<cost<<"("<<g.isBarier<<")\t";
        }
        std::cout<<std::endl;
    }
}

void dijkstra(){
    int H,W,K;
    std::cin>>H>>W>>K;
    index start,goal;
    std::vector<std::vector<grid>> pond(H,std::vector<grid>(W));
    auto compare=[](priorityNode l,priorityNode r){return l.second>r.second;};
    std::priority_queue<priorityNode,std::vector<priorityNode>,decltype(compare)> q{compare};
    std::cin>>start.first>>start.second;
    std::cin>>goal.first>>goal.second;

    start.first-=1;
    start.second-=1;
    goal.first-=1;
    goal.second-=1;

    for(int i=0;i<H;++i){
        for(int j=0;j<W;++j){
            char buf;
            std::cin>>buf;
            grid& p=pond[i][j];
            if (buf=='.'){
                p.isBarier=0;
                if(start.first==i && start.second==j){
                    for(int direct=0;direct<NUM_DIRECT;++direct){
                        q.push(priorityNode(indexWithDirect(index(i,j),(char)direct),0));
                        p.cost[direct]=0;
                    }
                }
            }else{
                p.isBarier=1;
                p.isSearched=1;
            }
        }
    }

    while(!q.empty()){
        priorityNode    node=q.top();
        indexWithDirect iwd=node.first;
        index           id=iwd.first;
        char            direct=iwd.second;
        double          cost=node.second;
        auto&           minCost=pond[id.first][id.second].cost[direct];
        double          goalCost=pond[goal.first][goal.second].cost[direct];
        q.pop();
        // print(node);
        if(minCost<cost){
            continue;
        }
        if(goalCost<cost){
            continue;
        }
        minCost=cost;

        for(int j=1;j<=K;++j){
            auto calcIndex=moveV(id,direct,j);
            if(isEnableMove(calcIndex,pond)==1){
                auto& calcGrid=pond[calcIndex.first][calcIndex.second];
                auto& calcGridCost=calcGrid.cost;
                for(char k=0;k<NUM_DIRECT;++k){
                    double movedCost=calcCost(cost,direct,k,j,K);
                    if(calcGridCost[k]>movedCost){
                        calcGridCost[k]=movedCost;
                        q.push(priorityNode(indexWithDirect(calcIndex,k),movedCost));
                        calcGrid.isSearched=1;
                    }
                }
            }
            else{
                break;
            }
        }
    }
    grid goalGrid=pond[goal.first][goal.second];
    if(goalGrid.isSearched==0){
        std::cout<<-1<<std::endl;
    }else{
        double ans=goalGrid.cost[0];
        for(int i=0;i<NUM_DIRECT;++i){
            if(ans>goalGrid.cost[i]){
                ans=goalGrid.cost[i];
            }
        }
        std::cout<<(int)(ceil(ans))<<std::endl;
    }
    // print(pond);
}

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

Score : 600 points

Problem Statement

Snuke, a water strider, lives in a rectangular pond that can be seen as a grid with H east-west rows and W north-south columns. Let (i,j) be the square at the i-th row from the north and j-th column from the west.

Some of the squares have a lotus leaf on it and cannot be entered.
The square (i,j) has a lotus leaf on it if c_{ij} is @, and it does not if c_{ij} is ..

In one stroke, Snuke can move between 1 and K squares (inclusive) toward one of the four directions: north, east, south, and west.
The move may not pass through a square with a lotus leaf. Moving to such a square or out of the pond is also forbidden.

Find the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2).
If the travel from (x_1,y_1) to (x_2,y_2) is impossible, point out that fact.

Constraints

1 \leq H,W,K \leq 10^6

H \times W \leq 10^6

1 \leq x_1,x_2 \leq H

1 \leq y_1,y_2 \leq W

x_1 \neq x_2 or y_1 \neq y_2.

c_{i,j} is . or @.

c_{x_1,y_1} = .

c_{x_2,y_2} = .

All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

H W K
x_1 y_1 x_2 y_2
c_{1,1}c_{1,2} .. c_{1,W}
c_{2,1}c_{2,2} .. c_{2,W}
:
c_{H,1}c_{H,2} .. c_{H,W}

Output

Print the minimum number of strokes Snuke takes to travel from the square (x_1,y_1) to (x_2,y_2), or print -1 if the travel is impossible.

Sample Input 1

3 5 2
3 2 3 4
.....
.@..@
..@..

Sample Output 1

5

Initially, Snuke is at the square (3,2).
He can reach the square (3, 4) by making five strokes as follows:

From (3, 2), go west one square to (3, 1).

From (3, 1), go north two squares to (1, 1).

From (1, 1), go east two squares to (1, 3).

From (1, 3), go east one square to (1, 4).

From (1, 4), go south two squares to (3, 4).

Sample Input 2

1 6 4
1 1 1 6
......

Sample Output 2

2

Sample Input 3

3 3 1
2 1 2 3
.@.
.@.
.@.

Sample Output 3

-1


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>
#include <sstream>
#include <utility>
#include <ctime>
#include <cassert>
#include <fstream>
using namespace std;
typedef long long LL;
#define MS(A) memset(A, 0, sizeof(A))
#define MSV(A,a) memset(A, a, sizeof(A))
#define MP make_pair
#define PB push_back
#define getcx getchar_unlocked
#define INF (int(1e9))
#define INFL (LL(1e18))
#define EPS 1e-12
#define chkbit(s, b) (s & (1<<b))
#define setbit(s, b) (s |= (1<<b))
#define chk(a) cerr << endl << #a << " : " << a << endl
#define chk2(a,b) cerr << endl << #a << " : " << a << "\t" << #b << " : " << b << endl
#define chk3(a,b,c) cerr << endl << #a << " : " << a << "\t" << #b << " : " << b << "\t" << #c << " : " << c << endl
#define chk4(a,b,c,d) cerr << endl << #a << " : " << a << "\t" << #b << " : " << b << "\t" << #c << " : " << c << "\t" << #d << " : " << d << endl
#define rep(i, a, n) for(i=a;i<n;i++)
#define rev(i, a, n) for(i=a;i>=n;i--)
#define rep_itr(itr, c) for(itr = (c).begin(); itr != (c).end(); itr++)
#define MOD 1000000007
#define MAX 1000000

int main() {

	LL a,v,b,w,t;
	cin>>a>>v;
	cin>>b>>w;
	cin>>t;

	LL f, s;

	if(b >= a) {

		f = a + (v*t);
		s = b + (w * t);
	} else{
		f = a - (v*t);
		s = b - (w * t);
	}

	if(f >= s) {
		printf("YES\n");
	}else {
		printf("NO\n");
	}

	return 0;
}

Score : 200 points

Problem Statement

Two children are playing tag on a number line. (In the game of tag, the child called "it" tries to catch the other child.) The child who is "it" is now at coordinate A, and he can travel the distance of V per second.
The other child is now at coordinate B, and she can travel the distance of W per second.

He can catch her when his coordinate is the same as hers.
Determine whether he can catch her within T seconds (including exactly T seconds later).
We assume that both children move optimally.

Constraints

-10^9 \leq A,B \leq 10^9

1 \leq V,W \leq 10^9

1 \leq T \leq 10^9

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A V
B W
T

Output

If "it" can catch the other child, print YES; otherwise, print NO.

Sample Input 1

1 2
3 1
3

Sample Output 1

YES

Sample Input 2

1 2
3 2
3

Sample Output 2

NO

Sample Input 3

1 2
3 3
3

Sample Output 3

NO


#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
typedef long double ld;
const int inf=1e9+7;
const ll longinf=1LL<<60;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
#define F first
#define S second
constexpr char ln = '\n';

const int mx=100010;
const ll mod=1e9+7;

#define bit(n,k) ((n>>k)&1) //*n no k bit me 1 or 0*/

int main(){
  int n;
  cin >> n;
  vector<int> v(n+1),w(n+1);
  rep(i,n){
    cin >> v[i+1] >> w[i+1];
  }
  vector<vector<P>> data((1<<9)+5); //d9 made hanbun-zenrekkyo
  REP(i,1,min(n+1,1<<9)){
    if(i==1){
      data[i].emplace_back(w[i],v[i]);
      data[i].emplace_back(0,0);
    } else {
      data[i] = data[i/2];
      for(auto it:data[i/2]){
        data[i].emplace_back(it.F+w[i],it.S+v[i]);
      }
    }

    sort(data[i].begin(),data[i].end());
    REP(j,1,data[i].size()){
      data[i][j].S = max(data[i][j].S, data[i][j-1].S);
    }
  }

  int Q;
  cin >> Q;
  vector<pair<int,P>> q(Q);
  rep(i,Q){
    cin >> q[i].F >> q[i].S.F;
    q[i].S.S = i;
  }
  sort(q.begin(),q.end());
  vector<int> A(Q);
  unordered_map<int,vector<P>> mp;
  rep(s,Q){
    int x=q[s].F, L=q[s].S.F;
    int ans;
    if(x<min(n+1,1<<9)){
      int pos = lower_bound(data[x].begin(),data[x].end(),P(L+1,0)) - data[x].begin();
      ans = data[x][pos-1].S;
//      cout << ans << ln;
    } else {
      vector<int> ls;
      int tmp = x;
      while(tmp>=(1<<9) && mp.find(tmp)==mp.end()){ ls.emplace_back(tmp); tmp/=2; }
//      sort(ls.rbegin(),ls.rend());
      int sz = ls.size();
      vector<P> sl;
      rep(b,1<<sz){
        int vsum = 0, wsum = 0;
        rep(j,sz)if(bit(b,j)){
          vsum += v[ls[j]];
          wsum += w[ls[j]];
        }
        if(mp.find(tmp)!=mp.end()){
          for(auto it:mp[tmp]){
            sl.emplace_back(it.F+wsum, it.S+vsum);
          }
        } else {
          sl.emplace_back(wsum,vsum);
        }
      }
      mp[x] = sl;
      while(tmp>=(1<<9)){ tmp/=2; }
      for(auto it:sl){
        if(it.F<=L){
          int pos = lower_bound(data[tmp].begin(),data[tmp].end(),P(L-it.F+1,0)) - data[tmp].begin();
          ans = max(ans, it.S+data[tmp][pos-1].S);
        }
      }
//      cout << ans << ln;
    }
    A[q[s].S.S] = ans;
  }

  for(auto ans:A){
    cout << ans << ln;
  }
  return 0;
}

Score : 700 points

Problem Statement

We have a rooted binary tree with N vertices, where the vertices are numbered 1 to N.
Vertex 1 is the root, and the parent of Vertex i (i \geq 2) is Vertex \left[ \frac{i}{2} \right].

Each vertex has one item in it. The item in Vertex i has a value of V_i and a weight of W_i.
Now, process the following query Q times:

Given are a vertex v of the tree and a positive integer L.
Let us choose some (possibly none) of the items in v and the ancestors of v so that their total weight is at most L.
Find the maximum possible total value of the chosen items.

Here, Vertex u is said to be an ancestor of Vertex v when u is an indirect parent of v, that is, there exists a sequence of vertices w_1,w_2,\ldots,w_k (k\geq 2) where w_1=v, w_k=u, and w_{i+1} is the parent of w_i for each i.

Constraints

All values in input are integers.

1 \leq N < 2^{18}

1 \leq Q \leq 10^5

1 \leq V_i \leq 10^5

1 \leq W_i \leq 10^5

For the values v and L given in each query, 1 \leq v \leq N and 1 \leq L \leq 10^5.

Input

Let v_i and L_i be the values v and L given in the i-th query.
Then, Input is given from Standard Input in the following format:

N
V_1 W_1
:
V_N W_N
Q
v_1 L_1
:
v_Q L_Q

Output

For each integer i from 1 through Q,
the i-th line should contain the response to the i-th query.

Sample Input 1

3
1 2
2 3
3 4
3
1 1
2 5
3 5

Sample Output 1

0
3
3

In the first query, we are given only one choice: the item with (V, W)=(1,2). Since L = 1, we cannot actually choose it, so our response should be 0.

In the second query, we are given two choices: the items with (V, W)=(1,2) and (V, W)=(2,3). Since L = 5, we can choose both of them, so our response should be 3.

Sample Input 2

15
123 119
129 120
132 112
126 109
118 103
115 109
102 100
130 120
105 105
132 115
104 102
107 107
127 116
121 104
121 115
8
8 234
9 244
10 226
11 227
12 240
13 237
14 206
15 227

Sample Output 2

256
255
250
247
255
259
223
253


#include <bits/stdc++.h>

using namespace std;

string s;

pair<int, int> inc(pair<int, int> u, pair<int, int> goodRange) {
    if (u.first == u.second && u.second == goodRange.second) return {1e9, -1e9};
    u.first++;
    u.second++;
    if (u.second > goodRange.second) u.second -= 2;
    return u;
}

pair<int, int> dec(pair<int, int> u, pair<int, int> goodRange) {
    if (u.first == u.second && u.first == goodRange.first) return {1e9, -1e9};
    u.first--;
    u.second--;
    if (u.first < goodRange.first) u.first += 2;
    return u;
}

int eval(int lowest, int delta) {
    pair<int, int> goodRange(lowest, lowest + delta);
    pair<int, int> curPos(0, 0);
    for (char u : s) {
        if (u == '1') {
            curPos = inc(curPos, goodRange);
            if (curPos.first > curPos.second) {
                return 1;
            }
        } else if (u == '0') {
            curPos = dec(curPos, goodRange);
            if (curPos.first > curPos.second) {
                return -1;
            }
        } else {
            pair<int, int> curPos1 = inc(curPos, goodRange);
            pair<int, int> curPos2 = dec(curPos, goodRange);
            curPos.first = min(curPos1.first, curPos2.first);
            curPos.second = max(curPos1.second, curPos2.second);
        }
        // cout << "char " << u << endl;
        // cout << curPos.first << ' ' << curPos.second << endl;
        // cout << goodRange.first << ' ' << goodRange.second << endl;
    }
    return 0;
}

bool check(int delta) {
    if (delta == 1) {
        return eval(-1, 1) == 0 || eval(0, 1) == 0;
    }
    // for (int i = -delta; i <= 0; i++) {
    //     cout << eval(i, delta) << endl;
    // }
    // return 0;

    //even
    int low = -delta;
    int high = 0;
    if (low & 1) low++;
    if (high & 1) high--;
    while (high - low > 2) {
        int mid = low + high >> 1;
        if (mid & 1) mid++;
        int val = eval(mid, delta);
        if (val == 0) return 1;
        if (val == 1) high = mid;
        else low = mid;
    }
    if (eval(low, delta) == 0 ||  eval(high, delta) == 0) return 1;

    //odd
    low = -delta;
    high = 0;
    if (low % 2 == 0) low++;
    if (high % 2 == 0) high--;
    while (high - low > 2) {
        int mid = low + high >> 1;
        if (mid % 2 == 0) mid++;
        int val = eval(mid, delta);
        if (val == 0) return 1;
        if (val == 1) high = mid;
        else low = mid;
    }
    if (eval(low, delta) == 0 ||  eval(high, delta) == 0) return 1;
    return 0;
}

void solve(int test) {
    cin >> s;

    int low = 0, high = s.size();

    int minVal=  0, maxVal = 0;
    int cur = 0;
    for (char u : s) {
        if (u == '?') {
            minVal = maxVal = 0;
            cur = 0;
        } else if (u == '1') {
            cur++;
            maxVal = max(maxVal, cur);
        } else {
            cur--;
            minVal = min(minVal, cur);
        }
        low = max(low, maxVal - minVal - 1);
    }

    while (high - low > 1) {
        int mid = low + high >> 1;
        if (check(mid)) high = mid;
        else low = mid;
    }
    cout << high << '\n';
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    int tc;
    // cin >> tc;
    tc = 1;
    for (int test = 1; test <= tc; test++) {
        solve(test);
    }
    return 0;
}

Score : 800 points

Problem Statement

Given is a string S, where each character is 0, 1, or ?.

Consider making a string S' by replacing each occurrence of ? with 0 or 1 (we can choose the character for each ? independently).
Let us define the unbalancedness of S' as follows:

(The unbalancedness of S') = \max \{ The absolute difference between the number of occurrences of 0 and 1 between the l-th and r-th character of S (inclusive) :\ 1 \leq l \leq r \leq |S|\}

Find the minimum possible unbalancedness of S'.

Constraints

1 \leq |S| \leq 10^6

Each character of S is 0, 1, or ?.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum possible unbalancedness of S'.

Sample Input 1

0??

Sample Output 1

1

We can make S'= 010 and achieve the minimum possible unbalancedness of 1.

Sample Input 2

0??0

Sample Output 2

2

Sample Input 3

??00????0??0????0?0??00??1???11?1?1???1?11?111???1

Sample Output 3

4


#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int N = 1000009, i, j;
vector<int> primes;
void collect_primes(){
	primes.push_back(2);
	primes.push_back(3);
	primes.push_back(5);
	for(i=7; i<=N; i+=2){
		bool is = 1;
		for(j=0; primes[j]*primes[j] <= i; j++){
			if(i % primes[j] == 0){
				is = 0;
				break;
			}
		}if(is){
			primes.push_back(i);
		}
	}
}
int main(){
	ll n;
	cin >> n;
	if(n == 1){
		cout << 0;
		return 0;
	}
	bool ok = 1;
	for(i=2;i*i<=n;i++){
		if(n%i==0){
			ok = 0;
			break;
		}
	}
	if(ok){
		cout << 1;
		return 0 ;
	}

	collect_primes();
	ll ans = 0;
	i = 0;
	ll now = 2;
	while(n>1){
		if(now<=n&&n%now == 0){
			n/=now;
			now = now * primes[i];
			ans++;
		}else if(now>n){
			while(n%primes[i] == 0){
				n/=primes[i];
			}now = primes[i+1];
			i++;
		}
		else {
			i++;
			now = primes[i];
		}
	}
	cout << ans;
}

Score : 400 points

Problem Statement

Given is a positive integer N. Consider repeatedly applying the operation below on N:

First, choose a positive integer z satisfying all of the conditions below:

z can be represented as z=p^e, where p is a prime number and e is a positive integer;

z divides N;

z is different from all integers chosen in previous operations.

Then, replace N with N/z.

Find the maximum number of times the operation can be applied.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum number of times the operation can be applied.

Sample Input 1

24

Sample Output 1

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=12.)

Choose z=3 (=3^1). (Now we have N=4.)

Choose z=4 (=2^2). (Now we have N=1.)

Sample Input 2

1

Sample Output 2

0

We cannot apply the operation at all.

Sample Input 3

64

Sample Output 3

3

We can apply the operation three times by, for example, making the following choices:

Choose z=2 (=2^1). (Now we have N=32.)

Choose z=4 (=2^2). (Now we have N=8.)

Choose z=8 (=2^3). (Now we have N=1.)

Sample Input 4

1000000007

Sample Output 4

1

We can apply the operation once by, for example, making the following choice:

z=1000000007 (=1000000007^1). (Now we have N=1.)

Sample Input 5

997764507000

Sample Output 5

7


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define repA(i,n) for(int i=n; i>=0; i--)
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (int)(a.size())
typedef long long ll;
const ll mod = 1e9+7;//998244353
int t = 1;
/*
int rooms,psgs;
vector<int> a[(int)1e5 + 3];
vector<int> p((int)1e5 + 3 , 0);
vector<bool> v((int)1e5 + 3, false);

void dfs(int cur) {
  vector<int>
  for(auto &i:a[cur]) {
    if(v[i])  continue;
    p[i] = cur;
    v[i]=true;
  }
}*/

int calc(int x) {
  long double ans = 0.25;
  ans = sqrt((long double)x*2 + ans);
  ans -=0.5;
  return (int)ans;
}

void solve(int BYasD) {
  ll n; cin>>n;
  int cnt = 0;
  while(n%2 == 0) {
    cnt++;
    n = n/2;
  }
  int ans = 0;
  ans += calc(cnt);
  for(int i=3; i<=sqrt(n); i+=2) {
    cnt = 0;
    while(n%i == 0) {
      cnt++;n=n/i;
    }
    ans += calc(cnt);
  }
  if(n!=1)  ans++;
  cout<<ans<<'\n';
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  #endif
  //cin>>t;
  rep(i,t)  {
  //  cout<<"Case #"<<(i+1)<<'\n';
    solve(i);
  //  cout<<'\n';
  }
  return 0;
}


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

#define int long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define range(i,a,b) ((a)<=(i) && (i)<(b))
#define debug(x) cout << #x << " = " << (x) << endl;
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define SP << " " <<

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vec;
typedef vector<P> pvec;
typedef vector<vector<int>> vvec;
typedef vector<vector<P>> pvvec;
typedef priority_queue<int> PQI;
typedef priority_queue<P> PQP;
typedef priority_queue<int,vector<int>,greater<int>> PQIG;
typedef priority_queue<P,vector<P>,greater<P>> PQPG;

const vector<int> DX = {0, -1, 0, 1, 1, 1, -1, -1};
const vector<int> DY = {1, 0, -1, 0, 1, -1, 1, -1};
constexpr int MOD = (1000000007);
// const int MOD = (998244353);
// const int INF = (1 << 30); // 1073741824
const ll INF = (1LL << 60); // 1152921504606846976
const double PI = (3.141592653589794);
const double EPS = (0.0000000001); // 10^(-10)

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return 1;} return 0;}
template<class T> inline T ceil(T a, T b) {return T((a + b - 1) / b);}
template<class T> inline T round(T a, T b) {return T(a / b);}
template< typename T1, typename T2 > istream &operator>>(istream &is, pair< T1, T2 > &p) { is >> p.first >> p.second; return is; }
template <class T> inline void out(T &a) { bool flag=true; for(auto&x:a){if(flag) {flag=false;} else{ cout << ' '; } cout<<x; } cout << endl; }

//----------------------------------------------------------------
int nmax=200000; // 2*(10^5)
vvec G(nmax);

void solve4ts()
{

    int a,b,c,d,e;
    cin>>a>>b>>c>>d>>e;
    int x=c-a;
    x*=60;
    x+=(60+(b-d))%60;
    cout<<x-e<<endl;

}
//-----------------------------------------------------------------

signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout << fixed << setprecision(15);
    int repeat = 1;
    // cin >> repeat;
    while(repeat--) solve4ts();
}

Score : 100 points

Problem Statement

In this problem, we use the 24-hour clock.

Takahashi gets up exactly at the time H_1 : M_1 and goes to bed exactly at the time H_2 : M_2. (See Sample Inputs below for clarity.)
He has decided to study for K consecutive minutes while he is up.
What is the length of the period in which he can start studying?

Constraints

0 \le H_1, H_2 \le 23

0 \le M_1, M_2 \le 59

The time H_1 : M_1 comes before the time H_2 : M_2.

K \ge 1

Takahashi is up for at least K minutes.

All values in input are integers (without leading zeros).

Input

Input is given from Standard Input in the following format:

H_1 M_1 H_2 M_2 K

Output

Print the length of the period in which he can start studying, as an integer.

Sample Input 1

10 0 15 0 30

Sample Output 1

270

Takahashi gets up at exactly ten in the morning and goes to bed at exactly three in the afternoon.
It takes 30 minutes to do the study, so he can start it in the period between ten o'clock and half-past two. The length of this period is 270 minutes, so we should print 270.

Sample Input 2

10 0 12 0 120

Sample Output 2

0

Takahashi gets up at exactly ten in the morning and goes to bed at exactly noon. It takes 120 minutes to do the study, so he has to start it at exactly ten o'clock. Thus, we should print 0.


#include <bits/stdc++.h>
#define Int int64_t

using namespace std;

int main() {
	int h1, m1, h2, m2, k;
	cin >> h1 >> m1 >> h2 >> m2 >> k;

	int t1 = 60 * h1 + m1;
	int t2 = 60 * h2 + m2;
	int ans = t2 - t1 - k;
	cout << ans << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long LL;
typedef pair<LL,LL> P;
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; }
template<typename T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
const LL mod=1000000007;
const LL LINF=1LL<<62;
const int INF=1<<30;
int dx[]={1,0,-1,0,1,-1,1,-1};
int dy[]={0,1,0,-1,1,-1,-1,1};

int main(){
    int n;cin >> n;
    vector<LL> a(n + 1);
    for (int i = 0; i < n + 1; i++) {
        cin >> a[i];
    }
    if(a[0]){
        puts("-1");
    }
    else{
        vector<LL> as(n + 2, 0);
        for (int i = n + 1; i >= 1; i--) {
            as[i - 1] = as[i] + a[i - 1];
        }
        LL now = 1;
        LL ans = 1;
        for (int i = 1; i < n + 1; i++) {
            LL nnow = min(2 * now, as[i]);
            ans += nnow;
            nnow -= a[i];
            if(nnow < 0){
                puts("-1");
                return 0;
            }
            now = nnow;
        }
        if(now == 0){
            cout << ans << endl;
        }
        else{
            puts("-1");
        }
    }
    return 0;
}

Score : 600 points

Problem Statement

Given is an integer sequence of length N+1: A_0, A_1, A_2, \ldots, A_N. Is there a binary tree of depth N such that, for each d = 0, 1, \ldots, N, there are exactly A_d leaves at depth d? If such a tree exists, print the maximum possible number of vertices in such a tree; otherwise, print -1.

Notes

A binary tree is a rooted tree such that each vertex has at most two direct children.

A leaf in a binary tree is a vertex with zero children.

The depth of a vertex v in a binary tree is the distance from the tree's root to v. (The root has the depth of 0.)

The depth of a binary tree is the maximum depth of a vertex in the tree.

Constraints

0 \leq N \leq 10^5

0 \leq A_i \leq 10^{8} (0 \leq i \leq N)

A_N \geq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 A_2 \cdots A_N

Output

Print the answer as an integer.

Sample Input 1

3
0 1 1 2

Sample Output 1

7

Below is the tree with the maximum possible number of vertices. It has seven vertices, so we should print 7.

Sample Input 2

4
0 0 1 0 2

Sample Output 2

10

Sample Input 3

2
0 3 1

Sample Output 3

-1

Sample Input 4

1
1 1

Sample Output 4

-1

Sample Input 5

10
0 0 1 1 2 3 5 8 13 21 34

Sample Output 5

264


#include <bits/stdc++.h>

using namespace std;

const int N = 1e5 + 7 , mod = 1e9 + 7 ;

unsigned long long n , m;
string s ;
unsigned long long l[N] , r[N];
int main()
{
        ios_base::sync_with_stdio(0);
        cin.tie(0);
       // freopen("in.in" , "r" , stdin) ;
        cin>>n ;
        unsigned long long a[n+1] ;
        unsigned long long ans = 0 , mx = 1 ;
        for(int i = 0 ;i <=n;i++){
                unsigned long long t ;
                cin>>t ;
                a[i] = t ;
                if(t > mx){
                        return cout<<-1,0 ;
                }
                r[i] = mx ;
                mx-=t ;
                mx*=2 ;
        }

        unsigned long long all = a[n] ;
        ans = a[n] ;
        for(int i = n ;i >0 ; i--){
                unsigned long long rem =min(r[i-1] - a[i-1] ,  (1<<(i-1)) - a[i-1] )  ;
                unsigned long long tws = 0 , ons = 0 ;
                if(all >= rem){
                        ons = min(rem , all) ;
                        all-=ons ;
                        tws = all ;
                        ons-=tws ;
                }
                else {
                        ons = all ;
                }
                ans+= ons + tws + a[i-1];
                all = ons + tws + a[i-1]  ;

        }

        cout<< ans ;
        return 0;
}


/* Author: Mrigank
Science is what we understand well enough to explain to a computer,
    Art is everything else we do - Donald E. Knuth */

#include <bits/stdc++.h>
#include <iostream>
#include <unordered_map>
#include <map>
#include <queue>
#include <stack>
#include <unordered_set>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

typedef long long int ll;
typedef long double ld;
typedef pair<ll, int> PLI;
typedef pair < ll , ll > PLL;
typedef pair<int, int> PII;
typedef vector < ll > vl;
typedef vector < int > vi;
typedef vector < bool > vb;
typedef vector < pair < ll , ll > > vpl;
typedef vector < pair < ll , bool > > vpb;
typedef vector < vector < int > > vvi;
typedef vector < vector < ll > > vvl;

#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;i<n;i++)
#define pi pair < ll , ll >
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define clr(x , val) memset(x , val , sizeof(x))
#define deb(x) cout<<#x<<" :: "<<x<<"\n"
#define deb2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\n"
#define deb3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\n"
#define all(x) x.begin() , x.end()
#define alls(x , y) x.begin() , x.end() , y
#define fastIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl "\n"

const ll mxsize = 5e5 + 10;
const ll linf = 1e9;
const ld inf = 10000000000.0;
const ll mod = 1e9 + 7;

/******************************************** CODE STARTS FROM HERE *************************************************/
int main()
{
    fastIO;
    ll n;
    cin >> n;
    int val = n % 10;
    if(val == 2 || val == 4 || val == 5 || val == 7 || val == 9)
    {
        cout << "hon" << endl;
    }
    else if(val == 0 || val == 1 || val == 6 || val == 8)
    {
        cout << "pon" << endl;
    }
    else
    cout << "bon" << endl;
}

Score: 100 points

Problem Statement

The cat Snuke wants to play a popular Japanese game called ÅtCoder, so Iroha has decided to teach him Japanese.

When counting pencils in Japanese, the counter word "本" follows the number. The pronunciation of this word varies depending on the number. Specifically, the pronunciation of "本" in the phrase "N 本" for a positive integer N not exceeding 999 is as follows:

hon when the digit in the one's place of N is 2, 4, 5, 7, or 9;

pon when the digit in the one's place of N is 0, 1, 6 or 8;

bon when the digit in the one's place of N is 3.

Given N, print the pronunciation of "本" in the phrase "N 本".

Constraints

N is a positive integer not exceeding 999.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

16

Sample Output 1

pon

The digit in the one's place of 16 is 6, so the "本" in "16 本" is pronounced pon.

Sample Input 2

2

Sample Output 2

hon

Sample Input 3

183

Sample Output 3

bon


#include <stdio.h>
#include <sstream>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <limits>
#include <iomanip>
#include <ctype.h>
#include <unordered_map>
#include <random>
#include <numeric>
#include <iostream>
#include <array>

#define _USE_MATH_DEFINES
#include <iostream>
#include <math.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, double> pld;
typedef pair<double, double> pdd;
typedef pair<double, ll> pdl;
typedef pair<int, char> pic;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef priority_queue<ll, vector<ll>, greater<ll>> llgreaterq;
typedef priority_queue<pll, vector<pll>, greater<pll>> pllgreaterq;
typedef priority_queue<pair<ll, pll>, vector<pair<ll, pll>>, greater<pair<ll, pll>>> plpllgreaterq;
typedef priority_queue<vi, vector<vi>, greater<vi>> vigreaterq;
typedef priority_queue<vl, vector<vl>, greater<vl >> vlgreaterq;
template <class o,class p,class q>
using tuple3q = priority_queue<tuple<o, p, q>, vector<tuple<o, p, q>>, greater<tuple<o,p,q>>> ;
template <class o, class p, class q, class r>
using tuple4q = priority_queue<tuple<o, p, q,r>, vector<tuple<o, p, q,r>>, greater<tuple<o, p, q,r>>>;
int dx[] = { -1,0,1,0 };
int dy[] = { 0,-1,0,1 };
#define bit(x,v) ((ll)x << v)
#define rep(x,n) for(ll x = 0;x < n;x++)
#define rep2(x,f,v) for(ll x=f;x<v;x++)
#define repe(v,x) for(auto v : x)
// 許容する誤差ε
#define EPS (1e-10)
// 2つのスカラーが等しいかどうか
#define EQ(a,b) (std::abs(a-b) < EPS)
// 2つのベクトルが等しいかどうか
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define all(a) a.begin(),a.end()
#define all0(a) memset(a,0,sizeof(a))
#define allm1(a) memset(a,-1,sizeof(a))
#define put_float(v) 	cout << fixed << setprecision(10); \
						cout << v << endl
#define put(v) cout << v << endl
#define vinsert(v,p,x) v.insert(v.begin() + p,x)
#define vsort(v) sort(all(v));
#define dup(v) v.erase(unique(all(v)),v.end())
#define ion(i,j) ((i & (1LL << j)) > 0)
#define next(i) i++;i%=2
#define Len size()
#define ull unsignd long long
#define psp(a,b) push_back(make_pair(a,b))
#define psp2(a,b) push(make_pair(a,b))
#define cini(a) a; cin >> a
#define infa(a,b) (a + b) % INF
#define infm(a,b) (a * b) % INF
#define infd(a,b) (a * modinv(b)) % INF
#define infs(a,b) (a + INF - b) % INF
#define inf(a) (a) %= INF
#define inff(a) ((a) % INF)
#define No cout << "No" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define YES cout << "YES" << endl
#define smal -INF*INF
#define big INF*INF
const ll INF = 1000000007;
const int MAX = 2000010;
const int MOD = 1000000007;

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

ll getpow(ll b, ll x, ll md) {
	ll t = b;
	ll res = 1;
	while (x > 0)
	{
		if (x & 1) {
			res *= t;
			res %= md;
		}
		x >>= 1;
		t *= t;
		t %= md;
	}
	return res;
}
ll getpow(ll b, ll x) {

	return getpow(b, x, INF);
}
ll modinv(ll x) {
	return getpow(x, INF - 2);
}
ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

class mint {
	int md = 1000000007;
public:
	long long x;
	mint(ll x, ll md) {
		this->md = md;
		this->x = (x % md + md) % md;
	}
	mint(long long x = 0) : x((x% md + md) % md) {}
	mint operator-() const {
		return mint(-x);
	}
	mint& operator+=(const mint& a) {
		if ((x += a.x) >= md) x -= md;
		return *this;
	}
	mint& operator-=(const mint& a) {
		if ((x += md - a.x) >= md) x -= md;
		return *this;
	}
	mint& operator*=(const  mint& a) {
		(x *= a.x) %= md;
		return *this;
	}
	mint operator+(const mint& a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint& a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint& a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}
	// for prime mod
	mint inv() const {
		return pow(md - 2);
	}
	mint& operator/=(const mint& a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint& a) const {
		mint res(*this);
		return res /= a;
	}

	friend ostream& operator<<(ostream& os, const mint& m) {
		os << m.x;
		return os;
	}
};

int pr[100010];
int lank[100010];
void uini(int n) {
	for (size_t i = 0; i <= n; i++)
	{
		pr[i] = i;
		lank[i] = 1;
	}
}

int parent(int x) {
	if (x == pr[x]) return x;
	return pr[x] = parent(pr[x]);
}

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

bool unit(int x, int y) {

	int px = parent(x);
	int py = parent(y);

	if (px == py) return false;
	if (lank[py] < lank[px]) {
		pr[py] = px;
		lank[px] += lank[py];
	}
	else {
		pr[px] = py;
		lank[py] += lank[px];
	}
	return true;
}

ll merge(ll* a, int left, int mid, int right) {
	ll n1 = mid - left;
	ll n2 = right - mid;
	vector<int> L(n1 + 1);
	vector<int> R(n2 + 1);
	for (size_t i = 0; i < n1; i++)
	{
		L[i] = a[left + i];
	}
	for (size_t i = 0; i < n2; i++)
	{
		R[i] = a[mid + i];
	}

	L[n1] = INF;
	R[n2] = INF;
	ll i = 0;
	ll j = 0;
	ll r = 0;
	for (size_t k = left; k < right; k++)
	{
		if (L[i] <= R[j]) {
			a[k] = L[i];
			i++;
		}
		else {
			a[k] = R[j];
			r += n1 - i;
			j++;
		}
	}
	return r;
}
ll merge2(pair<int, char>* a, int left, int mid, int right) {
	ll n1 = mid - left;
	ll n2 = right - mid;
	vector<pair<int, char>> L(n1 + 1);
	vector<pair<int, char>> R(n2 + 1);
	for (size_t i = 0; i < n1; i++)
	{
		L[i] = a[left + i];
	}
	for (size_t i = 0; i < n2; i++)
	{
		R[i] = a[mid + i];
	}

	L[n1] = make_pair(INF, ' ');
	R[n2] = make_pair(INF, ' ');
	ll i = 0;
	ll j = 0;
	ll r = 0;
	for (size_t k = left; k < right; k++)
	{
		if (L[i].first <= R[j].first) {
			a[k] = L[i];
			i++;
		}
		else {
			a[k] = R[j];
			r += n1 - i;
			j++;
		}
	}
	return r;
}
ll mergeSort2(pair<int, char>* a, int left, int right) {
	ll res = 0;
	if (left + 1 < right) {
		int mid = (left + right) / 2;
		res = mergeSort2(a, left, mid);
		res += mergeSort2(a, mid, right);
		res += merge2(a, left, mid, right);
	}
	return res;
}
ll mergeSort(ll* a, int left, int right) {
	ll res = 0;
	if (left + 1 < right) {
		int mid = (left + right) / 2;
		res = mergeSort(a, left, mid);
		res += mergeSort(a, mid, right);
		res += merge(a, left, mid, right);
	}
	return res;
}
int partition(pair<int, char>* a, int p, int r) {
	pair<int, char> x = a[r];
	int i = p - 1;
	for (size_t j = p; j < r; j++)
	{
		if (a[j].first <= x.first) {
			i++;
			swap(a[i], a[j]);
		}
	}
	swap(a[i + 1], a[r]);
	return i + 1;
}
void quick(pair<int, char>* a, int p, int r) {
	if (p < r) {
		int q = partition(a, p, r);
		quick(a, p, q - 1);
		quick(a, q + 1, r);
	}
}

ll n;
int ci = 0;
ll P[1000010];
struct Node {
	int key;
	int priority;
	Node* parent, * left, * right;
	Node(int key, int priority);
	Node() {}
};
Node NIL;
Node::Node(int key, int priority) : key(key), priority(priority) {
	left = &NIL;
	right = &NIL;
}
Node* root = new Node();
void cenrec(Node* k) {
	if (k->key == NIL.key) return;
	cenrec(k->left);
	cout << " " << k->key;
	cenrec(k->right);
}
void fastrec(Node* k)
{
	if (k->key == NIL.key) return;
	cout << " " << k->key;
	fastrec(k->left);
	fastrec(k->right);
}
void insert(Node* v) {
	Node* y = &NIL;
	Node* x = root;
	while (x->key != NIL.key)
	{
		y = x;
		if (v->key < x->key) {
			x = x->left;
		}
		else {
			x = x->right;
		}
	}
	v->parent = y;
	if (y->key == NIL.key) {
		root = v;
	}
	else if (v->key < y->key) {
		y->left = v;
	}
	else {
		y->right = v;
	}

}

Node* find(Node* k, ll v)
{
	if (k->key == NIL.key) return &NIL;
	if (k->key == v) return k;
	if (v < k->key) return find(k->left, v);
	return find(k->right, v);
}
void delp12(Node* x) {
	if (x->key == NIL.key)  return;
	Node* l = x->left;
	Node* r = x->right;
	Node* pr = x->parent;

	if (l->key == NIL.key
		&& r->key == NIL.key) {
		if (pr->left == x) {
			pr->left = &NIL;
		}
		else pr->right = &NIL;
	}
	else if (l->key != NIL.key) {
		if (pr->left == x) {
			pr->left = l;
		}
		else pr->right = l;
		l->parent = pr;
	}
	else if (r->key != NIL.key) {
		if (pr->left == x) {
			pr->left = r;
		}
		else pr->right = r;
		r->parent = pr;
	}
}
Node* get_next(Node* k) {
	if (k->key == NIL.key) return &NIL;
	Node* res = get_next(k->left);
	if (res->key != NIL.key) return res;
	return k;
}
void del(Node* x) {

	if (x->key == NIL.key) return;
	Node* l = x->left;
	Node* r = x->right;
	Node* pr = x->parent;

	if (l->key != NIL.key && r->key != NIL.key) {
		Node* nex = get_next(r);
		x->key = nex->key;
		delp12(nex);
	}
	else {
		delp12(x);
	}
}
Node* rightRotate(Node* t) {
	Node* s = t->left;
	t->left = s->right;
	s->right = t;
	return s;
}
Node* leftRotate(Node* t) {
	Node* s = t->right;
	t->right = s->left;
	s->left = t;
	return s;
}
Node* _insert(Node* t, int key, int priority) {
	if (t->key == NIL.key) {
		return new Node(key, priority);
	}
	if (key == t->key) {
		return t;
	}

	if (key < t->key) {
		t->left = _insert(t->left, key, priority);
		if (t->priority < t->left->priority) {
			t = rightRotate(t);
		}
	}
	else {
		t->right = _insert(t->right, key, priority);
		if (t->priority < t->right->priority) {
			t = leftRotate(t);
		}
	}
	return t;
}
Node* delete1(Node* t, int key);
Node* _delete(Node* t, int key) {
	if (t->left->key == NIL.key && t->right->key == NIL.key) {
		return &NIL;
	}
	else if (t->left->key == NIL.key) {
		t = leftRotate(t);
	}
	else if (t->right->key == NIL.key) {
		t = rightRotate(t);
	}
	else
	{
		if (t->left->priority > t->right->priority) {
			t = rightRotate(t);
		}
		else
			t = leftRotate(t);
	}
	return delete1(t, key);
}
Node* delete1(Node* t, int key) {
	if (t->key == NIL.key) {
		return &NIL;
	}
	if (key < t->key) {
		t->left = delete1(t->left, key);
	}
	else if (key > t->key) {
		t->right = delete1(t->right, key);
	}
	else return _delete(t, key);
	return t;
}
int H;
int left(int i) {
	return i * 2 + 1;
}
int right(int i) {
	return i * 2 + 2;
}
struct edge {
	int from, to;
	ll val;
	edge(int from, int to, ll val) : from(from), to(to), val(val) {}
};
ll k;
int _rank[1010];
int temp[1010];
bool compare_sa(int i, int j) {
	if (_rank[i] != _rank[j]) return _rank[i] < _rank[j];
	else {
		int ri = i + k <= n ? _rank[i + k] : -1;
		int rj = j + k <= n ? _rank[j + k] : -1;
		return ri < rj;
	}
}
void construct_sa(string S, int* sa) {
	n = S.length();

	for (size_t i = 0; i <= n; i++)
	{
		sa[i] = i;
		_rank[i] = i < n ? S[i] : -1;
	}

	for (k = 1; k <= n; k *= 2)
	{
		sort(sa, sa + n + 1, compare_sa);

		// saはソート後の接尾辞の並びになっている、rankは元の位置のindexを保持したまま、更新されている。
		// ピンとこなかった部分
		temp[sa[0]] = 0;
		for (size_t i = 1; i <= n; i++)
		{
			temp[sa[i]] = temp[sa[i - 1]] + (compare_sa(sa[i - 1], sa[i]) ? 1 : 0);
		}
		for (size_t i = 0; i <= n; i++)
		{
			_rank[i] = temp[i];
		}
	}
}
bool contain(string S, int* sa, string T) {
	int a = 0, b = S.length();
	// sa は 接尾辞が辞書順に並んでいる、入っているのはその位置のインデックス
	while (b - a > 1) {
		int c = (a + b) / 2;
		if (S.compare(sa[c], T.length(), T) < 0) a = c;
		else b = c;
	}
	return S.compare(sa[b], T.length(), T) == 0;
}

#define bit(x,v) ((ll)x << v)

class BIT {

	static const int MAX_N = 500010;
public:
	BIT() { memset(bit, 0, sizeof(bit)); }
	ll bit[MAX_N + 1], n;
	ll sum(int i) {
		ll s = 0;
		while (i > 0)
		{
			s += bit[i];
			i -= i & -i;
		}
		return s;
	}

	void add(int i, int x) {
		while (i <= n)
		{
			bit[i] += x;
			i += i & -i;
		}
	}

};
struct UnionFind {
	vector<int> A;
	UnionFind(int n) : A(n, -1) {}
	int find(int x) {
		if (A[x] < 0) return x;
		return A[x] = find(A[x]);
	}
	void unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y) return;
		if (A[x] > A[y]) swap(x, y);
		A[x] += A[y];
		A[y] = x;
	}
	int ngroups() {
		int ans = 0;
		for (auto a : A) if (a < 0) ans++;
		return ans;
	}
};
void yes() { cout << "Yes\n"; exit(0); }
void no() { cout << "No\n"; exit(0); }
vector<ll> getp(ll n) {

	vector<ll> res;
	ll a = 2;
	if (n % 2 == 0) {
		res.push_back(2);
		while (n % 2 == 0)n /= 2;
	}

	for (ll i = 3; i * i <= n; i += 2)
	{
		if (n % i == 0) {
			res.push_back(i);
			while (n % i == 0)n /= i;
		}
	}
	if (n != 1) res.push_back(n);
	return res;
}
vector<ll> getp2(ll n) {

	vector<ll> res;
	ll a = 2;
	if (n % 2 == 0) {

		while (n % 2 == 0) { n /= 2; res.push_back(2); }
	}

	for (ll i = 3; i * i <= n; i += 2)
	{
		if (n % i == 0) {

			while (n % i == 0) { n /= i; res.push_back(i); }
		}
	}
	if (n != 1) res.push_back(n);
	return res;
}
vector<pll> getp3(ll n) {
	vector<pll> res;
	ll a = 2;
	int si = 0;
	if (n % 2 == 0) {

		res.push_back(make_pair(2, 0));
		while (n % 2 == 0) { n /= 2; res[si].second++; }
		si++;
	}

	for (ll i = 3; i * i <= n; i += 2)
	{
		if (n % i == 0) {
			res.push_back(make_pair(i, 0));
			while (n % i == 0) { n /= i; res[si].second++; }
			si++;
		}
	}
	if (n != 1) { res.push_back(make_pair(n, 1)); }
	return res;
}

vector<ll> getDivisors(ll n) {

	vector<ll> res;
	ll a = 2;
	res.push_back(1);
	for (ll i = 2; i * i <= n; i++)
	{

		if (n % i == 0) {
			res.push_back(i);
			if (n / i != i)
				res.push_back(n / i);
		}
	}
	return res;
}

struct ve {
public:
	vector<ve> child;
	int _t = INF;
	ve(int t) :_t(t) {}
	ve(ve _left, ve _right) {
		_t = _left._t + _right._t;
		child.push_back(_left);
		child.push_back(_right);
	}
	bool operator<(const ve& t) const {
		return _t > t._t;
	}
};

vector<bool> elas(ll n) {
	vector<bool> r(n);
	fill(r.begin(), r.end(), 1);
	r[0] = 0;
	r[1] = 0;
	for (ll i = 2; i * i < n; i++)
	{
		if (!r[i]) continue;
		ll ti = i * 2;
		while (ti < n)
		{
			r[ti] = false;
			ti += i;
		}
	}
	return r;
}
bool isPrime(ll v) {
	for (ll i = 2; i * i <= v; i++)
	{
		if (v % i == 0) return false;
	}
	return true;
}

class SegTree {

public:
	const static int MAX_N = 100010;
	const static int DAT_SIZE = (1 << 18) - 1;
	int N, Q;
	int A[MAX_N];

	ll data[DAT_SIZE], datb[DAT_SIZE];
	void init(int _n) {
		N = 1;
		while (N < _n) N <<= 1;
		memset(data, 0, sizeof(data));
		memset(datb, 0, sizeof(datb));
	}
	void init(int _n, ll iv) {
		N = 1;
		while (N < _n) N <<= 1;
		rep(i, DAT_SIZE) {
			data[i] = iv;
			datb[i] = iv;
		}
	}
	void add(int a, int b, int x) {
		add(a, b + 1, x, 0, 0, N);
	}
	void add(int a, int b, int x, int k, int l, int r) {
		if (a <= l && r <= b) {
			data[k] += x;
		}
		else if (l < b && a < r) {
			datb[k] += (min(b, r) - max(a, l)) * x;
			add(a, b, x, k * 2 + 1, l, (l + r) / 2);
			add(a, b, x, k * 2 + 2, (l + r) / 2, r);
		}
	}

	void change(int a, int b, int x) {
		change(a, b + 1, x, 0, 0, N);
	}
	void change(int a, int b, int x, int k, int l, int r) {
		if (a <= l && r <= b) {
			data[k] = x;
		}
		else if (l < b && a < r) {
			datb[k] = x;
			change(a, b, x, k * 2 + 1, l, (l + r) / 2);
			change(a, b, x, k * 2 + 2, (l + r) / 2, r);
		}
	}

	ll sum(int a, int b) {
		return sum(a, b + 1, 0, 0, N);
	}
	ll sum(int a, int b, int k, int l, int r) {
		if (b <= l || r <= a) {
			return 0;
		}
		if (a <= l && r <= b) {
			return data[k] * (r - l) + datb[k];
		}

		ll res = (min(b, r) - max(a, l)) * data[k];
		res += sum(a, b, k * 2 + 1, l, (l + r) / 2);
		res += sum(a, b, k * 2 + 2, (l + r) / 2, r);
		return res;
	}
};
class Segment;
class Circle;

class Point {
public:
	double x, y;

	Point(double x = 0, double y = 0) :x(x), y(y) {}

	Point operator + (Point p) { return Point(x + p.x, y + p.y); }
	Point operator - (Point p) { return Point(x - p.x, y - p.y); }
	Point operator * (double a) { return Point(a * x, a * y); }
	Point operator / (double a) { return Point(x / a, y / a); }

	double abs() { return sqrt(norm()); }
	double norm() { return x * x + y * y; }

	bool operator < (const Point& p)const {
		return x != p.x ? x < p.x : y < p.y;
	}
	bool operator == (const Point& p) const {
		return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;
	}
	static double dot(Point a, Point b) {
		return a.x * b.x + a.y * b.y;
	}
	static double cross(Point a, Point b) {
		return a.x * b.y - a.y * b.x;
	}
	static bool isOrthogonal(Point a, Point b) {
		return EQ(dot(a, b), 0.0);
	}
	static bool isOrthogonal(Point a1, Point a2, Point b1, Point b2) {
		return isOrthogonal(a1 - a2, b1 - b2);
	}
	static bool isOrthogonal(Segment s1, Segment s2);

	static bool isPalallel(Point a, Point b) {
		return EQ(cross(a, b), 0.0);
	}
	static bool isPalallel(Point a1, Point a2, Point b1, Point b2) {
		return isPalallel(a1 - a2, b1 - b2);
	}
	static bool isPalallel(Segment s1, Segment s2);

	static const int COUNTER_CLOCKWISE = 1;
	static const int CLOCKWISE = -1;
	static const int ONLINE_BACK = 2;
	static const int ONLINE_FRONT = -2;
	static const int ON_SEGMENT = 0;
	static int ccw(Point p0, Point p1, Point p2) {
		// 線分はp0とp1でp2がどこにあるかを探る
		Point a = p1 - p0;
		Point b = p2 - p0;
		if (cross(a, b) > EPS) return COUNTER_CLOCKWISE;
		if (cross(a, b) < -EPS) return CLOCKWISE;
		if (dot(a, b) < -EPS) return ONLINE_BACK;
		if (a.norm() < b.norm()) return ONLINE_FRONT;
		return ON_SEGMENT;
	}

	static bool intersect(Point p1, Point p2, Point p3, Point p4) {
		return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0
			&& ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);
	}
	static bool intersect(Segment s1, Segment s2);
	static Point project(Segment s, Point p);

	static Point reflect(Segment s, Point p);

	static Point getDistance(Point a, Point b) {
		return (a - b).abs();
	}

	static double getDistanceLP(Segment s, Point p);

	static double getDistanceSP(Segment s, Point p);

	static double getDistance(Segment s1, Segment s2);

	static Point getIntersection(Segment s1, Segment s2);

	static pair<Point, Point> crossPoints(Circle c, Segment s);

	static int contains(vector<Point> g, Point p) {
		int n = g.size();
		bool x = false;
		rep(i, n) {
			Point a = g[i] - p, b = g[(i + 1) % n] - p;
			// 線の上に載っているか
			if (std::abs(cross(a, b)) < EPS && dot(a, b) < EPS) return 1;

			// pを基準として上下にあるか
			// または外積が正か?(→にあるか)
			if (a.y > b.y) swap(a, b);
			if (a.y < EPS && EPS < b.y && cross(a, b) > EPS) x = !x;
		}
		return x ? 2 : 0;
	}

	static vector<Point> andrewScan(vector<Point> s) {
		vector<Point> u, l;
		if (s.size() < 3) return s;
		sort(all(s));
		u.push_back(s[0]);
		u.push_back(s[1]);
		l.push_back(s[s.size() - 1]);
		l.push_back(s[s.size() - 2]);

		for (int i = 2; i < s.size(); i++) {

			for (int _n = u.size(); _n >= 2 && ccw(u[_n - 2], u[_n - 1], s[i]) > CLOCKWISE; _n--) {
				u.pop_back();
			}
			u.push_back(s[i]);
		}

		for (int i = s.size() - 3; i >= 0; i--) {

			for (int _n = l.size(); _n >= 2 && ccw(l[_n - 2], l[_n - 1], s[i]) > CLOCKWISE; _n--) {
				l.pop_back();
			}
			l.push_back(s[i]);
		}

		reverse(all(l));
		for (int i = u.size() - 2; i >= 1; i--)
		{
			l.push_back(u[i]);
		}

		return l;
	}
	void get_cin() {
		cin >> x >> y;
	}
};

class Segment {
public:
	Point p1, p2;
	Segment() {}
	Segment(Point p1, Point p2) :p1(p1), p2(p2) {}
	void get_cin() {
		cin >> p1.x >> p1.y >> p2.x >> p2.y;
	}
	Point p1tp2() {
		return p2 - p1;
	}
	Point p2tp1() {
		return p1 - p2;
	}
	double abs() {
		return std::abs(norm());
	}
	double norm() {
		return (p2 - p1).norm();
	}
};

bool Point::isOrthogonal(Segment s1, Segment s2) {
	return EQ(dot(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0);
}
bool Point::isPalallel(Segment s1, Segment s2) {
	return EQ(cross(s1.p2 - s1.p1, s2.p2 - s2.p1), 0.0);
}
bool Point::intersect(Segment s1, Segment s2) {
	return intersect(s1.p1, s1.p2, s2.p1, s2.p2);
}
Point Point::project(Segment s, Point p) {
	Point base = s.p2 - s.p1;
	double r = Point::dot(p - s.p1, base) / base.norm();
	return s.p1 + base * r;
}
Point Point::reflect(Segment s, Point p) {
	return (project(s, p) * 2) - p;
}
double Point::getDistanceLP(Segment s, Point p) {
	return std::abs(cross(s.p2 - s.p1, p - s.p1) / (s.p2 - s.p1).abs());
}
double Point::getDistanceSP(Segment s, Point p) {
	if (dot(s.p2 - s.p1, p - s.p1) < 0.0) return (p - s.p1).abs();
	if (dot(s.p1 - s.p2, p - s.p2) < 0.0) return (p - s.p2).abs();
	return getDistanceLP(s, p);
}
double Point::getDistance(Segment s1, Segment s2) {
	if (intersect(s1, s2)) return 0.0;
	return min({ getDistanceSP(s1,s2.p1),getDistanceSP(s1,s2.p2)
		,getDistanceSP(s2,s1.p1),getDistanceSP(s2,s1.p2) });
}

Point Point::getIntersection(Segment s1, Segment s2) {
	// (s1.p1 - s2.p1).norm()
	auto bs = s1.p2 - s1.p1;
	auto n1 = s2.p1 - s1.p1;
	auto n2 = s2.p2 - s1.p1;
	auto c1 = std::abs(cross(n1, bs)) / bs.norm();
	auto c2 = std::abs(cross(n2, bs)) / bs.norm();
	return s2.p1 + (s2.p2 - s2.p1) * (c1 / (c1 + c2));
	// c1:c2=t:1-t
	// c2t=(1-t)c1
	// t/(1-t)=c1/(c1+c2)
	//
}

double arg(Point p) { return atan2(p.y, p.x); }
Point polar(double a, double r) { return Point(cos(r) * a, sin(r) * a); }
class Circle {
public:
	Point c;
	double r;
	Circle(Point c = Point(), double r = 0.0) : c(c), r(r) {}
	void get_cin() {
		cin >> c.x >> c.y >> r;
	}
	static pair<Point, Point> getCrossPoints(Circle c1, Circle c2) {
		double d = (c1.c - c2.c).abs(); // 中心点どうしの距離
		double a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (2 * c1.r * d));
		double t = arg(c2.c - c1.c);
		return make_pair(c1.c + polar(c1.r, t + a), c1.c + polar(c1.r, t - a));

	}
};

pair<Point, Point> Point::crossPoints(Circle c, Segment s) {
	auto pp = project(s, c.c);
	auto f = (pp - c.c).norm();
	auto mu = sqrt(c.r * c.r - f);
	auto e = s.p1tp2() / s.p1tp2().abs();
	return make_pair(pp + e * mu, pp - e * mu);

}

ll divRm(string s, ll x) {

	ll r = 0;
	for (ll i = 0; i < s.size(); i++)
	{
		r *= 10;
		r += s[i] - '0';
		r %= x;
	}
	return r;
}
ll cmbi(ll x, ll b) {

	ll res = 1;
	for (size_t i = 0; i < b; i++)
	{
		res *= x - i;
		res %= INF;
		res *= inv[b - i];
		res %= INF;
	}
	return res;
}

double digsum(ll x) {
	ll res = 0;
	while (x > 0)
	{
		res += x % 10;
		x /= 10;
	}
	return res;
}
bool check_parindrome(string s) {
	int n = s.size();
	rep(i, n / 2) {
		if (s[i] != s[n - i - 1]) {
			return false;
		}

	}
	return true;
}
ll c[100010], d[100010];

vector<pll> es[100010];
vector<pll> coi[100010]; // 深さ、長さで保存
ll depth[100010];
ll dp[20][100010];
ll dis[100010];
void constr(ll x, ll dep,ll par) {
	dp[0][x] = par;
	depth[x] = dep;
	for (auto v : es[x]) {
		if (depth[v.first] != -2) continue;
		coi[c[v.second]].psp(dep, d[v.second]);
		dis[v.first] = dis[x] + d[v.second];
		constr(v.first, dep + 1,x);
	}
}
void eulerTour() {

}
void abc133f() {
	ll q;
	cin >> n >> q;
	rep(i, n - 1) {
		ll a, b;
		cin >> a >> b >> c[i] >> d[i];
		es[a].psp(b, i);
		es[b].psp(a, i);
	}
	fill(depth, depth + n + 1, -2);
	constr(1, 0, 0);

	rep2(i, 1, 20) {
		rep(j, n) {
			if (dp[i - 1][j] == 0) {
				dp[i][j] = 0;
			}
			else {
				dp[i][j] = dp[i - 1][dp[i - 1][j]];
			}
		}
	}
	ll lca[100010];

	rep(i, q) {
		ll x, y, u, v;
		cin >> x >> y >> u >> v;
		if (depth[u] > depth[v]) {
			swap(u, v);
		}

		ll rm = depth[v] - depth[u];
		ll tv = v;
		ll tu = u;
		int j = 0;
		while (rm > 0)
		{
			if (rm & 1) {
				tv = dp[j][tv];
			}
			j++;
			rm >>= 1;
		}
		vector<pll> color = coi[x];

		for (int j = 19; j >= 0; j--)
		{
			if (dp[j][tu] == dp[j][tv])
				continue;
			tu = dp[j][tu];
			tv = dp[j][tv];
		}
		lca[i] = tv;
	}

}
void solv() {
	cin >> n;
	ll p = n % 10;
	if (p == 3) {
		cout << "bon" << endl;
	}
	else if(p == 0 || p == 1 || p == 6 || p == 8){
		cout << "pon" << endl;
	}
	else {
		cout << "hon" << endl;
	}

}

int main() {
	//COMinit();
	solv();
	return 0;
}