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

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

  for (int i = 0; i < N; i++) {
    cin >> col;

    if (col == "y") {
      cout << "Four" << endl;
      return 0;
    }
  }
  cout << "Three" << endl;
}

Score : 200 points

Problem Statement

In Japan, people make offerings called hina arare, colorful crackers, on March 3.

We have a bag that contains N hina arare. (From here, we call them arare.)

It is known that the bag either contains arare in three colors: pink, white and green, or contains arare in four colors: pink, white, green and yellow.

We have taken out the arare in the bag one by one, and the color of the i-th arare was S_i, where colors are represented as follows - pink: P, white: W, green: G, yellow: Y.

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

Constraints

1 \leq N \leq 100

S_i is P, W, G or Y.

There always exist i, j and k such that S_i=P, S_j=W and S_k=G.

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_N

Output

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

Sample Input 1

6
G W Y P Y W

Sample Output 1

Four

The bag contained arare in four colors, so you should print Four.

Sample Input 2

9
G W W G P W P G G

Sample Output 2

Three

The bag contained arare in three colors, so you should print Three.

Sample Input 3

8
P Y W G Y W Y Y

Sample Output 3

Four


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

int main() {
    int n;
    cin >> n;
    map<char, int> m;
    for (int i = 0; i < n; i++) {
        string s;
        cin >> s;
        m[s[0]]++;
    }

    int ans = 0;
    string march = "MARCH";
    for (int i = 0; i + 2 < 5; i++) {
        for (int j = i + 1; j + 1 < 5; j++) {
            for (int k = j + 1; k < 5; k++) {
                ans += m[march[i]] * m[march[j]] * m[march[k]];
            }
        }
    }

    cout << ans << endl;
}

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

Score : 300 points

Problem Statement

There are N people. The name of the i-th person is S_i.

We would like to choose three people so that the following conditions are met:

The name of every chosen person begins with M, A, R, C or H.

There are no multiple people whose names begin with the same letter.

How many such ways are there to choose three people, disregarding order?

Note that the answer may not fit into a 32-bit integer type.

Constraints

1 \leq N \leq 10^5

S_i consists of uppercase English letters.

1 \leq |S_i| \leq 10

S_i \neq S_j (i \neq j)

Input

Input is given from Standard Input in the following format:

N
S_1
:
S_N

Output

If there are x ways to choose three people so that the given conditions are met, print x.

Sample Input 1

5
MASHIKE
RUMOI
OBIRA
HABORO
HOROKANAI

Sample Output 1

2

We can choose three people with the following names:

MASHIKE, RUMOI, HABORO

MASHIKE, RUMOI, HOROKANAI

Thus, we have two ways.

Sample Input 2

4
ZZ
ZZZ
Z
ZZZZZZZZZZ

Sample Output 2

0

Note that there may be no ways to choose three people so that the given conditions are met.

Sample Input 3

5
CHOKUDAI
RNG
MAKOTO
AOKI
RINGO

Sample Output 3

7

Submitted source-code file size, in bytes.

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

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


#include<bits/stdc++.h>
using namespace std;
const long long INF = (1LL << 32);
const long long MOD = 1000000007;
const long double PI = 3.1415926;
#define FOR(i,r,n) for(ll i = (ll)(r); i < (ll)(n); i++)
#define RFOR(i,r,n) for(ll i=(ll)(n-1);i>=r;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define ALL(x) x.begin(),x.end()
#define RALL(x) x.rbegin(),x.rend()
typedef long long int ll;
typedef vector<ll> vi;
typedef vector<pair<ll, ll>> vp;
typedef vector<string> vs;
typedef vector<char> vc;
typedef list<ll> lst;
typedef pair<ll, ll> P;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

ll n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

#define Endl endl

/*--------------------template--------------------*/

int main() {
    cin >> n;
    s = to_string(n);
    if(s[0]==1){
        cout << 9 * (s.size() - 1) << endl;
    }else{
        ll t = s[0] - '0' - 1;
        c = t + '0';
        string r;
        r += c;
        REP(i,s.size()-1) {
            r += '9';
        }
        if(s>r){
            cout << (s[0] - '0' - 1) + 9 * (s.size() - 1) << endl;
        }else{
            cout << (s[0] - '0') + 9 * (s.size() - 1) << endl;
        }
    }
}

Score : 300 points

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Constraints

1\leq N \leq 10^{16}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Sample Input 1

100

Sample Output 1

18

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.

Sample Input 2

9995

Sample Output 2

35

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.

Sample Input 3

3141592653589793

Sample Output 3

137


#include <bits/stdc++.h>
using namespace std;using ll=long long;using uint=unsigned int;using pii=pair<int,int>;using pll=pair<ll,ll>;using ull = unsigned long long;using ld=long double;template<typename T>void _(const char*s,T h){cerr<<s<<" = "<<h<<"\n";}template<typename T,typename...Ts>void _(const char*s,T h,Ts...t){int b=0;while(((b+=*s=='(')-=*s==')')!=0||*s!=',')cerr<<*s++;cerr<<" = "<<h<<",";_(s+1,t...);}// break continue pop_back 998244353
#define int ll
#define pii pll
#define f first
#define s second
#define pb emplace_back
#define forn(i,n) for(int i=0;i<(n);++i)
#define sz(a)((int)(a).size())
#define sqr(x) ((x)*(x))
struct init{init(){cin.tie(0);iostream::sync_with_stdio(0);cout<<fixed<<setprecision(10);cerr<<fixed<<setprecision(5);}~init(){
#ifdef LOCAL
#define dbg(...) _(#__VA_ARGS__,__VA_ARGS__)
cerr<<"Time elapsed: "<<(double)clock()/CLOCKS_PER_SEC<<"s.\n";
#else
#define dbg(...)
#endif
}}init;template<typename T,typename U>void upx(T&x,U y){if(x<y)x=y;}template<typename T,typename U>void upn(T&x,U y){if(x>y)x=y;}mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());const int D=4,dx[]={+1,0,-1,0},dy[]={0,+1,0,-1};
const int N=2020,OO=1e9;
//дописать точное
/*after all: #define vc vc<int/myf/whatever>*/
const double EPS=1e-9;//long double?
const double PI=3.1415926535897932384626433; //long double?
struct myf{
    double x; //long double?
    myf()=default;
    myf(double _x):x(_x){}
    myf operator-(){return -x;}
    myf operator+(const myf&rhs){return x+rhs.x;}
    myf operator-(const myf&rhs){return x-rhs.x;}
    myf operator*(const myf&rhs){return x*rhs.x;}
    myf operator/(const myf&rhs){return x/rhs.x;}
    myf& operator+=(const myf&rhs){x+=rhs.x;return*this;}
    myf& operator-=(const myf&rhs){x-=rhs.x;return*this;}
    myf& operator*=(const myf&rhs){x*=rhs.x;return*this;}
    myf& operator/=(const myf&rhs){x/=rhs.x;return*this;}
    bool operator<(const myf&rhs)const{return x+EPS<rhs.x;}
    bool operator>(const myf&rhs)const{return x>rhs.x+EPS;}
    bool operator==(const myf&rhs)const{return abs(x-rhs.x)/max(1.,abs(x))<=EPS;}//ld1
    bool operator!=(const myf&rhs)const{return abs(x-rhs.x)/max(1.,abs(x))>EPS;}//ld1
    bool operator<=(const myf&rhs)const{return*this<rhs||*this==rhs;}
    bool operator>=(const myf&rhs)const{return*this>rhs||*this==rhs;}
};
myf sqrt(myf x){return sqrt(x.x);}
myf abs(myf x){if(x.x<0)return -x;return x;}
std::istream&operator>>(std::istream&is,myf&p){return is>>p.x;}
std::ostream&operator<<(std::ostream&os,myf&p){return os<<p.x;}
template<class T>struct vc{
    T x,y;
    int i;
    vc(){}
    vc(T _x,T _y): x(_x),y(_y){}
    bool operator<(const vc&rhs)const{return x==rhs.x?y<rhs.y:x<rhs.x;}
    bool operator==(const vc&rhs)const{return x==rhs.x&&y==rhs.y;}
    vc(vc<T>a,vc<T>b){x=b.x-a.x;y=b.y-a.y;}
    T length(){return sqrt(sqr(x)+sqr(y));}
    T sqlength(){return sqr(x)+sqr(y);}
    vc operator +(vc b){return vc(x+b.x,y+b.y);}
    T operator *(vc b){return x*b.x+y*b.y;}
    T operator %(vc b){return x*b.y-y*b.x;}
    vc operator *(T k){return vc(x*k,y*k);}
    vc rotate(double angle){return vc(x*cos(angle)-y*sin(angle),x*sin(angle)+y*cos(angle));}
};
template<class T>T dist(vc<T>a,vc<T>b){return sqrt(sqr(b.x-a.x)+sqr(b.y-a.y));}
template<class T>T sqdist(vc<T>a,vc<T>b){return sqr(b.x-a.x)+sqr(b.y-a.y);}
template<class T>std::istream&operator>>(std::istream&is,vc<T>&p){return is>>p.x>>p.y;}
template<class T>std::ostream&operator<<(std::ostream&os,vc<T>&p){return os<<p.x<<' '<<p.y;}
template<class T>vc<T>turn_clockwise(vc<T>a){return vc<T>(a.y,-a.x);}
template<class T>vc<T>turn_anticlockwise(vc<T>a){return vc<T>(-a.y,a.x);}
template<class T>struct line
{
    T a,b,c;
    line(){}
    line(T _a,T _b,T _c): a(_a),b(_b),c(_c){
        //normalize();
    }
    line(vc<T>one,vc<T>two){
        a=one.y-two.y;
        b=two.x-one.x;
        c=-(a*one.x+b*one.y);
        //normalize();
    }
    line(vc<T>norm,vc<T>cont,int mycop){
        a=norm.x;
        b=norm.y;
        c=-(a*cont.x+b*cont.y);
        //normalize();
    }
    void normalize(){
        T div=sqrt(sqr(a)+sqr(b));
        a/=div;
        b/=div;
        c/=div;
    }
    bool on(vc<T>p){return a*p.x+b*p.y+c==0;}
};
template<class T>struct circle{
    vc<T>center;T radius;
    circle(){}
    circle(vc<T>_center,T _radius):center(_center),radius(_radius){}
    circle(vc<T>_center,vc<T>_v):center(_center){radius=_v.length();}
    bool inside(vc<T>p){return dist(p,center)<=radius;}
    bool strict_inside(vc<T>p){return dist(p,center)<radius;}
    bool on(vc<T>p){return dist(p,center)==radius;}
};
template<class T>T vc_segment_dist(vc<T>O,vc<T>p1,vc<T>p2){ // vc O,segment [p1,p2]
    return((vc<T>(p1,O)*vc(p1,p2))*(vc(p2,p1)*vc(p2,O))<=0)?min(dist(O,p1),dist(O,p2)):abs((vc(O,p1)%vc(O,p2))/dist(p1,p2));
}
template<class T>T det(T a,T b,T c,T d){return a*d-b*c;}
// 0 if parallel;{1,vc} if OK; 2 if match.
template<class T>int cross(line<T>one,line<T>two,vc<T>&p){
    T dx=det(one.c,one.b,two.c,two.b),dy=det(one.a,one.c,two.a,two.c),dd=det(one.a,one.b,two.a,two.b);
    if(dx==0&&dy==0&&dd==0)return 2;
    else if(dd==0)         return 0;
    p=vc(-dx/dd,-dy/dd);   return 1;
}
// 0 if A in C;{1,vc} if A on C;{2,vc,vc} if OK
template<class T>int kasat(vc<T>A,circle<T>C,vc<T>&p1,vc<T>&p2){
    vc OA(C.center,A);
    T l=OA.length();
    if(l<C.radius)return 0;
    T x=sqr(C.radius)/l;
    vc OP(OA.x*x/l,OA.y*x/l);
    vc P=C.center+OP;
    T hor=sqrt(sqr(C.radius)-OP.sqlength());
    p1=P+turn_clockwise(OP*(hor/x));
    p2=P+turn_anticlockwise(OP*(hor/x));
    return l==C.radius?1:2;
}
// 0 no intersection; 1/2=OK(1/2 vcs)
template<class T>int cross(line<T>l,circle<T>c,vc<T>&p1,vc<T>&p2){
    if(l.b==0){
        p1.x=-l.c/l.a;
        if(sqr(c.radius)>=sqr(p1.x-c.center.x)){
            p2.x=p1.x;
            p1.y=c.center.y+sqrt(max(T(0),sqr(c.radius)-sqr(c.center.x-p1.x)));
            p2.y=c.center.y-sqrt(max(T(0),sqr(c.radius)-sqr(c.center.x-p1.x)));
            return sqr(c.radius)==sqr(c.center.x-p1.x)?1:2;
        }
        return 0;
    }
    T tt=(l.c+l.b*c.center.y)/l.b;
    T ta=sqr(l.a/l.b)+1,tb=(tt*l.a/l.b-c.center.x)*2,tc=sqr(tt)-sqr(c.radius)+sqr(c.center.x);
    T td=sqr(tb)-ta*tc*4;
    if(td<0)return 0;
    p1.x=(-tb+sqrt(max(T(0),td)))/(ta+ta);
    p2.x=(-tb-sqrt(max(T(0),td)))/(ta+ta);
    p1.y=(-l.c-l.a*p1.x)/l.b;
    p2.y=(-l.c-l.a*p2.x)/l.b;
    return td==0?1:2;
}
// 0 no intersection; 1/2=OK(1/2 vcs); 3=match
template<class T>int cross(circle<T>one,circle<T>two,vc<T>&p1,vc<T>&p2){
    if(one.center.x==two.center.x&&one.center.y==two.center.y)return one.radius==two.radius?3:0;
    line l((two.center.x-one.center.x)*2,(two.center.y-one.center.y)*2,sqr(one.center.x)-sqr(two.center.x)+sqr(one.center.y)-sqr(two.center.y)+sqr(two.radius)-sqr(one.radius));
    return cross(l,one,p1,p2);
}
// !!!a[0]==a.back(),vcs anticlockwise
template<class T>bool inside_convex_polygon(const vector<vc<T>>&a,vc<T>p){
    int n=a.size()-1,l=1,r=a.size()-2;
    vc cur(a[0],p);
    if(vc(a[0],a[1])%cur<0||cur%vc(a[0],a[n-1])<0)return 0;
    while(r-l>1){
        int m=(l+r)/2;
        if(cur%vc(a[0],a[m])>=0)r=m;
        else l=m;
    }
    return vc(a[r],p)%vc(a[r],a[r+1])<=0&&vc(a[r-1],p)%vc(a[r-1],a[r])<=0;
}
template<class T>T area(const vector<vc<T>>&p){
    T res=0;
    for(int i=1;i+1<p.size();++i)res+=vc(p[0],p[i])%vc(p[0],p[i+1]);
    return abs(res);
}

