#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,t;
    cin >> n >> t;

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

    int ans = t;
    for (int i = 1; i < n - 1; i++)
    {
            ans += min(ti[i] - ti[i - 1],t);
    }

    cout << ans << endl;
}

Score : 300 points

Problem Statement

In a public bath, there is a shower which emits water for T seconds when the switch is pushed.

If the switch is pushed when the shower is already emitting water, from that moment it will be emitting water for T seconds.
Note that it does not mean that the shower emits water for T additional seconds.

N people will push the switch while passing by the shower.
The i-th person will push the switch t_i seconds after the first person pushes it.

How long will the shower emit water in total?

Constraints

1 ≤ N ≤ 200,000

1 ≤ T ≤ 10^9

0 = t_1 < t_2 < t_3 < , ..., < t_{N-1} < t_N ≤ 10^9

T and each t_i are integers.

Input

Input is given from Standard Input in the following format:

N T
t_1 t_2 ... t_N

Output

Assume that the shower will emit water for a total of X seconds. Print X.

Sample Input 1

2 4
0 3

Sample Output 1

7

Three seconds after the first person pushes the water, the switch is pushed again and the shower emits water for four more seconds, for a total of seven seconds.

Sample Input 2

2 4
0 5

Sample Output 2

8

One second after the shower stops emission of water triggered by the first person, the switch is pushed again.

Sample Input 3

4 1000000000
0 1000 1000000 1000000000

Sample Output 3

2000000000

Sample Input 4

1 1
0

Sample Output 4

1

Sample Input 5

9 10
0 3 5 7 100 110 200 300 311

Sample Output 5

67


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

int main(){
  ll n, T;
  cin >> n >> T;
  vector<ll> t(n);
  for(int i=0; i<n; i++) cin >> t[i];

  ll ans=0;
  for(int i=0; i<n-1; i++){
    ans+=min(t[i+1]-t[i],T);
  }
  cout << ans+T << 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 "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;
using ll = long long;
using P = pair<int, int>;
#define rep(i,n) for(int i = 0; i < n; i++)

int main() {
	string a, b;
	cin >> a >> b;
	int p = a.size();
	int q = b.size();
	if (p > q) cout << "GREATER" << endl;
	else if (p < q) cout << "LESS" << endl;
	else {
		string ans = "EQUAL";
		rep(i, p) {
			int nowa = a[i] - '0';
			int nowb = b[i] - '0';
			if (nowa > nowb) {
				ans = "GREATER";
				break;
			}
			else if (nowb > nowa) {
				ans = "LESS";
				break;
			}
		}
		cout << ans << endl;
	}
	return 0;
}

Score : 200 points

Problem Statement

You are given two positive integers A and B. Compare the magnitudes of these numbers.

Constraints

1 ≤ A, B ≤ 10^{100}

Neither A nor B begins with a 0.

Input

Input is given from Standard Input in the following format:

A
B

Output

Print GREATER if A>B, LESS if A<B and EQUAL if A=B.

Sample Input 1

36
24

Sample Output 1

GREATER

Since 36>24, print GREATER.

Sample Input 2

850
3777

Sample Output 2

LESS

Sample Input 3

9720246
22516266

Sample Output 3

LESS

Sample Input 4

123456789012345678901234567890
234567890123456789012345678901

Sample Output 4

LESS


Score : 900 points

Problem Statement

Alice lives on a line. Today, she will travel to some place in a mysterious vehicle.
Initially, the distance between Alice and her destination is D. When she input a number x to the vehicle, it will travel in the direction of the destination by a distance of x if this move would shorten the distance between the vehicle and the destination, and it will stay at its position otherwise. Note that the vehicle may go past the destination when the distance between the vehicle and the destination is less than x.

Alice made a list of N numbers. The i-th number in this list is d_i. She will insert these numbers to the vehicle one by one.

However, a mischievous witch appeared. She is thinking of rewriting one number in the list so that Alice will not reach the destination after N moves.

She has Q plans to do this, as follows:

Rewrite only the q_i-th number in the list with some integer so that Alice will not reach the destination.

Write a program to determine whether each plan is feasible.

Constraints

1≤ N ≤ 5*10^5

1≤ Q ≤ 5*10^5

1≤ D ≤ 10^9

1≤ d_i ≤ 10^9(1≤i≤N)

1≤ q_i ≤ N(1≤i≤Q)

D and each d_i are integers.

Input

Input is given from Standard Input in the following format:

N D
d_1 d_2 ... d_N
Q
q_1 q_2 ... q_Q

Output

Print Q lines. The i-th line should contain YES if the i-th plan is feasible, and NO otherwise.

Sample Input 1

4 10
3 4 3 3
2
4 3

Sample Output 1

NO
YES

For the first plan, Alice will already arrive at the destination by the first three moves, and therefore the answer is NO.
For the second plan, rewriting the third number in the list with 5 will prevent Alice from reaching the destination as shown in the following figure, and thus the answer is YES.

Sample Input 2

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

Sample Output 2

YES
YES
YES
YES
YES

Alice will not reach the destination as it is, and therefore all the plans are feasible.

Sample Input 3

6 15
4 3 5 4 2 1
6
1 2 3 4 5 6

Sample Output 3

NO
NO
YES
NO
NO
YES


#include "bits/stdc++.h"

using namespace std;
using ll = long long;

int direct(int x, int y) {
    if (x == y) return 0;
    if (x > y) return 1;
    return 2;
}

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

    vector<ll> A(N);
    for (int i = 0; i < N; ++i) cin >> A[i];

    int ans = 1, i = 0;
    while (i < N - 1) {
        if (A[i] == A[i + 1]) {
            ++i;
            continue;
        }
        int group = direct(A[i], A[i + 1]);
        int j = i;
        for (; j < N - 1; ++j) {
            if (A[j] == A[j + 1]) continue;
            if (group != direct(A[j], A[j + 1])) {
                ans += 1;
                break;
            }
        }
        i = j + 1;
    }
    cout << ans << endl;
}

Score : 300 points

Problem Statement

You are given an array A of length N.
Your task is to divide it into several contiguous subarrays.
Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order.
At least how many subarrays do you need to divide A into?

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Each A_i is an integer.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible number of subarrays after division of A.

Sample Input 1

6
1 2 3 2 2 1

Sample Output 1

2

One optimal solution is to divide the array into [1,2,3] and [2,2,1].

Sample Input 2

9
1 2 1 2 1 2 1 2 1

Sample Output 2

5

Sample Input 3

7
1 2 3 2 1 999999999 1000000000

Sample Output 3

3


#include<iostream>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
	int n;
	cin>>n;
	long long a[100001];
	for(int i=0;i<n;i++)
		cin>>a[i];
	int sum=1;
	for(int i=1;i<n-1;i++){
		if(a[i]==a[i-1])
			a[i-1]=a[i-2];
		if((a[i]>a[i-1]&&a[i]>a[i+1])||(a[i]<a[i-1]&&a[i]<a[i+1])){
			sum++;a[i]=a[i+1];
		}
	}
	cout<<sum;
}


#include <cstdio>
using namespace std;
const int maxn = 1e5+5;
int d[maxn];
int n;
int main(){
//freopen("input.txt", "r", stdin);
    while(scanf("%d", &n) == 1){
        for(int i = 0; i < n; i++)
            scanf("%d", &d[i]);
        bool fg1;
        if(n == 1)
        {
            printf("1\n");
            continue;
        }
//        fg1 = d[1] - d[0] ? true : false;
        int ans = 0;
        int len = 1;

        for(int i = 1; i < n; i++)
        {
            if(len == 1) fg1 = (d[i] - d[i-1] > 0)? true : false;
            if(fg1 == true)
            {
                if(d[i] >= d[i-1]) {len++; continue;}
                else{
                    fg1 = false;
                    if(len > 1){ans++; len = 1;}
                }
            }
            else{
                if(d[i] <= d[i-1]) {len++;continue;}
                else{
                    fg1 = true;
                    if(len > 1){ans++; len = 1;}
                }
            }
        }
        printf("%d\n", ans+1);
    }
    return 0;
}


/*
    Problem 14
    https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b
*/
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
/* function */
/* main */
int main(){
    int n, k;
    cin >> n >> k;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];

    ll ans = LINF;
    // 一番前は絶対見えるので
    // 2番目以降からK - 1個選んで見えるようにする
    for (int b = 0; b < (1 << (n - 1)); b++) {
        if (__builtin_popcount(b) != k - 1) continue;
        ll maxH = a[0], sum = 0;
        for (int i = 1; i < n; i++) {
            if ((b >> (i - 1)) & 1) {
                if (maxH >= a[i]) {  // 低ければmaxH+1まで増築
                    maxH += 1;
                    sum += maxH - a[i];
                }
            }
            maxH = max(a[i], maxH);  // 高さの更新
        }
        ans = min(sum, ans);
    }
    cout << ans << '\n';
}

Max Score: 350 Points

Problem Statement

There are N buildings along the line. The i-th building from the left is colored in color i, and its height is currently a_i meters.

Chokudai is a mayor of the city, and he loves colorful thigs. And now he wants to see at least K buildings from the left.

You can increase height of buildings, but it costs 1 yens to increase 1 meters. It means you cannot make building that height is not integer.

You cannot decrease height of buildings.

Calculate the minimum cost of satisfying Chokudai's objective.

Note: "Building i can see from the left" means there are no j exists that (height of building j) ≥ (height of building i) and j < i.

Input Format

N K
a_1 a_2 a_3 ... a_N

Output Format

Print the minimum cost in one line. In the end put a line break.

Constraints

1 ≤ K ≤ N ≤ 15

1 ≤ a_i ≤ 10^9

Scoring

Subtask 1 [120 points]

N = K

Subtask 2 [90 points]

N ≤ 5

a_i ≤ 7

Subtask 3 [140 points]

There are no additional constraints.

Sample Input 1

5 5
3949 3774 3598 3469 3424

Sample Output 1

1541

The optimal solution is (height of buildings from the left) = [3949, 3950, 3951, 3952, 3953].

Sample Input 2

5 3
7 4 2 6 4

Sample Output 2

7

The optimal solution is (height of buildings from the left) = [7, 8, 2, 9, 4].


#include <bits/stdc++.h>
#include <math.h>
#include <cmath>

using namespace std;
using ll =long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;
using vvs = vector<vs>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;

#define rep(i,n); for( int i =0; i < n ; i ++)
#define all(a) a.begin(),a.end()
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i=a;i<b;i++)
#define pri(a)  printf("%.14lf\n",a);
#define MOD 1000000007
typedef pair<int, int> P;
bool is_int_lround(double x){
  return std::lround(x)==x;
}
ll keta(ll x){
  ll n=0;
  while(x>0){
    x /=10 ;
    n ++;
  }
  return n;
}

ll conbi(int n,int m){

  cin>>n>>m;
  vector<ll> a(100);
  a[0] =1;
  for(int i=0;i<14;i++){
    a[i+1]=a[i]*(i+1);
  }
  return a[n] /(a[m] *a[n-m]);
}

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;//繰り返し二乗法
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}
ll kaijo(ll x){
  ll z=1;
  while(x>0){
    z *=x;
    z %=MOD;
    x--;
  }
  return z;
}
ll yakusu_num(ll n){
  vl yakusu(n+1,1);
  for(ll i=2;i*i<=n;i++){
    while(n%i==0){
      n /=i;
      yakusu[i]++;
    }
  }
  if(n!=1)yakusu[n]++;
  ll num=1;
  for(ll i=0;i <=n;i++){
    num*=yakusu[i];
  }
  return num;
}

