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

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

    if(S.size() <= N){
        cout << S << endl;
        return 0;
    } else if(N < S.size()) {
        for(int i = S.size()-1; i > N-1; i--) S[i] = '\0';
        cout << S << "..." << endl;
        return 0;
    }
}

Score: 200 points

Problem Statement

We have a string S consisting of lowercase English letters.

If the length of S is at most K, print S without change.

If the length of S exceeds K, extract the first K characters in S, append ... to the end of them, and print the result.

Constraints

K is an integer between 1 and 100 (inclusive).

S is a string consisting of lowercase English letters.

The length of S is between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

K
S

Output

Print a string as stated in Problem Statement.

Sample Input 1

7
nikoandsolstice

Sample Output 1

nikoand...

nikoandsolstice has a length of 15, which exceeds K=7.

We should extract the first 7 characters in this string, append ... to the end of them, and print the result nikoand....

Sample Input 2

40
ferelibenterhominesidquodvoluntcredunt

Sample Output 2

ferelibenterhominesidquodvoluntcredunt

The famous quote from Gaius Julius Caesar.


#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <map>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using ll = long long;
using namespace std;
int main()
{
	string s;
	int k;
	cin >> k >> s;
	if (s.size() <= k)	cout << s << endl;
	else {
		cout << s.substr(0, k) << "..." << endl;
	}
	return 0;
}

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

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>
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
    float a,b,h,m;
    cin>> a >> b >> h >> m;
    float p;
    p=22*(h+m/60)/42-(22*m)/210;

    float c;
    c=a*a+b*b-2*a*b*cos(p);

    cout << fixed << setprecision(20)<< sqrt(c);

    return 0;

}

Score: 300 points

Problem Statement

Consider an analog clock whose hour and minute hands are A and B centimeters long, respectively.

An endpoint of the hour hand and an endpoint of the minute hand are fixed at the same point, around which each hand rotates clockwise at constant angular velocity. It takes the hour and minute hands 12 hours and 1 hour to make one full rotation, respectively.

At 0 o'clock, the two hands overlap each other. H hours and M minutes later, what is the distance in centimeters between the unfixed endpoints of the hands?

Constraints

All values in input are integers.

1 \leq A, B \leq 1000

0 \leq H \leq 11

0 \leq M \leq 59

Input

Input is given from Standard Input in the following format:

A B H M

Output

Print the answer without units. Your output will be accepted when its absolute or relative error from the correct value is at most 10^{-9}.

Sample Input 1

3 4 9 0

Sample Output 1

5.00000000000000000000

The two hands will be in the positions shown in the figure below, so the answer is 5 centimeters.

Sample Input 2

3 4 10 40

Sample Output 2

4.56425719433005567605

The two hands will be in the positions shown in the figure below. Note that each hand always rotates at constant angular velocity.


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

int main() {
  int a,b,h,m;
  cin >> a >> b >> h >> m;

  cout << fixed <<setprecision(20);

  if (h*30 == m*6) {
    cout << a-b <<endl;
  }
  else if (h*30-m*6 == 180 || m*6-h*30 == 180) {
    cout << a+b <<endl;
  }
  else {
    double deg = h*30-m*6;
    double pi,unit,deg_r;
    pi = 2.0* asin(1.0);
    unit = pi / 180.0;
    deg_r = deg*unit;
    double x;
    x = cos(deg_r);
    double ans;
    ans = sqrt(a*a + b*b - 2*a*b*x);
    cout << ans <<endl;
  }
}


#pragma GCC optimize("Ofast")
#include<stdio.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
vector<int> g[100005];
queue<int> que;
int from[100005]={0};
int main(){
	int i,f,t,n,m;
	scanf("%d%d",&n,&m);
	for(i=0;i<m;i++){
		scanf("%d%d",&f,&t);
		g[f].emplace_back(t);
		g[t].emplace_back(f);
	}
	from[1]=1;
	que.push(1);
	while(que.size()){
		int v=que.front();
		que.pop();
		for(auto x:g[v]){
			if(!from[x]){
				from[x]=v;
				que.push(x);
			}
		}
	}
	for(i=1;i<=n;i++)
		if(!from[i]){
			puts("NO");
			return 0;
		}
	for(i=2;i<=n;i++)
		printf("%d\n",from[i]);
}

Score: 400 points

Problem Statement

There is a cave.

The cave has N rooms and M passages. The rooms are numbered 1 to N, and the passages are numbered 1 to M. Passage i connects Room A_i and Room B_i bidirectionally. One can travel between any two rooms by traversing passages. Room 1 is a special room with an entrance from the outside.

It is dark in the cave, so we have decided to place a signpost in each room except Room 1. The signpost in each room will point to one of the rooms directly connected to that room with a passage.

Since it is dangerous in the cave, our objective is to satisfy the condition below for each room except Room 1.

If you start in that room and repeatedly move to the room indicated by the signpost in the room you are in, you will reach Room 1 after traversing the minimum number of passages possible.

Determine whether there is a way to place signposts satisfying our objective, and print one such way if it exists.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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

1 \leq A_i, B_i \leq N\ (1 \leq i \leq M)

A_i \neq B_i\ (1 \leq i \leq M)

One can travel between any two rooms by traversing passages.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

If there is no way to place signposts satisfying the objective, print No.

Otherwise, print N lines. The first line should contain Yes, and the i-th line (2 \leq i \leq N) should contain the integer representing the room indicated by the signpost in Room i.

Sample Input 1

4 4
1 2
2 3
3 4
4 2

Sample Output 1

Yes
1
2
2

If we place the signposts as described in the sample output, the following happens:

Starting in Room 2, you will reach Room 1 after traversing one passage: (2) \to 1. This is the minimum number of passages possible.

Starting in Room 3, you will reach Room 1 after traversing two passages: (3) \to 2 \to 1. This is the minimum number of passages possible.

Starting in Room 4, you will reach Room 1 after traversing two passages: (4) \to 2 \to 1. This is the minimum number of passages possible.

Thus, the objective is satisfied.

Sample Input 2

6 9
3 4
6 1
2 4
5 3
4 6
1 5
6 2
4 5
5 6

Sample Output 2

Yes
6
5
5
1
1

If there are multiple solutions, any of them will be accepted.


#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<queue>

using namespace std;

int main() {

	int64_t N, M;

	cin >> N >> M;

	vector<vector<int64_t>> root(N, vector<int64_t>(N));

	for (int64_t i = 0; i < M; i++) {
		int64_t a, b;
		cin >> a >> b;
		root[a - 1][b - 1] = 1;
		root[b - 1][a - 1] = 1;
	}

	queue<uint64_t> a;
	a.push(0);
	vector<int64_t> check(N);
	vector<int64_t> arrange(N);
	check[0] = 1;
	int64_t l;
	int64_t count = 0;
	while (a.size() != 0) {

		l = a.front();

		for (int64_t i = 0; i < N; i++) {

			if (root[l][i] == 1 && check[i] == 0) {
				arrange[i] = l;
				a.push(i);
				check[i] = 1;
				count++;

			}
			if (count == N - 1) {
				break;
			}

		}

		if (count == N - 1) {
			break;
		}
		a.pop();

	}

	for(int64_t i = 0; i < N; i++) {
		if (check[i] == 0) {
			cout << "No";
			return 0;
		}
	}
	cout << "Yes" << endl;
	for (int64_t i = 1; i < N; i++) {
		cout << arrange[i] + 1 << endl;
	}

}


#pragma region Macros
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i, n) for(int i=(n); i>=0; --i)
#define FOR(i, n, m) for(int i=(m), i##_len=(n); i<i##_len; ++i)
#define EACH(i, v) for(const auto& i : v)
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<class Value>using vec = vector< Value >;
using ll = long long;
using P = pair< long long, long long >;
#define fi first
#define se second
#define el endl
#pragma endregion

const string YES = "Yes";

void Main();
void solve(long long N, long long M, std::vector<long long> A, std::vector<long long> B);

int main(){
    std::cin.tie(nullptr);
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}

void Main(){
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> A(M);
    std::vector<long long> B(M);
    for(int i = 0 ; i < M ; i++){
        scanf("%lld",&A[i]);
        scanf("%lld",&B[i]);
    }
    solve(N, M, std::move(A), std::move(B));
}

#pragma region bfs
#define BFSMAP vector<vector<BFSOBJ> >
struct BFSOBJ{
    ll x;
    ll y;
    vector<BFSOBJ*> next;
    bool is(const BFSOBJ* obj){
        return x==obj->x&&y==obj->y;
    }
    void set4(BFSMAP& ob, int i=-1, int j=-1){
        if(j<0||i<0){
            i=x;
            j=y;
        }
        int H=ob.size();
        int W=ob[0].size();
        if(i-1>=0) next.push_back(&ob[i-1][j]);
        if(j-1>=0) next.push_back(&ob[i][j-1]);
        if(i+1<H) next.push_back(&ob[i+1][j]);
        if(j+1<W) next.push_back(&ob[i][j+1]);
    }
    void set8(BFSMAP& ob, int i=-1, int j=-1){
        if(j<0||i<0){
            i=x;
            j=y;
        }
        int H=ob.size();
        int W=ob[0].size();
        bool n1, n2, n3, n4;
        if(n1=i-1>=0) next.push_back(&ob[i-1][j]);
        if(n2=j-1>=0) next.push_back(&ob[i][j-1]);
        if(n3=i+1<H) next.push_back(&ob[i+1][j]);
        if(n4=j+1<W) next.push_back(&ob[i][j+1]);
        if(n1&&n2) next.push_back(&ob[i-1][j-1]);
        if(n2&&n3) next.push_back(&ob[i+1][j-1]);
        if(n3&&n4) next.push_back(&ob[i+1][j+1]);
        if(n4&&n1) next.push_back(&ob[i-1][j+1]);
    }
};

ll coun[10005];
map<BFSOBJ*, int> bfs(BFSOBJ* start, BFSOBJ* end){
    queue<BFSOBJ*> q;
    map<BFSOBJ*, int> depth;
    q.push(start);
    depth[start] = 0;
    while(!q.empty()){
        BFSOBJ* now = q.front(); q.pop();
        for(const auto& next : now->next){
            if(depth.end() == depth.find(next)){
                depth[next] = depth[now] + 1;
                coun[next->x] = now->x;
                q.push(next);
            }
        }
    }
    return depth;
}
#pragma endregion

void solve(long long N, long long M, std::vector<long long> A, std::vector<long long> B){
    vec<BFSOBJ> G(N);
    fill(coun, coun+N+100, -1);
    REP(i, M){
        A[i]--;B[i]--;
        G[A[i]].x = A[i];
        G[B[i]].x = B[i];
        G[A[i]].next.push_back(&G[B[i]]);
        G[B[i]].next.push_back(&G[A[i]]);
    }
    bfs(&G[0], nullptr);
    FOR(i, N, 1){
        if(coun[i] == -1){
            cout << "No\n";return;
        }
    }
    cout << "Yes\n";
    FOR(i, N, 1){
        cout << coun[i]+1 << el;
    }
}


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

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