template<class T>bool btw(T o,T d1,T d2){return(d1<o&&o<d2)||(d2<o&&o<d1);}
template<class T>bool pbtw(vc<T>o,vc<T>p1,vc<T>p2){return btw(o.x,p1.x,p2.x)&&btw(o.y,p1.y,p2.y);}
#define vc vc<int>
vc ch_start;
bool ch_comp(const vc&p1,const vc&p2){
    vc op1(ch_start,p1),op2(ch_start,p2);
    if(op1%op2==0)return sqr(op1.x)+sqr(op1.y)<sqr(op2.x)+sqr(op2.y);
    return op1%op2>0;
}
vector<vc> convex_hull(const vector<vc>&p){
    if(p.empty())return{};
    ch_start=*min_element(p.begin(),p.end());
    vector<vc>a;
    for(const vc&i:p)if(i.x!=ch_start.x||i.y!=ch_start.y)a.pb(i);
    sort(a.begin(),a.end(),ch_comp);
    if(a.empty())return{ch_start};
    vector<vc> ans={ch_start,a[0]};
    for(uint i=1;i<a.size();++i){
        while(ans.size()>=2&&vc(ans[ans.size()-2],ans.back())%vc(ans.back(),a[i])<=0)ans.pop_back();
        ans.pb(a[i]);
    }
    return ans;
}
double ans[111];
int32_t main(){
    int n;
    cin>>n;
    vector<vc> a(n);
    forn(i,n){
        cin>>a[i];
        a[i].i=i;
    }
    auto h=convex_hull(a);
    int m=sz(h);
    h.pb(h[0]);
    h.pb(h[1]);
    for(int i=1;i<=m;++i){
        vc v1(h[i],h[i-1]);
        vc v2(h[i],h[i+1]);
        double ang=atan2(v2%v1,v1*v2);
        ans[h[i].i]=(PI-ang)/(PI+PI);
    }
    forn(i,n)cout<<ans[i]<<'\n';
    return 0;
}

Score : 600 points

Problem Statement

There are N holes in a two-dimensional plane. The coordinates of the i-th hole are (x_i,y_i).

Let R=10^{10^{10^{10}}}. Ringo performs the following operation:

Randomly choose a point from the interior of a circle of radius R centered at the origin, and put Snuke there. Snuke will move to the hole with the smallest Euclidean distance from the point, and fall into that hole. If there are multiple such holes, the hole with the smallest index will be chosen.

For every i (1 \leq i \leq N), find the probability that Snuke falls into the i-th hole.

Here, the operation of randomly choosing a point from the interior of a circle of radius R is defined as follows:

Pick two real numbers x and y independently according to uniform distribution on [-R,R].

If x^2+y^2\leq R^2, the point (x,y) is chosen. Otherwise, repeat picking the real numbers x,y until the condition is met.

Constraints

2 \leq N \leq 100

|x_i|,|y_i| \leq 10^6(1\leq i\leq N)

All given points are pairwise distinct.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print N real numbers. The i-th real number must represent the probability that Snuke falls into the i-th hole.

The output will be judged correct when, for all output values, the absolute or relative error is at most 10^{-5}.

Sample Input 1

2
0 0
1 1

Sample Output 1

0.5
0.5

If Ringo put Snuke in the region x+y\leq 1, Snuke will fall into the first hole. The probability of this happening is very close to 0.5.
Otherwise, Snuke will fall into the second hole, the probability of which happening is also very close to 0.5.

Sample Input 2

5
0 0
2 8
4 5
2 6
3 10

Sample Output 2

0.43160120892732328768
0.03480224363653196956
0.13880483535586193855
0.00000000000000000000
0.39479171208028279727


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()

struct point {
  ll x, y;
  point(ll x_ = 0, ll y_ = 0) : x(x_), y(y_) {}
  friend point operator-(point a, point b) {
    return point(a.x - b.x, a.y - b.y);
  }
  friend bool operator<(point a, point b) {
    return a.x != b.x ? a.x < b.x : a.y < b.y;
  }
};

ll cross(point a, point b) {
  return a.x * b.y - a.y * b.x;
}

vector<point> convex_hull(vector<point> ps) {
  const int n = ps.size();
  if (n <= 2) return ps;
  sort(ps.begin(), ps.end());
  vector<point> a, b;
  rep(i, ps.size()) {
    while (a.size() >= 2 && cross(a.back() - a.rbegin()[1], ps[i] - a.back()) < 0) a.pop_back();
    a.push_back(ps[i]);
  }
  repr(i, ps.size()) {
    while (b.size() >= 2 && cross(b.back() - b.rbegin()[1], ps[i] - b.back()) < 0) b.pop_back();
    b.push_back(ps[i]);
  }
  a.pop_back();
  b.pop_back();
  a.insert(a.end(), b.begin(), b.end());
  return a;
}

constexpr double PI = acos(-1);

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int N; cin >> N;
  vector<point> P(N);
  map<point, int> mp;
  rep(i, N) {
    ll x, y; cin >> x >> y;
    P[i] = point(x, y);
    mp[P[i]] = i;
  }
  P = convex_hull(P);
  vector<double> theta(N);
  for (int i = 0; i < P.size(); i++) {
    int j = (i + 1) % P.size();
    point d = P[j] - P[i];
    theta[i] = atan2(d.y, d.x) - PI / 2;
  }
  vector<double> ans(N);
  for (int i = 0; i < P.size(); i++) {
    int j = (i + P.size() - 1) % P.size();
    double t = theta[i] - theta[j];
    if (t < 0) t += 2 * PI;
    ans[mp[P[i]]] = t / (2 * PI);
  }
  rep(i, N) {
    cout << ans[i] << '\n';
  }
}


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

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

    if (N - (N/500) * 500 <= A) cout << "Yes" << endl;

    else cout << "No" << endl;
}

Score: 100 points

Problem Statement

E869120 has A 1-yen coins and infinitely many 500-yen coins.

Determine if he can pay exactly N yen using only these coins.

Constraints

N is an integer between 1 and 10000 (inclusive).

A is an integer between 0 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
A

Output

If E869120 can pay exactly N yen using only his 1-yen and 500-yen coins, print Yes; otherwise, print No.

Sample Input 1

2018
218

Sample Output 1

Yes

We can pay 2018 yen with four 500-yen coins and 18 1-yen coins, so the answer is Yes.

Sample Input 2

2763
0

Sample Output 2

No

When we have no 1-yen coins, we can only pay a multiple of 500 yen using only 500-yen coins. Since 2763 is not a multiple of 500, we cannot pay this amount.

Sample Input 3

37
514

Sample Output 3

Yes


#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  vector<int> v(N);
  for(int i=0;i<N;i++) cin >> v[i];
  sort(v.begin(), v.end(), greater<int>());
  int a=0,b=0;
  for(int i=0;i<v.size();i+=2) {a += v[i]; if (i+1<v.size()) b += v[i+1];}
  cout << a - b << endl;
}

Score: 200 points

Problem Statement

We have N cards. A number a_i is written on the i-th card.

Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.

The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.

Constraints

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

a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.

Sample Input 1

2
3 1

Sample Output 1

2

First, Alice will take the card with 3. Then, Bob will take the card with 1.
The difference of their scores will be 3 - 1 = 2.

Sample Input 2

3
2 7 4

Sample Output 2

5

First, Alice will take the card with 7. Then, Bob will take the card with 4. Lastly, Alice will take the card with 2. The difference of their scores will be 7 - 4 + 2 = 5. The difference of their scores will be 3 - 1 = 2.

Sample Input 3

4
20 18 2 18

Sample Output 3

18


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

using ll = long long;

int main() {

    std::vector<std::vector<int>> c(3, std::vector<int>(3));
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            std::cin >> c[i][j];
        }
    }

    std::vector<int> a(3), b(3);
    for (int i = 0; i <= c[0][0]; i++) {
        a[0] = i;
        int b1 = c[0][0] - i;
        if (b1 < 0)continue;
        b[0] = b1;
        int b2 = c[0][1] - i;
        if (b2 < 0)continue;
        b[1] = b2;
        int b3 = c[0][2] - i;
        if (b3 < 0)continue;
        b[2] = b3;
        int a2 = c[1][0] - b1;
        if (a2 < 0)continue;
        if (a2 + b2 != c[1][1])continue;
        if (a2 + b3 != c[1][2])continue;
        int a3 = c[2][0] - b1;
        if (a3 < 0)continue;
        if (a3 + b2 != c[2][1])continue;
        if (a3 + b3 != c[2][2])continue;
        std::cout << "Yes" << std::endl;
        return 0;
    }

    std::cout << "No" << std::endl;
    return 0;
}

Score: 300 points

Problem Statement

We have a 3 \times 3 grid. A number c_{i, j} is written in the square (i, j), where (i, j) denotes the square at the i-th row from the top and the j-th column from the left.