//cout<<""<<endl;
int main(){
  ll N,K;cin>>N>>K;vl a(N);
  rep(i,N){
    cin>>a[i];
  }

  ll ans=10000000000;
  for(int tmp ; tmp<(1<<(N-1));tmp++){
    bitset<14> s(tmp);
    ll cnt=0;
    ll ok=1;
    ll ma=a[0];
    for(int i=0;i<N-1;i++){
      if(a[i+1]>ma)ok++;
      if(s.test(i)){
        if(ma<a[i+1])ma=a[i+1];
        else{
          cnt+=-a[i+1]+ma+1;
          ma++;ok++;
        }
      }
      else ma =max(ma,a[i+1]);
    }
    if(ok>=K)ans=min(ans,cnt);
  }
  cout<<ans<<endl;
}


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

int main(){
int a,b,c;
  cin>>a>>b>>c;
  if(b-a==c-b){
cout<<"YES"<<endl;
  }
  else{
cout<<"NO"<<endl;
  }
}

Score : 100 points

Problem Statement

Three poles stand evenly spaced along a line. Their heights are a, b and c meters, from left to right.
We will call the arrangement of the poles beautiful if the tops of the poles lie on the same line, that is, b-a = c-b.

Determine whether the arrangement of the poles is beautiful.

Constraints

1 \leq a,b,c \leq 100

a, b and c are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

Print YES if the arrangement of the poles is beautiful; print NO otherwise.

Sample Input 1

2 4 6

Sample Output 1

YES

Since 4-2 = 6-4, this arrangement of poles is beautiful.

Sample Input 2

2 5 6

Sample Output 2

NO

Since 5-2 \neq 6-5, this arrangement of poles is not beautiful.

Sample Input 3

3 2 1

Sample Output 3

YES

Since 1-2 = 2-3, this arrangement of poles is beautiful.


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

int main() {
  int n; cin>>n;
  vector<string> v(n);
  rep(i,n){cin>>v[i];}
  string s="abcdefghijklmnopqrstuvwxyz";
  rep(i,26){
    int x=50;
    rep(j,n){
      int c=count(v[j].begin(),v[j].end(),s.at(i));
      if(x>c){x=c;}
    }
    rep(k,x){cout<<s.at(i);}
    //cout<<x;
  }
}

Score : 300 points

Problem Statement

Snuke loves "paper cutting": he cuts out characters from a newspaper headline and rearranges them to form another string.

He will receive a headline which contains one of the strings S_1,...,S_n tomorrow.
He is excited and already thinking of what string he will create.
Since he does not know the string on the headline yet, he is interested in strings that can be created regardless of which string the headline contains.

Find the longest string that can be created regardless of which string among S_1,...,S_n the headline contains.
If there are multiple such strings, find the lexicographically smallest one among them.

Constraints

1 \leq n \leq 50

1 \leq |S_i| \leq 50 for every i = 1, ..., n.

S_i consists of lowercase English letters (a - z) for every i = 1, ..., n.

Input

Input is given from Standard Input in the following format:

n
S_1
...
S_n

Output

Print the lexicographically smallest string among the longest strings that satisfy the condition.
If the answer is an empty string, print an empty line.

Sample Input 1

3
cbaa
daacc
acacac

Sample Output 1

aac

The strings that can be created from each of cbaa, daacc and acacac, are aa, aac, aca, caa and so forth.
Among them, aac, aca and caa are the longest, and the lexicographically smallest of these three is aac.

Sample Input 2

3
a
aa
b

Sample Output 2

The answer is an empty string.


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MOD 1000000007
#define PI 3.141592653589793
int dx[] = {0,1,-1,0,1,-1,1,-1};
int dy[] = {1,0,0,-1,1,-1,-1,1};

int main(){
    int n;
    cin >> n;
    vector<string> v(n);
    for(int i = 0; i < n; i++)
        cin >> v[i];
    map<char, int> m;
    for(auto s: v){
        map<char, int> m2;
        for(auto c: s){
            m2[c]++;
        }
        for(auto p: m2){
            if(m[p.first] == 0 || m[p.first] > p.second)
                m[p.first] = p.second;
        }
    }
    for(auto s: v){
        for(auto p: m){
            if(s.find(p.first) == string::npos)
                m[p.first] = 0;
        }
    }
    string ans = "";
    for(auto p: m){
        for(int i = 0; i < p.second; i++)
            ans += p.first;
    }
    sort(ans.begin(), ans.end());
    cout << ans << endl;
}


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <cmath>

using namespace std;

typedef long long ll;
typedef long double ld;

const ll MOD_CONST = 1000000007ll;

ll modSum(ll a, ll b, ll MOD = MOD_CONST) {
  return ((a % MOD) + (b % MOD)) % MOD;
}

ll modSubtract(ll a, ll b, ll MOD = MOD_CONST) {
  return (((a % MOD) - (b % MOD)) + MOD + MOD) % MOD;
}

ll modProd(ll a, ll b, ll MOD = MOD_CONST) {
  return ((a % MOD) * (b % MOD)) % MOD;
}

ll getPowMod(ll x, ll e, ll MOD = MOD_CONST) {
  if (e == 0) return 1;
  if (e % 2 == 0) {
    ll tmp = getPowMod(x, e/2, MOD);
    return modProd(tmp, tmp);
  } else {
    ll tmp = getPowMod(x, e-1, MOD);
    return modProd(tmp, x);
  }
}

ll getPow(ll x, ll e) {
  if (e == 0) return 1;
  if (e % 2 == 0) {
    ll tmp = getPow(x, e/2);
    return tmp * tmp;
  } else {
    ll tmp = getPow(x, e-1);
    return tmp * x;
  }
}

ll getInverse(ll x, ll MOD = MOD_CONST) {
  return getPowMod(x, MOD-2, MOD);
}

bool isEven(ll x) {
  ll tmp = ((x % 2) + 2) % 2;
  return tmp == 0;
}

ll getSumOfDigitsInBase(ll n, ll b) {
  ll ret = 0;
  while (n > 0) {
    ret += n % b;
    n /= b;
  }
  return ret;
}

vector<int> getKMP(string &s) {
  int len = (int)s.size();
  vector<int> ret (len, 0);
  for (int i = 1 ; i < len ; i++) {
    int at = ret[i-1];
    while (at > 0 && s[i] != s[at]) {
      at = ret[at-1];
    }
    if (s[i] == s[at]) {
      at++;
    }
    ret[i] = at;
  }
  return ret;
}

string getSubstring(string &s, int from, int to) {
  int l = to-from+1;
  if (l <= 0) {
    return "";
  }
  return s.substr(from, l);
}

ll gcd(ll a, ll b, ll & x, ll & y) {
    if (a == 0) {
        x = 0;
        y = 1;
        return b;
    }
    ll x1, y1;
    ll d = gcd(b % a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}

pair<ll, ll> getIntersectingRange(ll a1, ll b1, ll a2, ll b2) {
  ll s = max(a1, a2);
  ll e = min(b1, b2);

  return {s, e};
}

bool isNonEmptyIntersection(ll a1, ll b1, ll a2, ll b2) {
  auto p = getIntersectingRange(a1, b1, a2, b2);
  return p.first <= p.second;
}

double getPointDistance(double x1, double y1, double x2, double y2) {
  double dx = x1-x2;
  double dy = y1-y2;
  double d = (dx * dx) + (dy * dy);
  return sqrt(d);
}

bool isPrime(ll x) {
  if (x == 2 || x == 3 || x == 5 || x == 7) return true;
  if (x < 10) return false;
  ll till = min((ll)sqrt(x) + 1, x-1);
  for (ll i = 2 ; i <= till ; i++) {
    if (x % i == 0) {
      return false;
    }
  }
  return true;
}

const int TREE_SIZE = 1;

ll segTree[TREE_SIZE], lazyTree[TREE_SIZE];

void updateRange(int node, int start, int end, int l, int r, int val)
{
    if(lazyTree[node] != 0)
    {
        // This node needs to be updated
        segTree[node] += (end - start + 1) * lazyTree[node];    // Update it
        if(start != end)
        {
            lazyTree[node*2] += lazyTree[node];                  // Mark child as lazyTree
            lazyTree[node*2+1] += lazyTree[node];                // Mark child as lazyTree
        }
        lazyTree[node] = 0;                                  // Reset it
    }
    if(start > end or start > r or end < l)              // Current segment is not within range [l, r]
        return;
    if(start >= l and end <= r)
    {
        // Segment is fully within range
        segTree[node] += (end - start + 1) * val;
        if(start != end)
        {
            // Not leaf node
            lazyTree[node*2] += val;
            lazyTree[node*2+1] += val;
        }
        return;
    }
    int mid = (start + end) / 2;
    updateRange(node*2, start, mid, l, r, val);        // Updating left child
    updateRange(node*2 + 1, mid + 1, end, l, r, val);   // Updating right child
    segTree[node] = segTree[node*2] + segTree[node*2+1];        // Updating root with max value
}

ll queryRange(int node, int start, int end, int l, int r)
{
    if(start > end or start > r or end < l)
        return 0;         // Out of range
    if(lazyTree[node] != 0)
    {
        // This node needs to be updated
        segTree[node] += (end - start + 1) * lazyTree[node];            // Update it
        if(start != end)
        {
            lazyTree[node*2] += lazyTree[node];         // Mark child as lazyTree
            lazyTree[node*2+1] += lazyTree[node];    // Mark child as lazyTree
        }
        lazyTree[node] = 0;                 // Reset it
    }
    if(start >= l and end <= r)             // Current segment is totally within range [l, r]
        return segTree[node];
    int mid = (start + end) / 2;
    int p1 = queryRange(node*2, start, mid, l, r);         // Query left child
    int p2 = queryRange(node*2 + 1, mid + 1, end, l, r); // Query right child
    return (p1 + p2);
}

const int MAXN = 100001;

int Q;
string s, t;
int memo_sa[MAXN], memo_sb[MAXN], memo_ta[MAXN], memo_tb[MAXN];

int getClass(int from, int to, int * memo_a, int * memo_b) {

  int ca = memo_a[to] - memo_a[from-1];
  int cb = memo_b[to] - memo_b[from-1];

  return (ca + cb + cb) % 3;
}

void input() {
  cin >> s >> t;
  cin >> Q;
}

void preprocess() {
  for (int i = 1 ; i <= (int)s.size() ; i++) {
    memo_sa[i] = memo_sa[i-1] + (s[i-1] == 'A');
  }
  for (int i = 1 ; i <= (int)s.size() ; i++) {
    memo_sb[i] = memo_sb[i-1] + (s[i-1] == 'B');
  }
  for (int i = 1 ; i <= (int)t.size() ; i++) {
    memo_ta[i] = memo_ta[i-1] + (t[i-1] == 'A');
  }
  for (int i = 1 ; i <= (int)t.size() ; i++) {
    memo_tb[i] = memo_tb[i-1] + (t[i-1] == 'B');
  }
}

void solve() {
  int a, b, c, d;
  for (int i = 1 ; i <= Q ; i++) {
    cin >> a >> b >> c >> d;
    int c1 = getClass(a, b, memo_sa, memo_sb);
    int c2 = getClass(c, d, memo_ta, memo_tb);
    //cerr << "C1 = " << c1 << ", C2 = " << c2 << "\n";
    if (c1 == c2) {
      cout << "YES\n";
    } else {
      cout << "NO\n";
    }
  }
}

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

  int T;
//  cin >> T;
  T = 1;

  for (int i = 0 ; i < T ; i++) {
    input();
    preprocess();
    solve();
  }

  return 0;
}