template< typename T >
T mod_pow(T x, T n, const T &p) {
  T ret = 1;
  while(n > 0) {
    if(n & 1) (ret *= x) %= p;
    (x *= x) %= p;
    n >>= 1;
  }
  return ret;
}
class bunsu
{
	public:
	ll a,b;
	bool pn;
	bunsu()
	{
		a = 0;
		b= 0;
		pn = false;
	}
	bunsu(ll a,ll b,ll pn)
	{
		this->a = a;
		this->b = b;
		this->pn = pn;
	}
	bool operator<(const bunsu& value)const
	{
		if(pn!=value.pn)return pn<value.pn;
		if(value.a!=a)return a<value.a;
		return b<value.b;
	}
};

int main()
{
	ll zero = 0;
	ll n;
	cin >> n;
	map<bunsu,ll> mp;
	map<bunsu,bool> exsit;
	vector<pair<ll,ll>> AB;
	vector<bunsu> vb;
	for(ll i = 0;i<n;i++)
	{
		ll a,b;
		cin >> a >> b;
		AB.emplace_back(a,b);
		if(a==0&&b==0)
		{
			vb.emplace_back(0,0,0);
			zero ++;
			continue;
		}
		else if(a==0)
		{
			vb.emplace_back(0,1,0);
			continue;
		}
		else if(b==0)
		{
			vb.emplace_back(1,0,0);
			continue;
		}
		ll G = gcd(abs(a),abs(b));

		vb.emplace_back(abs(a/G),abs(b/G),(a<0&&b>0)||(a>0&&b<0));
	}
	for(ll i = 0;i<n;i++)
	{
		exsit[vb[i]] = true;

		mp[vb[i]]++;
		mp[bunsu(vb[i].b,vb[i].a,!vb[i].pn)]+=0;
	}
	//ll ans = 1;
	ll add = 0;
	vector<ll> ANS;

	for(auto i:mp)
	{
		if(i.first.a==0&&i.first.b==0)
		{
			continue;
		}
		else if(i.first.a==0)
		{
			ll qwe = n - mp[bunsu(1,0,0)] - mp[bunsu(0,0,0)]-i.second;
			ll asd = mod_pow(0LL+2,i.second,MOD)-1;
			(asd *= (mod_pow(0LL+2,qwe,MOD)+MOD))%=MOD;
			//((asd -= 2)+=MOD)%=MOD;
			if(exsit[i.first])
			{
				//ANS.push_back(asd);
				//cout<<asd<<endl;
				(add += asd)%=MOD;
			}
			continue;
		}
		else if(i.first.b==0)
		{
			ll qwe = n - mp[bunsu(0,1,0)]-mp[bunsu(0,0,0)]-i.second;
			ll asd = mod_pow(0LL+2,i.second,MOD)-1;
			(asd *= (mod_pow(0LL+2,qwe,MOD)+MOD))%=MOD;
			//((asd -= 2)+=MOD)%=MOD;
			if(exsit[i.first])
			{
				//ANS.push_back(asd);
				(add += asd)%=MOD;
				//cout<<add<<endl;
				//exsit[i.first] = false;
			}
			continue;
		}
		bunsu alt(i.first.b,i.first.a,!i.first.pn);
		ll now =  mod_pow(0LL+2,i.second,MOD)-1;
		(now += MOD)%=MOD;
		now += mod_pow(0LL+2,mp[alt],MOD)-1;
		(now += MOD)%=MOD;
		(now +=1)%=MOD;
		if(exsit[i.first])
		{
			ANS.push_back(now);
			exsit[alt] = false;
		}
		//cout<<now<<endl;
		//cout<<mp[bunsu(i.first.b,i.first.a,!i.first.pn)]<<endl;
		//cout<<i.first.a<<' '<<i.first.b<<' '<<i.first.pn<<endl;
	}
	ll ans = 1;
	for(auto i:ANS)
	{
		(ans *= i)%=MOD;
		//cout<<ans<<';'<<i<<endl;
	}
	cout<<(ans-1+add+MOD+zero)%MOD<<endl;
}

Score: 500 points

Problem Statement

We have caught N sardines. The deliciousness and fragrantness of the i-th sardine is A_i and B_i, respectively.

We will choose one or more of these sardines and put them into a cooler. However, two sardines on bad terms cannot be chosen at the same time.

The i-th and j-th sardines (i \neq j) are on bad terms if and only if A_i \cdot A_j + B_i \cdot B_j = 0.

In how many ways can we choose the set of sardines to put into the cooler? Since the count can be enormous, print it modulo 1000000007.

Constraints

All values in input are integers.

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

-10^{18} \leq A_i, B_i \leq 10^{18}

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the count modulo 1000000007.

Sample Input 1

3
1 2
-1 1
2 -1

Sample Output 1

5

There are five ways to choose the set of sardines, as follows:

The 1-st

The 1-st and 2-nd

The 2-nd

The 2-nd and 3-rd

The 3-rd

Sample Input 2

10
3 2
3 2
-1 1
2 -1
-3 -9
-8 12
7 7
8 1
8 2
8 4

Sample Output 2

479


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(v) (v).begin(), (v).end()
using ll = long long;
using P = pair<int, int>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr long long MOD = 1e9 + 7;

template <int MOD = 1000000007>
struct Mint {
    int x;

    Mint() : x(0) {}
    Mint(long long t) {
        x = t % MOD;
        if (x < 0) x += MOD;
    }

    Mint pow(int n) {
        Mint res(1), t(x);
        while (n > 0) {
            if (n & 1) res *= t;
            t *= t;
            n >>= 1;
        }
        return res;
    }

    Mint inv() const {
        int a = x, b = MOD, u = 1, v = 0, t;
        while (b > 0) {
            t = a / b;
            a -= t * b;
            swap(a, b);
            u -= t * v;
            swap(u, v);
        }
        return Mint(u);
    }

    Mint &operator+=(Mint a) {
        x += a.x;
        if (x >= MOD) x -= MOD;
        return *this;
    }

    Mint &operator-=(Mint a) {
        x += MOD - a.x;
        if (x >= MOD) x -= MOD;
        return *this;
    }

    Mint &operator*=(Mint a) {
        x = int(1LL * x * a.x % MOD);
        return *this;
    }

    Mint &operator/=(Mint a) {
        return (*this) *= a.inv();
    }

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

    Mint operator-() const {
        return Mint(-x);
    }

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

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

    friend istream &operator>>(istream &is, Mint &a) {
        int t;
        is >> t;
        a = Mint<MOD>(t);
        return (is);
    }
};

long long GCD(long long m, long long n) {
    if (n == 0)
        return m;
    else
        return GCD(n, m % n);
}

signed main() {
    int n;
    cin >> n;
    ll a[n], b[n];
    bool c[n] = {};
    rep(i, n) {
        cin >> a[i] >> b[i];
        if ((a[i] < 0 && b[i] > 0) || (a[i] > 0 && b[i] < 0)) c[i] = true;
        a[i] = abs(a[i]);
        b[i] = abs(b[i]);
    }
    using P = pair<bool, pair<ll, ll>>;
    Mint<> ans = 1;
    map<P, int> mp;
    int zero1 = 0, zero2 = 0, all = 0;
    rep(i, n) {
        if (a[i] == 0 && b[i] == 0) {
            all++;
            continue;
        }
        if (a[i] == 0) {
            ans = ans + ans / Mint<>(2).pow(zero2 + all);
            zero1++;
            continue;
        } else if (b[i] == 0) {
            ans = ans + ans / Mint<>(2).pow(zero1 + all);
            zero2++;
            continue;
        }
        ll g = GCD(a[i], b[i]);
        a[i] /= g;
        b[i] /= g;
        ans = ans +
              ans / Mint<>(2).pow(mp[P(c[i], pair<ll, ll>(a[i], b[i]))] + all);
        mp[P(!c[i], pair<ll, ll>(b[i], a[i]))]++;
    }
    cout << ans - Mint<>(1) << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define FOR(i,l,r) for(long long i=(l);i<(r);++i)
#define REP(i,n) FOR(i,0,n)
#define REPS(i,n) FOR(i,1,n+1)
#define RFOR(i,l,r) for(long long i=(l);i>=(r);--i)
#define RREP(i,n) RFOR(i,N-1,0)
#define RREPS(i,n) RFOR(i,N,1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
const int INF=1e18;
const int MOD=1e9+7;
const int MAX=200001;
int 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;
    }
}

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

int powMOD(int n,int r){
    if(r<=0)return 1;
    if(r%2==0)return powMOD(n,r/2)*powMOD(n,r/2)%MOD;
    if(r%2==1)return powMOD(n,r-1)*n%MOD;
}

signed main(){
    int N;cin>>N;
    double X[N],Y[N];
  	REP(i,N)X[i]=INF;
    map<double,int>umemo;
    map<double,int>smemo;
    map<double,bool>used;
  	int Ac=0,Bc=0;
    REP(i,N){
        double a,b;cin>>a>>b;
      	if((a==0)||(b==0)){
          	if(a==0&&b==0){
              	continue;
            }
          	if(a==0)Ac++;
          	if(b==0)Bc++;
        }
        X[i]=a/b;
        umemo[a/b]++;
        smemo[(-b/a)]++;
        used[a/b]=false;
    }
    int ans=1;
    REP(i,N){
      	if(X[i]==INF)continue;
        if(used[X[i]])continue;
        used[X[i]]=true;
        used[-1.0/X[i]]=true;
        int d=0;
        if(smemo[X[i]]==0){
            d=powMOD(2,umemo[X[i]]);
        }
        else{
          	d=-1;
            d=(d+powMOD(2,umemo[X[i]]))%MOD;
            d=(d+powMOD(2,smemo[X[i]]))%MOD;
        }
      	while(d<0)d+=MOD;
		ans=ans*d%MOD;
    }
  	int d=-1;
  	d=(d+powMOD(2,Ac)%MOD);
    d=(d+powMOD(2,Bc)%MOD);
    ans=ans*d%MOD;
  	ans--;
    while(ans<0)ans+=MOD;
    cout<<ans<<endl;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <utility>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <climits>
#include <numeric>
#include<stdio.h>
#include <queue>
#include <iomanip>
#include <float.h>
#include <set>
#include<functional>
#include <stack>
#include <time.h>
#include <climits>
#include <bitset>
#include <fstream>
#include<map>
#include <cassert>
#include<sstream>
#include<fstream>
#include <cstring>
using namespace std;
typedef pair<int,int>p;
long long gcd(long long a, long long b) {
	if (b == 0) return a;
	else return gcd(b, a%b);
}
template< typename Monoid, typename OperatorMonoid = Monoid >
struct LazySegmentTree
{
	using F = function< Monoid(Monoid, Monoid) >;
	using G = function< Monoid(Monoid, OperatorMonoid) >;
	using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;
	using P = function< OperatorMonoid(OperatorMonoid, int) >;

	int sz;
	vector< Monoid > data;
	vector< OperatorMonoid > lazy;
	const F f;
	const G g;
	const H h;
	const P p;
	const Monoid M1;
	const OperatorMonoid OM0;

	LazySegmentTree(int n, const F f, const G g, const H h, const P p,
		const Monoid &M1, const OperatorMonoid OM0)
		: f(f), g(g), h(h), p(p), M1(M1), OM0(OM0)
	{
		sz = 1;
		while (sz < n) sz <<= 1;
		data.assign(2 * sz, M1);
		lazy.assign(2 * sz, OM0);
	}

	void set(int k, const Monoid &x)
	{
		data[k + sz] = x;
	}

	void build()
	{
		for (int k = sz - 1; k > 0; k--) {
			data[k] = f(data[2 * k + 0], data[2 * k + 1]);
		}
	}

	void propagate(int k, int len)
	{
		if (lazy[k] != OM0) {
			if (k < sz) {
				lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
				lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
			}
			data[k] = g(data[k], p(lazy[k], len));
			lazy[k] = OM0;
		}
	}

	Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r)
	{
		propagate(k, r - l);
		if (r <= a || b <= l) {
			return data[k];
		}
		else if (a <= l && r <= b) {
lazy[k] = h(lazy[k], x);
propagate(k, r - l);
return data[k];
		}
		else {
			return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
				update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
		}
	}

	Monoid update(int a, int b, const OperatorMonoid &x)
	{
		return update(a, b, x, 1, 0, sz);
	}

	Monoid query(int a, int b, int k, int l, int r)
	{
		propagate(k, r - l);
		if (r <= a || b <= l) {
			return M1;
		}
		else if (a <= l && r <= b) {
			return data[k];
		}
		else {
			return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
				query(a, b, 2 * k + 1, (l + r) >> 1, r));
		}
	}

	Monoid query(int a, int b)
	{
		return query(a, b, 1, 0, sz);
	}

	Monoid operator[](const int &k)
	{
		return query(k, k + 1);
	}
};
map< int64_t, int > prime_factor(int64_t n) {
	map< int64_t, int > ret;
	for (int64_t i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ret[i]++;
			n /= i;
		}
	}
	if (n != 1) ret[n] = 1;
	return ret;
}
long long MOD = 998244353;
const int MAX = 510000;
long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算
long long COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	string s, t;
	cin >> s >> t;
	bool judge = true;
	for (int i = 0; i < s.length() - 1; i++) {
		if (s[i] != t[i])judge = false;
	}
	if (judge)cout << "Yes";
	else cout << "No";
}