According to Takahashi, there are six integers a_1, a_2, a_3, b_1, b_2, b_3 whose values are fixed, and the number written in the square (i, j) is equal to a_i + b_j.

Determine if he is correct.

Constraints

c_{i, j} \ (1 \leq i \leq 3, 1 \leq j \leq 3) is an integer between 0 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

c_{1,1} c_{1,2} c_{1,3}
c_{2,1} c_{2,2} c_{2,3}
c_{3,1} c_{3,2} c_{3,3}

Output

If Takahashi's statement is correct, print Yes; otherwise, print No.

Sample Input 1

1 0 1
2 1 2
1 0 1

Sample Output 1

Yes

Takahashi is correct, since there are possible sets of integers such as: a_1=0,a_2=1,a_3=0,b_1=1,b_2=0,b_3=1.

Sample Input 2

2 2 2
2 1 2
2 2 2

Sample Output 2

No

Takahashi is incorrect in this case.

Sample Input 3

0 8 8
0 8 8
0 8 8

Sample Output 3

Yes

Sample Input 4

1 8 6
2 9 7
0 7 7

Sample Output 4

No


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <functional>
#include <string>
#include <queue>
using namespace std;

#define rep(i,a,b) for(long long int i=a; i<b; i++ )
#define REP(i,n) rep(i,0,n)

int main()
{
  typedef pair<int,int> P;

  int H, W; cin >> H >> W;
  int INF = 1000000000;
  vector< vector<int> > maze(H), dist(H);

  char c_tmp;
  int sx,sy,gx,gy; // start, goal
  int white_count = 0;
  sx=0; sy=0; gx=H-1; gy=W-1; // init

  REP(i,H){
    maze[i].resize(W);
    dist[i].resize(W,INF);
    REP(j,W){
      cin >> c_tmp;
      if(c_tmp == '#'){ maze[i][j]=1;
      } else{ maze[i][j]=0; white_count++; }

      if(c_tmp == 's'){ sx=i; sy=j;}
      if(c_tmp == 'g'){ gx=i; gy=j;}
    }
  }

  vector<int> dx = {1,0,-1,0};   vector<int> dy = {0,1,0,-1}; // 十字方向

  // bfs
  queue<P> q;
  q.push( P(sx,sy) ); dist[sx][sy] = 0;
  while( q.size() ){
    P p = q.front(); q.pop();
    if( p.first==gx && p.second==gy ){ break; }
    REP(i,dx.size()){
      int nx = p.first + dx[i]; int ny = p.second + dy[i];
      if( 0<=nx && nx<H && 0<=ny && ny<W && maze[nx][ny]==0 && dist[nx][ny]==INF ){
        // 未訪問かつ移動可能な場合
        q.push( P(nx,ny) );
        dist[nx][ny] = dist[p.first][p.second] + 1;
      }
    }
  }

  int min_dist = dist[gx][gy];
  if( min_dist!=INF ){   cout << white_count-min_dist-1 << endl; }
  else{ cout << -1 << endl; }

  return 0;
}

Score: 400 points

Problem statement

We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).

Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.

Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.

When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

Constraints

H is an integer between 2 and 50 (inclusive).

W is an integer between 2 and 50 (inclusive).

s_{i, j} is . or # (1 \leq i \leq H, 1 \leq j \leq W).

s_{1, 1} and s_{H, W} are ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}
s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}
: :
s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}

Output

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

Sample Input 1

3 3
..#
#..
...

Sample Output 1

2

The score 2 can be achieved by changing the color of squares as follows:

Sample Input 2

10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Sample Output 2

209


#include<bits/stdc++.h>
//cervello|Phoenix
using namespace std;
#define ll long long int
#define eb emplace_back
#define mk make_pair
#define pr pair<int,int>
#define all(x) x.begin(),x.end()
#define mod 1000000007

int main(){
  ios_base::sync_with_stdio(false);
    // #ifndef ONLINE_JUDGE
    //   freopen("input.txt", "r", stdin);
    //   freopen("output.txt", "w", stdout);
    // #endif
  ll x,y; cin>>x>>y;
  bool flag=0;
  while((x*x)<=y){
    if(y%x){
      flag=1;
      break;
    }
    x*=2;
  }
  if(y%x)
    flag=1;
  if(flag)
    cout<<x<<"\n";
  else
    cout<<"-1\n";
  return 0;
}

Score : 100 points

Problem Statement

You are given positive integers X and Y.
If there exists a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, choose one such integer and print it.
If it does not exist, print -1.

Constraints

1 ≤ X,Y ≤ 10^9

X and Y are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print a positive integer not greater than 10^{18} that is a multiple of X but not a multiple of Y, or print -1 if it does not exist.

Sample Input 1

8 6

Sample Output 1

16

For example, 16 is a multiple of 8 but not a multiple of 6.

Sample Input 2

3 3

Sample Output 2

-1

A multiple of 3 is a multiple of 3.


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for(int i=0;i<(int)n;i++)
#define PI acos(-1)
#define fast_io ios_base::sync_with_stdio(false) ; cin.tie(0); cout.tie(0);
ll mod=1e9+7;

ll rep_pow(ll a, ll b){//繰り返し2乗法
    if(b==0) return 1;
    else if(b%2==0){
        ll memo = rep_pow(a, b/2);
        return memo*memo%mod;
    }else{
        return rep_pow(a, b-1)*a%mod;
    }
}

ll nCr(ll n, ll r){//mod逆元使ったnCr
    ll ret=1;
    rep(i,r){
        ret *= n-i;//分子
        ret %= mod;
        ret *=rep_pow(r-i, mod-2);//mod逆元によりr!で割ると同値
        ret %= mod;
    }
    return ret;
}
ll gcd(ll a, ll b){ if(b==0) return a; return gcd(b, a%b);}

int main(){
    fast_io

    ll x,y;
    cin>>x>>y;
    if(x==y){
        cout<<-1<<endl;
        return 0;
    }
    ll ans=x;
    int i=1;
    ll MAX=1e18;
    while(ans<=MAX){
        ans+=x;
        i++;
        if(x%y!=0) {
            cout<<ans<<endl;
            return 0;
        }
    }
    cout<<-1<<endl;
    return 0;
}


#include <iostream>
using namespace std;

const int maxN = 10000;
int a[maxN], b[maxN];

int main()
{

    int n;
    int i, j;
    bool flag = false;
    long long amore=0;
    long long bmore=0;
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> a[i];
    }
    for(i=0;i<n;i++)
    {
        cin >> b[i];
        if(a[i]<=b[i]) flag = true;
        if(a[i]>b[i]) amore += a[i] - b[i];
        if(a[i]<b[i]) bmore += b[i] - a[i];
    }
    if(flag)
    {
        bmore -= 2*amore;
        if(bmore>=0) cout << "Yes\n";
        else cout << "No\n";
    }
    else cout << "No\n";

    return 0;
}

Score : 300 points

Problem Statement

You are given two integer sequences of length N: a_1,a_2,..,a_N and b_1,b_2,..,b_N.
Determine if we can repeat the following operation zero or more times so that the sequences a and b become equal.

Operation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:

Add 2 to a_i.

Add 1 to b_j.

Constraints

1 ≤ N ≤ 10 000

0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N
b_1 b_2 .. b_N

Output

If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.

Sample Input 1

3
1 2 3
5 2 2

Sample Output 1

Yes

For example, we can perform three operations as follows to do our job:

First operation: i=1 and j=2. Now we have a = \{3,2,3\}, b = \{5,3,2\}.

Second operation: i=1 and j=2. Now we have a = \{5,2,3\}, b = \{5,4,2\}.

Third operation: i=2 and j=3. Now we have a = \{5,4,3\}, b = \{5,4,3\}.

Sample Input 2

5
3 1 4 1 5
2 7 1 8 2

Sample Output 2

No

Sample Input 3

5
2 7 1 8 2
3 1 4 1 5

Sample Output 3

No


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

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod (ll)(1e9+7)
#define int ll

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int par[100001], ran[100001];

int find(int x) {
  if(par[x] == x) return x;
  else return par[x] = find(par[x]);
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if(x == y) return;
  if(ran[x] < ran[y]) {
    par[x] = y;
  }else {
    par[y] = x;
    if(ran[x] == ran[y]) ran[x]++;
  }
}

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

int s[100001], si[100001];

signed main() {
  int v, e, rc = 0;
  cin >> v >> e;
  for(int i = 0; i < v; i++) par[i] = i, ran[i] = 0;
  rep(i, 0, v) {
    cin >> s[i];
  }
  if((e + 1) * 2 < v) {
    cout << "Impossible" << endl;
    return 0;
  }
  rep(i, 0, e) {
    int a, b;
    cin >> a >> b;
    unite(a, b);
  }
  int ans = 0;
  while(1) {
    map<int, P> mp;
    map<int, vector<int> > mmp;
    rep(i, 0, v) find(i);
    rep(i, 0, v) {
      if(si[i]) continue;
      if(mp[par[i]].second) {
        if(mp[par[i]].second > s[i]) {
          mp[par[i]].second = s[i];
          mp[par[i]].first = i;
        }
      }
      else mp[par[i]] = P(i, s[i]);
      mmp[s[i]].pb(i);
    }
    if(mp.size() <= 1) break;
    for(auto it = mmp.begin(); it != mmp.end(); it++) {
      rep(i, 0, it->second.size()) {
        for(auto itr = mp.begin(); itr != mp.end(); itr++) {
          P c;
          c = itr->second;
          if(si[c.second] || si[it->second[i]]) continue;
          if(c.first == it->second[i] || same(c.first, it->second[i])) continue;
          unite(c.first, it->second[i]);
          si[c.first] = 1;
          si[it->second[i]] = 1;
          ans += it->first + c.second;
          break;
        }
      }
    }
  }
  cout << ans << endl;
}

Score : 600 points

Problem Statement

You are given a forest with N vertices and M edges. The vertices are numbered 0 through N-1.
The edges are given in the format (x_i,y_i), which means that Vertex x_i and y_i are connected by an edge.

Each vertex i has a value a_i.
You want to add edges in the given forest so that the forest becomes connected.
To add an edge, you choose two different vertices i and j, then span an edge between i and j.
This operation costs a_i + a_j dollars, and afterward neither Vertex i nor j can be selected again.

Find the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Constraints

1 ≤ N ≤ 100,000

0 ≤ M ≤ N-1

1 ≤ a_i ≤ 10^9

0 ≤ x_i,y_i ≤ N-1

The given graph is a forest.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M
a_0 a_1 .. a_{N-1}
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the minimum total cost required to make the forest connected, or print Impossible if it is impossible.

Sample Input 1

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

Sample Output 1

7

If we connect vertices 0 and 5, the graph becomes connected, for the cost of 1 + 6 = 7 dollars.

Sample Input 2

5 0
3 1 4 1 5

Sample Output 2

Impossible

We can't make the graph connected.

Sample Input 3

1 0
5

Sample Output 3

0

The graph is already connected, so we do not need to add any edges.


#include <bits/stdc++.h>

#define MAX 100010

using namespace std;

int n;
int n1, n2, n3;
int ans;

bool marc[MAX][4];

vector<int> grafo[MAX][4];

void DFS(int i, int bit, int& szComponent, int& qtdLeafs)
{
	szComponent++;
	if(grafo[i][bit].size() == 1) qtdLeafs++;

	marc[i][bit] = true;

	for(int g = 0 ; g < grafo[i][bit].size() ; g++)
	{
		int prox = grafo[i][bit][g];

		if(marc[prox][bit]) continue;

		DFS(prox , bit , szComponent , qtdLeafs);
	}
}