Score : 600 points

Problem Statement

Let us consider the following operations on a string consisting of A and B:

Select a character in a string. If it is A, replace it with BB. If it is B, replace with AA.

Select a substring that is equal to either AAA or BBB, and delete it from the string.

For example, if the first operation is performed on ABA and the first character is selected, the string becomes BBBA.
If the second operation is performed on BBBAAAA and the fourth through sixth characters are selected, the string becomes BBBA.

These operations can be performed any number of times, in any order.

You are given two string S and T, and q queries a_i, b_i, c_i, d_i.
For each query, determine whether S_{a_i} S_{{a_i}+1} ... S_{b_i}, a substring of S, can be made into T_{c_i} T_{{c_i}+1} ... T_{d_i}, a substring of T.

Constraints

1 \leq |S|, |T| \leq 10^5

S and T consist of letters A and B.

1 \leq q \leq 10^5

1 \leq a_i \leq b_i \leq |S|

1 \leq c_i \leq d_i \leq |T|

Input

Input is given from Standard Input in the following format:

S
T
q
a_1 b_1 c_1 d_1
...
a_q b_q c_q d_q

Output

Print q lines. The i-th line should contain the response to the i-th query. If S_{a_i} S_{{a_i}+1} ... S_{b_i} can be made into T_{c_i} T_{{c_i}+1} ... T_{d_i}, print YES. Otherwise, print NO.

Sample Input 1

BBBAAAABA
BBBBA
4
7 9 2 5
7 9 1 4
1 7 2 5
1 7 2 4

Sample Output 1

YES
NO
YES
NO

The first query asks whether the string ABA can be made into BBBA.
As explained in the problem statement, it can be done by the first operation.

The second query asks whether ABA can be made into BBBB, and the fourth query asks whether BBBAAAA can be made into BBB.
Neither is possible.

The third query asks whether the string BBBAAAA can be made into BBBA.
As explained in the problem statement, it can be done by the second operation.

Sample Input 2

AAAAABBBBAAABBBBAAAA
BBBBAAABBBBBBAAAAABB
10
2 15 2 13
2 13 6 16
1 13 2 20
4 20 3 20
1 18 9 19
2 14 1 11
3 20 3 15
6 16 1 17
4 18 8 20
7 20 3 14

Sample Output 2

YES
YES
YES
YES
YES
YES
NO
NO
NO
NO


#include <bits/stdc++.h>

using namespace std;

int N;
int a[100000];

void solve() {
    long long num = 0;
    sort(a, a + N);
    for (int i = 0; i < N; i++) {
        int index = 3 * N - 2 * i - 1;
        num += (long long) a[index];
    }
    printf("%lld\n", num);
}

int main() {
    cin >> N;
    for (int i = 0; i < N * 3; ++i) {
        cin >> a[i];
    }
    solve();
    return 0;
}

Score : 300 points

Problem Statement

There are 3N participants in AtCoder Group Contest.
The strength of the i-th participant is represented by an integer a_i.
They will form N teams, each consisting of three participants.
No participant may belong to multiple teams.

The strength of a team is defined as the second largest strength among its members.
For example, a team of participants of strength 1, 5, 2 has a strength 2, and a team of three participants of strength 3, 2, 3 has a strength 3.

Find the maximum possible sum of the strengths of N teams.

Constraints

1 ≤ N ≤ 10^5

1 ≤ a_i ≤ 10^{9}

a_i are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the answer.

Sample Input 1

2
5 2 8 5 1 5

Sample Output 1

10

The following is one formation of teams that maximizes the sum of the strengths of teams:

Team 1: consists of the first, fourth and fifth participants.

Team 2: consists of the second, third and sixth participants.

Sample Input 2

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 2

10000000000

The sum of the strengths can be quite large.


// This amazing code is by Eric Sunli Chen.
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
template<typename T> void get_int(T &x)
{
	char t=getchar();
	bool neg=false;
	x=0;
	for(; (t>'9'||t<'0')&&t!='-'; t=getchar());
	if(t=='-')neg=true,t=getchar();
	for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0';
	if(neg)x=-x;
}
template<typename T> void print_int(T x)
{
	if(x<0)putchar('-'),x=-x;
	short a[20]= {},sz=0;
	while(x>0)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1; i>=0; i--)putchar('0'+a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a,b) get1(a),get1(b)
#define get3(a,b,c) get1(a),get2(b,c)
#define printendl(a) print_int(a),puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const LL Linf=1ll<<61;
const double pi=acos(-1.0);

int n,v,a[200111],nxt[20][200111],pre[20][200111],k,v2[20],dp1[1<<20],dp2[1<<20];
bool ans[200111];

int main()
{
	get2(n,v);
	int tmp=v;
	while(tmp)
	{
		v2[k++]=tmp;
		tmp>>=1;
	}
	v2[k++]=0;reverse(v2,v2+k);
	for(int i=1;i<=n;i++)get1(a[i]);
	for(int i=0;i<k;i++)
	{
		nxt[i][n+1]=n+1;pre[i][0]=0;
		for(int j=1,j2;j<=n;j=j2)
		{
			j2=j+1;
			while(j2<=n&&a[j2]-a[j2-1]<=v2[i])j2++;
			for(int t=j;t<j2;t++)
			{
				nxt[i][t]=j2;
				pre[i][t]=j-1;
			}
		}
	}

	for(int i=0;i<(1<<k-1);i++)
	{
		dp1[i]=1;
		dp2[i]=n;
	}
	for(int i=0;i<(1<<k-1);i++)
	{
		for(int j=0;j<k-1;j++)if(((i>>j)&1)==0)
		{
			dp1[i^(1<<j)]=max(dp1[i^(1<<j)],nxt[j][dp1[i]]);
			dp2[i^(1<<j)]=min(dp2[i^(1<<j)],pre[j][dp2[i]]);
		}
	}
	if(dp1[(1<<k-1)-1]==n+1)
	{
		for(int i=1;i<=n;i++)puts("Possible");
		return 0;
	}
	int all=(1<<k-1)-1;
	for(int i=0;i<(1<<k-1);i++)if(dp2[all^i]<nxt[k-1][dp1[i]])ans[dp1[i]]=1;
	for(int i=1;i<=n;i=nxt[k-1][i])
	{
		for(int j=i;j<nxt[k-1][i];j++)
		{
			if(ans[i])puts("Possible");
			else puts("Impossible");
		}
	}
	return 0;
}

Score : 1000 points

Problem Statement

There are N oases on a number line. The coordinate of the i-th oases from the left is x_i.

Camel hopes to visit all these oases.
Initially, the volume of the hump on his back is V. When the volume of the hump is v, water of volume at most v can be stored. Water is only supplied at oases. He can get as much water as he can store at a oasis, and the same oasis can be used any number of times.

Camel can travel on the line by either walking or jumping:

Walking over a distance of d costs water of volume d from the hump. A walk that leads to a negative amount of stored water cannot be done.

Let v be the amount of water stored at the moment. When v>0, Camel can jump to any point on the line of his choice. After this move, the volume of the hump becomes v/2 (rounded down to the nearest integer), and the amount of stored water becomes 0.

For each of the oases, determine whether it is possible to start from that oasis and visit all the oases.

Constraints

2 ≤ N,V ≤ 2 × 10^5

-10^9 ≤ x_1 < x_2 < ... < x_N ≤ 10^9

V and x_i are all integers.

Input

Input is given from Standard Input in the following format:

N V
x_1 x_2 ... x_{N}

Output

Print N lines. The i-th line should contain Possible if it is possible to start from the i-th oasis and visit all the oases, and Impossible otherwise.

Sample Input 1

3 2
1 3 6

Sample Output 1

Possible
Possible
Possible

It is possible to start from the first oasis and visit all the oases, as follows:

Walk from the first oasis to the second oasis. The amount of stored water becomes 0.

Get water at the second oasis. The amount of stored water becomes 2.

Jump from the second oasis to the third oasis. The amount of stored water becomes 0, and the volume of the hump becomes 1.

Sample Input 2

7 2
-10 -4 -2 0 2 4 10

Sample Output 2

Impossible
Possible
Possible
Possible
Possible
Possible
Impossible

A oasis may be visited any number of times.

Sample Input 3

16 19
-49 -48 -33 -30 -21 -14 0 15 19 23 44 52 80 81 82 84

Sample Output 3

Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Possible
Impossible
Impossible
Impossible
Impossible


#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<utility>
#include <functional>
#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <stack>

using namespace std;

int main() {

	int n;
	cin >> n;
	int m;
	cin >> m;
	cout << (n + m) % 24;
}

Score : 100 points

Problem Statement

Dolphin loves programming contests. Today, he will take part in a contest in AtCoder.

In this country, 24-hour clock is used. For example, 9:00 p.m. is referred to as "21 o'clock".

The current time is A o'clock, and a contest will begin in exactly B hours.
When will the contest begin? Answer in 24-hour time.

Constraints

0 \leq A,B \leq 23

A and B are integers.

Input

The input is given from Standard Input in the following format:

A B

Output

Print the hour of the starting time of the contest in 24-hour time.

Sample Input 1

9 12

Sample Output 1

21

In this input, the current time is 9 o'clock, and 12 hours later it will be 21 o'clock in 24-hour time.

Sample Input 2

19 0

Sample Output 2

19

The contest has just started.

Sample Input 3

23 2

Sample Output 3

1

The contest will begin at 1 o'clock the next day.


#include<iostream>

using namespace std;

int main(){
  int A,B;

  cin>>A>>B;

  if(A+B>24){
    cout<<A+B-24<<endl;
  }
  else{
    cout<<A+B<<endl;
  }

}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(int)(n); i++)
#define all(vec) vec.begin(), vec.end()
#define MOD 1000000007
#define int long long

signed main(){
    int n,m;cin>>n>>m;
    pair<int,int> s[n];
    pair<int,int> p[m];
    for(int i=0;i<n;i++)cin>>s[i].first>>s[i].second;
    for(int i=0;i<m;i++)cin>>p[i].first>>p[i].second;
    for(int i=0;i<n;i++){
        int min[2]={-1,(int)MOD*MOD};
        for(int j=0;j<m;j++){
            int manhattan=abs(s[i].first-p[j].first)+abs(s[i].second-p[j].second);
            if(manhattan<min[1]){
                min[0]=j+1;
                min[1]=manhattan;
            }
        }
        cout<<min[0]<<endl;
    }
}

Score : 200 points

Problem Statement

There are N students and M checkpoints on the xy-plane.