Score : 100 points

Problem Statement

Takahashi wants to be a member of some web service.

He tried to register himself with the ID S, which turned out to be already used by another user.

Thus, he decides to register using a string obtained by appending one character at the end of S as his ID.

He is now trying to register with the ID T. Determine whether this string satisfies the property above.

Constraints

S and T are strings consisting of lowercase English letters.

1 \leq |S| \leq 10

|T| = |S| + 1

Input

Input is given from Standard Input in the following format:

S
T

Output

If T satisfies the property in Problem Statement, print Yes; otherwise, print No.

Sample Input 1

chokudai
chokudaiz

Sample Output 1

Yes

chokudaiz can be obtained by appending z at the end of chokudai.

Sample Input 2

snuke
snekee

Sample Output 2

No

snekee cannot be obtained by appending one character at the end of snuke.

Sample Input 3

a
aa

Sample Output 3

Yes


#include <bits/stdc++.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <algorithm>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define acc(v) accumulate(v.begin(), v.end(), 0)

using namespace std;
typedef long long ll;
const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;

int main()
{
    string s,t;
    cin >> s >> t;
    rep(i,s.length()){
        if(s[i]!=t[i]){
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}


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

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

class ARegistration {
public:
    void solve(istream& cin, ostream& cout) {
      SPEED;
      string s, t; cin >> s >> t;
      rep(i, s.size()) {
        if (s[i] != t[i]) {
          cout << "No" << endl;
          return;
        }
      }
      cout << "Yes" << endl;
    }
};

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


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

int main() {
  long A,B,C,K;
  cin >> A >> B >> C >> K;
  if (K<=A+B){
    cout << A << endl;
  }
  else {
    cout << A+A+B-K << endl;
  }
}

Score : 200 points

Problem Statement

We have A cards, each of which has an integer 1 written on it. Similarly, we also have B cards with 0s and C cards with -1s.

We will pick up K among these cards. What is the maximum possible sum of the numbers written on the cards chosen?

Constraints

All values in input are integers.

0 \leq A, B, C

1 \leq K \leq A + B + C \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

A B C K

Output

Print the maximum possible sum of the numbers written on the cards chosen.

Sample Input 1

2 1 1 3

Sample Output 1

2

Consider picking up two cards with 1s and one card with a 0.
In this case, the sum of the numbers written on the cards is 2, which is the maximum possible value.

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000


#include <iostream>

int main(int argc, const char * argv[]) {
    int a,b,c,k;
    std::cin>>a>>b>>c>>k;
    if(k-a<=0){
        std::cout<<k<<std::endl;
    }else if(k-a-b<=0){
        std::cout<<a<<std::endl;
    }else {
        std::cout<<a-k+a+b<<std::endl;
    }
    return 0;
}


#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i = 0; i < (ll)(n); i++)
#define inf 1000000000000000000
#define mod 1000000007
#define mod2 998244353
#define pi acos(-1)
#define all(x) (x).begin(),(x).end()
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(15) << fixed;

  ll N, M, X;
  cin >> N >> M >> X;
  vector<ll> C(N);
  vector<vector<ll>> A(N, vector<ll>(M));
  rep(i, N) {
    cin >> C[i];
    rep(j, M) {
      cin >> A[i][j];
    }
  }
  ll ans = inf;
  rep(i, 1 << N) {
    ll cost = 0;
    vector<ll> B(M, 0);
    rep(j, N) {
      if (i & (1 << j)) {
        cost += C[j];
        rep(k, M) {
          B[k] += A[j][k];
        }
      }
    }
    ll flag = 1;
    rep(j, M) {
      if (B[j] < X) {
        flag = 0;
        break;
      }
    }
    if (flag) ans = min(ans, cost);
  }
  ans = ans == inf? -1 : ans;
  cout << ans << endl;
}

Score : 300 points

Problem

Takahashi, who is a novice in competitive programming, wants to learn M algorithms.
Initially, his understanding level of each of the M algorithms is 0.

Takahashi is visiting a bookstore, where he finds N books on algorithms.
The i-th book (1\leq i\leq N) is sold for C_i yen (the currency of Japan). If he buys and reads it, his understanding level of the j-th algorithm will increase by A_{i,j} for each j (1\leq j\leq M).
There is no other way to increase the understanding levels of the algorithms.

Takahashi's objective is to make his understanding levels of all the M algorithms X or higher. Determine whether this objective is achievable. If it is achievable, find the minimum amount of money needed to achieve it.

Constraints

All values in input are integers.

1\leq N, M\leq 12

1\leq X\leq 10^5

1\leq C_i \leq 10^5

0\leq A_{i, j} \leq 10^5

Input

Input is given from Standard Input in the following format:

N M X
C_1 A_{1,1} A_{1,2} \cdots A_{1,M}
C_2 A_{2,1} A_{2,2} \cdots A_{2,M}
\vdots
C_N A_{N,1} A_{N,2} \cdots A_{N,M}

Output

If the objective is not achievable, print -1; otherwise, print the minimum amount of money needed to achieve it.

Sample Input 1

3 3 10
60 2 2 4
70 8 7 9
50 2 3 9

Sample Output 1

120

Buying the second and third books makes his understanding levels of all the algorithms 10 or higher, at the minimum cost possible.

Sample Input 2

3 3 10
100 3 1 4
100 1 5 9
100 2 6 5

Sample Output 2

-1

Buying all the books is still not enough to make his understanding levels of all the algorithms 10 or higher.

Sample Input 3

8 5 22
100 3 7 5 3 1
164 4 5 2 7 8
334 7 2 7 2 9
234 4 7 2 8 2
541 5 4 3 3 6
235 4 8 6 9 7
394 3 6 1 6 2
872 8 4 3 7 2

Sample Output 3

1067


// #pragma GCC target("avx")  // CPU 処理並列化
// #pragma GCC optimize("O3")  // CPU 処理並列化
// #pragma GCC optimize("unroll-loops")  // 条件処理の呼び出しを減らす
#include <bits/stdc++.h>
#include <complex>

// #include <iostream> // cout, endl, cin
// #include <string> // string, to_string, stoi
// #include <vector> // vector
// #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
// #include <utility> // pair, make_pair
// #include <tuple> // tuple, make_tuple
// #include <cstdint> // int64_t, int*_t
// #include <cstdio> // printf
// #include <map> // map
// #include <queue> // queue, priority_queue
// #include <set> // set
// #include <stack> // stack
// #include <deque> // deque
// #include <unordered_map> // unordered_map
// #include <unordered_set> // unordered_set
// #include <bitset> // bitset
// #include <climits>
// #include <cmath>
// #include <iomanip>
// #include <functional>
// #include <numeric>
// #include <random>

using namespace std;

#define int long long
#define pb push_back
#define eb emplace_back
// #define F first
// #define S second
#define FOR(i,a,b) for(int (i)=(a);(i)<(int)(b);(i)++)
#define rep(i,n) FOR(i,0,n)
#define RFOR(i,a,b) for(int (i)=(a);(i)>=(int)(b);(i)--)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define ve vector
#define vi vector<int>
#define vp vector<pair<int,int>>
#define vvi vector<vector<int>>
#define UNIQUE(a) sort(all(a)), a.erase(unique(all(a)), a.end())
#define Double double
// #define endl '\n'

template<typename T> using pq = priority_queue<T,vector<T>,greater<T>>;
using ll = long long;
using UnWeightedGraph = vector<vector<int>>;
ll INF = LLONG_MAX / 4 - 100;
int IINF = INT_MAX / 4;
ll mod = 1e9 + 7;
int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
vector<ll> prime;

long double pi = 3.141592653589793238;

class fact {
public:
    long long fmod = 1e9+7;
    vector<long long> fac, finv, inv;
    fact (int n, long long Mod = 1e9+7) {
        fmod = Mod;
        fac = vector<long long>(n + 1, 0);
        finv = vector<long long>(n + 1, 0);
        inv = vector<long long>(n + 1, 0);
        fac[0] = fac[1] = 1;
        finv[0] = finv[1] = 1;
        inv[1] = 1;
        for (int i = 2; i < n + 1; i++) {
            fac[i] = fac[i-1] * i % fmod;
            inv[i] = mod - inv[mod%i] * (mod/i) % mod;
            finv[i] = finv[i-1] * inv[i] % mod;
        }
    }
    ll nCr(ll n, ll r) {if(n < r) return 0; return fac[n] * finv[r] % fmod * finv[n-r] % fmod;}
    ll POW(ll a, ll b) {ll c = 1; while (b > 0) {if (b & 1) {c = a * c%fmod;}a = a * a%fmod; b >>= 1;}return c;}
    inline int operator [] (int i) {return fac[i];}
    ll DeBuG(ll n, ll r);
};

void DEBUG(vector<int> a) {for(int i=0;i<a.size();i++)cout<<a[i]<<" ";cout<<endl;}
void EMP(int x) {cout<<"!!!"<<x<<"!!!"<<endl;}
ll GCD(ll a, ll b) {ll c; while (b != 0) {c = a % b; a = b; b = c;}return a;}
ll LCM(ll a, ll b) {return (a / GCD(a, b)) * (b / GCD(a, b)) * GCD(a, b);}
ll POW(ll a, ll b) {ll c = 1; while (b > 0) {if (b & 1) {c = a * c%mod;}a = a * a%mod; b >>= 1;}return c;}
void PRI(ll n) {bool a[n + 1]; for (int i = 0; i < n + 1; i++) {a[i] = 1;}for (int i = 2; i < n + 1; i++) {if (a[i]) {prime.pb(i); ll b = i; while (b <= n) {a[b] = 0; b += i;}}}}
template <typename T> T chmin(T& a, T b) {if(a>b)a=b;return a;}
template <typename T> T chmax(T& a, T b) {if(a<b)a=b;return b;}
bool isSqrt(ll a) {return pow(sqrt(a),2) == a ? 1 : 0;}
void YesNo(bool a) {if (a) cout << "Yes"; else cout << "No"; cout << endl;}
void yesno(bool a) {if (a) cout << "yes"; else cout << "no"; cout << endl;}
void YESNO(bool a) {if (a) cout << "YES"; else cout << "NO"; cout << endl;}
double dis(int x1, int x2, int y1, int y2) {
    return sqrt((double)abs(x1-x2)*(double)abs(x1-x2)+(double)abs(y1-y2)*(double)abs(y1-y2));
}
int ceili(int x, int y) {
    if (x % y == 0) return x / y;
    else return x / y + 1;
}
unsigned int randxor() {
    static unsigned int x=123456789,y=362436069,z=521288629,w=88675123;
    unsigned int t;
    t=(x^(x<<11));x=y;y=z;z=w; return( w=(w^(w>>19))^(t^(t>>8)) );
}

uint64_t xor64(void) {
    static uint64_t x = 88172645463325252ULL;
    x = x ^ (x << 7);
    return x = x ^ (x >> 9);
}

template<typename Monoid>
class SegTree {

//半開区間!!!!!!!!!!!!!!!
public:
    int p = 2;
    vector<Monoid> node;
    using Func = function<Monoid(Monoid,Monoid)>;
    Func pick; Monoid init;//操作と単位元

    SegTree(vector<Monoid> A, string type, Monoid m) {
        if (type == "min") {
            pick = [](Monoid a, Monoid b){return min(a,b);};
        } else if (type == "max") {
            pick = [](Monoid a, Monoid b){return max(a,b);};
        } else if (type == "sum") {
            // pick = [](Monoid a, Monoid b){return a + b;};
        }

        init = m;
        while (A.size() > p) p *= 2;
        node = vector<Monoid>(2*p-1,init);

        for (int i = 0;i < A.size();i++) {
            node[p-1+i] = A[i];
        }

        for (int i = p - 2;i >= 0;i--) {
            node[i] = pick(node[i*2+1],node[i*2+2]);
        }
    }

    SegTree(vector<Monoid> A, Func f, Monoid m) {
        pick = f; init = m;
        while (A.size() > p) p *= 2;
        node = vector<Monoid>(2*p-1,init);

        for (int i = 0;i < A.size();i++) {
            node[p-1+i] = A[i];
        }

        for (int i = p - 2;i >= 0;i--) {
            node[i] = pick(node[i*2+1],node[i*2+2]);
        }
    }

    //1点更新
    void update(int i, Monoid v) {
        for (node[(i+=p)-1] = v; i >>= 1;) {
            node[i-1] = pick(node[i*2-1], node[i*2]);
        }
    }

    //1点加算
    void add(int i, Monoid v) {
        update(i, this[i] + v);
    }

    Monoid getval(int start,int end,int k = 0,int l = 0,int r = -1) {
        if (r < 0) r = p;
        if (r <= start || end <= l) {return init;}
        if (start <= l && r <= end) {return node[k];}
        Monoid L,R;
        L = getval(start,end,k*2+1,l,(l+r)/2);
        R = getval(start,end,k*2+2,(l+r)/2,r);
        return {pick(L,R)};
    }

    inline Monoid operator [] (int i) {
        return node[i+p-1];
    }
};

void solve() {
    int n; cin >> n;
    vector<pair<int, int>> ue, si;
    vector<string> s(n); rep (i, n) cin >> s[i];

    vector<vector<int>> A(n);
    int sum = 0;
    rep (i, n) {
        int now = 0, mi = 0;
        for (auto elm : s[i]) {
            if (elm == '(') now++;
            else now--, chmin(mi, now);
        }
        A[i] = {-mi, now, i};
        sum += now;
    }
    if (sum != 0) {
        cout << "No" << endl;
        return;
    }
    sort(all(A));
    vector<int> Mis(n); rep (i, n) Mis[i] = A[i][0];
    vector<vector<int>> His(n); rep (i, n) His[i] = {A[i][1], i};
    SegTree<vector<int>> seg(His, "max", {-INF, -INF});

    sum = 0;
    rep (i, n) {
        int ind = upper_bound(all(Mis), sum)-Mis.begin();
        auto res = seg.getval(0, ind);
        if (res[0] == -INF) {
            cout << "No" << endl;
            return;
        }
        sum += res[0];
        seg.update(res[1], {-INF, res[1]});
    }
    cout << "Yes" << endl;
}

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

Score : 600 points

Problem Statement

A bracket sequence is a string that is one of the following:

An empty string;

The concatenation of (, A, and ) in this order, for some bracket sequence A ;

The concatenation of A and B in this order, for some non-empty bracket sequences A and B /

Given are N strings S_i. Can a bracket sequence be formed by concatenating all the N strings in some order?

Constraints

1 \leq N \leq 10^6

The total length of the strings S_i is at most 10^6.

S_i is a non-empty string consisting of ( and ).

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If a bracket sequence can be formed by concatenating all the N strings in some order, print Yes; otherwise, print No.

Sample Input 1

2
)
(()

Sample Output 1

Yes

Concatenating (() and ) in this order forms a bracket sequence.

Sample Input 2

2
)(
()

Sample Output 2

No

Sample Input 3

4
((()))
((((((
))))))
()()()

Sample Output 3

Yes

Sample Input 4

3
(((
)
)

Sample Output 4

No


#include <iostream>
#include <string>

int main() {
  std::string s; std::cin >> s;
  if (s[1] == 'B') std::cout << "ARC" << std::endl;
  else std::cout << "ABC" << std::endl;

  return 0;
}

Score : 100 points

Problem Statement

AtCoder Inc. holds a contest every Saturday.

There are two types of contests called ABC and ARC, and just one of them is held at a time.

The company holds these two types of contests alternately: an ARC follows an ABC and vice versa.

Given a string S representing the type of the contest held last week, print the string representing the type of the contest held this week.

Constraints

S is ABC or ARC.

Input

Input is given from Standard Input in the following format:

S

Output

Print the string representing the type of the contest held this week.

Sample Input 1

ABC

Sample Output 1

ARC

They held an ABC last week, so they will hold an ARC this week.


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{ string s;
  cin >> s;
 cout << s <<endl;
 return 0;}


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

const llong MOD = 1e9+7;
const llong inf = 1<<30;

int main() {
    string S; cin >> S;
    string word[2] = {"ABC", "ARC"};
    string res;
    if (S == word[0]) {
        res = word[1];
    }else{
        res == word[0];
    }
    cout << res << endl;
    return 0;
}


#include <bits/stdc++.h>
#define FIO ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define FOR(a,b,c) for(int a = (b); a <= (c); a++)
#define RFOR(a,b,c) for(int a = (b); a >= (c); a--)
#define all(x) (x).begin(), (x).end()
#define vi vector <int>
#define vvi vector <vi>
#define ll long long
#define vll vector <ll>
#define vvl vector <vll>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define eb emplace_back
#define ep emplace
#define fs first
#define sd second
#define mod 998244353
#define EPS 1e-9
#define tc int t; cin >> t; while(t--)
using namespace std;

int main() {
    FIO;
    string s; cin >> s;
    if(s == "ABC") cout << "ARC" << '\n';
    else cout << "ABC" << '\n';
    return 0;
}


#include<bits/stdc++.h>

#define ll                 long long
#define lcm(a,b)           (a*b)/__gcd(a,b)
#define pb                 push_back
#define vecll              vector<ll>
#define F                  first
#define S                  second
#define all(v)             v.begin(),v.end()
#define mp(a,b)            make_pair(a,b)
#define MOD                1000000007
#define forn(i,n)          for(ll i=0;i<n;i++)
#define fori(i,a,n)        for(int i=a; i<n; i++)
using namespace std;
int n;
ll gcd(ll a,ll b)
{
  if(b==0)
    return a;
  else
    return gcd(b,a%b);
}
void solve()
{
  int n,k;
  cin>>n>>k;
  int arr[n]={0},a;
  for(int i=0;i<k;i++)
  {
    cin>>a;
    for(int j=0;j<a;j++)
    {
      int b;
      cin>>b;
      arr[b-1]++;
    }
  }
  int c=0;
  for(int i=0;i<n;i++)
  {
    if(arr[i]==0)
    {
      c++;
    }
  }
  cout<<c<<endl;
  return;
}
int main()
{
  int t;
  // cin>>t;
  t=1;
  while(t--)
  {
    solve();
  }
  return 0;
}

Score : 200 points

Problem Statement

N Snukes called Snuke 1, Snuke 2, ..., Snuke N live in a town.

There are K kinds of snacks sold in this town, called Snack 1, Snack 2, ..., Snack K. The following d_i Snukes have Snack i: Snuke A_{i, 1}, A_{i, 2}, \cdots, A_{i, {d_i}}.

Takahashi will walk around this town and make mischief on the Snukes who have no snacks. How many Snukes will fall victim to Takahashi's mischief?

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 100

1 \leq d_i \leq N

1 \leq A_{i, 1} < \cdots < A_{i, d_i} \leq N

Input

Input is given from Standard Input in the following format:

N K
d_1
A_{1, 1} \cdots A_{1, d_1}
\vdots
d_K
A_{K, 1} \cdots A_{K, d_K}

Output

Print the answer.

Sample Input 1

3 2
2
1 3
1
3

Sample Output 1

1

Snuke 1 has Snack 1.

Snuke 2 has no snacks.

Snuke 3 has Snack 1 and 2.

Thus, there will be one victim: Snuke 2.

Sample Input 2

3 3
1
3
1
3
1
3

Sample Output 2

2


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

int main() {
    ll N, M;
    cin >> N >> M;
    vector<ll> H(N);
    vector<ll> A(M);
    vector<ll> B(M);

    vector<ll> cost(N + 1, -2);

    rep(i, N) {
        cin >> H[i];
    }
    rep(i, M) {
        cin >> A[i] >> B[i];
    }
    rep(i, M) {
        ll maxValue = max(H[A[i] - 1], H[B[i] - 1]);
        if(H[A[i] - 1] == H[B[i] - 1]) maxValue = LONG_LONG_MAX;
        if(cost[A[i]] < maxValue) cost[A[i]] = maxValue;
        if(cost[B[i]] < maxValue) cost[B[i]] = maxValue;
    }
    ll cnt = 0;
    rep(i, N) {
        if(cost[i + 1] <= H[i]) cnt++;
    }

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

Score : 300 points

Problem Statement

There are N observatories in AtCoder Hill, called Obs. 1, Obs. 2, ..., Obs. N. The elevation of Obs. i is H_i.
There are also M roads, each connecting two different observatories. Road j connects Obs. A_j and Obs. B_j.

Obs. i is said to be good when its elevation is higher than those of all observatories that can be reached from Obs. i using just one road.
Note that Obs. i is also good when no observatory can be reached from Obs. i using just one road.

How many good observatories are there?

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq H_i \leq 10^9

1 \leq A_i,B_i \leq N

A_i \neq B_i

Multiple roads may connect the same pair of observatories.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
H_1 H_2 ... H_N
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Print the number of good observatories.

Sample Input 1

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

Sample Output 1

2

From Obs. 1, you can reach Obs. 3 using just one road. The elevation of Obs. 1 is not higher than that of Obs. 3, so Obs. 1 is not good.

From Obs. 2, you can reach Obs. 3 and 4 using just one road. The elevation of Obs. 2 is not higher than that of Obs. 3, so Obs. 2 is not good.

From Obs. 3, you can reach Obs. 1 and 2 using just one road. The elevation of Obs. 3 is higher than those of Obs. 1 and 2, so Obs. 3 is good.

From Obs. 4, you can reach Obs. 2 using just one road. The elevation of Obs. 4 is higher than that of Obs. 2, so Obs. 4 is good.

Thus, the good observatories are Obs. 3 and 4, so there are two good observatories.

Sample Input 2

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

Sample Output 2

3


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

#define each(i, c) for (auto& i : c)
#define mkp(a, b) make_pair(a, b)

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> Pll;
const ll MOD = 1e9+7;

template<typename P, typename Q> ostream& operator << (ostream& os, pair<P, Q> p) { os << "(" << p.first << ": " << p.second << ")"; return os; }
template<typename T> ostream& operator << (ostream& os, vector<T> v) { os << "("; each (i, v) os << i << ", "; os << ")"; return os; }
template<typename K, typename V> ostream& operator << (ostream& os, map<K, V> m) { os << "{"; each (i, m) os << i << ", "; os << "}"; return os; }

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

  vector<ll> h(n);
  for (ll i = 0; i < n; ++i) {
    cin >> h[i];
  }

  vector<vector<ll>> g(n);
  for (ll i = 0; i < m; ++i) {
    ll a, b;
    cin >> a >> b;
    a--; b--;
    g[a].push_back(h[b]);
    g[b].push_back(h[a]);
  }

  ll ans = 0;
  for (ll i = 0; i < n; ++i) {
    if (g[i].empty()) {
      ++ans;
      continue;
    }

    sort(g[i].begin(), g[i].end(), greater<ll>());
    if (h[i] > g[i][0]) ++ans;
  }

  cout << ans << endl;

  return 0;
}


#include <bits/stdc++.h>
#include <math.h>
#define ALL(a)    a.begin(), a.end()
using namespace std;

int main() {
long long int X;cin >> X;
bool C = 0;

for(long long int A = -200; A < 200; A++){
for(long long int B = -200; B < 200; B++){

if(pow(A, 5) - pow(B, 5) == X){cout << A << " " << B;C = 1;}

}if(C)break;}
}

Score : 400 points

Problem Statement

Give a pair of integers (A, B) such that A^5-B^5 = X.
It is guaranteed that there exists such a pair for the given integer X.

Constraints

1 \leq X \leq 10^9

X is an integer.

There exists a pair of integers (A, B) satisfying the condition in Problem Statement.

Input

Input is given from Standard Input in the following format:

X

Output

Print A and B, with space in between.
If there are multiple pairs of integers (A, B) satisfying the condition, you may print any of them.

A B

Sample Input 1

33

Sample Output 1

2 -1

For A=2 and B=-1, A^5-B^5 = 33.

Sample Input 2

1

Sample Output 2

0 -1


#include <iostream>
#include <cmath>
#include <vector>

using namespace std;

int main(){
    int64_t X;
    cin >> X;
    vector<int64_t> temp;

    temp.push_back(0);

    int64_t bufA=0,bufB=0;

    for(int i=0;;i++){
        temp.push_back((i+1)*(i+1)*(i+1)*(i+1)*(i+1));
        for(int j =0;j<=i;j++){
            if(temp[i] - temp[j] == X){
                bufA = i;
                bufB = j;
                cout << bufA <<" "<<bufB<<endl;
                return 0;
            }
            else if(temp[i] + temp[j] == X){
                bufA = i;
                bufB = -1 *j;
                cout << bufA <<" "<<bufB<<endl;
                return 0;
            }
        }
    }
}


#include <bits/stdc++.h>
#define REP(i, n) for(long long i=0; i<n; i++)
#define REPR(i, n) for(long long i=n-1; i>=0; i--)
#define FOR(i, m, n) for(long long i=m; i<=n; i++)
#define FORR(i, m, n) for(long long i=m; i>=n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(),(v).end()
#define FIN ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;
const ll mod = 1e9+7;
const ll inf = 1e15;

vector<ll> divisor(ll n) {
  vector<ll> ret;
  for(ll i = 1; i * i <= n; i++) {
    if(n % i == 0) {
      ret.push_back(i);
      if(i * i != n) ret.push_back(n / i);
    }
  }
  sort(begin(ret), end(ret));
  return (ret);
}

int main(){FIN

    ll x; cin>>x;
    if (x==1){
        cout<<1<<' '<<0<<endl;
        return 0;
    }

    vll ret = divisor(x);

    for(auto p:ret){
        ll b=0; ll a=p;
        ll f;
        if(pow(a,5)-pow(b,5)>x) f=1;
        else f=0;

        while(f){
            a--; b--;
            if(pow(a,5)-pow(b,5)==x){
                cout <<a<<' '<<b<<endl;
                return 0;
            }
            if(pow(a,5)-pow(b,5)<x) break;
        }

        while(!f){
            a++; b++;
            if(pow(a,5)-pow(b,5)==x){
                cout<<a<<' '<<b<<endl;
                return 0;
            }
            if(pow(a,5)-pow(b,5)>x) break;
        }
    }
    cout<<-1<<endl;
    return 0;
}


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

int main() {
    int N, A, B, C;
    cin >> N >> A >> B >> C;
    vector<string> S(N);
    for(auto &e : S) cin >> e;
    vector<char> ans;

    auto solve = [&]() -> bool {
        for(int i = 0; i < N; ++i) {
            if(S[i] == "AB") {
                if(A == 0) {
                    A++, B--, ans.push_back('A');
                } else if(B == 0) {
                    A--, B++, ans.push_back('B');
                } else if(C == 0) {
                    if(i < N - 1 and S[i + 1] == "BC") {
                        A--, B++, ans.push_back('B');
                    } else if(i < N - 1 and S[i + 1] == "AC") {
                        A++, B--, ans.push_back('A');
                    } else {
                        A++, B--, ans.push_back('A');
                    }
                } else {
                    A++, B--, ans.push_back('A');
                }
            }
            if(S[i] == "BC") {
                if(B == 0) {
                    B++, C--, ans.push_back('B');
                } else if(C == 0) {
                    B--, C++, ans.push_back('C');
                } else if(A == 0) {
                    if(i < N - 1 and S[i + 1] == "AC") {
                        B--, C++, ans.push_back('C');
                    } else if(i < N - 1 and S[i + 1] == "AB") {
                        B++, C--, ans.push_back('B');
                    } else {
                        B++, C--, ans.push_back('B');
                    }
                } else {
                    B++, C--, ans.push_back('B');
                }
            }
            if(S[i] == "AC") {
                if(A == 0) {
                    A++, C--, ans.push_back('A');
                } else if(C == 0) {
                    A--, C++, ans.push_back('C');
                } else if(B == 0) {
                    if(i < N - 1 and S[i + 1] == "BC") {
                        A--, C++, ans.push_back('C');
                    } else if(i < N - 1 and S[i + 1] == "AB") {
                        A++, C--, ans.push_back('A');
                    } else {
                        C++, A--, ans.push_back('C');
                    }
                } else {
                    C++, A--, ans.push_back('C');
                }
            }
            if(A < 0 or B < 0 or C < 0) return false;
        }
        return true;
    };

    if(solve()) {
        cout << "Yes" << '\n';
        for(auto &e : ans) cout << e << '\n';
    } else {
        cout << "No" << '\n';
    }
    return 0;
}

Score : 600 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 10^5

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

N, A, B, C are integers.

s_i is AB, AC, or BC.

Input

Input is given from Standard Input in the following format:

N A B C
s_1
s_2
:
s_N

Output

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

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

Sample Input 1

2 1 3 0
AB
AC

Sample Output 1

Yes
A
C

You can successfully make two choices, as follows:

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

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

Sample Input 2

3 1 0 0
AB
BC
AB

Sample Output 2

No

Sample Input 3

1 0 9 0
AC

Sample Output 3

No

Sample Input 4

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

Sample Output 4

Yes
C
B
B
C
C
B
A
A


#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
inline ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
inline ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T>& V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> >& Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T>& mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }
template<typename T>vector<T> make_v(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type fill_v(T &t,const V &v){t=v;}
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type fill_v(T &t,const V &v){for(auto &e:t) fill_v(e,v);}
/*
 <url:>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

template<class Type>
Type solve(Type res = Type()){
    int N; cin >> N;
    vector<ll> V(3); cin >> V[0] >> V[1] >> V[2];

    vector<string> s(N);
    for(auto& in:s) cin >> in;

    for(int i = 0; i < N;i++){
        s[i][0] -= 'A';
        s[i][1] -= 'A';
    }

    {
        bool ok = true;
        vector<char> ans;
        auto TV = V;

        auto update = [&](int l,int r){
            ans.emplace_back('A'+l);
            TV[l]++; TV[r]--;
        };

        for(int i = 0; i < N;i++){
            for(int j = 0; j < 3;j++){
                if(TV[j] < 0){
                    ok = false;
                }
            }
            if(!ok) break;

            int l = s[i][0],r = s[i][1];
            if(TV[l] == 0){
                update(l,r);
                continue;
            }
            if(TV[r] == 0){
                update(r,l);
                continue;
            }

            if(TV[l] == 1 && TV[r] == 1){
                if(i != N-1){

                    int nl = s[i+1][0];
                    int nr = s[i+1][1];

                    if(l == nl || l == nr){
                        update(l,r);
                    }else{
                        update(r,l);
                    }
                    continue;
                }
            }
            update(l,r);
        }
        for(int j = 0; j < 3;j++){
            if(TV[j] < 0){
                ok = false;
            }
        }
        if(ok){
            cout << "Yes" << endl;
            for(auto a:ans) cout << a << endl;
            return res;
        }
    }
    cout << "No" << endl;
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    solve(0);
    // cout << fixed << setprecision(15) << solve<ll>() << endl;
    return 0;
}


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

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i,k,N) for(int i=k;i<N;i++)
#define MP make_pair
#define MT make_tuple //tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(),(x).end()

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

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

//0 0 ?
vector<string> s;
ll N;
vector<char> ans;//
bool ok =false;

bool memo[3][3][3][100001];
void dfs(ll a,ll b,ll c,ll now){
    if(ok)return;
    if(memo[a][b][c][now] == false)return;
    if(now == N){
        cout<<"Yes"<<endl;
        rep(i,0,N){
            cout<<ans[i]<<endl;
        }
        ok = true;
        return;
    }
    if(s[now] == "AB"){
        if(a == 0 && b == 0){
            return;
        }
        else if(a == 0){
            ans[now] = 'A';
            dfs(a+1,b-1,c,now+1);
            ans[now] = '0';
        }
        else if(b == 0){
            ans[now] = 'B';
            dfs(a-1,b+1,c,now+1);
            ans[now] = '0';
        }
        else{
            ans[now] = 'A';
            dfs(a+1,b-1,c,now+1);
            ans[now] = '0';
            ans[now] = 'B';
            dfs(a-1,b+1,c,now+1);
            ans[now] = '0';
        }
    }
    else if(s[now] == "BC"){
        if(b == 0 && c == 0){
            return;
        }
        else if(b == 0){
            ans[now] = 'B';
            dfs(a,b+1,c-1,now+1);
            ans[now] = '0';
        }
        else if(c == 0){
            ans[now] = 'C';
            dfs(a,b-1,c+1,now+1);
            ans[now] = '0';
        }
        else{
            ans[now] = 'B';
            dfs(a,b+1,c-1,now+1);
            ans[now] = '0';
            ans[now] = 'C';
            dfs(a,b-1,c+1,now+1);
            ans[now] = '0';
        }
    }
    else{
        if(a == 0 && c == 0){
            return;
        }
        else if(a == 0){
            ans[now] = 'A';
            dfs(a+1,b,c-1,now+1);
            ans[now] = '0';
        }
        else if(c == 0){
            ans[now] = 'C';
            dfs(a+1,b,c-1,now+1);
            ans[now] = '0';
        }
        else{
            ans[now] = 'A';
            dfs(a+1,b,c-1,now+1);
            ans[now] = '0';
            ans[now] = 'C';
            dfs(a+1,b,c-1,now+1);
            ans[now] = '0';
        }
    }
    memo[a][b][c][now] = false;
}
int main(){
    ll A,B,C;
    cin>>N>>A>>B>>C;
    s.resize(N);
    ans.resize(N);
    rep(i,0,N){
        cin>>s[i];
    }
    if(A+B+C<=2){
        dfs(A,B,C,0);
        if(!ok){
            cout<<"No";
        }
    }
    else if(A == 0 && B == 0 && s[0] == "AB"){
        cout<<"No";
    }
    else if(B == 0 && C == 0 && s[0] == "BC"){
        cout<<"No";
    }
    else if(C == 0 && A == 0 && s[0] == "CA"){
        cout<<"No";
    }
    else{
        rep(i,0,N){
            if(s[i] == "AB"){
                if(A == 0){
                    cout<<"A"<<endl;
                    A++;
                    B--;
                }
                else{
                    cout<<"B"<<endl;
                    B++;
                    A--;
                }
            }
            if(s[i] == "BC"){
                if(C == 0){
                    cout<<"C"<<endl;
                    C++;
                    B--;
                }
                else{
                    cout<<"B"<<endl;
                    B++;
                    C--;
                }
            }
            if(s[i] == "AC"){
                if(A == 0){
                    cout<<"A"<<endl;
                    A++;
                    C--;
                }
                else{
                    cout<<"C"<<endl;
                    C++;
                    A--;
                }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <assert.h>
#include <numeric>
#include <time.h>

using namespace std;

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

int main()
{
  int n;
  ll a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> s(n);
  for (int i = 0; i < n; i++)
  {
    cin >> s[i];
  }

  ll sm = a + b + c;
  vector<char> v(n);
  if (sm == 0)
  {
    cout << "No" << endl;
    return 0;
  }
  else if (sm == 1)
  {
    for (int i = 0; i < n; i++)
    {
      if (s[i] == "AB")
      {
        if (a == 0 && b == 0)
        {
          cout << "No" << endl;
          return 0;
        }
        else
        {
          if (a == 0)
          {
            a++;
            b--;
            v[i] = 'A';
          }
          else
          {
            a--;
            b++;
            v[i] = 'B';
          }
        }
      }
      else if (s[i] == "AC")
      {
        if (a == 0 && c == 0)
        {
          cout << "No" << endl;
          return 0;
        }
        else
        {
          if (a == 0)
          {
            a++;
            c--;
            v[i] = 'A';
          }
          else
          {
            a--;
            c++;
            v[i] = 'C';
          }
        }
      }
      else
      {
        if (b == 0 && c == 0)
        {
          cout << "No" << endl;
          return 0;
        }
        else
        {
          if (b == 0)
          {
            b++;
            c--;
            v[i] = 'B';
          }
          else
          {
            b--;
            c++;
            v[i] = 'C';
          }
        }
      }
    }
    cout << "Yes" << endl;
    for (int i = 0; i < n; i++)
    {
      cout << v[i] << endl;
    }
    return 0;
  }
  else if (sm == 2)
  {
    for (int i = 0; i < n; i++)
    {
      if (s[i] == "AB")
      {
        if (a == 0 && b == 0)
        {
          cout << "No" << endl;
          return 0;
        }
        else
        {
          if (a == 0)
          {
            a++;
            b--;
            v[i] = 'A';
          }
          else if (b == 0)
          {
            a--;
            b++;
            v[i] = 'B';
          }
          else
          {
            if (i == n - 1)
            {
              a++;
              b--;
              v[i] = 'A';
            }
            else
            {
              if (s[i + 1] == "AB")
              {
                a++;
                b--;
                v[i] = 'A';
              }
              else if (s[i + 1] == "AC")
              {
                a++;
                b--;
                v[i] = 'A';
              }
              else
              {
                a--;
                b++;
                v[i] = 'B';
              }
            }
          }
        }
      }
      else if (s[i] == "AC")
      {
        if (a == 0 && c == 0)
        {
          cout << "No" << endl;
          return 0;
        }
        else
        {
          if (a == 0)
          {
            a++;
            c--;
            v[i] = 'A';
          }
          else if (c == 0)
          {
            a--;
            c++;
            v[i] = 'C';
          }
          else
          {
            if (i == n - 1)
            {
              a++;
              c--;
              v[i] = 'A';
            }
            else
            {
              if (s[i + 1] == "AC")
              {
                a++;
                c--;
                v[i] = 'A';
              }
              else if (s[i + 1] == "AB")
              {
                a++;
                c--;
                v[i] = 'A';
              }
              else
              {
                a--;
                c++;
                v[i] = 'C';
              }
            }
          }
        }
      }
      else
      {
        if (b == 0 && c == 0)
        {
          cout << "No" << endl;
          return 0;
        }
        else
        {
          if (b == 0)
          {
            b++;
            c--;
            v[i] = 'B';
          }
          else if (c == 0)
          {
            b--;
            c++;
            v[i] = 'C';
          }
          else
          {
            if (i == n - 1)
            {
              b++;
              c--;
              v[i] = 'B';
            }
            else
            {
              if (s[i + 1] == "BC")
              {
                b++;
                c--;
                v[i] = 'B';
              }
              else if (s[i + 1] == "AB")
              {
                b++;
                c--;
                v[i] = 'B';
              }
              else
              {
                b--;
                c++;
                v[i] = 'C';
              }
            }
          }
        }
      }
    }
    cout << "Yes" << endl;
    for (int i = 0; i < n; i++)
    {
      cout << v[i] << endl;
    }
    return 0;
  }
  else
  {
    if (a == 0 && b == 0 && s[0] == "AB")
    {
      cout << "No" << endl;
      return 0;
    }
    else if (a == 0 && c == 0 && s[0] == "AC")
    {
      cout << "No" << endl;
      return 0;
    }
    else if (b == 0 && c == 0 && s[0] == "BC")
    {
      cout << "No" << endl;
      return 0;
    }
    cout << "Yes" << endl;
    for (int i = 0; i < n; i++)
    {
      if (s[i] == "AB")
      {
        if (a == 0 || a == 1)
        {
          a++;
          b--;
          v[i] = 'A';
        }
        else
        {
          a--;
          b++;
          v[i] = 'B';
        }
      }
      else if (s[i] == "AC")
      {
        if (a == 0 || a == 1)
        {
          a++;
          c--;
          v[i] = 'A';
        }
        else
        {
          a--;
          c++;
          v[i] = 'C';
        }
      }
      else
      {
        if (b == 0 || b == 1)
        {
          b++;
          c--;
          v[i] = 'B';
        }
        else
        {
          b--;
          c++;
          v[i] = 'C';
        }
      }
    }
    for (int i = 0; i < n; i++)
    {
      cout << v[i] << endl;
    }
    return 0;
  }
}


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

typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<double> vd;
typedef vector<vd> vdd;

#define FOR(i,x,y) for(ll i=(ll)x; i<(ll)y; ++i)
#define REP(i,y) FOR(i, 0, y)
#define RFOR(i,x,y) for(ll i=(ll)x; i>=(ll)y; --i)
#define RREP(i,x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define debug_print(x...) cerr << "line " << __LINE__ << " : "; debug_print_in(x);

template <typename First>
void debug_print_in(First first){
  cerr << first << endl;
  return;
}

template <typename First, typename... Rest>
void debug_print_in(First first, Rest... rest){
  cerr << first << " ";
  debug_print_in(rest...);
  return;
}

void IN(void){
  return;
}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
  cin >> first;
  IN(rest...);
  return;
}

template <typename First>
void OUT(First first){
  cout << first << endl;
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
  cout << first << " ";
  OUT(rest...);
  return;
}

template<class t, class u> void chmax(t&a,u b){if(a<b)a=b;};
template<class t, class u> void chmin(t&a,u b){if(a>b)a=b;};
int popcount(int t){return __builtin_popcount(t);} //GCC
int popcount(ll t){return __builtin_popcountll(t);} //GCC

template <typename T>
void vec_print(vector<T> VEC){
  REP(i, VEC.size()){
    cerr << VEC[i] << " ";
  }
  cerr << endl;
};

template <typename T>
void mat_print(vector<vector<T> > MAT){
  REP(i,MAT.size()){
    REP(j,MAT[i].size()){
      cerr << MAT[i][j] << " ";
    }
    cerr << endl;
  }
};

constexpr int INF = (1<<30);
constexpr ll INFLL = 1LL<<62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9)+7);

ll N, A, B, C;
ll ABC;
vll s;
vvll dp;

void dp_search(ll index){
  if(index==N){
    return;
  }
  if(s[index]==1){
    dp[index+1][0] = dp[index][5];
    dp[index+1][1] = dp[index][5];
    dp[index+1][2] = 0;
    dp[index+1][3] = dp[index][4];
    dp[index+1][4] = dp[index][3];
    dp[index+1][5] = dp[index][0] + dp[index][1];
  }else if(s[index]==2){
    dp[index+1][0] = dp[index][4];
    dp[index+1][1] = 0;
    dp[index+1][2] = dp[index][4];
    dp[index+1][3] = dp[index][5];
    dp[index+1][4] = dp[index][0] + dp[index][2];
    dp[index+1][5] = dp[index][3];
  }else{
    dp[index+1][0] = 0;
    dp[index+1][1] = dp[index][3];
    dp[index+1][2] = dp[index][3];
    dp[index+1][3] = dp[index][1] + dp[index][2];
    dp[index+1][4] = dp[index][5];
    dp[index+1][5] = dp[index][4];
  }
  dp_search(index+1);
  return;
}

vvll hoge = {
  {2, 0, 0},
  {0, 2, 0},
  {0, 0, 2},
  {0, 1, 1},
  {1, 0, 1},
  {1, 1, 0}
};

void rev(vvll& ans, ll index, ll state){
  ans.push_back(hoge[state]);
  if(index<0) return;
  ll new_state = -1;
  if(s[index]==1){
    switch(state){
      case 0:
        new_state = 5;
        break;
      case 1:
        new_state = 5;
        break;
      case 2:
        break;
      case 3:
        new_state = 4;
        break;
      case 4:
        new_state = 3;
        break;
      case 5:
        if(dp[index][0]>0){
          new_state = 0;
        }else{
          new_state = 1;
        }
        break;
    }
  }else if(s[index]==2){
    switch(state){
      case 0:
        new_state = 4;
        break;
      case 1:
        break;
      case 2:
        new_state = 4;
        break;
      case 3:
        new_state = 5;
        break;
      case 4:
        if(dp[index][0]>0){
          new_state = 0;
        }else{
          new_state = 1;
        }
        break;
      case 5:
        new_state = 3;
        break;
    }
  }else{
    switch(state){
      case 0:
        break;
      case 1:
        new_state = 3;
        break;
      case 2:
        new_state = 3;
        break;
      case 3:
        if(dp[index][1]>0){
          new_state = 1;
        }else{
          new_state = 2;
        }
        break;
      case 4:
        new_state = 5;
        break;
      case 5:
        new_state = 4;
        break;
    }
  }

  rev(ans, index-1, new_state);
  return;
}

vvll search(void){
  dp_search(0);
  vvll ans(0);

  if(dp[N][0] + dp[N][1] + dp[N][2] + dp[N][3] + dp[N][4] + dp[N][5] == 0){
    return ans;
  }

  ll state = 0;
  REP(i,6){
    if(dp[N][i]>0) state = i;
  }

  rev(ans, N-1, state);

  return ans;
}

int main(){
  cin.tie(0); // cut the cin and cout (default, std::flush is performed after std::cin)
  ios::sync_with_stdio(false); // cut the iostream and stdio (DON'T endl; BUT "\n";)

  IN(N,A,B,C);

  ABC = A+B+C;
  // ABC = 1 -> do
  // ABC >= 3 -> go lower
  // ABC == 2 -> ?

  s.resize(N);
  dp.resize(N+10, vll(6, 0));

  if(A==2 && B==0 && C==0){
    dp[0][0] = 1;
  }else if(A==0 && B==2 && C==0){
    dp[0][1] = 1;
  }else if(A==0 && B==0 && C==2){
    dp[0][2] = 1;
  }else if(A==0 && B==1 && C==1){
    dp[0][3] = 1;
  }else if(A==1 && B==0 && C==1){
    dp[0][4] = 1;
  }else if(A==1 && B==1 && C==0){
    dp[0][5] = 1;
  }

  REP(i,N){
    string str;
    IN(str);
    if(str=="AB"){
      s[i] = 1;
    }else if(str=="AC"){
      s[i] = 2;
    }else{
      s[i] = 3;
    }
  }

  vll ans(N, -1);

  if(ABC==1){
    REP(i,N){
      switch(s[i]){
        case 1:
          if(A==0 && B==0){
            OUT("No");
            return 0;
          }
          if(A>0){
            A--;
            B++;
            ans[i] = 1;
          }else{
            A++;
            B--;
            ans[i] = 0;
          }
          break;
        case 2:
          if(A==0 && C==0){
            OUT("No");
            return 0;
          }
          if(A>0){
            A--;
            C++;
            ans[i] = 2;
          }else{
            A++;
            C--;
            ans[i] = 0;
          }
          break;
        case 3:
          if(B==0 && C==0){
            OUT("No");
            return 0;
          }
          if(B>0){
            B--;
            C++;
            ans[i] = 2;
          }else{
            B++;
            C--;
            ans[i] = 1;
          }
      }
    }
  }else if(ABC>=3){
    REP(i,N){
      switch(s[i]){
        case 1:
          if(A==0 && B==0){
            OUT("No");
            return 0;
          }
          if(A>B){
            A--;
            B++;
            ans[i] = 1;
          }else{
            A++;
            B--;
            ans[i] = 0;
          }
          break;
        case 2:
          if(A==0 && C==0){
            OUT("No");
            return 0;
          }
          if(A>C){
            A--;
            C++;
            ans[i] = 2;
          }else{
            A++;
            C--;
            ans[i] = 0;
          }
          break;
        case 3:
          if(B==0 && C==0){
            OUT("No");
            return 0;
          }
          if(B>C){
            B--;
            C++;
            ans[i] = 2;
          }else{
            B++;
            C--;
            ans[i] = 1;
          }
      }
    }
  }else{
    vvll tmp = search();
    if(tmp.size()<N){
      OUT("No");
      return 0;
    }
    for(ll i=N; i>0; --i){
      char c = 'A';
      REP(j,3){
        if(tmp[i-1][j]-tmp[i][j]>0){
          c += j;
          break;
        }
      }
      printf("%c\n", c);
    }
    return 0;
  }

  printf("Yes\n");
  REP(i,N){
    if(ans[i]==0){
      printf("A\n");
    }else if(ans[i]==1){
      printf("B\n");
    }else{
      printf("C\n");
    }
  }

  return 0;
}


// I SELL YOU...!
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
#include<map>
#include<set>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using TP = tuple<ll,ll,ll>;
void init_io(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(18);
}
ll n;
signed main(){
  init_io();
  ll a,b,c,ac=0,bc=0,cc=0;
  ll bc_cnt=0,ab_cnt=0,ac_cnt=0;
  cin >> n >> a >> b >> c;
  vector<string> s(n);
  vector<char> ans(n,' '),n_c(n);
  vector<ll> le_b(n),le_c(n);
  for(int i=0;i<n;i++){
    cin >> s[i];
    if(s[i]=="AB") n_c[i] = 'C',ab_cnt++;
    if(s[i]=="BC") n_c[i] = 'A',bc_cnt++;
    if(s[i]=="AC") n_c[i] = 'B',ac_cnt++;
    for(auto ch:s[i]){
      if(ch=='A') a++,ac++;
      else if(ch=='B') b++,bc++;
      else c++,cc++;
    }
  }
  if(a<0||b<0||c<0){
    cout << "No\n";
    return 0;
  }
  a /= 2;
  b /= 2;
  c /= 2;
  for(int i=0;i<n;i++){
    if(n_c[i]!='A'){
      a--;
      if(s[i]=="AB"){
        ans[i] = 'B';
      }else{
        ans[i] = 'C';
      }
    }
  }
  for(int i=0;i<n;i++){
    if(s[i]=="AB"){
      if(b>0&&a<0){
        b--;
        a++;
        ans[i] = 'A';
      }
    }else if(s[i]=="AC"){
      if(c>0&&a<0){
        c--;
        a++;
        ans[i] = 'A';
      }
    }
  }
  for(int i=0;i<n;i++){
    if(s[i]=="BC"){
      if(b>0){
        b--;
        ans[i] = 'C';
      }else{
        c--;
        ans[i] = 'B';
      }
    }
  }
  if(a<0||b<0||c<0){
    cout << "No\n";
    return 0;
  }
  cout <<"Yes\n";
  for(int i=0;i<n;i++){
    cout << ans[i]<<endl;
  }
}


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

int main(){
  int A,B,K;
  bool ans=false;
  cin>>K>>A>>B;

  for(int i=A; i<=B; i++){
    if(i%K==0) {
      ans=true;
      break;
    }
  }
  if(ans) cout<<"OK";
  else cout<<"NG";
}

Score: 100 points

Problem Statement

Takahashi the Jumbo will practice golf.

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

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

Constraints

All values in input are integers.

1 \leq A \leq B \leq 1000

1 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

K
A B

Output

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

Sample Input 1

7
500 600

Sample Output 1

OK

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

Sample Input 2

4
5 7

Sample Output 2

NG

No multiple of 4 lies between 5 and 7.

Sample Input 3

1
11 11

Sample Output 3

OK


#include <bits/stdc++.h>

using namespace std;

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

    for( int i = 1; i <= m; i++ )
        cout << i << " " << n - i - (( n + 1 ) % 2 ) << endl;

    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define S scanf
#define P printf
#define G getline
#define SZ size()
#define C clear()
#define B begin()
#define F front()
#define T top()
#define E end()
#define EM empty()
#define V vector
#define Q queue
#define DQ deque
#define PQ priority_queue
#define ST stack
#define FI first
#define SE second
#define PI acos(-1)
#define PS push
#define PP pop()
#define PSF push_front
#define PSB push_back
#define PPF pop_front()
#define PPB pop_back()
#define MP make_pair
#define LL long long int
#define ULL unsigned long long int
#define PII pair<int,int>
#define PSI pair<string,int>
#define PIS pair<int,string>
#define PLI pair<long long int,int>
#define PLL pair<long long int,long long int>
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define MLI map<long long int,int>
#define FAST() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

//int dx[]={-1,1,0,0};
//int dy[]={0,0,-1,1};
//int dx[]= {-1,0,1,-1,1,-1,0,1};
//int dy[]= {1,1,1,0,0,-1,-1,-1};

int main()
{
    FAST();

//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);

    int n,x,y;
    cin >> n >> x >> y;
    if((y/n)*n >= x)cout << "OK";
    else cout << "NG";
    return 0;
}


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

int main () {
 int K,A,B;
  cin >> K >> A >> B;
  string t;
  for (int x = B;A<=x;x--) {
   if (x%K == 0) {
    t = "OK" ;
     break;
   }
   if (x%K != 0) {
    t = "NG" ;
   }
  }
  cout << t << endl;
}


#include <iostream>
using namespace std;

int main() {
	int K, A, B;
	cin >> K >> A >> B;
	bool ans = false;
	for (int i = A; i <= B; i++) {
		if (i % K == 0) {
			ans = true;
			break;
		}
	}
	if (ans) cout << "OK";
	else cout << "NG";
}


#include<bits/stdc++.h>

using namespace std;
using ll = long long;

ll N,M,Q;
ll sum = 0,ans = 0;
vector<ll> a(50);
vector<ll> b(50);
vector<ll> c(50);
vector<ll> d(50);
vector<ll> A(10);

void dfs(ll NN){

    if(NN == 0){

        sum = 0;

        for(ll i = 0;i < Q;++i){
            if(A[b[i]-1]-A[a[i]-1] == c[i]){
                sum = sum + d[i];
            }
        }

        if(ans < sum) ans = sum;

    }
    else{

        for(ll i = 0;i < M;++i){
            A[NN-1] = i + 1;
            dfs(NN-1);
        }

    }

}

int main(){

cin >> N >> M >> Q;

for(ll i = 0;i < Q;++i)    cin >> a[i] >> b[i] >> c[i] >> d[i];

dfs(N);

cout << ans << endl;

return 0;

}

Score : 300 points

Problem Statement

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

Consider a sequence A satisfying the following conditions:

A is a sequence of N positive integers.

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

Let us define a score of this sequence as follows:

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

Find the maximum possible score of A.

Constraints

All values in input are integers.

2 ≤ N ≤ 10

1 \leq M \leq 10

1 \leq Q \leq 50

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

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

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

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

Input

Input is given from Standard Input in the following format:

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

Output

Print the maximum possible score of A.

Sample Input 1

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

Sample Output 1

110

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

Sample Input 2

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

Sample Output 2

357500

Sample Input 3

10 10 1
1 10 9 1

Sample Output 3

1


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define mp make_pair
#define pb push_back
#define all(v) v.begin(), v.end()
#define ub upper_bound
#define lb lower_bound
#define F first
#define S second

struct abcd{
    int a,b,c,d;
};

ll ans = 0;
vector<abcd> v(55);
int n,m,q;

void checkAll(int arr[], int m, int n, int index, int ma){
    if(n==0){
        ll lans = 0;
        for(int i=0; i<q; i++){
            if(arr[v[i].b-1] - arr[v[i].a-1] == v[i].c) lans += v[i].d;
        }
        ans = max(lans, ans);
    }
    if(n>0){
        for(int i=ma; i<=m; i++){
            arr[index] = i;
            checkAll(arr, m, n-1, index+1, i);
        }
    }
}

int main(){
    fastio;
    cin >> n >> m >> q;

    for(int i=0; i<q; i++){
        cin >> v[i].a >> v[i].b >> v[i].c >> v[i].d;
    }

    int arr[10];

    checkAll(arr, m, n, 0, 1);
    cout << ans << endl;
    return 0;
}


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

int main()
{
    ll a, b, n;
    cin >> a >> b >> n;

    ll ans = (a * min(b - 1, n)) / b - a * (min(b - 1, n) / b);
    cout << ans << endl;
    return 0;
}

Score : 400 points

Problem Statement

Given are integers A, B, and N.

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

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

Constraints

1 ≤ A ≤ 10^{6}

1 ≤ B ≤ 10^{12}

1 ≤ N ≤ 10^{12}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B N

Output

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

Sample Input 1

5 7 4

Sample Output 1

2

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

Sample Input 2

11 10 9

Sample Output 2

9


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using T = tuple<string,int,int>;

int main(){
  ll a,b,n; cin >> a >> b >> n;
  ll ans = 0;
  if(n < b){
    ans = (a*n)/b;
  }else{
    ans = (a*(b-1))/b;
  }
  cout << ans << endl;
  return 0;
}


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

int main()
{

        long long n,m,a,b,c,d,e,f,g,h,p,q,r,fr,sc,tr,sz,tz,sz1,sz2,i,j,k,mx=LLONG_MIN,mn=LLONG_MAX; string str,ttr,ntr;
        long long x=0,y=0,cnt=0,res=0,ttl=0,ans=0,sum=0; long long flg=0,flag=1,na=0,as=1;
        vector<long long>u,v,w; vector< pair<long long,long long> >vct,tct; vector<string>vst;
        set<long long>st,nt,tt; map<long long,long long>mp,nq,qr,mp1,mp2;

        scanf("%lld %lld %lld",&a,&b,&n);
        fr=min(b-1,n);
        sc=a*fr;  p=sc/b;  printf("%lld\n",p);

    return 0;
}


#include "bits/stdc++.h"

using namespace std;
using ll = long long;
using indx = size_t;
#define endl '\n'

int main() {
    int n, m;
    cin >> n >> m;
    ll a = n/2;
    ll b = a + 1;
    while(m) {
        m--;
        if (n == 2*(b-a) or n == 2*(b-a-1))
            b++;
        cout << a << " " << b << endl;
        a--, b++;
    }
}

Score : 500 points

Problem Statement

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

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

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

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

Constraints

1 \leq M

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

Input

Input is given from Standard Input in the following format:

N M

Output

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

a_1 b_1
a_2 b_2
:
a_M b_M

Sample Input 1

4 1

Sample Output 1

2 3

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

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

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

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

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

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

Sample Input 2

7 3

Sample Output 2

1 6
2 5
3 4


#include <bits/stdc++.h>

using namespace std;

const int N = 2e5+2;

//compress A[i] first
//so that we can use LIS via Segment tree

long long s;
long long e;
long long val;
long long segtree[N << 2];

void update(int P , int L , int R){
     if(L > e || R < s){
        return;
     } else if(s <= L && R <= e) {
        segtree[P] = val;
        return;
     }
     int mid = (L + R) >> 1;
     update( (P << 1) | 0 , L , mid + 0);
     update( (P << 1) | 1 , mid + 1 , R);
     //segtree[P] = segtree[P << 1] + segtree[P << 1 | 1];
     segtree[P] = max(segtree[P << 1] , segtree[P << 1 | 1]);
}

long long query(int P , int L , int R){
    if(L > e || R < s){
        return 0;
    } else if(s <= L && R <= e){
        return segtree[P];
    } else {
        int mid = (L + R) >> 1;
        long long a = query( (P << 1) | 0 , L , mid + 0);
        long long b = query( (P << 1) | 1 , mid + 1 , R);
        return max(a , b);
    }
}

int n;
int a[N];
int ans[N];
map < int , int > to;
vector < int > adj[N];

void dfs(int u , int p){
    //cout << u << "\n";
    s = 1 , e = a[u] - 1;

    ans[u] = query(1 , 1 , n) + 1;
    e++;
    s = e;
    val = ans[u];
    update(1 , 1 ,  n);

    ans[u] = max(ans[u] , ans[p]);

    for(auto v : adj[u]){
        if(v == p)
            continue;
        dfs(v , u);
    }

    s = a[u];
    e = a[u];
    val = 0;
    update(1 , 1 , n);
}

int main(){
    scanf("%d" , &n);
    vector < int > all;
    for(int i = 1; i <= n; i++){
        scanf("%d" , &a[i]);
        all.push_back(a[i]);
    }
    int idx = 2;
    sort(all.begin() , all.end());
    for(int i = 0; i < n; i++){
        if(!i){
            to[all[i]] = idx;
            continue;
        }
        if(all[i] == all[i - 1]){
            to[all[i]] = to[all[i - 1]];
        } else {
            to[all[i]] = ++idx;
        }
    }
    for(int i = 1; i <= n; i++){
        a[i] = to[a[i]];
    }

    for(int i = 1; i < n; i++){
        int u , v;
        scanf("%d%d" , &u , &v);
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1 , 0);
    for(int i = 1; i <= n; i++){
        printf("%d\n" , ans[i]);
    }
}

Score : 600 points

Problem Statement

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

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

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

Constraints

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

1 \leq a_i \leq 10^9

1 \leq u_i , v_i \leq N

u_i \neq v_i

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

1
2
3
3
4
4
5
2
2
3

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


#include<bits/stdc++.h>
#define rep0(i,n) for(i=0;i<n;i++)
#define rep(i,n) for(i=1;i<=n;i++)
#define reps(i,a,n) for(i=a;i<=n;i++)
#define mem(ara,n) memset(ara,n,sizeof(ara))
#define memb(ara) memset(ara,false,sizeof(ara))
#define all(x) (x).begin(),(x).end()
#define fast ios_base:: sync_with_stdio( false ); cin.tie(0); cout.tie(0);
#define eb              emplace_back
#define em              emplace
#define pb              push_back
#define Mp              make_pair
#define ff              first
#define ss              second
#define mod 1000000007
typedef long long ll;
using namespace std;

const int M = (int)(2e6 + 239);

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

    string a;
    ll b;
    set<string>s;
    cin>>b;
    while(b--)
    {
        cin>>a;
        s.insert(a);
    }
    cout<<s.size()<<endl;
}

Score : 300 points

Problem Statement

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

How many kinds of items did you get?

Constraints

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

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

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

Print the number of kinds of items you got.

Sample Input 1

3
apple
orange
apple

Sample Output 1

2

You got two kinds of items: apple and orange.

Sample Input 2

5
grape
grape
grape
grape
grape

Sample Output 2

1

Sample Input 3

4
aaaa
a
aaa
aa

Sample Output 3

4


#include <bits/stdc++.h>
using namespace std;
// g++ -std=c++17 -Wshadow -Wall sol.cpp -o  s -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vvi = vector< vi > ;
using ld = long double;
const int inf = 0x3f3f3f3f;
const ll linf = 0x3f3f3f3f3f3f3f3fll;
template<typename T> T abs (T x) { return x < 0 ? -x : x; }
template<typename T> void max_self(T &a, T b) {  a = max(a, b); }
template<typename T> void min_self(T &a, T b) {  a = min(a, b); }

void solve(int tc) {
  long long n, m, x;
  cin >> n >> m;
  long long sum = 0;
  for(int i = 0; i < m; ++i){
    cin >> x;
    sum+=x;
  }
  cout << max(-1LL, n - sum) << endl;
 }

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

Score : 200 points

Problem Statement

Takahashi has N days of summer vacation.

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

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

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

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

Constraints

1 \leq N \leq 10^6

1 \leq M \leq 10^4

1 \leq A_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 ... A_M

Output

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

Sample Input 1

41 2
5 6

Sample Output 1

30

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

Sample Input 2

10 2
5 6

Sample Output 2

-1

He cannot finish his assignments.

Sample Input 3

11 2
5 6

Sample Output 3

0

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

Sample Input 4

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

Sample Output 4

9


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
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

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

int main(){
  ll n,m;
  cin >> n >> m;
  ll sum = 0;
  rep(i,m){
    ll x; cin >> x;
    sum += x;
  }
  if(n<sum){
    cout << -1 << endl;
  } else {
    cout << n-sum << endl;
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main(){
   long long n,x;
   cin>>n;
    vector<long long>a(n,0);
   for(long long i=0;i<n-1;i++){
    cin>>x;
    a[x-1]++;
   }
   for(long long i=0;i<n;i++)
    cout<<a[i]<<endl;
}

Score : 300 points

Problem Statement

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

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

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

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

Constraints

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

1 \leq A_i < i

Input

Input is given from Standard Input in the following format:

N
A_2 ... A_N

Output

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

Sample Input 1

5
1 1 2 2

Sample Output 1

2
2
0
0
0

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

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

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

Sample Input 2

10
1 1 1 1 1 1 1 1 1

Sample Output 2

9
0
0
0
0
0
0
0
0
0

Sample Input 3

7
1 2 3 4 5 6

Sample Output 3

1
1
1
1
1
1
0


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n); for(int i = 0;i < (int)(n);i++)
#define all(x) (x).begin(),(x).end()
typedef long long ll;
ll per(ll n,ll x){
    ll ans = 1;
    rep(i,x)ans *= n-i;
    rep(i,x)ans /= i+1;
    ans %= 1000000000+7;
    return ans;
}
int main(){
    int n,k;
    cin >> n >> k;
    ll ans = 0;
    vector<ll>sum(n+1);
    sum[0] = 0;
    rep(i,n)sum[i+1] = sum[i] + i+1;
    for(int i = k;i <= n+1;i++){
        ll big = 0,sma = 0;
        if(n-i < 0)big = sum[n];
        else big = sum[n] - sum[n-i];

        sma = sum[i-1];
        //cout << i << ' ' << big << ' ' << sma << endl;
        ans += big - sma+1;
        ans %= 1000000000+7;
    }
    cout << ans << endl;
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
int main() {
	long long n, k;
	cin >> n >> k;
	long long ans = 0;
	for (long long i = k; i <= n; i++) {
		long long min = i * (i - 1) / 2;
		long long max = (n + n - i + 1) * i / 2;
		ans = (ans + (max - min + 1)) % 1000000007;
	}
	cout << (ans + 1) % 1000000007 << endl;
}