int main()
{
	scanf("%d",&n);

	for(int g = 0 ; g < n - 1 ; g++)
	{
		scanf("%d %d %d",&n1,&n2,&n3);

		for(int h = 0 ; h < 4 ; h++)
		{
			if( !(n3 & (1 << h)) ) continue;

			grafo[ n1 ][h].push_back( n2 );
			grafo[ n2 ][h].push_back( n1 );
		}
	}

	for(int h = 0 ; h < 4 ; h++)
	{
		for(int g = 1 ; g <= n ; g++)
		{
			if(marc[g][h]) continue;

			int qtdLeafs = 0;
			int szComponent = 0;

			DFS(g , h , szComponent , qtdLeafs);

			if(szComponent > 1) ans += (qtdLeafs + 1)/2;
		}
	}

	printf("%d\n",ans);
}

Score : 1000 points

Problem Statement

You are given a tree with N vertices. The vertices are numbered 0 through N-1, and the edges are numbered 1 through N-1.
Edge i connects Vertex x_i and y_i, and has a value a_i.
You can perform the following operation any number of times:

Choose a simple path and a non-negative integer x, then for each edge e that belongs to the path, change a_e by executing a_e ← a_e ⊕ x (⊕ denotes XOR).

Your objective is to have a_e = 0 for all edges e.
Find the minimum number of operations required to achieve it.

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ N-1

0 ≤ a_i ≤ 15

The given graph is a tree.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1 a_1
x_2 y_2 a_2
:
x_{N-1} y_{N-1} a_{N-1}

Output

Find the minimum number of operations required to achieve the objective.

Sample Input 1

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

Sample Output 1

3

The objective can be achieved in three operations, as follows:

First, choose the path connecting Vertex 1, 2, and x = 1.

Then, choose the path connecting Vertex 2, 3, and x = 2.

Lastly, choose the path connecting Vertex 0, 4, and x = 4.

Sample Input 2

2
1 0 0

Sample Output 2

0


#include <iostream>
#include <string>
using namespace std;
int main(void){
    // Your code here!
    int x,a,b,out;
    cin >> x;
    cin >> a;
    cin >> b;
    out = (x - a)%b;
    cout << out << endl;
    return 0;
}

Score : 100 points

Problem Statement

You went shopping to buy cakes and donuts with X yen (the currency of Japan).

First, you bought one cake for A yen at a cake shop.
Then, you bought as many donuts as possible for B yen each, at a donut shop.

How much do you have left after shopping?

Constraints

1 \leq A, B \leq 1 000

A + B \leq X \leq 10 000

X, A and B are integers.

Input

Input is given from Standard Input in the following format:

X
A
B

Output

Print the amount you have left after shopping.

Sample Input 1

1234
150
100

Sample Output 1

84

You have 1234 - 150 = 1084 yen left after buying a cake.
With this amount, you can buy 10 donuts, after which you have 84 yen left.

Sample Input 2

1000
108
108

Sample Output 2

28

Sample Input 3

579
123
456

Sample Output 3

0

Sample Input 4

7477
549
593

Sample Output 4

405


#include<bits/stdc++.h>
using namespace std;
int main() {
    int a,b,c;
    cin>>a>>b>>c;
    cout<<(a-b)%c<<endl;
    return 0;
}


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

using namespace std;

struct Edge
{
  int to, cost;
};

int main()
{
  int N, M;
  cin >> N >> M;
  vector<Edge> edge[N];
  for (int i = 0; i < M; i++)
  {
    int L, R, D;
    cin >> L >> R >> D;
    L--;
    R--;
    edge[L].push_back({R, D});
    edge[R].push_back({L, -D});
  }
  bool ok = true;
  vector<long long> cost(N, 1e18);
  for (int i = 0; i < N; i++)
    if (cost[i] == 1e18)
    {
      queue<int> que;
      que.push(i);
      cost[i] = 0;
      while (que.size())
      {
        int now = que.front();
        que.pop();
        for (Edge t : edge[now])
          if (cost[t.to] == 1e18)
          {
            cost[t.to] = cost[now] + t.cost;
            que.push(t.to);
          }
          else if (cost[t.to] != cost[now] + t.cost)
            ok = false;
      }
    }
  if (ok)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;

  return 0;
}

Score : 400 points

Problem Statement

There are N people standing on the x-axis.
Let the coordinate of Person i be x_i.
For every i, x_i is an integer between 0 and 10^9 (inclusive).
It is possible that more than one person is standing at the same coordinate.

You will given M pieces of information regarding the positions of these people.
The i-th piece of information has the form (L_i, R_i, D_i).
This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.

It turns out that some of these M pieces of information may be incorrect.
Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.

Constraints

1 \leq N \leq 100 000

0 \leq M \leq 200 000

1 \leq L_i, R_i \leq N (1 \leq i \leq M)

0 \leq D_i \leq 10 000 (1 \leq i \leq M)

L_i \neq R_i (1 \leq i \leq M)

If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).

D_i are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M

Output

If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes; if it does not exist, print No.

Sample Input 1

3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

Yes

Some possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103).

Sample Input 2

3 3
1 2 1
2 3 1
1 3 5

Sample Output 2

No

If the first two pieces of information are correct, x_3 - x_1 = 2 holds, which is contradictory to the last piece of information.

Sample Input 3

4 3
2 1 1
2 3 5
3 4 2

Sample Output 3

Yes

Sample Input 4

10 3
8 7 100
7 9 100
9 8 100

Sample Output 4

No

Sample Input 5

100 0

Sample Output 5

Yes


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <deque>
using namespace std;

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

class Edge{
public:
  int x;
  int y;
  ll d;
  void set(int st, int go, ll dis){
    x=st;y=go;d=dis;
  }
  bool operator <(const Edge& f){return x<f.x;}
};

int main(){
  int n,m;
  ll loc[100010]={};
  bool ans=true;
  bool used[100010]={};
  Edge e[200010];
  int minp;
  cin>>n>>m;
  minp=n;
  for(int i=0;i<m;i++){
    int x,y;
    ll d;
    cin>>x>>y>>d;
    if(y<x){
      swap(x,y);
      d*=-1;
    }
    minp=min(minp,x);
    e[i].set(x,y,d);
  }
  sort(e,e+m);
  used[minp]=true;
  loc[minp]=0;
  for(int i=0;i<m;i++){
    int x,y;
    ll d;
    x=e[i].x;y=e[i].y;d=e[i].d;
    if(!used[x]){
      loc[x]=0;
      used[x]=true;
    }
    if(used[y]){
      if(loc[x]+d!=loc[y]){
       ans=false;
       cout<<x<<" "<<y<<" "<<loc[x]+d<<" "<<loc[y]<<endl;
       break;
      }
    }else{
      loc[y]=loc[x]+d;
      used[y]=true;
    }
  }
  cout<<(ans?"Yes":"No")<<endl;
  return 0;
}


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <queue>
#include <bitset>

typedef long long ll;
typedef std::vector< int > VI;

const double EPS = 1e-10;
const double PI  = acos(-1);
const ll     INF = 1 << 30;

int main(int argc, char* argv[])
{
  std::string a, b;
  std::cin >> a >> b;

  int x = std::atoi((a+b).c_str());

  std::string judge = "No";
  for (int i = 1; i * i <= x; i++) {
    if (i * i == x) {
      judge = "yes";
    }
  }

  std::cout << judge << std::endl;

  return 0;
}

Score : 200 points

Problem Statement

AtCoDeer the deer has found two positive integers, a and b.
Determine whether the concatenation of a and b in this order is a square number.

Constraints

1 ≤ a,b ≤ 100

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the concatenation of a and b in this order is a square number, print Yes; otherwise, print No.

Sample Input 1

1 21

Sample Output 1

Yes

As 121 = 11 × 11, it is a square number.

Sample Input 2

100 100

Sample Output 2

No

100100 is not a square number.

Sample Input 3

12 10

Sample Output 3

No


#include <iostream>
using namespace std;

int main() {
    int N;
    int t[110000], x[110000], y[110000];
    cin >> N;
    t[0] = x[0] = y[0] = 0;  // 初期状態
    for (int i = 0; i < N; ++i) cin >> t[i+1] >> x[i+1] >> y[i+1];  // 1-index にしておく

    bool can = true;
    for (int i = 0; i < N; ++i) {
        int dt = t[i+1] - t[i];
        int dist = abs(x[i+1] - x[i]) + abs(y[i+1] - y[i]);
        if (dt < dist) can = false;
        if (dist % 2 != dt % 2) can = false;  // dist と dt の偶奇は一致する必要あり！
    }

    if (can) cout << "Yes" << endl;
    else cout << "No" << endl;
}

Score : 300 points

Problem Statement

AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.

If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).
Note that he cannot stay at his place.
Determine whether he can carry out his plan.

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

t_i < t_{i+1} (1 ≤ i ≤ N-1)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

If AtCoDeer can carry out his plan, print Yes; if he cannot, print No.

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

For example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1).

Sample Input 2

1
2 100 100

Sample Output 2

No

It is impossible to be at (100,100) two seconds after being at (0,0).

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
const int INF = 1001001001;
const int MOD = 1000000007;
typedef long long ll;

template<typename T> void print(const T & v);