The coordinates of the i-th student (1 \leq i \leq N) is (a_i,b_i), and the coordinates of the checkpoint numbered j (1 \leq j \leq M) is (c_j,d_j).

When the teacher gives a signal, each student has to go to the nearest checkpoint measured in Manhattan distance.

The Manhattan distance between two points (x_1,y_1) and (x_2,y_2) is |x_1-x_2|+|y_1-y_2|.

Here, |x| denotes the absolute value of x.

If there are multiple nearest checkpoints for a student, he/she will select the checkpoint with the smallest index.

Which checkpoint will each student go to?

Constraints

1 \leq N,M \leq 50

-10^8 \leq a_i,b_i,c_j,d_j \leq 10^8

All input values are integers.

Input

The input is given from Standard Input in the following format:

N M
a_1 b_1
:
a_N b_N
c_1 d_1
:
c_M d_M

Output

Print N lines.

The i-th line (1 \leq i \leq N) should contain the index of the checkpoint for the i-th student to go.

Sample Input 1

2 2
2 0
0 0
-1 0
1 0

Sample Output 1

2
1

The Manhattan distance between the first student and each checkpoint is:

For checkpoint 1: |2-(-1)|+|0-0|=3

For checkpoint 2: |2-1|+|0-0|=1

The nearest checkpoint is checkpoint 2. Thus, the first line in the output should contain 2.

The Manhattan distance between the second student and each checkpoint is:

For checkpoint 1: |0-(-1)|+|0-0|=1

For checkpoint 2: |0-1|+|0-0|=1

When there are multiple nearest checkpoints, the student will go to the checkpoint with the smallest index. Thus, the second line in the output should contain 1.

Sample Input 2

3 4
10 10
-10 -10
3 3
1 2
2 3
3 5
3 5

Sample Output 2

3
1
2

There can be multiple checkpoints at the same coordinates.

Sample Input 3

5 5
-100000000 -100000000
-100000000 100000000
100000000 -100000000
100000000 100000000
0 0
0 0
100000000 100000000
100000000 -100000000
-100000000 100000000
-100000000 -100000000

Sample Output 3

5
4
3
2
1


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

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

  int a[N],b[N];
  rep(i,N) cin >>a[i]>>b[i];

  int c[M], d[M];
  rep(i,M) cin >>c[i]>>d[i];

  int ans;
  rep(i,N){
    int mnD = abs(a[i]-c[0]) + abs(b[i]-d[0]);
    ans = 1;
    rep(j,M){
      int D = abs(a[i]-c[j]) + abs(b[i]-d[j]);
      if(D<mnD){
        ans = j+1;
        mnD = D;
      }
    }
    cout <<ans<< endl;
  }
}


/*
これを入れて実行
g++ code.cpp
./a.out
 */

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>

#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
typedef long long ll;
typedef long double ld;

int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};

const long long INF = 1LL << 62;
const ll MOD = 1e9 + 7;

bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}

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

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

ll conbinationMemo[100][100];

ll cmemoInit(){
    rep(i, 100){
        rep(j, 100){
            conbinationMemo[i][j] = -1;
        }
    }
}

ll nCr(ll n, ll r){
    if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}

ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}

//-----------------------ここから-----------
int main(void){
    ll n, a, b;
    cin >> n >> a >> b;
    vector<ld> vec(n);
    rep(i, n) cin >> vec[i];
    sort(all(vec), greater<ld>());

    ld total = 0;
    rep(i, a){
        total += vec[i];
    }

    ld ave = total / (ld)a;

    ll cnt = 0;

    for(int i = a - 1; i >= 0; i--){
        if(vec[a - 1] == vec[i]){
            cnt++;
        }
    }

    ll cnt2 = 0;
    for(int i = a; i < n; i++){
        if(vec[a - 1] == vec[i]){
            cnt2++;
        }
    }

    cmemoInit();

    cout << fixed << setprecision(10) << ave << endl;
    if(cnt != a){
        cout << nCr(cnt2 + cnt, cnt) << endl;
    } else {
        ll ans = 0;
        for(int i = a; i <= b; i++){
            if(cnt2 + cnt < i) continue;
            ans += nCr(cnt2 + cnt, (ll)i);
        }
        cout << ans << endl;
    }

}

Score : 400 points

Problem Statement

You are given N items.

The value of the i-th item (1 \leq i \leq N) is v_i.

Your have to select at least A and at most B of these items.

Under this condition, find the maximum possible arithmetic mean of the values of selected items.

Additionally, find the number of ways to select items so that the mean of the values of selected items is maximized.

Constraints

1 \leq N \leq 50

1 \leq A,B \leq N

1 \leq v_i \leq 10^{15}

Each v_i is an integer.

Input

The input is given from Standard Input in the following format:

N A B
v_1
v_2
...
v_N

Output

Print two lines.

The first line should contain the maximum possible arithmetic mean of the values of selected items. The output should be considered correct if the absolute or relative error is at most 10^{-6}.

The second line should contain the number of ways to select items so that the mean of the values of selected items is maximized.

Sample Input 1

5 2 2
1 2 3 4 5

Sample Output 1

4.500000
1

The mean of the values of selected items will be maximized when selecting the fourth and fifth items. Hence, the first line of the output should contain 4.5.

There is no other way to select items so that the mean of the values will be 4.5, and thus the second line of the output should contain 1.

Sample Input 2

4 2 3
10 20 10 10

Sample Output 2

15.000000
3

There can be multiple ways to select items so that the mean of the values will be maximized.

Sample Input 3

5 1 5
1000000000000000 999999999999999 999999999999998 999999999999997 999999999999996

Sample Output 3

1000000000000000.000000
1


#include <iostream>
using namespace std;

int main(void) {
    char a,b;cin>>a>>b;
    if (a == b) cout << "H" << endl;
    else cout << "D" << endl;
    return 0;
}

Score : 100 points

Problem Statement

Two deer, AtCoDeer and TopCoDeer, are playing a game called Honest or Dishonest.
In this game, an honest player always tells the truth, and an dishonest player always tell lies.
You are given two characters a and b as the input. Each of them is either H or D, and carries the following information:

If a=H, AtCoDeer is honest; if a=D, AtCoDeer is dishonest.
If b=H, AtCoDeer is saying that TopCoDeer is honest; if b=D, AtCoDeer is saying that TopCoDeer is dishonest.

Given this information, determine whether TopCoDeer is honest.

Constraints

a=H or a=D.

b=H or b=D.

Input

The input is given from Standard Input in the following format:

a b

Output

If TopCoDeer is honest, print H. If he is dishonest, print D.

Sample Input 1

H H

Sample Output 1

H

In this input, AtCoDeer is honest. Hence, as he says, TopCoDeer is honest.

Sample Input 2

D H

Sample Output 2

D

In this input, AtCoDeer is dishonest. Hence, contrary to what he says, TopCoDeer is dishonest.

Sample Input 3

D D

Sample Output 3

H


#include <iostream>
#include <vector>
#include <algorithm>

bool isValid(int target, const std::vector<int>& a, int K, int sum) {
    // calc minimum value X which satisfies X >= K
    // if X - a[target] >= K, card[target] is invalid.

    std::vector<int> mins;
    mins.reserve(4096);
    mins.push_back(sum);

    int min = sum;
    for (int i = a.size()-1; i >= 0; i--) {
        if (i == target)
            continue;

        int size = mins.size();
        for (int j = 0; j < size; j++) {
            if (mins[j] - a[i] >= K) {
                mins.push_back(mins[j] - a[i]);
                min = std::min(mins[j] - a[i], min);
            }
        }
    }

    // std::cout << "min: " << min << std::endl;
    if (min - a[target] < K) {
        return true;
    }

    return false;
}
// bool isValid(int idx, const std::vector<int>& a, int K, int sum) {
//     // calc minimum value X which satisfies X >= K
//     // if X - a[i] >= K, card i is invalid.

//     int X = sum;
//     for (int i = a.size()-1; i >= 0; i--) {
//         if (i == idx)
//             continue;

//         if (X - a[i] >= K) {
//             X -= a[i];
//             std::cout << "sum: " << sum << ", a[i]: " << a[i] << ", X: " << X << std::endl;
//         }
//     }

//     std::cout << X << std::endl;

//     return (X - a[idx] < K);
// }

int main(void) {
    int N, K;
    std::vector<int> a;

    std::cin >> N;
    std::cin >> K;

    int sum = 0;
    a.resize(N);
    for (int i = 0; i < N; i++) {
        std::cin >> a[i];
        sum += a[i];
    }

    std::sort(a.begin(), a.end());

    if (sum < K) {
        std::cout << N << std::endl;
        return 0;
    }

    int ninvalid = 0;
    for (int i = 0; i < N; i++) {
        if (isValid(i, a, K, sum)) {
            break;
        } else {
            ninvalid++;
        }
    }

    std::cout << ninvalid << std::endl;
}

Score : 600 points

Problem Statement

AtCoDeer the deer has N cards with positive integers written on them. The number on the i-th card (1≤i≤N) is a_i.
Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is K or greater.

Then, for each card i, he judges whether it is unnecessary or not, as follows:

If, for any good subset of the cards containing card i, the set that can be obtained by eliminating card i from the subset is also good, card i is unnecessary.

Otherwise, card i is NOT unnecessary.

Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.

Constraints

All input values are integers.

1≤N≤5000

1≤K≤5000

1≤a_i≤10^9 (1≤i≤N)

Partial Score

300 points will be awarded for passing the test set satisfying N,K≤400.

Input

The input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the number of the unnecessary cards.

Sample Input 1

3 6
1 4 3

Sample Output 1

1

There are two good sets: {2,3} and {1,2,3}.

Card 1 is only contained in {1,2,3}, and this set without card 1, {2,3}, is also good. Thus, card 1 is unnecessary.

For card 2, a good set {2,3} without card 2, {3}, is not good. Thus, card 2 is NOT unnecessary.

Neither is card 3 for a similar reason, hence the answer is 1.

Sample Input 2

5 400
3 1 4 1 5

Sample Output 2

5

In this case, there is no good set. Therefore, all the cards are unnecessary.

Sample Input 3

6 20
10 4 3 10 25 2

Sample Output 3

3


#include <iostream>
#include <algorithm>

using namespace std;

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

    sort(a,a+n);

    long long int b[n];
    b[0]=a[0];
    b[1]=a[1];
    for(int i=1;i<n;i++){
        b[i]=b[i-1]+a[i];
    }

    //for(int i=0;i<n;i++)cout<<b[i]<<endl;

    int ans=0;
    for(int i=n-1;i>=0;i--){
        if(b[i-1]*2>=a[i]){
            ans++;
        }else{
            break;
        }
    }
    cout<<min(ans+1,n)<<endl;
    return 0;
}

Score : 400 points

Problem Statement

Snuke found N strange creatures.
Each creature has a fixed color and size. The color and size of the i-th creature are represented by i and A_i, respectively.

Every creature can absorb another creature whose size is at most twice the size of itself.
When a creature of size A and color B absorbs another creature of size C and color D (C \leq 2 \times A), they will merge into one creature of size A+C and color B.
Here, depending on the sizes of two creatures, it is possible that both of them can absorb the other.

Snuke has been watching these creatures merge over and over and ultimately become one creature.
Find the number of the possible colors of this creature.