int main() {
    ll N;
    cin >> N;
    vector<ll> t(N), x(N), y(N);

    for (int i = 0; i < N; i++) {
        cin >> t.at(i) >> x.at(i) >> y.at(i);
    }
    ll a = 0, b = 0, ten = 0;
    bool ans = true;
    for (int i = 0; i < N; i++) {
        ll c = abs(x.at(i)-a) + abs(y.at(i)-b);
        ll d = t.at(i) - ten;
        if(d < c){
            cout << "No" << endl;
            return 0;
        }
        if(d == c){
            a = x.at(i);
            b = y.at(i);
            ten = t.at(i);
        }else if(d >= c && (d-c) % 2 == 0){
            a = x.at(i);
            b = y.at(i);
            ten = t.at(i);
        } else {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}

// Use For Debug
template <typename T>
void print(T const & v){
    for(int i = 0; i < v.size(); i++){
        if(i) cout << " ";
        cout << v[i];
    }
    cout << endl;
}

Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4


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

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

#include<bits/stdc++.h>
using namespace std;
using lli = long long int;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
const int MOD = 1000000007;
const int MOD1 = 998244353;
const int maxn = 100010;
const int lim = (int)1e9;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int n = 0, a = 0, b = 0;
	cin >> n >> a >> b;
	cout << (abs(a - b) & 1 ? "Borys" : "Alice") << endl;
}

Score : 300 points

Problem Statement

A game is played on a strip consisting of N cells consecutively numbered from 1 to N.

Alice has her token on cell A. Borys has his token on a different cell B.

Players take turns, Alice moves first.
The moving player must shift his or her token from its current cell X to the neighboring cell on the left, cell X-1, or on the right, cell X+1.
Note that it's disallowed to move the token outside the strip or to the cell with the other player's token.
In one turn, the token of the moving player must be shifted exactly once.

The player who can't make a move loses, and the other player wins.

Both players want to win. Who wins if they play optimally?

Constraints

2 \leq N \leq 100

1 \leq A < B \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print Alice if Alice wins, Borys if Borys wins, and Draw if nobody wins.

Sample Input 1

5 2 4

Sample Output 1

Alice

Alice can move her token to cell 3.
After that, Borys will be unable to move his token to cell 3, so he will have to move his token to cell 5.
Then, Alice moves her token to cell 4. Borys can't make a move and loses.

Sample Input 2

2 1 2

Sample Output 2

Borys

Alice can't make the very first move and loses.

Sample Input 3

58 23 42

Sample Output 3

Borys


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

ll binary_search(
    ll lo, ll hi, function<bool(ll)> is_ok,
    bool maximize
){
    while(lo < hi){
        if(maximize){
            const ll mid = (lo + hi + 1) / 2;
            if(is_ok(mid)){
                lo = mid;
            }else{
                hi = mid - 1;
            }
        }else{
            const ll mid = (lo + hi) / 2;
            if(is_ok(mid)){
                hi = mid;
            }else{
                lo = mid + 1;
            }
        }
    }
    return lo;
}

void solve(long long K, std::vector<long long> A){
    // max
    ll ans_max = 2;
    for(ll i = K - 1; i >= 0; --i){
        if(ans_max % A.at(i) != 0){
            cout << -1 << endl;
            return;
        }
        ll nxt_max = ans_max + A.at(i) - 1;
        if(i > 0){
            nxt_max -= nxt_max % A.at(i - 1);
            if(nxt_max < ans_max){
                cout << -1 << endl;
                return;
            }
        }
        ans_max = nxt_max;
    }

    ll ans_min = binary_search(
        0, ans_max,
        [&](ll mid) -> bool {
            REP(i, K){
                mid -= mid % A.at(i);
            }
            return mid == 2;
        },
        false
    );

    cout << ans_min << ' ' << ans_max << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
int main(){
    long long K;
    scanf("%lld",&K);
    std::vector<long long> A(K);
    for(int i = 0 ; i < K ; i++){
        scanf("%lld",&A[i]);
    }
    solve(K, std::move(A));
    return 0;
}

Score : 500 points

Problem Statement

An adult game master and N children are playing a game on an ice rink.
The game consists of K rounds.
In the i-th round, the game master announces:

Form groups consisting of A_i children each!

Then the children who are still in the game form as many groups of A_i children as possible.
One child may belong to at most one group.
Those who are left without a group leave the game. The others proceed to the next round.
Note that it's possible that nobody leaves the game in some round.

In the end, after the K-th round, there are exactly two children left, and they are declared the winners.

You have heard the values of A_1, A_2, ..., A_K. You don't know N, but you want to estimate it.

Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.

Constraints

1 \leq K \leq 10^5

2 \leq A_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

K
A_1 A_2 ... A_K

Output

Print two integers representing the smallest and the largest possible value of N, respectively,
or a single integer -1 if the described situation is impossible.

Sample Input 1

4
3 4 3 2

Sample Output 1

6 8

For example, if the game starts with 6 children, then it proceeds as follows:

In the first round, 6 children form 2 groups of 3 children, and nobody leaves the game.

In the second round, 6 children form 1 group of 4 children, and 2 children leave the game.

In the third round, 4 children form 1 group of 3 children, and 1 child leaves the game.

In the fourth round, 3 children form 1 group of 2 children, and 1 child leaves the game.

The last 2 children are declared the winners.

Sample Input 2

5
3 4 100 3 2

Sample Output 2

-1

This situation is impossible.
In particular, if the game starts with less than 100 children, everyone leaves after the third round.

Sample Input 3

10
2 2 2 2 2 2 2 2 2 2

Sample Output 3

2 3


#include <bits/stdc++.h>

#define __STDC_FORMAT_MACROS
#define p64 PRId64

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)    FOR (i,0,n)
#define ALL(f,x,...) ([&](decltype((x)) ALL) {                          \
            return (f)(begin(ALL), end(ALL), ## __VA_ARGS__); })(x)

using namespace std;

using ll = int64_t;

vector<ll> A;

template<class T> void gc(T &x)
{
    char c;
    int s = 0;
    x = 0;

    c = getchar_unlocked();

    if (c == '-') s = 1;
    else if ('0' <= c && c <= '9')  x = c - '0';

    while (true)
    {
        c = getchar_unlocked();

        if (c < '0' || '9' < c) break;
        x = x * 10 + (c - '0');
    }

    if (s) x = -x;
}

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

    A.resize(K);

    REP(i, K) gc(A[i]);

    bool flg = true;
    ll maxa = 2;
    ll mina = 2;

    if (A[K - 1] == 2)
    {
        for (int i = K - 1; 0 <= i; i--)
        {
            if (maxa < A[i])
            {
                flg = false;
                break;
            }
            else
            {
                if ((mina % A[i] == 0) && (maxa % A[i] == 0))
                {
                    maxa = (maxa / A[i] + 1) * A[i] - 1;
                }
                else
                {
                    if (mina % A[i] != 0)
                    {
                        mina = (mina / A[i] + 1) * A[i];
                    }

                    maxa = (maxa / A[i] + 1) * A[i] - 1;
                }
            }
        }
    }
    else
    {
        flg = false;
    }

    if (flg) cout << mina << ' ' << maxa << endl;
    else cout << -1 << endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin>>
#define Z class
#define ll long long
#define ln cout<<'\n'
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\n",a)
#define mem(a) memset(a,0,sizeof(a))
#define all(c) (c).begin(),(c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define tr(it,c) for(iter(c) it=(c).begin();it!=(c).end();it++)
template<Z A>void pr(A a){cout<<a;ln;}
template<Z A,Z B>void pr(A a,B b){cout<<a<<' ';pr(b);}
template<Z A,Z B,Z C>void pr(A a,B b,C c){cout<<a<<' ';pr(b,c);}
template<Z A,Z B,Z C,Z D>void pr(A a,B b,C c,D d){cout<<a<<' ';pr(b,c,d);}
template<Z A>void PR(A a,ll n){rep(i,n){if(i)cout<<' ';cout<<a[i];}ln;}
ll check(ll n,ll m,ll x,ll y){return x>=0&&x<n&&y>=0&&y<m;}
const ll MAX=1e9+7,MAXL=1LL<<61,dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
typedef pair<int,int> P;

void Main() {
  ll n;
  R n;
  ll a[n];
  rep(i,n) R a[i];
  ll x=2;
  if(a[n-1]!=2) {
    pr(-1);
    return;
  }
  rrep(i,n) {
    if(a[i]>=x*2) {
      pr(-1);
      return;
    }
    x=max(x,a[i]);
    ll t=ceil((double)x/a[i]);
    x=a[i]*t;
  }
  ll l=0,r=MAXL;
  while(l+1<r) {
    ll m=(l+r)/2;
    ll y=m+x;
    rep(i,n) y-=y%a[i];
    if(y==2) l=m;
    else r=m;
  }
  pr(x,x+l);
}

int main(){ios::sync_with_stdio(0);cin.tie(0);Main();return 0;}


#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define lp(i,a,n) for(int i=a;i<=n;++i)
#define lpd(i,a,n) for(int i=a;i>=n;--i)
#define mem(a,b) memset(a,b,sizeof a)
#define all(v) v.begin(),v.end()
#define println(a) cout <<(a) <<endl
#define sz(x) ((int)(x).size())
#define readi(x) scanf("%d",&x)
#define read2i(x,y) scanf("%d%d",&x,&y)
#define read3i(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define mod 1000000007
#define eps 1e-8
#define infi 1000000000
#define infll 1000000000000000000ll
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef set<int> si;
typedef map<int,int> mii;

const int N = 2002;
int n,a[N],mx;

int main(){
    readi(n);
    lp(i,1,n) readi(a[i]), mx = max(mx, a[i]);
    cout <<mx;
}

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

Score : 700 points

Problem Statement

You are given N integers A_1, A_2, ..., A_N.

Consider the sums of all non-empty subsequences of A. There are 2^N - 1 such sums, an odd number.

Let the list of these sums in non-decreasing order be S_1, S_2, ..., S_{2^N - 1}.

Find the median of this list, S_{2^{N-1}}.

Constraints

1 \leq N \leq 2000

1 \leq A_i \leq 2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the median of the sorted list of the sums of all non-empty subsequences of A.

Sample Input 1

3
1 2 1

Sample Output 1

2

In this case, S = (1, 1, 2, 2, 3, 3, 4). Its median is S_4 = 2.

Sample Input 2

1
58

Sample Output 2

58

In this case, S = (58).


#include<bits/stdc++.h>
using namespace std;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(V) (V).begin(),(V).end()
#define SORT(V) sort(ALL(V))

vector<string> split(string,string);

//template<typename T>
//void swap(T*,T*);
//char ToUpper (char cX) { return toupper(cX); }

int main()
{
    string s;cin >> s;
    vector<string> t = split(s,"/");
    cout << "2018" << "/" << t[1] << "/" <<t[2];
}

template<typename T>
void swap(T* a,T* b)
{
    int c = *a;
    *a = *b;
    *b = c;
}

vector<string> split(string str, string separator)
{
    if (separator == "")
        return {str};
    vector<string> result;
    string tstr = str + separator;
    long long unsigned int l = tstr.length(), sl = separator.length();
    string::size_type pos = 0, prev = 0;

    for (;pos < l && (pos = tstr.find(separator, pos)) != string::npos; prev = (pos += sl))
    {
        result.emplace_back(tstr, prev, pos - prev);
    }
    return result;
}

Score : 100 points

Problem Statement

On some day in January 2018, Takaki is writing a document. The document has a column where the current date is written in yyyy/mm/dd format. For example, January 23, 2018 should be written as 2018/01/23.

After finishing the document, she noticed that she had mistakenly wrote 2017 at the beginning of the date column. Write a program that, when the string that Takaki wrote in the date column, S, is given as input, modifies the first four characters in S to 2018 and prints it.

Constraints

S is a string of length 10.

The first eight characters in S are 2017/01/.

The last two characters in S are digits and represent an integer between 1 and 31 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Replace the first four characters in S with 2018 and print it.

Sample Input 1

2017/01/07

Sample Output 1

2018/01/07

Sample Input 2

2017/01/31

Sample Output 2

2018/01/31


#include <iostream>
#include <string>
using namespace std;

int main(){
  string s;
  cin>>s;

  s.at(3)='7';

  cout<<s<<endl;
}


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <string>

using namespace std;

int main() {

	string s;
	cin >> s;

	s[0] = '2';
	s[1] = '0';
	s[2] = '1';
	s[3] = '8';

	cout << s << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b)  for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n)    FOR(i,0,n)
#define ALL(a)      a.begin(),a.end()
#define vi          vector<int>
#define vvi         vector<vi>
#define P           pair<int,int>
#define vP          vector<P>
#define vvP         vector<vP>

int main(){
    string s;cin>>s;
    cout<<"2018";
    REP(i,s.length()-4){
        cout<<s[i+4];
    }
    cout<<"\n";
    return 0;
}


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

int main() {
	int M;
  	cin >> M;
  	cout << 48 - M << endl;
}

Score : 100 points

Problem Statement

How many hours do we have until New Year at M o'clock (24-hour notation) on 30th, December?

Constraints

1≤M≤23

M is an integer.

Input

Input is given from Standard Input in the following format:

M

Output

If we have x hours until New Year at M o'clock on 30th, December, print x.

Sample Input 1

21

Sample Output 1

27

We have 27 hours until New Year at 21 o'clock on 30th, December.

Sample Input 2

12

Sample Output 2

36


#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<list>
#include<set>
#include<map>
#include<utility>
#include<cmath>

#define rep(i,x) for(int i=0;i<(int)(x);i++)
#define reps(i,x) for(int i=1;i<=(int)(x);i++)
#define rrep(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define rreps(i,x) for(int i=((int)(x));i>0;i--)

#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define INF 2e9

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
typedef long long ll;
typedef std::pair<ll, ll> P;
int gcd(int a,int b){return b?gcd(b,a%b):a;}

using namespace std;

int main()
{
    ll n;
    cin >> n;
    vector<ll> c(n-1), s(n-1), f(n-1);

    rep(i, n-1){
        cin >> c[i] >> s[i] >> f[i];
    }

    rep(i, n){
        ll t = 0;
        for(int j=i; j<n-1; j++){
            if(t < s[j]) t = s[j];
            else if (t%f[j] == 0);
            else t += f[j] - t%f[j];
            t += c[j];
        }
        cout << t << endl;
    }
}

Score : 300 points

Problem Statement

A railroad running from west to east in Atcoder Kingdom is now complete.

There are N stations on the railroad, numbered 1 through N from west to east.

Tomorrow, the opening ceremony of the railroad will take place.

On this railroad, for each integer i such that 1≤i≤N-1, there will be trains that run from Station i to Station i+1 in C_i seconds. No other trains will be operated.

The first train from Station i to Station i+1 will depart Station i S_i seconds after the ceremony begins. Thereafter, there will be a train that departs Station i every F_i seconds.

Here, it is guaranteed that F_i divides S_i.

That is, for each Time t satisfying S_i≤t and t％F_i=0, there will be a train that departs Station i t seconds after the ceremony begins and arrives at Station i+1 t+C_i seconds after the ceremony begins, where A％B denotes A modulo B, and there will be no other trains.

For each i, find the earliest possible time we can reach Station N if we are at Station i when the ceremony begins, ignoring the time needed to change trains.

Constraints

1≤N≤500

1≤C_i≤100

1≤S_i≤10^5

1≤F_i≤10

S_i％F_i=0

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 S_1 F_1
:
C_{N-1} S_{N-1} F_{N-1}

Output

Print N lines. Assuming that we are at Station i (1≤i≤N) when the ceremony begins, if the earliest possible time we can reach Station N is x seconds after the ceremony begins, the i-th line should contain x.

Sample Input 1

3
6 5 1
1 10 1

Sample Output 1

12
11
0

We will travel from Station 1 as follows:

5 seconds after the beginning: take the train to Station 2.

11 seconds: arrive at Station 2.

11 seconds: take the train to Station 3.

12 seconds: arrive at Station 3.

We will travel from Station 2 as follows:

10 seconds: take the train to Station 3.

11 seconds: arrive at Station 3.

Note that we should print 0 for Station 3.

Sample Input 2

4
12 24 6
52 16 4
99 2 2

Sample Output 2

187
167
101
0

Sample Input 3

4
12 13 1
44 17 17
66 4096 64

Sample Output 3

4162
4162
4162
0


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

int main(){
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  if (A + B < C + D) {
    cout << "Left" << endl;
  }
  else if (A + B == C + D) {
    cout << "Balanced" << endl;
  }
  else {
    cout << "Right" << endl;
  }
}

Score : 100 points

Problem Statement

A balance scale tips to the left if L>R, where L is the total weight of the masses on the left pan and R is the total weight of the masses on the right pan. Similarly, it balances if L=R, and tips to the right if L<R.

Takahashi placed a mass of weight A and a mass of weight B on the left pan of a balance scale, and placed a mass of weight C and a mass of weight D on the right pan.

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Constraints

1\leq A,B,C,D \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Sample Input 1

3 8 7 1

Sample Output 1

Left

The total weight of the masses on the left pan is 11, and the total weight of the masses on the right pan is 8. Since 11>8, we should print Left.

Sample Input 2

3 4 5 2

Sample Output 2

Balanced

The total weight of the masses on the left pan is 7, and the total weight of the masses on the right pan is 7. Since 7=7, we should print Balanced.

Sample Input 3

1 7 6 4

Sample Output 3

Right

The total weight of the masses on the left pan is 8, and the total weight of the masses on the right pan is 10. Since 8<10, we should print Right.


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

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;

  int L = A + B;
  int R = C + D;

  if( L > R ){
    cout << "Left" << endl;
  } else if ( L < R ) {
    cout << "Right" << endl;
  } else {
    cout << "Balanced" << endl;
  }
}