Constraints

2 \leq N \leq 100000

1 \leq A_i \leq 10^9

A_i is an integer.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

Print the number of the possible colors of the last remaining creature after the N creatures repeatedly merge and ultimately become one creature.

Sample Input 1

3
3 1 4

Sample Output 1

2

The possible colors of the last remaining creature are colors 1 and 3.
For example, when the creature of color 3 absorbs the creature of color 2, then the creature of color 1 absorbs the creature of color 3, the color of the last remaining creature will be color 1.

Sample Input 2

5
1 1 1 1 1

Sample Output 2

5

There may be multiple creatures of the same size.

Sample Input 3

6
40 1 30 2 7 20

Sample Output 3

4


#include<ctime>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cassert>
#include<string>
#include<sstream>
#include<fstream>
#include<deque>
#include<queue>
#include<vector>
#include<map>
#include<list>
#include<stack>
#include<set>
#include<bitset>
#include<iomanip>
#include<utility>
#include<functional>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cwchar>
#include<cwctype>
#include<exception>
#include<locale>
#include<numeric>
#include<new>
#include<stdexcept>
#include<limits>
using namespace std;

#define ll long long
#define INF 1e9
#define rep(i,n) for(int (i)=0;(i)<n;i++)
#define REP(i,n) for(int (i)=1;(i)<=n;i++)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
#define pii pair<int,int>

const int maxn=100005;
int n,ans;
int a[maxn],dp[maxn];

int main()
{
	scanf("%d",&n);
	rep(i,n)scanf("%d",&a[i]);
	sort(a,a+n);
	ll cur=0;
	rep(i,n-1)
	{
		cur+=(ll)a[i];
		if((ll)a[i+1]>cur*2)ans=i+1;
	}
	printf("%d",n-ans);
	return 0;
}


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

using ll = long long;

const int MAXN = 100010;
int N, M;
vector<int> adj[MAXN];
int col[MAXN];

int main() {
	ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0);
	cin >> N >> M;
	for (int i = 0; i < M; i++) {
		int u, v; cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}

	int nIsolated = 0;
	int nBipartite = 0;
	int nOther = 0;
	fill_n(col + 1, N, -1);
	for (int s = 1; s <= N; s++) {
		if (col[s] != -1) continue;
		vector<int> q = {s};
		col[s] = 0;
		bool isBip = true;
		for (size_t i = 0; i < q.size(); i++) {
			int cur = q[i];
			int colNxt = 1 - col[cur];
			for (size_t j = 0; j < adj[cur].size(); j++) {
				int nxt = adj[cur][j];
				if (col[nxt] == -1) {
					col[nxt] = colNxt;
					q.push_back(nxt);
				} else {
					isBip &= (col[nxt] == colNxt);
				}
			}
		}
		if (q.size() == 1) {
			nIsolated++;
		} else if (isBip) {
			nBipartite++;
		} else {
			nOther++;
		}
	}

	ll ans = 0;
	ans += ll(2) * nIsolated * N - ll(nIsolated) * nIsolated;
	ans += ll(2) * nBipartite * nBipartite;
	ans += ll(nOther) * nOther;
	ans += ll(2) * nOther * nBipartite;
	cout << ans << endl;

	return 0;
}

Score : 800 points

Problem Statement

Takahashi has received an undirected graph with N vertices, numbered 1, 2, ..., N.
The edges in this graph are represented by (u_i, v_i).
There are no self-loops and multiple edges in this graph.

Based on this graph, Takahashi is now constructing a new graph with N^2 vertices, where each vertex is labeled with a pair of integers (a, b) (1 \leq a \leq N, 1 \leq b \leq N).
The edges in this new graph are generated by the following rule:

Span an edge between vertices (a, b) and (a', b') if and only if both of the following two edges exist in the original graph: an edge between vertices a and a', and an edge between vertices b and b'.

How many connected components are there in this new graph?

Constraints

2 \leq N \leq 100,000

0 \leq M \leq 200,000

1 \leq u_i < v_i \leq N

There exists no pair of distinct integers i and j such that u_i = u_j and v_i = v_j.

Input

The input is given from Standard Input in the following format:

N M
u_1 v_1
u_2 v_2
:
u_M v_M

Output

Print the number of the connected components in the graph constructed by Takahashi.

Sample Input 1

3 1
1 2

Sample Output 1

7

The graph constructed by Takahashi is as follows.

Sample Input 2

7 5
1 2
3 4
3 5
4 5
2 6

Sample Output 2

18


#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;

const int maxn = 610000;

int a[maxn],n,k;
int sum;
void mul()
{
	for(int i=0;i<n;i++) a[i]*=9;
	for(int i=0;i<n;i++) if(a[i]>9)
	{
		a[i+1]+=a[i]/10;
		a[i]%=10;
	}
	while(a[n]) n++;
	for(int i=0;i<n;i++) sum+=a[i];
}
void add()
{
	a[0]+=9; sum+=9;
	int nowi=0;
	while(a[nowi]>9)
	{
		sum++; a[nowi+1]++;
		sum-=10; a[nowi]/=10;
	}
	if(a[n]) n++;
}

int main()
{
	char c;
	while((c=getchar())>='0'&&c<='9') a[n++]=c-'0';
	for(int i=0;i<n-i-1;i++) swap(a[i],a[n-i-1]);
	mul();
	for(int k=1;k<=n;k++)
	{
		add();
		if(sum<=9*k) return printf("%d\n",k),0;
	}

	return 0;
}

Score : 1300 points

Problem Statement

We will call a non-negative integer increasing if, for any two adjacent digits in its decimal representation, the digit to the right is greater than or equal to the digit to the left.
For example, 1558, 11, 3 and 0 are all increasing; 10 and 20170312 are not.

Snuke has an integer N. Find the minimum number of increasing integers that can represent N as their sum.

Constraints

1 \leq N \leq 10^{500000}

Input

The input is given from Standard Input in the following format:

N

Output

Print the minimum number of increasing integers that can represent N as their sum.

Sample Input 1

80

Sample Output 1

2

One possible representation is 80 = 77 + 3.

Sample Input 2

123456789

Sample Output 2

1

123456789 in itself is increasing, and thus it can be represented as the sum of one increasing integer.

Sample Input 3

20170312

Sample Output 3

4

Sample Input 4

7204647845201772120166980358816078279571541735614841625060678056933503

Sample Output 4

31


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

  #define vi vector<int>
  #define all(v) v.begin(),v.end()
  #define ll long long int
  #define  IOS    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  #define D1(x) {cerr<<" [" <<#x<<": "<<x<<"]";cout << endl;}
  #define D2(x) {cerr<<" [" <<#x<<": ";for(auto it:x)cerr<<it<< " ";cerr<<"] ";cout << endl;}

  const ll MOD=1e9+7;

  void solve(){
     ll n;
     cin >> n;
     cout << 800LL*(n) - 200*(n/15);
  }

  int main(){
    IOS
       solve();
       cerr <<endl <<"[ Time : " << (float)clock() / CLOCKS_PER_SEC << " secs ]" << endl;
  }
// integer overflow
// remember to clear visited array
// take input as vector<string> while using grid
// don't assign after modifying

Score : 100 points

Problem Statement

Snuke has a favorite restaurant.

The price of any meal served at the restaurant is 800 yen (the currency of Japan), and each time a customer orders 15 meals, the restaurant pays 200 yen back to the customer.

So far, Snuke has ordered N meals at the restaurant.
Let the amount of money Snuke has paid to the restaurant be x yen, and let the amount of money the restaurant has paid back to Snuke be y yen.
Find x-y.

Constraints

1 ≤ N ≤ 100

Input

The input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

20

Sample Output 1

15800

So far, Snuke has paid 16000 yen, and the restaurant has paid back 200 yen. Thus, the answer is 15800.

Sample Input 2

60

Sample Output 2

47200

Snuke has paid 48000 yen for 60 meals, and the restaurant has paid back 800 yen.


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