#include <bits/stdc++.h>
#define i64 long long
#define ui64 unsigned long long
#define eps 1e-6
using namespace std;

int main() {
	// your code goes here
	double X,Y;
	cin>>X>>Y;
	double res = Y/X;
	printf("%llu\n", (ui64)log2(res)+1);
	//the sequence with max len will be of this form: X*(1,2,4,8,.. 2^k)

	return 0;
}

Score : 300 points

Problem Statement

As a token of his gratitude, Takahashi has decided to give his mother an integer sequence.
The sequence A needs to satisfy the conditions below:

A consists of integers between X and Y (inclusive).

For each 1\leq i \leq |A|-1, A_{i+1} is a multiple of A_i and strictly greater than A_i.

Find the maximum possible length of the sequence.

Constraints

1 \leq X \leq Y \leq 10^{18}

All input values are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

Print the maximum possible length of the sequence.

Sample Input 1

3 20

Sample Output 1

3

The sequence 3,6,18 satisfies the conditions.

Sample Input 2

25 100

Sample Output 2

3

Sample Input 3

314159265 358979323846264338

Sample Output 3

31


#include <bits/stdc++.h>

using namespace std;

string s;
int ss[100050];
inline int query(int x, int y){ if(x == 0) return ss[y]; else return ss[y]-ss[x-1]; }
int main(){
	cin >> s;
	int n = s.length();
	for(int i = 0; i<n; i++) ss[i] = s[i]-'0';
	for(int i = 1; i<n; i++) ss[i] += ss[i-1];
	if(query(0,n-1) == 0){ cout << n; return 0; }
	int ans = INT_MAX;
	for(int i = 0; i<n; i++){
		if(query(i,i) == 1){
			int cur = 0;
			if(query(0,i) == i+1) cur = max(i+1, cur);
			if(query(i,n-1) == n-i) cur = max(n-i, cur);
			cur = max(i, cur);
			cur = max(n-i-1, cur);
			ans = min(ans, cur);
		}
	}
	for(int i = 0; i<n; i++) ss[i] = s[i]-'0';
	for(int i = 0; i<n; i++) ss[i] = 1-ss[i];
	for(int i = 1; i<n; i++) ss[i] += ss[i-1];
	if(query(0,n-1) == 0){ cout << n; return 0; }
	int ans2 = INT_MAX;
	for(int i = 0; i<n; i++){
		if(query(i,i) == 1){
			int cur = 0;
			if(query(0,i) == i+1) cur = max(i+1, cur);
			if(query(i,n-1) == n-i) cur = max(n-i, cur);
			cur = max(i, cur);
			cur = max(n-i-1, cur);
			ans2 = min(ans2, cur);
		}
	}
	cout << max(ans,ans2);
}

Score : 500 points

Problem Statement

You are given a string S consisting of 0 and 1.
Find the maximum integer K not greater than |S| such that we can turn all the characters of S into 0 by repeating the following operation some number of times.

Choose a contiguous segment [l,r] in S whose length is at least K (that is, r-l+1\geq K must be satisfied). For each integer i such that l\leq i\leq r, do the following: if S_i is 0, replace it with 1; if S_i is 1, replace it with 0.

Constraints

1\leq |S|\leq 10^5

S_i(1\leq i\leq N) is either 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum integer K such that we can turn all the characters of S into 0 by repeating the operation some number of times.

Sample Input 1

010

Sample Output 1

2

We can turn all the characters of S into 0 by the following operations:

Perform the operation on the segment S[1,3] with length 3. S is now 101.

Perform the operation on the segment S[1,2] with length 2. S is now 011.

Perform the operation on the segment S[2,3] with length 2. S is now 000.

Sample Input 2

100000000

Sample Output 2

8

Sample Input 3

00001111

Sample Output 3

4


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
#define pr(x) cerr<<#x<<"="<<x<<endl
#define pri(x,lo) {cerr<<#x<<"={";for (int ol=0;ol<=lo;ol++)cerr<<x[ol]<<",";cerr<<"}"<<endl;}
#define N 1000
int n,ans=10000000,i;
char ch[1000000];
int main()
{
	scanf("%s",ch);int len=strlen(ch);for (i=1;i<len;i++) if (ch[i-1]!=ch[i])ans=min(ans,max(i,len-i));
	if (ans==10000000) printf("%d\n",n);
	else
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n; cin >> n;
  VV<> g(n);
  V<> deg(n);
  for (int _ = 0; _ < n - 1; ++_) {
    int u, v; cin >> u >> v, --u, --v;
    g[u].push_back(v);
    g[v].push_back(u);
    ++deg[u], ++deg[v];
  }
  cout << count_if(begin(deg), end(deg), [](int x) { return x & 1; }) / 2 << ' ';
  auto chk = [&](int x) -> bool {
    V<> dp(n, 1e9);
    auto dfs = [&](const auto& dfs, int v, int p) -> void {
      multiset<int> ms;
      for (int w : g[v]) if (w != p) {
        dfs(dfs, w, v);
        ms.insert(dp[w] + 1);
      }
      if (~ms.size() & 1) ms.insert(0);
      while (ms.size() >= 2) {
        auto itr = ms.upper_bound(x - *prev(end(ms)));
        if (itr == begin(ms)) return;
        ms.erase(--itr);
        ms.erase(prev(end(ms)));
      }
      dp[v] = *begin(ms);
    };
    dfs(dfs, 0, -1);
    return dp[0] <= x;
  };
  int ng = 0, ok = n - 1;
  while (ok - ng > 1) {
    int mid = ng + ok >> 1;
    (chk(mid) ? ok : ng) = mid;
  }
  cout << ok << '\n';
}

Score : 900 points

Problem Statement

Takahashi has decided to make a Christmas Tree for the Christmas party in AtCoder, Inc.

A Christmas Tree is a tree with N vertices numbered 1 through N and N-1 edges, whose i-th edge (1\leq i\leq N-1) connects Vertex a_i and b_i.

He would like to make one as follows:

Specify two non-negative integers A and B.

Prepare A Christmas Paths whose lengths are at most B. Here, a Christmas Path of length X is a graph with X+1 vertices and X edges such that, if we properly number the vertices 1 through X+1, the i-th edge (1\leq i\leq X) will connect Vertex i and i+1.

Repeat the following operation until he has one connected tree:

Select two vertices x and y that belong to different connected components. Combine x and y into one vertex. More precisely, for each edge (p,y) incident to the vertex y, add the edge (p,x). Then, delete the vertex y and all the edges incident to y.

Properly number the vertices in the tree.

Takahashi would like to find the lexicographically smallest pair (A,B) such that he can make a Christmas Tree, that is, find the smallest A, and find the smallest B under the condition that A is minimized.

Solve this problem for him.

Constraints

2 \leq N \leq 10^5

1 \leq a_i,b_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N
a_1 b_1
:
a_{N-1} b_{N-1}

Output

For the lexicographically smallest (A,B), print A and B with a space in between.

Sample Input 1

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

Sample Output 1

3 2

We can make a Christmas Tree as shown in the figure below:

Sample Input 2

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

Sample Output 2

2 5

Sample Input 3

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

Sample Output 3

3 4


#include <iostream>

#include <vector>
#include <map>
#include <algorithm>
#include <fstream>
#include<cstdio>
#include<iomanip>
#include<stack>
#include<queue>
#include<string>
#include <cstdlib>
#include <typeinfo>

#define REP(i, n) for(int i=0;i<n;i++)
#define REP2(i, s, n) for(int i=s;i<n;i++)
#define REP_1(i, n) for(int i=1;i<n+1;i++)
#define bitSearch(bit, n) for(int bit = 0; bit < (1 << N); bit++)
using namespace std;

void printAns(long long a) { cout << a << endl; }

void yesno(bool a) { if (a)cout << "Yes" << endl; else cout << "No" << endl; }

void YESNO(bool a) { if (a)cout << "YES" << endl; else cout << "NO" << endl; }

typedef long long ll;
typedef unsigned long ul;
typedef long double ld;

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

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

//ll INF = 10000000;
//ll mod = 1000000007;//10^9+7

//using Graph = vector<vector<pair<int, int>>>;
//Graph G(100100);

//番号ズレ注意！！
int main() {
    int a, b;
    cin >> a >> b;
    printAns((a + b - 1) / 2 + 1);
}

Score : 100 points

Problem Statement

You are given two positive integers a and b.
Let x be the average of a and b.
Print x rounded up to the nearest integer.

Constraints

a and b are integers.

1 \leq a, b \leq 100

Input

Input is given from Standard Input in the following format:

a b

Output

Print x rounded up to the nearest integer.

Sample Input 1

1 3

Sample Output 1

2

The average of 1 and 3 is 2.0, and it will be rounded up to the nearest integer, 2.

Sample Input 2

7 4

Sample Output 2

6

The average of 7 and 4 is 5.5, and it will be rounded up to the nearest integer, 6.

Sample Input 3