int main(){
	ll N;
    cin>>N;
    cout<<800*N - (N/15)*200<<endl;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define repr(i, j) for(int i = j; i >= 0; --i)
#define INF (1 << 30)
#define TMP 1000000007

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

int main() {
	int n;
	scanf("%d", &n);
	printf("%d\n", n * 800 - n / 15 * 200);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin >> n;
  long long int answer = 1;
  for(int i = 1;i <= n;i++){
    answer *= i;
    answer = answer % 1000000007;
  }
  cout << answer << endl;
}

Score : 200 points

Problem Statement

Snuke loves working out. He is now exercising N times.

Before he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i.

Find Snuke's power after he exercises N times. Since the answer can be extremely large, print the answer modulo 10^{9}+7.

Constraints

1 ≤ N ≤ 10^{5}

Input

The input is given from Standard Input in the following format:

N

Output

Print the answer modulo 10^{9}+7.

Sample Input 1

3

Sample Output 1

6

After Snuke exercises for the first time, his power gets multiplied by 1 and becomes 1.

After Snuke exercises for the second time, his power gets multiplied by 2 and becomes 2.

After Snuke exercises for the third time, his power gets multiplied by 3 and becomes 6.

Sample Input 2

10

Sample Output 2

3628800

Sample Input 3

100000

Sample Output 3

457992974

Print the answer modulo 10^{9}+7.


#include <stdio.h>
#include <math.h>
int factorial(int n)
{
	return (n==0)? 1:factorial(n-1)*n;
}

int main()
{
	int N;
	scanf("%d",&N);
	printf("%d",factorial(N)%(int)(pow(10, 9)+7));
	return 0;
}


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

using namespace std;

int mumujun(string s, string temp, int k, int n){
	if (s[k] == 'o'){
		if (temp[k] == 'S' && temp[(k-1+n)%n] == temp[(k+1)%n]){
			return 1;
		}
		if (temp[k] == 'W' && temp[(k-1+n)%n] != temp[(k+1)%n]){
			return 1;
		}
	}
	if (s[k] == 'x'){
		if (temp[k] == 'W' && temp[(k-1+n)%n] == temp[(k+1)%n]){
			return 1;
		}
		if (temp[k] == 'S' && temp[(k-1+n)%n] != temp[(k+1)%n]){
			return 1;
		}
	}

}

int main(void){
	int n;
	string s;

	char zero[2] = {'S', 'W'}, one[2] = {'S', 'W'};

	cin >> n >> s;

	int ok = 0;

	string temp, res;

	for (int i = 0; i < 2; i++){
		for (int j = 0; j < 2; j++){
			temp = "";
			for (int k = 0; k < n; k++){
				if (k == 0){
					temp += zero[i];
				}
				else if (k == 1)
					temp += one[j];
				else {
					if (s[k-1] == 'o'){
						if (temp[k-1] == 'S')
							temp += temp[k-2];
						else {
							if (temp[k-2] == 'S')
								temp += "W";
							else
								temp += "S";
						}
					}
					else {
						if (temp[k-1] == 'W')
							temp += temp[k-2];
						else {
							if (temp[k-2] == 'S')
								temp += "W";
							else
								temp += "S";
						}
					}
				}

				if (k == n - 1 && ok == 0){
					if (mumujun(s, temp, k, n) && mumujun(s, temp, 0, n))
						ok = 1;

					if (ok == 1){
						res = temp;
					}
				}
			}
		}
	}

	if (ok)
		cout << res << endl;
	else
		cout << -1 << endl;

}

Score : 500 points

Problem Statement

Snuke, who loves animals, built a zoo.

There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle.
The animal numbered i (2≤i≤N-1) is adjacent to the animals numbered i-1 and i+1. Also, the animal numbered 1 is adjacent to the animals numbered 2 and N, and the animal numbered N is adjacent to the animals numbered N-1 and 1.

There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.

Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered i answered s_i. Here, if s_i is o, the animal said that the two neighboring animals are of the same species, and if s_i is x, the animal said that the two neighboring animals are of different species.

More formally, a sheep answered o if the two neighboring animals are both sheep or both wolves, and answered x otherwise.
Similarly, a wolf answered x if the two neighboring animals are both sheep or both wolves, and answered o otherwise.

Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print -1.

Constraints

3 ≤ N ≤ 10^{5}

s is a string of length N consisting of o and x.

Input

The input is given from Standard Input in the following format:

N
s

Output

If there does not exist an valid assignment that is consistent with s, print -1.
Otherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s.

t is a string of length N consisting of S and W.

If t_i is S, it indicates that the animal numbered i is a sheep. If t_i is W, it indicates that the animal numbered i is a wolf.

Sample Input 1

6
ooxoox

Sample Output 1

SSSWWS

For example, if the animals numbered 1, 2, 3, 4, 5 and 6 are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.

Let us remind you: if the neiboring animals are of the same species, a sheep answers o and a wolf answers x. If the neiboring animals are of different species, a sheep answers x and a wolf answers o.

Sample Input 2

3
oox

Sample Output 2

-1

Print -1 if there is no valid assignment of species.

Sample Input 3

10
oxooxoxoox

Sample Output 3

SSWWSSSWWS


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//editorial AC
// The theme is understandable but installation is much more difficult than my ability
int main(){
  ll n; cin >> n;
  vector<ll> a(n);
  for(ll i = 0; i < n; i++){
    cin >> a[i];
  }
  vector<ll> vec;

  ll res = -1;
  for(ll i = 0; i < n; i++){
    if( res < a[i] ){ res = a[i]; vec.push_back(i);}
  }

  map<ll,ll> mp;

  for(ll i = 0; i < n; i++){
    mp[a[i]]++;
  }

  vector<ll> ans(n,0LL);

  for(ll i = (ll)vec.size()-1; i >= 1; i--){ //index == 0 は別途調べる
    // index-vec[i] is the largest element in the current array A

    while( mp.rbegin()->first > a[vec[i-1]] ){

       auto v = *mp.rbegin();
       ans[vec[i]] += (v.first-a[vec[i-1]])*v.second;
       mp[a[vec[i-1]]] += v.second;

       mp.erase(v.first);

          }
  }

  for(auto v : mp ){
    ans[0] += v.first*v.second;
  }

  for(auto ne : ans ){ cout << ne << endl;}

  return 0;
}

Score : 700 points

Problem Statement

Snuke loves constructing integer sequences.

There are N piles of stones, numbered 1 through N.
The pile numbered i consists of a_i stones.

Snuke will construct an integer sequence s of length Σa_i, as follows:

Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.

Select a pile with one or more stones remaining, and remove a stone from that pile.

If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.

We are interested in the lexicographically smallest sequence that can be constructed. For each of the integers 1,2,3,...,N, how many times does it occur in the lexicographically smallest sequence?

Constraints

1 ≤ N ≤ 10^{5}

1 ≤ a_i ≤ 10^{9}

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Print N lines. The i-th line should contain the number of the occurrences of the integer i in the lexicographically smallest sequence that can be constructed.

Sample Input 1

2
1 2

Sample Output 1

2
1

The lexicographically smallest sequence is constructed as follows:

Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.

Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.

Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.

The resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2 occurs once.

Sample Input 2

10
1 2 1 3 2 4 2 5 8 1

Sample Output 2

10
7
0
4
0
3
0
2
3
0


#include <iostream>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
if(x==1)
x=250250;
if(y==1)
x=250250;
if(x>y)
cout<<"Alice"<<endl;
if(x<y)
cout<<"Bob"<<endl;
if(x==y)
cout<<"Draw"<<endl;
return 0;
}

Score : 100 points

Problem Statement

Alice and Bob are playing One Card Poker.

One Card Poker is a two-player game using playing cards.

Each card in this game shows an integer between 1 and 13, inclusive.

The strength of a card is determined by the number written on it, as follows:

Weak 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < 11 < 12 < 13 < 1 Strong

One Card Poker is played as follows:

Each player picks one card from the deck. The chosen card becomes the player's hand.

The players reveal their hands to each other. The player with the stronger card wins the game.

If their cards are equally strong, the game is drawn.

You are watching Alice and Bob playing the game, and can see their hands.

The number written on Alice's card is A, and the number written on Bob's card is B.

Write a program to determine the outcome of the game.

Constraints

1≦A≦13

1≦B≦13

A and B are integers.

Input

The input is given from Standard Input in the following format:

A B

Output

Print Alice if Alice will win. Print Bob if Bob will win. Print Draw if the game will be drawn.

Sample Input 1

8 6

Sample Output 1

Alice

8 is written on Alice's card, and 6 is written on Bob's card.
Alice has the stronger card, and thus the output should be Alice.

Sample Input 2

1 1

Sample Output 2

Draw

Since their cards have the same number, the game will be drawn.

Sample Input 3

13 1

Sample Output 3

Bob


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

int main () {
  int N, M;
  cin >> N >> M;
  vector<vector<char>> A(N, vector<char>(N));
  vector<vector<char>> B(M, vector<char>(M));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> A.at(i).at(j);
    }
  }
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < M; j++) {
      cin >> B.at(i).at(j);
    }
  }
  for (int i = 0; i < N-M+1; i++) {
    bool ans = true;
    for (int j = 0; j < M; j++) {
      for (int k = 0; k < M; k++) {
        if (B.at(j).at(k) != A.at(j+i).at(k+i)) {
          ans = false;
          break;
        }
      }
      if (ans == false) break;
    }
    if (ans) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
}

Score : 200 points

Problem Statement

You are given an image A composed of N rows and N columns of pixels, and a template image B composed of M rows and M columns of pixels.

A pixel is the smallest element of an image, and in this problem it is a square of size 1×1.

Also, the given images are binary images, and the color of each pixel is either white or black.

In the input, every pixel is represented by a character: . corresponds to a white pixel, and # corresponds to a black pixel.

The image A is given as N strings A_1,...,A_N.

The j-th character in the string A_i corresponds to the pixel at the i-th row and j-th column of the image A (1≦i,j≦N).

Similarly, the template image B is given as M strings B_1,...,B_M.

The j-th character in the string B_i corresponds to the pixel at the i-th row and j-th column of the template image B (1≦i,j≦M).

Determine whether the template image B is contained in the image A when only parallel shifts can be applied to the images.

Constraints

1≦M≦N≦50

A_i is a string of length N consisting of # and ..

B_i is a string of length M consisting of # and ..

Input

The input is given from Standard Input in the following format:

N M
A_1
A_2
:
A_N
B_1
B_2
:
B_M

Output

Print Yes if the template image B is contained in the image A. Print No otherwise.

Sample Input 1

3 2
#.#
.#.
#.#
#.
.#

Sample Output 1

Yes

The template image B is identical to the upper-left 2 × 2 subimage and the lower-right 2 × 2 subimage of A. Thus, the output should be Yes.

Sample Input 2

4 1
....
....
....
....
#

Sample Output 2

No

The template image B, composed of a black pixel, is not contained in the image A composed of white pixels.


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int n,m;
vector<int> adj[8];
unordered_set<string> ans;
void dfs(int i,vector<int> visited,int time){
    visited[i] = time;
    if(time == n){
        string str = "";
        for(int k=0;k<n;k++){
            str += to_string(visited[k]);
        }
        ans.insert(str);
        return;
    }
    for(auto j : adj[i]){
        if(visited[j] == -1){
            dfs(j,visited,time+1);
        }
    }

}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int a,b;
        cin>>a>>b;
        a--;b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    dfs(0,vector<int>(n,-1),1);
    cout<<ans.size()<<"\n";
    return 0;

}

Score : 300 points

Problem Statement

You are given an undirected unweighted graph with N vertices and M edges that contains neither self-loops nor double edges.

Here, a self-loop is an edge where a_i = b_i (1≤i≤M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≤i<j≤M).

How many different paths start from vertex 1 and visit all the vertices exactly once?

Here, the endpoints of a path are considered visited.

For example, let us assume that the following undirected graph shown in Figure 1 is given.

Figure 1: an example of an undirected graph

The following path shown in Figure 2 satisfies the condition.

Figure 2: an example of a path that satisfies the condition

However, the following path shown in Figure 3 does not satisfy the condition, because it does not visit all the vertices.

Figure 3: an example of a path that does not satisfy the condition

Neither the following path shown in Figure 4, because it does not start from vertex 1.

Figure 4: another example of a path that does not satisfy the condition

Constraints

2≦N≦8

0≦M≦N(N-1)/2

1≦a_i<b_i≦N

The given graph contains neither self-loops nor double edges.

Input

The input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the number of the different paths that start from vertex 1 and visit all the vertices exactly once.

Sample Input 1

3 3
1 2
1 3
2 3

Sample Output 1

2

The given graph is shown in the following figure:

The following two paths satisfy the condition:

Sample Input 2

7 7
1 3
2 7
3 4
4 5
4 6
5 6
6 7

Sample Output 2

1

This test case is the same as the one described in the problem statement.


// In the name of Allah the Most Merciful.

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

typedef long long ll;
ll dp[(1<<8)+10][10];
int Set(int n , int pos)
{
    return n|(1<<pos);
}
bool Check(int n , int pos)
{
    return n&(1<<pos);
}
vector<int>edges[10];
int n , m;

ll f(int mask , int u)
{
    if(mask==(1<<n)-1)return 1;
    if(dp[mask][u]!=-1)return dp[mask][u];
    ll value = 0;
    for(auto v:edges[u]){
        if(Check(mask , v)==false){
            value+=f(Set(mask , v) , v);
        }
    }
    return dp[mask][u] = value;
}

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

    cin >> n >> m;
    for(int i=0;i<m;i++){
        int in1 , in2;
        cin >> in1 >> in2;
        in1--;
        in2--;
        edges[in1].push_back(in2);
        edges[in2].push_back(in1);
    }
    memset(dp , -1 , sizeof(dp));
    ll ans = 0;
    int mask = Set(0 , 0);
    for(int i=0;i<edges[0].size();i++){
        ans+=f(Set(mask , edges[0][i]) , edges[0][i]);
    }
    cout << ans << endl;
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <queue>
#include <list>
#include <stack>

using namespace std;

struct drag
{
	int a, b, c;
};

int n;
int ma, mb;
int result;
vector<drag> v;

void calc(int cost, int i, int m)
{
	if (i == n)
	{
		if (m != 0)
			return;
		if (cost == 0)
			return;
		if (cost < result)
			result = cost;
		return;
	}
	else
	{
		//	use i
		calc(cost + v[i].c, i + 1, m + v[i].a * mb - v[i].b * ma);
		//	not use i
		calc(cost, i + 1, m);
	}
}