5 5

Sample Output 3

5


        #include <bits/stdc++.h>
        using namespace std;
   #define int long long
const int MAX = 510000;
const int MOD = 1000000007;

     signed main(){
string s,t;
cin>>s>>t;
sort(s.begin(),s.end());
sort(t.begin(),t.end(),greater<char>());
if(s<t)cout<<"Yes"<<endl;
else cout<<"No"<<endl;

 }

Score : 200 points

Problem Statement

You are given strings s and t, consisting of lowercase English letters.
You will create a string s' by freely rearranging the characters in s.
You will also create a string t' by freely rearranging the characters in t.
Determine whether it is possible to satisfy s' < t' for the lexicographic order.

Notes

For a string a = a_1 a_2 ... a_N of length N and a string b = b_1 b_2 ... b_M of length M, we say a < b for the lexicographic order if either one of the following two conditions holds true:

N < M and a_1 = b_1, a_2 = b_2, ..., a_N = b_N.

There exists i (1 \leq i \leq N, M) such that a_1 = b_1, a_2 = b_2, ..., a_{i - 1} = b_{i - 1} and a_i < b_i. Here, letters are compared using alphabetical order.

For example, xy < xya and atcoder < atlas.

Constraints

The lengths of s and t are between 1 and 100 (inclusive).

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If it is possible to satisfy s' < t', print Yes; if it is not, print No.

Sample Input 1

yx
axy

Sample Output 1

Yes

We can, for example, rearrange yx into xy and axy into yxa. Then, xy < yxa.

Sample Input 2

ratcode
atlas

Sample Output 2

Yes

We can, for example, rearrange ratcode into acdeort and atlas into tslaa. Then, acdeort < tslaa.

Sample Input 3

cd
abc

Sample Output 3

No

No matter how we rearrange cd and abc, we cannot achieve our objective.

Sample Input 4

w
ww

Sample Output 4

Yes

Sample Input 5

zzz
zzz

Sample Output 5

No


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

const int N = 1e5 + 100;

int n, num, ans, cnt[N];

int main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n;
	while(n--) {
		cin >> num;
		if(num >= N)
			ans++;
		else
			cnt[num]++;
	}
	for (int i = 1; i < N; i++)
		if(cnt[i] < i)
			ans += cnt[i];
		else
			ans += cnt[i] - i;
	cout << ans;
	return 0;
}

Score : 300 points

Problem Statement

You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).
Your objective is to remove some of the elements in a so that a will be a good sequence.

Here, an sequence b is a good sequence when the following condition holds true:

For each element x in b, the value x occurs exactly x times in b.

For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.

Find the minimum number of elements that needs to be removed so that a will be a good sequence.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of elements that needs to be removed so that a will be a good sequence.

Sample Input 1

4
3 3 3 3

Sample Output 1

1

We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

We can, for example, remove two occurrences of 4. Then, (2, 1, 2) is a good sequence.

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

Remove one occurrence of 10^9. Then, () is a good sequence.

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5


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

map<int,int> m;
vector<int> v;

int main(){
    int n,ans;
    cin>>n;
    for(register int i=1;i<=n;++i){
        int x;
        cin>>x;
        if(m[x]==0) v.push_back(x);
        ++m[x];
    }
    for(register int i=0;i<v.size();++i) if(m[v[i]]>=v[i]) ans+=m[v[i]]-v[i]; else ans+=m[v[i]];
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define int long long
#define double long double
#define mod 1000000007
#define F first
#define S second
#define P pair<long long,long long>
#define all(a) a.begin(),a.end()
#define INF 10000000000000000
#define endl '\n'
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
using namespace std;

bool X[16001],Y[16001];
bool tmp2[16001];

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

    string s; cin>>s;
    int x,y; cin>>x>>y;
    s+='T'; X[x+8000]=Y[y+8000]=true;

    int cnt=0,che=0;
    vector<int> xc,yc;
    rep(i,s.size()){
        if(s.at(i)=='T'){
            if(cnt>0){
                if(che%2==0)xc.push_back(cnt);
                else yc.push_back(cnt);
            }
            cnt=0; che++;
        }else cnt++;
    }
    reverse(all(xc));
    reverse(all(yc));

    if(xc.size()==0)goto heaven;
    rep(i,xc.size()-1){
        bool tmp[16001]={false};
        rep(j,16001){
            if(X[j]){
                if(j+xc.at(i)<=16000)tmp[j+xc.at(i)]=true;
                if(j-xc.at(i)>=0)tmp[j-xc.at(i)]=true;
            }
        }
        rep(j,16001)X[j]=tmp[j];
    }
    rep(j,16001){
        if(X[j]){
            if(j-xc.back()>=0)tmp2[j-xc.back()]=true;
            if(s.front()=='T')if(j+xc.back()<=16000)tmp2[j+xc.back()]=true;
        }
    }
    rep(i,16001)X[i]=tmp2[i];

heaven:
    if(yc.size()==0)goto hell;
    rep(i,yc.size()){
        bool tmp[16001]={false};
        rep(j,16001){
            if(Y[j]){
                if(j+yc.at(i)<=16000)tmp[j+yc.at(i)]=true;
                if(j-yc.at(i)>=0)tmp[j-yc.at(i)]=true;
            }
        }
        rep(j,16001)Y[j]=tmp[j];
    }

hell:
    cout<<(X[8000] and Y[8000] ? "Yes":"No")<<endl;
}

Score : 500 points

Problem Statement

A robot is put at the origin in a two-dimensional plane.
Initially, the robot is facing in the positive x-axis direction.

This robot will be given an instruction sequence s.
s consists of the following two kinds of letters, and will be executed in order from front to back.

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

The objective of the robot is to be at coordinates (x, y) after all the instructions are executed.
Determine whether this objective is achievable.

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

|x|, |y| \leq |s|

Input

Input is given from Standard Input in the following format:

s
x y

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.

Sample Input 6

TTTT
1 0

Sample Output 6

No


#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
#define ll long long

int main () {
  string h;
  cin >> h;
  // 区切って行く.
  vector<ll> tate, yoko;
  ll now = 0;
  ll itr = -1;
  ll right = 0;
  ll x, y;
  cin >> x >> y;
  for (ll i = 0; i < h.size(); i++) {
    if (h[i] == 'T') {
      itr = i;
      right = now;
      break;
    }
    else {
      now++;
    }
    if (i == h.size() - 1) {
      itr = i;
      right = now;
    }
  }
  now = 0;
  for (ll i = itr + 1; i < h.size(); i++) {
    if (h[i] == 'T') {
      if (tate.size() > yoko.size()) {
        yoko.push_back(now);
      }
      else {
        tate.push_back(now);
      }
      now = 0;
    }
    else {
      now++;
    }
    if (i == h.size() - 1 && h[i] == 'F') {
      if (tate.size() > yoko.size()) {
        yoko.push_back(now);
      }
      else {
        tate.push_back(now);
      }
    }
  }

  // xについて
  ll dp1[yoko.size() + 1][8000 * 2 + 1];
  for (ll i = 0; i <= yoko.size(); i++) {
    for (ll j = 0; j <= 8000 * 2; j++) {
      dp1[i][j] = -1;
    }
  }
  dp1[0][right + 8000] = 0;
  for (ll i = 1; i <= yoko.size(); i++) {
    for (ll j = 0; j <= 8000 * 2; j++) {
      if (dp1[i - 1][j] == 0) {
        dp1[i][j + yoko[i - 1]] = 0;
        dp1[i][j - yoko[i - 1]] = 0;
      }
    }
  }
  // yについて
  ll dp2[tate.size() + 1][8000 * 2 + 1];
  for (ll i = 0; i <= tate.size(); i++) {
    for (ll j = 0; j <= 8000 * 2; j++) {
      dp2[i][j] = -1;
    }
  }
  dp2[0][8000] = 0;
  for (ll i = 1; i <= tate.size(); i++) {
    for (ll j = 0; j <= 8000 * 2; j++) {
      if (dp2[i - 1][j] == 0) {
        dp2[i][j + tate[i - 1]] = 0;
        dp2[i][j - tate[i - 1]] = 0;
      }
    }
  }
  return 0;

}


#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>
#include <map>

using namespace std;
string yn(bool a)
{
  if (a){return "Yes";}
  else{return "No";}
}

int main()
{
  ios::sync_with_stdio(false);
  string s;
  int x,y;
  cin>>s>>x>>y;
  vector<int> a,b;
  bool f=1;
  int tmp=0;
  for(auto itr=s.begin();itr !=s.end();itr++)
  {
    if(*itr=='F'){tmp++;}
    else {
      if(f){a.push_back(tmp);}
      else{b.push_back(tmp);}
      f=!f;
    tmp=0;
  }

  }
  if(f){a.push_back(tmp);}
  else{b.push_back(tmp);}
  x-=a[0];
  x=abs(x);
  y=abs(y);
  a.erase(a.begin());
  sort(a.begin(),a.end());
  sort(b.begin(),b.end());
  int suma=0,sumb=0;
  for(auto itr=a.begin();itr !=a.end();itr++)
  {
    //cout<<*itr<<endl;
    suma+=*itr;
  }
  for(auto itr=b.begin();itr !=b.end();itr++)
  {
    sumb+=*itr;
    //cout<<*itr<<endl;
  }
  int flag=0;
  //cout<<x<<' '<<y<<' '<<suma<<' '<<sumb;
  if((suma-x)%2 or (sumb-y)%2){flag=0;}
  else
  {
    x=(suma-x)/2;
    y=(sumb-y)/2;
    //cout<<x<<' '<<y;
    vector<int> dpx(8001),dpy(8001);
    for(int i=0;i<x+1;i++){dpx[i]=0;dpy[i]=0;}
    dpx[0]=1;dpx[suma-x]=1;
    dpy[0]=1;dpy[sumb-y]=1;
    for(auto itr1=a.begin();itr1 !=a.end();itr1++)
    {
      if(dpx[x]!=0 or dpx[suma-x]!=0){flag+=1;break;}
      for(int i=0;i<suma+1;i++)
      {
        if(dpx[i]==1){dpx[i+*itr1]=1;}
      }
      if(dpx[x]!=0 or dpx[suma-x]!=0){flag+=1;break;}

    }
    //cout<<flag<<endl;
    for(auto itr2=b.begin();itr2 !=b.end();itr2++)
    {
      if(dpy[y]!=0 or dpy[sumb-y]!=0){flag+=1;break;}
      for(int i=0;i<sumb+1;i++)
      {
        if(dpy[i]==1){dpy[i+*itr2]=1;}
      }
      if(dpy[y]!=0 or dpy[sumb-y]!=0){flag+=1;break;}
    }
  }
  cout<<yn(flag==2)<<endl;

  return 0;

}


#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
using namespace std;

void solve(){
  int n; cin >> n;
  vector<int> a(n);
  for(int i = 0; i < n; i++){
    cin >> a[i];
  }
  int ans = 1000000;
  for(int i = 0; i < n; i++){
    int x = a[i], sum = 0;
    while(x % 2 == 0){
      x /= 2;
      sum++;
    }
    ans = min(sum, ans);
  }
  cout << ans << endl;
  return;
}

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

Score : 200 points

Problem Statement

There are N positive integers written on a blackboard: A_1, ..., A_N.

Snuke can perform the following operation when all integers on the blackboard are even:

Replace each integer X on the blackboard by X divided by 2.

Find the maximum possible number of operations that Snuke can perform.

Constraints

1 \leq N \leq 200

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of operations that Snuke can perform.

Sample Input 1

3
8 12 40

Sample Output 1

2

Initially, [8, 12, 40] are written on the blackboard.
Since all those integers are even, Snuke can perform the operation.

After the operation is performed once, [4, 6, 20] are written on the blackboard.
Since all those integers are again even, he can perform the operation.

After the operation is performed twice, [2, 3, 10] are written on the blackboard.
Now, there is an odd number 3 on the blackboard, so he cannot perform the operation any more.