int main()
{
	result = INT_MAX;
	cin >> n >> ma >> mb;
	v.resize(n);
	for (int i = 0; i < n; i++)
		cin >> v[i].a >> v[i].b >> v[i].c;
	calc(0, 0, 0);
	if (result == INT_MAX)
		result = -1;
	printf("%d", result);
	return 0;
}

Score : 400 points

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C.

In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.

He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.

The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).

Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.

Find the minimum amount of money required to generate the substance C.

If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.

Constraints

1≦N≦40

1≦a_i,b_i≦10

1≦c_i≦100

1≦M_a,M_b≦10

gcd(M_a,M_b)=1

a_i, b_i, c_i, M_a and M_b are integers.

Input

The input is given from Standard Input in the following format:

N M_a M_b
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.


#include<iostream>
#define N 401
using namespace std;
int n,ma,mb,a,b,c,ans=1e9,dp[N][N],i,j;
int main(){
	cin>>n>>ma>>mb;
	for(i=0;i<N;i++)for(j=0;j<N;j++)dp[i][j]=1e9;
	dp[0][0]=0;
	while(n--){
		cin>>a>>b>>c;
		for(i=N;i>=a;i--)for(j=N;j>=b;j--)dp[i][j]=min(dp[i][j],dp[i-a][j-b]+c);
	}
	for(i=1;i*ma<=N&&i*mb<=N;i++)ans=min(ans,dp[i*ma][i*mb]);
	cout<<(ans<1e9?ans:-1)<<endl;
}


#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
#define REP(i, limit) for(int i=0; i< limit; ++i)
#define LINT long long
int inf = 100000;

int main(){
  int N, ma, mb;
  cin>>N>>ma>>mb;
  int a[N], b[N], c[N];
  REP(i, N) cin>>a[i]>>b[i]>>c[i];
  int dp[401][401];
  REP(j,401)REP(k, 401){
    dp[j][k]=inf;
  }
  dp[0][0]=0;

  REP(i, N)REP(j, 401)REP(k, 401){
    if(j-a[i]>-1 && k-b[i]>-1){
      if(dp[j-a[i]][k-b[i]]==inf && dp[j][k]==inf) continue;
      dp[j][k] = min(dp[j][k], dp[j-a[i]][k-b[i]] + c[i]);
    }
  }
  int ans=inf;
  for(int i=ma, j=mb; i<401&&j<401; i+=ma, j+=mb){
      ans = min(ans, dp[i][j]);
  }
  //cout<<ans<<endl;

  if(ans==inf) cout<<-1<<endl;
  else cout<<ans<<endl;

  return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
static const double PI2 = 8.0 * atan(1.0);

#define REP(i,n)	for(int i=0;i<(int)n;++i)
#define ALL(c)		(c).begin(),(c).end()
#define CLEAR(v)	memset(v,0,sizeof(v))
#define MP(a,b)		make_pair((a),(b))
#define ABS(a)		((a)>0?(a):-(a))
#define FOR(i,s,n)	for(int i=s;i<(int)n;++i)

int main(){
    ll n;
    cin >> n;
    vector<ll> a(n);
    for(int i=0;i<n;i++) cin >> a[i];
    ll cnt = 0;
    for(int i=0;i<n;i++) {
        if(a[i]%2==1)
            cnt++;
    }
    if(cnt%2==1)
        cout << "NO" << endl;
    else
        cout << "YES" << endl;

}

Score : 300 points

Problem Statement

There are N integers written on a blackboard. The i-th integer is A_i.

Takahashi will repeatedly perform the following operation on these numbers:

Select a pair of integers, A_i and A_j, that have the same parity (that is, both are even or both are odd) and erase them.

Then, write a new integer on the blackboard that is equal to the sum of those integers, A_i+A_j.

Determine whether it is possible to have only one integer on the blackboard.

Constraints

2 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9

A_i is an integer.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

If it is possible to have only one integer on the blackboard, print YES. Otherwise, print NO.

Sample Input 1

3
1 2 3

Sample Output 1

YES

It is possible to have only one integer on the blackboard, as follows:

Erase 1 and 3 from the blackboard, then write 4. Now, there are two integers on the blackboard: 2 and 4.

Erase 2 and 4 from the blackboard, then write 6. Now, there is only one integer on the blackboard: 6.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

NO


#include <iostream>
using namespace std;

int N;
long A[100000];

void Diff() {
  long tmp = A[0];
  for(int i = 1; i < N; ++i) {
    A[i - 1] = A[i] - A[i - 1];
  }
  A[N - 1] = tmp - A[N - 1];
}

int main() {
  cin >> N;
  long sum = 0;
  for(int i = 0; i < N; ++i) {
    cin >> A[i];
    sum += A[i];
  }
  Diff();
  Diff();
  bool ok = true;
  long t = 0;
  for(int i = 0; i < N; ++i) {
    if(A[i] % N != 0) ok = false;
    t += A[i] / N * i;
  }
  if((sum - t * N * (N + 1) / 2) % N != 0) ok = false;

  cout << (ok ? "YES" : "NO") << endl;
}

Score : 500 points

Problem Statement

There are N boxes arranged in a circle. The i-th box contains A_i stones.

Determine whether it is possible to remove all the stones from the boxes by repeatedly performing the following operation:

Select one box. Let the box be the i-th box. Then, for each j from 1 through N, remove exactly j stones from the (i+j)-th box. Here, the (N+k)-th box is identified with the k-th box.

Note that the operation cannot be performed if there is a box that does not contain enough number of stones to be removed.

Constraints

1 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

If it is possible to remove all the stones from the boxes, print YES. Otherwise, print NO.

Sample Input 1

5
4 5 1 2 3

Sample Output 1

YES

All the stones can be removed in one operation by selecting the second box.

Sample Input 2

5
6 9 12 10 8

Sample Output 2

YES

Sample Input 3

4
1 2 3 1

Sample Output 3

NO


Score : 700 points

Problem Statement

There is a tree with N vertices, numbered 1 through N.
The i-th of the N-1 edges connects vertices a_i and b_i.

Currently, there are A_i stones placed on vertex i.
Determine whether it is possible to remove all the stones from the vertices by repeatedly performing the following operation:

Select a pair of different leaves. Then, remove exactly one stone from every vertex on the path between those two vertices.
Here, a leaf is a vertex of the tree whose degree is 1, and the selected leaves themselves are also considered as vertices on the path connecting them.

Note that the operation cannot be performed if there is a vertex with no stone on the path.

Constraints

2 ≦ N ≦ 10^5

1 ≦ a_i,b_i ≦ N

0 ≦ A_i ≦ 10^9

The given graph is a tree.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

If it is possible to remove all the stones from the vertices, print YES. Otherwise, print NO.

Sample Input 1

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

Sample Output 1

YES

All the stones can be removed, as follows:

Select vertices 4 and 5. Then, there is one stone remaining on each vertex except 4.

Select vertices 1 and 5. Then, there is no stone on any vertex.

Sample Input 2

3
1 2 1
1 2
2 3

Sample Output 2

NO

Sample Input 3

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

Sample Output 3

YES


#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")

using namespace std;

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

const int N = 100000 + 10;
const int MOD = 1000000007;
const int LOG = 20;
const int INF = 1000000010;
const int delta = 11353;

int n, A[N];
vector<int> G[N];
 int vis[N];

void DFS(int u) {
	vis[u] = 1;
	for (int v = 1; v <= n; ++v)
		if (!vis[v] && __gcd(A[u], A[v]) > 1)
			G[u].push_back(v), DFS(v);
}

int main() {
	cin >> n;
	for (int i = 1; i <= n; ++i) cin >> A[i];
	sort(A + 1, A + n + 1);
	priority_queue<int> q;
	for (int i = 1; i <= n; ++i) if (!vis[i]) DFS(i), q.push(i);
	while (!q.empty()) {
		int u = q.top(); q.pop();
		cout << A[u] << ' ';
		for (int v : G[u]) q.push(v);
	}
	return 0;
}

Score : 1000 points

Problem Statement

There are N integers written on a blackboard. The i-th integer is A_i, and the greatest common divisor of these integers is 1.

Takahashi and Aoki will play a game using these integers. In this game, starting from Takahashi the two player alternately perform the following operation:

Select one integer on the blackboard that is not less than 2, and subtract 1 from the integer.

Then, divide all the integers on the black board by g, where g is the greatest common divisor of the integers written on the blackboard.

The player who is left with only 1s on the blackboard and thus cannot perform the operation, loses the game.
Assuming that both players play optimally, determine the winner of the game.

Constraints

1 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9

The greatest common divisor of the integers from A_1 through A_N is 1.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

If Takahashi will win, print First. If Aoki will win, print Second.

Sample Input 1

3
3 6 7

Sample Output 1

First

Takahashi, the first player, can win as follows:

Takahashi subtracts 1 from 7. Then, the integers become: (1,2,2).

Aoki subtracts 1 from 2. Then, the integers become: (1,1,2).

Takahashi subtracts 1 from 2. Then, the integers become: (1,1,1).

Sample Input 2

4
1 2 4 8

Sample Output 2

First

Sample Input 3

5
7 8 8 8 8

Sample Output 3

Second


#include <bits/stdc++.h>
#define FOR(i,a,b) for(ut i=(a);i<(ut)(b);i++)
#define REP(i,b) FOR(i,0,b)
#define ALL(c) c.begin(),c.end()
#define PB push_back
#define cat //cout << __LINE__ << endl;
using namespace std;
typedef long long LL;
typedef double ld;
typedef LL ut;
typedef vector<ut> VI;
typedef pair<ut,ut> pr;
const int SIZE=5+3*1e5;
const int INF=10+1e9;
const LL p =7+1e9;
const LL c= 1<<30;
LL A[SIZE];
int N;
LL gcd(LL a, LL b){
	int ans=0;
	if(a<=1 or b<=1) return 1;
	while((a+1)&(b+1)&1==1){
		ans++;
		a>>=1;
		b>>=1;
	}
	return 1LL<<ans;
}
bool solve(){
	LL gcds=c;
	LL sum=0;
	int odds=0;
	bool isEnd=false;
	REP(i,N){
		sum+=A[i]-1;
		if(A[i]%2) odds++;
		if(A[i]==1) isEnd=true;
		else gcds=gcd(gcds,A[i]/2*2);
	}
	sum%=2;
	bool isFirst=sum;
	if(N==2 or isEnd or odds>=2 ) return isFirst;
	if(isFirst) return isFirst;

	REP(i,N)
		A[i]/=gcds;
	return !solve();

}
int main(){
	cin >> N;
	REP(i,N)
		cin >> A[i];
	if(solve()) cout << "First" << endl;
	else cout << "Second" << endl;
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#define ll long long
#define rep(i, a, b) for (int i = a; i <= (int)b; i ++)
void read(int & x) {
    x = 0; int flag = 1; char ch = ' ';
    while (ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    x *= flag;
}
void read(ll & x) {
    x = 0; int flag = 1; char ch = ' ';
    while (ch < '0' || ch > '9') { if(ch == '-') flag = -1; ch = getchar(); }
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    x *= flag;
}
using namespace std;
const int maxn = 2010;
int n, degree[maxn], a[maxn];
bool vis[maxn], mp[maxn][maxn];
vector < int > ans, edge[maxn];
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
void dfs(int u) {
    vis[u] = 1;
    rep(i, 1, n)
        if(!vis[i] && mp[u][i]) edge[u].push_back(i), degree[i] ++, dfs(i);
}
void solve() {
    priority_queue < int > pq;
    memset(vis, 0, sizeof(vis));
    rep(i, 1, n) if(degree[i] == 0) pq.push(i), vis[i] = 1;
    while(!pq.empty()) {
        int u = pq.top(); pq.pop();
        ans.push_back(u);
        rep(i, 0, edge[u].size() - 1) {
            int v = edge[u][i];
            if(-- degree[v] == 0) pq.push(v);
        }
    }
}
int main()
{
    read(n);
    rep(i, 1, n) read(a[i]);
    sort(a + 1, a + 1 + n);
    rep(i, 1, n - 1) rep(j, i + 1, n)
        if(gcd(a[i], a[j]) != 1) mp[i][j] = mp[j][i] = 1;
    rep(i, 1, n) dfs(i);
    solve();
    rep(i, 0, n - 1) printf("%d ", a[ans[i]]);
    return 0;
}

Score : 1600 points

Problem Statement

There are N integers written on a blackboard. The i-th integer is A_i.

Takahashi and Aoki will arrange these integers in a row, as follows:

First, Takahashi will arrange the integers as he wishes.

Then, Aoki will repeatedly swap two adjacent integers that are coprime, as many times as he wishes.

We will assume that Takahashi acts optimally so that the eventual sequence will be lexicographically as small as possible, and we will also assume that Aoki acts optimally so that the eventual sequence will be lexicographically as large as possible.
Find the eventual sequence that will be produced.

Constraints

1 ≦ N ≦ 2000

1 ≦ A_i ≦ 10^8

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 … A_N

Output

Print the eventual sequence that will be produced, in a line.

Sample Input 1

5
1 2 3 4 5

Sample Output 1

5 3 2 4 1

If Takahashi arranges the given integers in the order (1,2,3,4,5), they will become (5,3,2,4,1) after Aoki optimally manipulates them.

Sample Input 2

4
2 3 4 6

Sample Output 2

2 4 6 3


#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<int,P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
#define mp1(a,b,c) P1(a,P(b,c))

const int INF=1000000000;
const int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

int gcd(int x,int y){
	if(y == 0)return x;
	return gcd(y,x%y);
}

int main(){
	int n;
	int a[2002];
	scanf("%d",&n);
	rep(i,n)scanf("%d",&a[i]);

	sort(a,a+n);
	int cnt[2002] = {};
	vector<int> vec[2002];
	rep(i,n){
		rep(j,n){
			if(gcd(a[i],a[j]) != 1){
				cnt[i] ++;
				vec[j].pb(i);
			}
		}
	}

	vector<int> ans[2002];
	bool used[2002];
	rep(i,2002)used[i] = false;
	rep(i,n){
		//cout << i << endl;
		if(!used[i]){
			int loc = i;
			while(1){
				ans[i].pb(loc);
				used[loc] = true;
				int loc_ = loc;
				rep(j,vec[loc_].size()){
					if(!used[vec[loc_][j]]){
						loc_ = vec[loc_][j];
						break;
					}
				}
				if(loc == loc_)break;
				loc = loc_;
			}
		}
	}

	vector<int> A;
	priority_queue<P1> que;
	rep(i,2002){
		if(ans[i].size() > 0){
			que.push(mp1(ans[i][0],i,0));
		}
	}
	while(!que.empty()){
		P1 p = que.top(); que.pop();
		A.pb(a[p.fr]);
		if(ans[p.sc.fr].size() > p.sc.sc+1){
			que.push(mp1(ans[p.sc.fr][p.sc.sc+1],p.sc.fr,p.sc.sc+1));
		}
	}

	/*priority_queue<int> que;
	rep(i,n)if(cnt[i] == 0)que.push(i);
	while(!que.empty()){
		int p = que.top(); que.pop();
		ans.pb(a[p]);
		rep(i,vec[p].size()){
			cnt[vec[p][i]] --;
			if(cnt[vec[p][i]] == 0)que.push(vec[p][i]);
		}
	}*/

	rep(i,A.size()){
		printf("%d%c",A[i],i+1==A.size()?'\n':' ');
	}
}


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

using ll    = long long;
using ull   = unsigned long long;
using veci  = vector<int>;
using vecll = vector<ll>;
#define EPS (1e-7)
#define INF (1e9)
#define PI  (acos(-1))
#define FOR(i,a,b) for(int i = (a);i < (b); ++i)
#define REP(i,n) for(int i= 0;i<(int)(n);++i)
#define pb push_back

veci enum_div(int n){
	veci ret;
	ret.pb(1);
	for(int i=1;i*i<n;i++){
		if(n%i==0){
			ret.pb(i);
			if(i != n/i){ret.pb(n/i);}
		}
	}
	ret.pb(n);
	return ret;
}
int gcb(int a,int b){return ((b == 0)? a : gcb(b,a%b));}
int gcd(int a,int b){return (a*b) / gcb(a,b);}

int main(){
    int x;
    cin >> x;
    if(x <1200){
        cout << "ABC" << endl;
    }else{
        cout << "ARC" << endl;
    }
    return 0;
}

Score : 100 points

Problem Statement

Smeke has decided to participate in AtCoder Beginner Contest (ABC) if his current rating is less than 1200, and participate in AtCoder Regular Contest (ARC) otherwise.

You are given Smeke's current rating, x. Print ABC if Smeke will participate in ABC, and print ARC otherwise.

Constraints

1 ≦ x ≦ 3{,}000

x is an integer.

Input

The input is given from Standard Input in the following format:

x

Output

Print the answer.

Sample Input 1

1000

Sample Output 1

ABC

Smeke's current rating is less than 1200, thus the output should be ABC.

Sample Input 2

2000

Sample Output 2

ARC

Smeke's current rating is not less than 1200, thus the output should be ARC.


#include <bits/stdc++.h>

using namespace std;

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

    int x;

    cin >> x;
    cout << (x < 1200 ? "ABC" : "ARC") << endl;

    return 0;
}


#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> vll;
typedef vector<vector<ll> > vvll;
typedef deque<ll> dqll;

#define MOD 1000000007

int main() {
  ll N;

  cin >> N;

  ll res = N % 11;
  ll ans = 2*(N / 11);
  if (res == 0) {
  } else if(res <= 6){
    ans++;
  } else{
    ans += 2;
  }

  cout << ans << endl;

  return 0;
}

Score : 300 points

Problem Statement

Snuke has decided to play with a six-sided die. Each of its six sides shows an integer 1 through 6, and two numbers on opposite sides always add up to 7.

Snuke will first put the die on the table with an arbitrary side facing upward, then repeatedly perform the following operation:

Operation: Rotate the die 90° toward one of the following directions: left, right, front (the die will come closer) and back (the die will go farther). Then, obtain y points where y is the number written in the side facing upward.

For example, let us consider the situation where the side showing 1 faces upward, the near side shows 5 and the right side shows 4, as illustrated in the figure.
If the die is rotated toward the right as shown in the figure, the side showing 3 will face upward.
Besides, the side showing 4 will face upward if the die is rotated toward the left, the side showing 2 will face upward if the die is rotated toward the front, and the side showing 5 will face upward if the die is rotated toward the back.

Find the minimum number of operation Snuke needs to perform in order to score at least x points in total.

Constraints

1 ≦ x ≦ 10^{15}

x is an integer.

Input

The input is given from Standard Input in the following format:

x

Output

Print the answer.

Sample Input 1

7

Sample Output 1

2

Sample Input 2

149696127901

Sample Output 2

27217477801


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

int main() {
    int n;
    cin >> n;
    vector<int> v;
    map<int, int> mp;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        mp[a]++;
        v.push_back(a);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());

    int even = 0;
    for (int i = 0; i < v.size(); i++) {
        if (mp[v[i]] % 2 == 0) {
            even++;
        }
    }

    if (even % 2 == 0) {
        cout << v.size() << endl;
    } else {
        cout << (v.size() - 1) << endl;
    }

    return 0;
}

Score : 400 points

Problem Statement

Snuke has decided to play a game using cards.
He has a deck consisting of N cards. On the i-th card from the top, an integer A_i is written.

He will perform the operation described below zero or more times, so that the values written on the remaining cards will be pairwise distinct. Find the maximum possible number of remaining cards. Here, N is odd, which guarantees that at least one card can be kept.

Operation: Take out three arbitrary cards from the deck. Among those three cards, eat two: one with the largest value, and another with the smallest value. Then, return the remaining one card to the deck.

Constraints

3 ≦ N ≦ 10^{5}

N is odd.

1 ≦ A_i ≦ 10^{5}

A_i is an integer.

Input

The input is given from Standard Input in the following format:

N
A_1 A_2 A_3 ... A_{N}

Output

Print the answer.

Sample Input 1

5
1 2 1 3 7

Sample Output 1

3

One optimal solution is to perform the operation once, taking out two cards with 1 and one card with 2. One card with 1 and another with 2 will be eaten, and the remaining card with 1 will be returned to deck. Then, the values written on the remaining cards in the deck will be pairwise distinct: 1, 3 and 7.

Sample Input 2

15
1 3 5 2 1 3 2 8 8 6 2 6 11 1 1

Sample Output 2

7


#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <list>

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

using namespace std;

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

    //vector<int> a(N, 0);
    map<int, int> cnt;

    REP(i, N){
        int a;
        cin >> a;
        cnt[a]++;
    }

    int cnt2 = 0;

    for(auto&& x : cnt){
        if(x.second%2 == 0){
            cnt2++;
        }
    }

    int ans;

    if(cnt2%2){
        ans = cnt.size() - 1;
    }else{
        ans = cnt.size();
    }

    cout << ans << endl;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    int N;
    cin >> N;
    long long a[N], b[N];

    for (int i = 0; i < N; i++)
    {
        cin >> a[i] >> b[i];
    }

    long long ans = 0;
    for (int i = N - 1; i >= 0; i--)
    {
        if((a[i] + ans) % b[i] != 0 ) ans += b[i] - ((a[i] + ans) % b[i]);
    }

    cout << ans << endl;
}

Score : 300 points

Problem Statement

There are an integer sequence A_1,...,A_N consisting of N terms, and N buttons.
When the i-th (1 ≦ i ≦ N) button is pressed, the values of the i terms from the first through the i-th are all incremented by 1.

There is also another integer sequence B_1,...,B_N. Takahashi will push the buttons some number of times so that for every i, A_i will be a multiple of B_i.

Find the minimum number of times Takahashi will press the buttons.

Constraints

All input values are integers.

1 ≦ N ≦ 10^5

0 ≦ A_i ≦ 10^9(1 ≦ i ≦ N)

1 ≦ B_i ≦ 10^9(1 ≦ i ≦ N)

Input

The input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print an integer representing the minimum number of times Takahashi will press the buttons.

Sample Input 1

3
3 5
2 7
9 4

Sample Output 1

7

Press the first button twice, the second button twice and the third button three times.

Sample Input 2

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

Sample Output 2

22