Thus, Snuke can perform the operation at most twice.

Sample Input 2

4
5 6 8 10

Sample Output 2

0

Since there is an odd number 5 on the blackboard already in the beginning, Snuke cannot perform the operation at all.

Sample Input 3

6
382253568 723152896 37802240 379425024 404894720 471526144

Sample Output 3

8


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

int main() {
  int n;
  cin >> n;
  int a[n];
  for(int i=0; i<n; i++){
  cin >> a[i];
  }
  int x=0;
  while(1){
    for(int j=0; j<n; j++){
      if(a[j]%2 == 0){
        a[j] = a[j]/2;
      }
      else{
        cout << x << endl;
        return 0;
      }
    }
    x++;
  }
}


#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define erep(i,a,b) for(int i=a;i<=(int)(b);i++)
#define per(i,a,b) for(int i=(b)-1;i>(a);i--)
#define eper(i,a,b) for(int i=((int)(a));i>=b;i--)
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
const int INF=(1<<30)-1;
const int MOD=1000000007;
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int gcd(int a,int b){return b?gcd(b,a%b):a;}

int n, a[200];
int main() {
 ios::sync_with_stdio ( false );
 cin.tie ( 0 );
    cin >> n;
    int k = 0;
    rep(i, 0, n) {
        cin >> a[i];
    }
    k = a[0];
    int cnt = 0;
    rep(i, 1, n) k = gcd(k, a[i]);
    if (k%2) {
        cout << 0 << endl;
    } else {
        while (k > 0) {
            k/=2;
            cnt++;
        }
        printf("%d\n", cnt-1);
    }
    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
const ll MOD = 1000000007;
const ll INF = 1000000000000000000;
#define all(v) v.begin(), v.end()

int main(){
  ll N,ma=-INF,x=0,mi=INF,y=0;
  cin >> N;
  vector<ll> A(N);
  rep(i,N){
    cin >> A.at(i);
    if(A.at(i)>ma){
      ma=max(ma,A.at(i));
      x=i;
    }
    if(mi>A.at(i)){
      mi=min(mi,A.at(i));
      y=i;
    }
  }
  if(abs(ma)<abs(mi)){
    rep(i,N){
      cout << y+1 << " " << i+1 << endl;;
    }
    rep(i,N-1){
      cout << N-i << " " << N-1-i << endl;
    }
  }else{
    rep(i,N){
      cout << x+1 << " " << i+1 << endl;;
    }
    rep(i,N-1){
      cout << i+1 << " " << i+2 << endl;
    }
  }
}

Score : 600 points

Problem Statement

Snuke has an integer sequence, a, of length N. The i-th element of a (1-indexed) is a_{i}.

He can perform the following operation any number of times:

Operation: Choose integers x and y between 1 and N (inclusive), and add a_x to a_y.

He would like to perform this operation between 0 and 2N times (inclusive) so that a satisfies the condition below. Show one such sequence of operations.
It can be proved that such a sequence of operations always exists under the constraints in this problem.

Condition: a_1 \leq a_2 \leq ... \leq a_{N}

Constraints

2 \leq N \leq 50

-10^{6} \leq a_i \leq 10^{6}

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Let m be the number of operations in your solution. In the first line, print m.
In the i-th of the subsequent m lines, print the numbers x and y chosen in the i-th operation, with a space in between.
The output will be considered correct if m is between 0 and 2N (inclusive) and a satisfies the condition after the m operations.

Sample Input 1

3
-2 5 -1

Sample Output 1

2
2 3
3 3

After the first operation, a = (-2,5,4).

After the second operation, a = (-2,5,8), and the condition is now satisfied.

Sample Input 2

2
-1 -3

Sample Output 2

1
2 1

After the first operation, a = (-4,-3) and the condition is now satisfied.

Sample Input 3

5
0 0 0 0 0

Sample Output 3

0

The condition is satisfied already in the beginning.


#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 1120000000

int n;
vector<long> v;

using P = pair<int,int>;

vector<P> solve(int l, int r){
  if(l+1 >= r) return {};

  bool ok = true;
  rep(i,l,r-1) if(v[i] > v[i+1]) ok = false;
  if(ok) return {};

  int mx = 0;
  int mi = -1;
  rep(i,l,r){
    if(abs(v[i]) > mx || (v[i] == -mx)){
      mx = abs(v[i]);
      mi = i;
    }
  }
  assert(mi>=0);

  if(v[mi]>0){
    auto ret = solve(l, mi);
    rep(i,mi,r) if(i < n-1){
      while(v[i] > v[i+1]){
        ret.pb(mp(i,i+1));
        v[i+1] += v[i];
      }
    }
    return ret;
  }
  else {
    auto ret = solve(mi+1, r);
    for(int i=mi; i>=l; i--) if(i > 0){
      while(v[i-1] > v[i]){
        ret.pb(mp(i, i-1));
        v[i-1] += v[i];
      }
    }
    return ret;
  }
}

int main(){
  cin>>n;
  v.resize(n);
  rep(i,n) cin>>v[i];

  auto ans = solve(0,n);
// dbg(v);
  cout << ans.size() << endl;
  for(auto &p : ans) cout << p.fi+1 << " " << p.se+1 << "\n";

  return 0;
}


#include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<ll(n);++i)
#define RREP(i,n) for(ll i=ll(n)-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<ll(n);++i)
#define RFOR(i,m,n) for(ll i=ll(n)-1;i>=ll(m);--i)
#define ALL(v) (v).begin(),(v).end()
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(aa, (v).size()) { cout << v[aa]; if (aa != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

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

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(´・ω・`)(´・ω・`)(´・ω・`)(´・ω・`)(´・ω・`)(´・ω・`)///

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	vi v(n);
	int maxv = -INF, minv = INF;
	int maxp = -1, minp = -1;
	REP(i, n) {
		cin >> v[i];
		if (maxv < v[i]) {
			maxv = v[i];
			maxp = i;
		}
		if (minv > v[i]) {
			minv = v[i];
			minp = i;
		}
	}
	int cnt = 0;

	vector<pii> op;
	if (abs(maxv) > abs(minv)||(maxv==minv&&maxv>0)) {
		assert(maxv >= 0);
		FOR(i, 1, n) {
			while (v[i] < v[i - 1]) {
				cnt++;
				v[i] += maxv;
				op.push_back(pii{ maxp + 1,i + 1 });
			}
			if (v[i] > maxv) {
				maxv = v[i];
				maxp = i;
			}

		}
	}
	else {
		assert(minv <= 0);
		REP(i, n - 1) {
			while (v[i+1] < v[i]) {
				cnt++;
				v[i] += minv;
				op.push_back(pii{ minp + 1,i + 1 });
			}
			if (v[i] < minv) {
				minv = v[i];
				minp = i;
			}
		}
	}
	cout << cnt << endl;
	REP(i, cnt) {
		cout << op[i].first << " " << op[i].second << endl;
	}

}


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

int main() {
  int N, A, B;
  cin >> N >> A >> B;

  if ((N * A) < B) {
    cout << N * A << endl;
  }
  else if ((N * A) > B) {
    cout << B << endl;
  }
 }

Score : 100 points

Problem Statement

You are parking at a parking lot. You can choose from the following two fee plans:

Plan 1: The fee will be A×T yen (the currency of Japan) when you park for T hours.

Plan 2: The fee will be B yen, regardless of the duration.

Find the minimum fee when you park for N hours.

Constraints

1≤N≤20

1≤A≤100

1≤B≤2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

When the minimum fee is x yen, print the value of x.

Sample Input 1

7 17 120

Sample Output 1

119

If you choose Plan 1, the fee will be 7×17=119 yen.

If you choose Plan 2, the fee will be 120 yen.

Thus, the minimum fee is 119 yen.

Sample Input 2

5 20 100

Sample Output 2

100

The fee might be the same in the two plans.

Sample Input 3

6 18 100

Sample Output 3

100


#include<iostream>
using namespace std;
int main()
{
    int n;
	cin>>n;
	int s=0, m=n;
    while(m!=0)
	{
    s+=m%10;
    m/=10;
    }
    if(n%s==0) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

Score : 200 points

Problem Statement

An integer X is called a Harshad number if X is divisible by f(X), where f(X) is the sum of the digits in X when written in base 10.

Given an integer N, determine whether it is a Harshad number.

Constraints

1?N?10^8

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print Yes if N is a Harshad number; print No otherwise.

Sample Input 1

12

Sample Output 1

Yes

f(12)=1+2=3. Since 12 is divisible by 3, 12 is a Harshad number.

Sample Input 2

57

Sample Output 2

No

f(57)=5+7=12. Since 57 is not divisible by 12, 12 is not a Harshad number.

Sample Input 3

148

Sample Output 3

No


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

int main()
{
    long long N, C;
    cin >> N >> C;

    long long s, t, c;
    vector<vector<long long>> imos(35, vector<long long>(100005, 0));
    for (long long i = 0; i < N; i++)
    {
        cin >> s >> t >> c;
        if (imos[c][s] == -1)
        {
            imos[c][s]++;
        }
        else
        {
            imos[c][s - 1]++;
        }
        imos[c][t]--;
    }
    // for (int i = 0; i < 5; i++)
    // {
    //     for (int j = 0; j < 15; j++)
    //     {
    //         cout << imos[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    // cout << endl;

    vector<vector<long long>> sum = imos;
    for (long long i = 1; i < sum.size(); i++)
    {
        sum[i][0] += sum[i - 1][0];
    }
    for (long long i = 1; i < sum.size(); i++)
    {
        for (long long j = 1; j < sum[i].size(); j++)
        {
            assert(-1 <= imos[i][j] && imos[i][j] <= 1);
            sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
        }
    }

    // for (int i = 0; i < 5; i++)
    // {
    //     for (int j = 0; j < 15; j++)
    //     {
    //         cout << sum[i][j] << " ";
    //     }
    //     cout << endl;
    // }
    // cout << endl;

    long long ans = 0;
    for (long long s : sum.back())
    {
        ans = max(ans, s);
    }

    // for (int i = 0; i < 15; i++)
    // {
    //     cout << sum[sum.size() - 1][i] << " ";
    // }
    // cout << endl;

    cout << ans << endl;
}

Score : 400 points

Problem Statement

Joisino is planning to record N TV programs with recorders.

The TV can receive C channels numbered 1 through C.

The i-th program that she wants to record will be broadcast from time s_i to time t_i (including time s_i but not t_i) on Channel c_i.

Here, there will never be more than one program that are broadcast on the same channel at the same time.

When the recorder is recording a channel from time S to time T (including time S but not T), it cannot record other channels from time S-0.5 to time T (including time S-0.5 but not T).

Find the minimum number of recorders required to record the channels so that all the N programs are completely recorded.

Constraints

1≤N≤10^5

1≤C≤30

1≤s_i<t_i≤10^5

1≤c_i≤C

If c_i=c_j and i≠j, either t_i≤s_j or s_i≥t_j.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N C
s_1 t_1 c_1
:
s_N t_N c_N

Output

When the minimum required number of recorders is x, print the value of x.

Sample Input 1

3 2
1 7 2
7 8 1
8 12 1

Sample Output 1

2

Two recorders can record all the programs, for example, as follows:

With the first recorder, record Channel 2 from time 1 to time 7. The first program will be recorded. Note that this recorder will be unable to record other channels from time 0.5 to time 7.

With the second recorder, record Channel 1 from time 7 to time 12. The second and third programs will be recorded. Note that this recorder will be unable to record other channels from time 6.5 to time 12.

Sample Input 2

3 4
1 3 2
3 4 4
1 4 3

Sample Output 2

3

There may be a channel where there is no program to record.

Sample Input 3

9 4
56 60 4
33 37 2
89 90 3
32 43 1
67 68 3
49 51 3
31 32 3
70 71 1
11 12 3

Sample Output 3

2