#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
using namespace std;

#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define RFOR(i, a, b) for (int i = a; i > b; --i)
#define REP(i, n) for (int i = 0; i < n; ++i)

string s;
int gx, gy;
int x = 0, y = 0;
string ans = "";

typedef pair<int, int> Pair;
vector<Pair> goals;

int main() {
  cin >> s;
  cin >> gx >> gy;
  goals.push_back(Pair(gx, gy));

  bool to_x = true;
  REP(i, s.length()) {
    if (s[i] == 'F') {
      if (to_x) { ++x; } else { ++y; }
    } else {
      if (to_x) {
        REP(j, goals.size()) {
          if (goals[j].second < y) {
            goals[j].second += 2 * (y - goals[j].second);
          }
        }
        int gs = goals.size();
        REP(j, gs) {
          goals.push_back(Pair(
            goals[j].first,
            y + 2*(goals[i].second - y)));
        }
        to_x = false;
      } else {
        REP(j, goals.size()) {
          if (goals[j].first < x) {
            goals[j].first += 2 * (x - goals[j].first);
          }
        }
        int gs = goals.size();
        REP(j, gs) {
          goals.push_back(Pair(
            x + 2*(goals[j].first - x),
            goals[j].second));
        }
        to_x = true;
      }
    }
  }

  bool f = false;
  REP(i, goals.size()) {
    if (x == goals[i].first && y == goals[i].second) {
      f = true;
      break;
    }
  }
  if (f) {
    ans = "Yes";
  } else {
    ans = "No";
  }

  cout << ans << endl;
  return 0;
}

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 <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <utility>
#include <bitset>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define mp make_pair
#define pb push_back
#define off 10000
using namespace std;
typedef long long ll;
bitset<off * 2 + 5> xx, yy;
char str[off + 5];
int main()
{
	// freopen("ARC087-D.in", "r", stdin);
	scanf("%s", str);
	int n = strlen(str), cnt = 0;
	bool f = false, fst = true;
	xx.set(off);
	yy.set(off);
	for (int i = 0; i <= n; i++)
	{
		if (str[i] == 'F')
			cnt++;
		else
		{
			auto &t = f ? yy : xx;
			if (fst)
				t <<= cnt;
			else
				t = (t << cnt) | (t >> cnt);
			fst = false;
			cnt = 0;
			f ^= 1;
		}
	}
	int x, y;
	scanf("%d%d", &x, &y);
	if (x < -off || y < -off || x > off || y > off)
		puts("No");
	else
		puts(xx.test(x + off) && yy.test(y + off) ? "Yes" : "No");
	return 0;
}

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

Submitted source-code file size, in bytes.

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

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


#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    int N,cnt,B;
    cnt = 0;
    B = 0;
    cin >> N;
    int A[N];
    cin >> A[N];

    for (int i = 0; i < N; i++){
    B = B + A[i];
    }
    while (B%2 == 0){
        cnt = cnt++;
        B/=2;
    }

    cout << cnt << endl;
}

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
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

int main(){
    int N,tar;
    tar = 0;
    cin >> N;
    int Nind = N-1;
    vector< pair<int,int> > seq(N, make_pair(0,0) );
    int count = 0;
    EACH(iter,seq){
        int val;
        cin >> val;
        *iter = make_pair(val, count);
        count++;
    }
   int delta;

    queue< pair<int, int> > q;
    vector< pair<int,int> >  ord = seq;
    while(tar != N){
        delta = seq[tar+1].first - seq[tar].first;

        if( delta >= 0 ){
            tar++;
            continue;
        }else{
            ord = seq;
            SORT(ord);
            auto iter = lower_bound( ALL(ord), make_pair(-delta,-1) );
            if( iter == ord.end() ) iter--;
            seq[tar+1].first += iter->first ;
            q.push( make_pair( iter->second + 1  ,(tar+1)+1 ) ) ;
                }
    }

    cout << q.size() << endl;
    while( !q.empty() ){
        auto f = q.front().first;
        auto s = q.front().second;
        cout << f << " " << s << endl;
        q.pop();
    }
}

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>
constexpr long long INFL = 1LL << 60;
constexpr int INF = 1e9;

using namespace std;

typedef long long ll;

int main()
{
    string N;
    cin >> N;
    int f = 0;
    for(auto c : N)
        f += c - '0';
    istringstream iss(N);
    int x;
    iss >> x;
    if(x % f == 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() {
  int N, n, f=0;
  cin >> N;
  n = N;
  while (n>0)f+=n%10,n/=10;
  if (N%f==0)cout << "Yes" << endl;
  else cout << "No" << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
  ll n,C;cin>>n>>C;
  ll s[n],t[n],c[n];
  vector<double>tukeru[31],kesu[31];
  vector<pair<ll,ll>>v[31];
  for(ll i=0;i<n;i++){
    cin>>s[i]>>t[i]>>c[i];
    v[c[i]].push_back(make_pair(s[i],t[i]));
  }
  for(ll i=1;i<31;i++){
    if(v[i].size()==0)continue;
    sort(v[i].begin(),v[i].end());
    tukeru[i].push_back(v[i][0].first-0.5);
    kesu[i].push_back(v[i][0].second);
    for(ll j=1;j<v[i].size();j++){
      ll f=v[i][j].first,s=v[i][j].second;
      if(f==kesu[i].back()){
        kesu[i].pop_back();
      }
      else{
        //kesu[i][kesu[i].size()-1]++;
        tukeru[i].push_back(f-0.5);
      }
      kesu[i].push_back(s);
    }
    //kesu[i][kesu[i].size()-1]++;
  }
  //for(ll i=1;i<=C;i++)for(ll j=0;j<kesu[i].size();j++)cout<<tukeru[i][j]<<endl;
  ll now=0,ans=0;
  ll ban[31];
  //cout<<tukeru[2][0];return 0;
  fill(ban,ban+31,0);
  for(double i=0;i<=100001;i+=0.5){
    for(ll j=1;j<31;j++){
      if(v[j].size()==0)continue;
      if(tukeru[j][ban[j]]==i){
        now++;
      }
      if(kesu[j][ban[j]]==i){
        now--;
        ban[j]++;
      }
      //cout<<j<<" "<<ban[j]<<endl;
    }
   //if(i<=100)cout<<now<<endl;
      ans=max(now,ans);
  }
  ll d[100010];
  fill(d,d+100010,0);
  for(ll i=0;i<n;i++){

    d[s[i]]++;
    d[t[i]+1]--;
  }
  ll noww=0,anss=0;
  for(ll i=1;i<=100000;i++){
    noww+=d[i];
    anss=max(anss,noww);
  }
  cout<<min(ans,anss);
}

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


#include<bits/stdc++.h>
using namespace std;
int main(){
  string s;
  cin >> s;
  vector<int> c(3);
  int n=s.size();
  for(int i=0;i<n;i++){
    c[s[i]-'a']++;
  }
  string ans="YES";
  for(int i=0;i<3;i++){
    if(c[0]*c[1]*c[2]!=0&&(n+3-1)/3<c[i]){
      ans="NO";
    }
  }
  cout << ans << endl;
}

Score : 400 points

Problem Statement

Snuke has a string S consisting of three kinds of letters: a, b and c.

He has a phobia for palindromes, and wants to permute the characters in S so that S will not contain a palindrome of length 2 or more as a substring. Determine whether this is possible.

Constraints

1 \leq |S| \leq 10^5

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If the objective is achievable, print YES; if it is unachievable, print NO.

Sample Input 1

abac

Sample Output 1

YES

As it stands now, S contains a palindrome aba, but we can permute the characters to get acba, for example, that does not contain a palindrome of length 2 or more.

Sample Input 2

aba

Sample Output 2

NO

Sample Input 3

babacccabab

Sample Output 3

YES


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

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

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

// Self settings
// clang-format off
#define MAX_N 100000
#define REP(i, N) for (int i = 0; i < (int)(N); ++i)
#define SLN(i,N) (i == N-1 ? "\n" : " ")
ll fact(ll n) { ll res = 1; for(ll i=2;i<=n;++i) res = res * i; return res;}
ll nCr(ll n, ll r) {return (fact(n)/fact(n-r)*fact(r)) ;}
ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
const ll MOD = 1e9+7;
const ll INF = 1LL << 60;
const int inf = 1 << 31;
// clang-format on

int N;

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

  int m = 0, cnt = 0;
  int prev = 100;
  int n = N;
  while (n > 0) {
    int cur = n % 10;
    if (prev == cur) {
      cnt++;
    }
    else {
      cnt = 1;
    }
    m = max(m, cnt);
    prev = cur;
    n /= 10;
  }
  if (m >= 3) {
    cout << "Yes" << endl;
  }
  else {
    cout << "No" << endl;
  }

  return 0;
}

Score : 100 points

Problem Statement

We call a 4-digit integer with three or more consecutive same digits, such as 1118, good.

You are given a 4-digit integer N. Answer the question: Is N good?

Constraints

1000 ≤ N ≤ 9999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is good, print Yes; otherwise, print No.

Sample Input 1

1118

Sample Output 1

Yes

N is good, since it contains three consecutive 1.

Sample Input 2

7777

Sample Output 2

Yes

An integer is also good when all the digits are the same.

Sample Input 3

1234

Sample Output 3

No


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

int main() {
  int n, a, b, c, d;
  cin >> n;
  a = n/1000;
  n -= a*1000;
  b = n/100;
  n -= b*100;
  c = n/10;
  n -= c*10;
  d = n;
  if ((a == b && a == c) || (b == c && b == d)) {
    cout << "Yes" << endl;
  }else{
    cout << "No" << endl;
  }
}


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

int main() {
  int N,a,b,c,d;
  cin >> N;
  a = N/1000;
  b = (N/100)-(a*10);
  c = (N/10)-(N/100*10);
  d = N-(N/10)*10;
  if ( a == b && b == c ){
    cout << "Yes" << endl;
  if ( b == c && c == d ){
    cout << "Yes" << endl;
  }
  }

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

}


#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <numeric>
#include <cstdlib>
#include <queue>
#include <stack>
#include <climits>

using namespace std;

typedef long long ll;
#define pb push_back

#define rep(i, n) for (int i=0;i<(n);++i)
#define rep2(i, s, n) for (int i=s;i<(n);++i)
#define rev(i, n) for (int i=(n);i>0;--i)
#define INF (1e9+1e9)

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

    auto cnt = 0;
    int j;
    rep(i,10) {
        auto c = count(n.begin(), n.end(), to_string(i)[0]);
        if (c >= cnt) j = i;
        cnt = max(cnt, (int)c);
    }
    string s = to_string(j);
    s.append(2,s[0]);
    if (cnt >= 3 && n.find(s) != string::npos ) cout << "Yes" << endl;
    else cout << "No" << endl;

}


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

#define P(p) cout<<(p)<<endl
#define rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define rrep(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(),greater<int>());
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define ret return
#define lb(v,n) lower_bound(v.begin(),v.end(),n)
#define ub(v,n) upper_bound(v.begin(),v.end(),n)
#define mae(v) max_element(v.begin(),v.end())
#define mie(v) min_element(v.begin(),v.end())
#define INF 0x7FFFFFFF
#define mod 1000000007
typedef long long ll;
////////////////////////////////////////////////////////////

bool f[8][8];
int ans = 0;
int n;
void solve( int now , vector<bool> c , int cnt ){
	/*
	if( cnt == 0 ){
		ans++;
		ret;
	}
	*/

	rep(i,0,n){
		if( !c[i] && f[now][i] ){
			vector<bool> tmp = c;
			tmp[i] = 1;
			solve( i , tmp , cnt-1 );
		}
	}

}

int main(){
	int m;
	cin >> n >> m;
	rep(i,0,m){
		int a,b;
		cin >> a >> b;
		a--; b--;
		f[a][b] = 1;
		f[b][a] = 1;
	}

	vector<bool> c(n,0);
	c[0] = 1;
	solve( 0 , c , n-1);

	cout << ans << endl;

	ret 0;
}

Score : 200 points

Problem Statement

It is November 18 now in Japan. By the way, 11 and 18 are adjacent Lucas numbers.

You are given an integer N. Find the N-th Lucas number.

Here, the i-th Lucas number L_i is defined as follows:

L_0=2

L_1=1

L_i=L_{i-1}+L_{i-2} (i≥2)

Constraints

1≤N≤86

It is guaranteed that the answer is less than 10^{18}.

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


#include <iostream>
#include <string>

using namespace std;

int main(int argc, char const *argv[])
{
    string s;
    cin >> s;
    int num[4];
    for(int i=0; i<4; i++){
        num[i] = s[i] - '0';
    }

    for(int i=0; i<8; i++){
        int sum = num[0];
        for(int j=0; j<3; j++){
            if((i>>j)&1) sum += num[j+1];
            else sum -= num[j+1];
        }
        if(sum==7){
            cout << num[0];
            for(int j=0; j<3; j++){
                if((i>>j)&1) cout << '+';
                else cout << '-';
                cout << num[j+1];
            }
            cout << endl;
            return 0;
        }
    }
    return 0;
}

Score : 300 points

Problem Statement

Sitting in a station waiting room, Joisino is gazing at her train ticket.

The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).

In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.

The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

Constraints

0≤A,B,C,D≤9

All input values are integers.

It is guaranteed that there is a solution.

Input

Input is given from Standard Input in the following format:

ABCD

Output

Print the formula you made, including the part =7.

Use the signs + and -.

Do not print a space between a digit and a sign.

Sample Input 1

1222

Sample Output 1

1+2+2+2=7

This is the only valid solution.

Sample Input 2

0290

Sample Output 2

0-2+9+0=7

0 - 2 + 9 - 0 = 7 is also a valid solution.

Sample Input 3

3242

Sample Output 3

3+2+4-2=7


#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

const int INF = pow(2, 31) - 1;
#define ll long long
#define DEBUG(x) cout << "DEBUG>" << #x << ":" << x << endl;
#define REP(i,n) for(ll i=0;i<(n);i++)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

int main() {
  vector<string> ki(2);
  ki[0] = "+";
  ki[1] = "-";
  int s;
  cin >> s;
  int D = s % 10;
  s /= 10;
  int C = s % 10;
  s /= 10;
  int B = s % 10;
  s /= 10;
  int A = s % 10;
  REP(i, 2) {
    REP(j, 2) {
      REP(k, 2) {
        int a = A;
        if(i == 0) {
          a += B;
        } else {
          a -= B;
        }
        if(j == 0) {
          a += C;
        } else {
          a -= C;
        }
        if(k == 0) {
          a += D;
        } else {
          a -= D;
        }
        if(a == 7) {
          cout << A << ki[i] << B << ki[j] << C << ki[k] << D << "=7" << endl;
          return 0;
        }
      }
    }
  }
  return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
int main() {
	char x, y;
	cin >> x >> y;
	if (x < y) {
		cout << "<" << endl;
	}
	else if (x == y) {
		cout << "=" << endl;
	}
	else {
		cout << ">" << endl;
	}
}

Score : 100 points

Problem Statement

In programming, hexadecimal notation is often used.

In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters A, B, C, D, E and F are used to represent the values 10, 11, 12, 13, 14 and 15, respectively.

In this problem, you are given two letters X and Y. Each X and Y is A, B, C, D, E or F.

When X and Y are seen as hexadecimal numbers, which is larger?

Constraints

Each X and Y is A, B, C, D, E or F.

Input

Input is given from Standard Input in the following format:

X Y

Output

If X is smaller, print <; if Y is smaller, print >; if they are equal, print =.

Sample Input 1

A B

Sample Output 1

<

10 < 11.

Sample Input 2

E C

Sample Output 2

>

14 > 12.

Sample Input 3

F F

Sample Output 3

=

15 = 15.


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

typedef long long int ll;
typedef long double ld;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef pair<ll,ll> P;
typedef vector<P> vp;

#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define REPR(i,n) for(int i=n-1;i>=0;i--)
#define FORR(i,n,m) for(int i=n-1;i>=m;i--)
#define ARR(array) for(int i=0;i<array.size();i++)
#define all(in) in.begin(),in.end()
#define ALL(in,K) (in),(in)+(K)

#define INF 1e18
#define MOD 1000000007
#define SIZE 10005
#define PI 	3.14159265358979323846

ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } //最大公約数
ll lcm(ll a, ll b) { return a/gcd(a, b)*b; } //最小公倍数

ll input(){
ll a;
  cin >> a;
  return a;
}

vi sub(vector<bool> fin,ll n) {
  vi ret;
  REP(i,n) {
    if(fin.at(i))continue;
    vector<bool> cp = fin;
    cp.at(i) = true;
    vi result = sub(cp,n);
    ret.insert(ret.end(),result.begin(),result.end());
  }
  return ret;
}

int main() {
  char a,b;
  cin >> a >> b;
  if(a==b)cout << "=" << endl;
  if(a<b)cout << "<" << endl;
  if(a>b)cout << ">" << endl;
}


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

int main(){
    char x, y;
    cin >> x >> y;
    char ans;
    if(x > y)ans = '>';
    else if(x < y)ans = '<';
    else ans = '=';
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define pb push_back
using ll = long long;
using vin=vector<int>;
using vvin=vector<vin>;
using vll=vector<ll>;
using vvll=vector<vll>;
using vst=vector<string>;
using P = pair<ll,ll>;
const int inf=1e9+7;
const ll INF=9e18;
template <typename T> bool chmin(T &a, const T& b){if(a > b){a = b;return true;}return false;}
template <typename T> bool chmax(T &a, const T& b){if(a < b){a = b;return true;}return false;}
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };

int main(){
  //考察問題
  int n,z,w;
  cin>>n>>z>>w;
  if(n==3&&z==100)return 0;
  vin a(n);
  rep(i,0,n){
   cin>>a[i];
  }
  if(n!=1){
  cout<<max(abs(a[n-1]-w),(a[n-1],a[n]))<<endl;
  }
  else{

   cout<<abs(a[0]-w)<<endl;
  }
}

Score : 500 points

Problem Statement

We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is a_i.

Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:

Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.

The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

Constraints

All input values are integers.

1 \leq N \leq 2000

1 \leq Z, W, a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N Z W
a_1 a_2 ... a_N

Output

Print the score.

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900

If X draws two cards first, Y will draw the last card, and the score will be |1000 - 100| = 900.

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900

If X draws all the cards first, the score will be |1000 - 100| = 900.

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999


#include <iostream>
using namespace std;

int main() {
    string a,b;
    cin>>a>>b;
    int i,n=a.size();
    for(i=0;i<n;i++){
        if(a[i]!=b[n-1-i]){
            cout<<"NO";return 0;
        }

    }
    cout<<"YES";
	return 0;
}

Score : 100 points

Problem Statement

You are given a grid with 2 rows and 3 columns of squares.
The color of the square at the i-th row and j-th column is represented by the character C_{ij}.

Write a program that prints YES if this grid remains the same when rotated 180 degrees, and prints NO otherwise.

Constraints

C_{i,j}(1 \leq i \leq 2, 1 \leq j \leq 3) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

C_{11}C_{12}C_{13}
C_{21}C_{22}C_{23}

Output

Print YES if this grid remains the same when rotated 180 degrees; print NO otherwise.

Sample Input 1

pot
top

Sample Output 1

YES

This grid remains the same when rotated 180 degrees.

Sample Input 2

tab
bet

Sample Output 2

NO

This grid does not remain the same when rotated 180 degrees.

Sample Input 3

eye
eel

Sample Output 3

NO


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

int main(){
  int n;
  cin>>n;
  cout<<(int)(sqrt(n));
}

Score : 200 points

Problem Statement

Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer.

Constraints

1 \leq N \leq 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the largest square number not exceeding N.

Sample Input 1

10

Sample Output 1

9

10 is not square, but 9 = 3 × 3 is. Thus, we print 9.

Sample Input 2

81

Sample Output 2

81

Sample Input 3

271828182

Sample Output 3

271821169


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

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL<<60;
const double PI=3.14159265358979323846;
const int NMAX=100005;
using namespace std;

int search_upper(int i, int j, ll v, vector<ll> s){
    int k = (i+j)/2;
    if(s[k] <= v){
        if(k == j) return j+1;
        return search_upper(k+1,j,v,s);
    } else {
        if(k == 0) return 0;
        return search_upper(i,k-1,v,s);
    }
}

int search_lower(int i, int j, ll v, vector<ll> s){
    int k = (i+j)/2;
    if(s[k] >= v){
        if(k == 0) return -1;
        return search_lower(i,k-1,v,s);
    } else {
        if(k == j) return j;
        return search_lower(k+1,j,v,s);
    }
}

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

    ll ans = 0;
    for(int i = 0; i < n; i++){
        ll nc = n-search_upper(0,n-1,b[i],c);
        ll na = search_lower(0,n-1,b[i],a)+1;

        ans += na*nc;
    }

    cout << ans << endl;

    return 0;
}

Score : 300 points

Problem Statement

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.

He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i.

To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.

How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints

1 \leq N \leq 10^5

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

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

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N
B_1 ... B_N
C_1 ... C_N

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

Upper: 1-st part, Middle: 1-st part, Lower: 1-st part

Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part

Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

87


#include<bits/stdc++.h>

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

using namespace std;
int main() {

	int N;
	cin >> N;

	vector<int> A(N), B(N), C(N);

	REP(i, N) 	cin >> A[i];
	REP(i, N) 	cin >> B[i];
	REP(i, N) 	cin >> C[i];

	sort(ALL(A));
	sort(ALL(B));
	sort(ALL(C));
	long long ans = 0;
	long long a = 0, c = 0;
	REP(i, N) {
		a = (lower_bound(ALL(A), B[i]) - A.begin());
		c = (C.end() - upper_bound(ALL(C), B[i]));
		ans += a * c;
	}

	cout << ans;

}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

ll N;
ll A[100005], B[100005], C[100005];

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

	cin >> N;

	for (int i = 0;i < N;i++){
		cin >> A[i];
	}
	for (int i = 0;i < N;i++){
		cin >> B[i];
	}
	for (int i = 0;i < N;i++){
		cin >> C[i];
	}

	sort(A,A+N); sort(C,C+N);

	ll ans = 0;

	for (int i = 0;i < N;i++){
		ll nA = lower_bound(A,A+N,B[i])-A;
		ll nC = C+N-upper_bound(C,C+N,B[i]);
		ans += nA*nC;
	}

	cout << ans << endl;

	return 0;

}


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

int flg[100009] = {};

int main(){

	int k;
	cin>>k;
	deque<int>DQ;
	DQ.push_back(1);
	flg[1]=1;
	while(1){
		deque<int>::iterator it = DQ.begin();
		int tmp = *it;
		DQ.pop_front();

		if(tmp%k==0){
			cout<<flg[tmp%k]<<endl;
			return 0;
		}

		if(flg[(tmp+1)%k]==0){
			DQ.push_back((tmp+1)%k);
			flg[(tmp+1)%k]=flg[tmp] + 1;
		}
		if(flg[(tmp*10)%k]==0){
			DQ.push_front((tmp*10)%k);
			flg[(tmp*10)%k]=flg[tmp];
		}

	}

	return 0;
}

Score : 700 points

Problem Statement

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Constraints

2 \leq K \leq 10^5

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Sample Input 1

6

Sample Output 1

3

12=6×2 yields the smallest sum.

Sample Input 2

41

Sample Output 2

5

11111=41×271 yields the smallest sum.

Sample Input 3

79992

Sample Output 3

36


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

const int maxn = 300005;
int a[maxn], top, n, K;

int main() {
	scanf("%d%d", &K, &n);
	if (!(K&1)) {
		printf("%d ", K / 2);
		rep (i, 2, n) printf("%d ", K / 2 + 1);
		return 0;
	}
	rep (i, 1, n) a[i] = (K + 1) / 2;
	top = n;
	rep (i, 1, n / 2)
		if (a[top] == 1) top--;
		else {
			a[top]--;
			while (top < n) a[++top] = K;
		}
	rep (i, 1, top) printf("%d ", a[i]);

	return 0;
}

Score : 800 points

Problem Statement

In Finite Encyclopedia of Integer Sequences (FEIS), all integer sequences of lengths between 1 and N (inclusive) consisting of integers between 1 and K (inclusive) are listed.

Let the total number of sequences listed in FEIS be X. Among those sequences, find the (X/2)-th (rounded up to the nearest integer) lexicographically smallest one.

Constraints

1 \leq N,K \leq 3 × 10^5

N and K are integers.

Input

Input is given from Standard Input in the following format:

K N

Output

Print the (X/2)-th (rounded up to the nearest integer) lexicographically smallest sequence listed in FEIS, with spaces in between, where X is the total number of sequences listed in FEIS.

Sample Input 1

3 2

Sample Output 1

2 1

There are 12 sequences listed in FEIS: (1),(1,1),(1,2),(1,3),(2),(2,1),(2,2),(2,3),(3),(3,1),(3,2),(3,3).
The (12/2 = 6)-th lexicographically smallest one among them is (2,1).

Sample Input 2

2 4

Sample Output 2

1 2 2 2

Sample Input 3

5 14

Sample Output 3

3 3 3 3 3 3 3 3 3 3 3 3 2 2


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

#include <assert.h>    // LON
#include <math.h>    // sqrt()

using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
//#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) ll(x.size())
typedef long long ll;
//typedef pair<int, int> P;
//typedef pair<ll, ll> P;
//const double INF = 1e10;
//const ll INF = 10e15;
const ll MINF = -10e10;
const int INF = INT_MAX;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

//typedef priority_queue<P, vector<P>, greater<P>> PQ_ASK;
const int mod = 1000000007;

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

    vector<ll> over_lines;

    {
        vector<ll> ts(n);
        vector<ll> vs(n);
        rep(i, n) {
            cin >> ts[i];
        }
        rep(i, n) {
            cin >> vs[i];
        }

        ll ts_sum = 0;
        for (ll i : ts) {
            ts_sum += i;
        }
        over_lines.resize(ts_sum + 2, 0);

        ll index = 1;
        for (ll i = 0; i < n; i++) {
            for (ll j = 0; j < ts[i]; j++) {
                over_lines[index] = vs[i];
                index++;
            }
        }

    }
    for (ll i = 1; i < over_lines.size(); i++) {
        over_lines[i] = min(over_lines[i], over_lines[i - 1] + 1);
    }

    for (ll i = over_lines.size() - 2; i >= 0; i--) {
        over_lines[i] = min(over_lines[i], over_lines[i + 1] + 1);
    }

    ll ans = 0;

    for (ll i = 1; i < over_lines.size() - 1; i++) {
        ll a = over_lines[i - 1];
        ll b = over_lines[i];
        ll c = over_lines[i + 1];
        assert(b > 0);
        assert(abs(a - b) <= 1);
        assert(abs(b - c) <= 1);
        if (a == b && b == c) {
            ans += b * 100ll;
        } else if (a > b && b < c) {
            ans += b * 100ll;
        } else if ((a > b && b == c) || (a == b && b < c)) {
            ans += b * 100ll;
        } else if ((a == b && b > c) || (a < b && b == c)) {
            ans += ((b - 1) * 100ll) + 50;
        } else if (a < b && b > c) {
            ans += ((b - 1) * 100ll) + 25;
        } else if ((a < b && b < c) || (a > b && b > c)) {
            ans += ((b - 1) * 100ll) + 50;
        } else {
            assert(false);
        }
    }

    cout << setprecision(10) << ((long double) ans) / 100 << endl;
}

Score : 400 points

Problem Statement

In the year 2168, AtCoder Inc., which is much larger than now, is starting a limited express train service called AtCoder Express.

In the plan developed by the president Takahashi, the trains will run as follows:

A train will run for (t_1 + t_2 + t_3 + ... + t_N) seconds.

In the first t_1 seconds, a train must run at a speed of at most v_1 m/s (meters per second). Similarly, in the subsequent t_2 seconds, a train must run at a speed of at most v_2 m/s, and so on.

According to the specifications of the trains, the acceleration of a train must be always within ±1m/s^2. Additionally, a train must stop at the beginning and the end of the run.

Find the maximum possible distance that a train can cover in the run.

Constraints

1 \leq N \leq 100

1 \leq t_i \leq 200

1 \leq v_i \leq 100

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 t_2 t_3 … t_N
v_1 v_2 v_3 … v_N

Output

Print the maximum possible that a train can cover in the run.

Output is considered correct if its absolute difference from the judge's output is at most 10^{-3}.

Sample Input 1

1
100
30

Sample Output 1

2100.000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 30 seconds, it accelerates at a rate of 1m/s^2, covering 450 meters.

In the subsequent 40 seconds, it maintains the velocity of 30m/s, covering 1200 meters.

In the last 30 seconds, it decelerates at the acceleration of -1m/s^2, covering 450 meters.

The total distance covered is 450 + 1200 + 450 = 2100 meters.

Sample Input 2

2
60 50
34 38

Sample Output 2

2632.000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 34 seconds, it accelerates at a rate of 1m/s^2, covering 578 meters.

In the subsequent 26 seconds, it maintains the velocity of 34m/s, covering 884 meters.

In the subsequent 4 seconds, it accelerates at a rate of 1m/s^2, covering 144 meters.

In the subsequent 8 seconds, it maintains the velocity of 38m/s, covering 304 meters.

In the last 38 seconds, it decelerates at the acceleration of -1m/s^2, covering 722 meters.

The total distance covered is 578 + 884 + 144 + 304 + 722 = 2632 meters.

Sample Input 3

3
12 14 2
6 2 7

Sample Output 3

76.000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 6 seconds, it accelerates at a rate of 1m/s^2, covering 18 meters.

In the subsequent 2 seconds, it maintains the velocity of 6m/s, covering 12 meters.

In the subsequent 4 seconds, it decelerates at the acceleration of -1m/s^2, covering 16 meters.

In the subsequent 14 seconds, it maintains the velocity of 2m/s, covering 28 meters.

In the last 2 seconds, it decelerates at the acceleration of -1m/s^2, covering 2 meters.

The total distance covered is 18 + 12 + 16 + 28 + 2 = 76 meters.

Sample Input 4

1
9
10

Sample Output 4

20.250000000000000000

The maximum distance is achieved when a train runs as follows:

In the first 4.5 seconds, it accelerates at a rate of 1m/s^2, covering 10.125 meters.

In the last 4.5 seconds, it decelerates at the acceleration of -1m/s^2, covering 10.125 meters.

The total distance covered is 10.125 + 10.125 = 20.25 meters.

Sample Input 5

10
64 55 27 35 76 119 7 18 49 100
29 19 31 39 27 48 41 87 55 70

Sample Output 5

20291.000000000000


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<string> vs;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
	ll to;
 	ll cost;
 };
using Graph = vector<vector<Edge>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 3.1415926535897932;
const ll INF = LLONG_MAX;
string ch = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
/****** define constant end ********/
/****** define variable start ******/
string s;
/******* define variable end *******/
int solve() {
	cin >> s;
	string serasex = "";
	REP(i, s.size()){
		if(s.substr(i, 1) != "x"){
			serasex += s.substr(i, 1);
		}
	}
	ll hanbun = serasex.size() / 2;
	bool flag = true;
	REP(i, hanbun){
		if(serasex.substr(i, 1) != serasex.substr(serasex.size() - 1 - i,1)){
			flag = false;
		}
	}
	ll count = 0;
	ll ite;
	ll ite2;
	REP(i, s.size()){
		if(s.substr(i, 1) != "x"){
			count++;
			if(count == ceil((long double)serasex.size()/2)){
				ite = i;
			}
		}
	}
	count = 0;
	if(serasex.size() % 2 == 0){
		REP(i, s.size()){
			if(s.substr(i, 1) != "x"){
				count++;
				if(count == serasex.size()/2 + 1){
					ite2 = i;
				}
			}
		}
	}
	if(!flag){
		cout << -1 << endl;
	}else{
		if(serasex.size() % 2 == 0){
			ll ans = 0;
			bool flag2 = true;
			ll i = ite;
			ll j = ite2;
			while(flag2){
				if(i < 0 && j >= s.size()){
					flag2 = false;
					break;
				}
				if(i < 0){
					ans++;
					j++;
					continue;
				}
				if(j >= s.size()){
					ans++;
					i--;
					continue;
				}
				if(s.substr(i, 1) != s.substr(j, 1)){
					ans++;
					if(s.substr(i, 1) == "x"){
						i--;
					}else{
						j++;
					}
				}
				i--;
				j++;
			}
			cout << ans << endl;
		}else{
			ll ans = 0;
			bool flag2 = true;
			ll i = ite-1;
			ll j = ite+1;
			while(flag2){
				if(i < 0 && j >= s.size()){
					flag2 = false;
					break;
				}
				if(i < 0){
					ans++;
					j++;
					continue;
				}
				if(j >= s.size()){
					ans++;
					i--;
					continue;
				}
				if(s.substr(i, 1) != s.substr(j, 1)){
					ans++;
					if(s.substr(i, 1) == "x"){
						i--;
					}else{
						j++;
					}
				}
				i--;
				j++;
			}
			cout << ans << endl;
		}
	}
	return 0;
}

int main() {
	// clock_t start = clock();
	cout << std::fixed << std::setprecision(10);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int end_solve = solve();
	/*
	clock_t end = clock();
	const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
	1000.0; printf("time %lf[ms]\n", time);
	*/

	return end_solve;
}

Score : 400 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke can perform the following operation repeatedly:

Insert a letter x to any position in s of his choice, including the beginning and end of s.

Snuke's objective is to turn s into a palindrome.
Determine whether the objective is achievable. If it is achievable, find the minimum number of operations required.

Notes

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are palindromes, while ab, abab and abcda are not.

Constraints

1 \leq |s| \leq 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

If the objective is achievable, print the number of operations required.
If it is not, print -1 instead.

Sample Input 1

xabxa

Sample Output 1

2

One solution is as follows (newly inserted x are shown in bold):

xabxa → xaxbxa → xaxbxax

Sample Input 2

ab

Sample Output 2

-1

No sequence of operations can turn s into a palindrome.

Sample Input 3

a

Sample Output 3

0

s is a palindrome already at the beginning.

Sample Input 4

oxxx

Sample Output 4

3

One solution is as follows:

oxxx → xoxxx → xxoxxx → xxxoxxx


#include<bits/stdc++.h>
#define For(i,a,b) for(i=(a);i<=(b);++i)
#define Forward(i,a,b) for(i=(a);i>=(b);--i)
using namespace std;
template<typename T>inline void read(T &x)
{
	T s=0,f=1;char k=getchar();
	while(!isdigit(k)&&(k^'-'))k=getchar();
	if(!isdigit(k)){f=-1;k=getchar();}
	while(isdigit(k)){s=s*10+(k^48);k=getchar();}
	x=s*f;
}
void file()
{
	#ifndef ONLINE_JUDGE
	freopen("a.in","r",stdin);
	freopen("a.out","w",stdout);
	#endif
}
const int MAXN=700010;
int n,ans;
char a[MAXN];
int main()
{
	scanf("%s",a);
	n=strlen(a)-1;
	int i=0,j=n,ans=0;
	while(i<j)
	{
		int k=0,lj=j;
		while(a[i]=='x'&&i<=n)++i,++k;
		while(a[j]=='x'&&j>=i)--j,--k;
		if(i>j&&j==lj)break;
		ans+=abs(k);
		if(i>j)break;
		if(a[i]!=a[j])
		{
			printf("-1\n");
			return 0;
		}
		++i;--j;
		if(i>j)break;
	}
	printf("%d\n",ans);
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i=0;i < (int)(n);i++)

int main(){
   int a,b,c;
   string s;
   cin >> a >> b >> c;
   if (a+b == 2*a) cout << c<< endl;
   else if (a+c == a*2) cout << b << endl;
   else if (b+c == c*2) cout << a << endl;

   return 0;

}

Score : 100 points

Problem Statement

You are given three integers, A, B and C.

Among them, two are the same, but the remaining one is different from the rest.

For example, when A=5,B=7,C=5, A and C are the same, but B is different.

Find the one that is different from the rest among the given three integers.

Constraints

-100 \leq A,B,C \leq 100

A, B and C are integers.

The input satisfies the condition in the statement.

Input

Input is given from Standard Input in the following format:

A B C

Output

Among A, B and C, print the integer that is different from the rest.

Sample Input 1

5 7 5

Sample Output 1

7

This is the same case as the one in the statement.

Sample Input 2

1 1 7

Sample Output 2

7

In this case, C is the one we seek.

Sample Input 3

-100 100 100

Sample Output 3

-100


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

map<int,int>vis;

int main(){
	int a,b,c;scanf("%d %d %d",&a,&b,&c);
	vis[a]++,vis[b]++,vis[c]++;
	if(vis[a] == 1) cout << a;
	else if(vis[b] == 1) cout << b;
	else if(vis[c] == 1) cout << c;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <bits/stdc++.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
using edge = struct {ll to; ll cost;};
typedef string str;
typedef std::pair<ll,ll> pl;
typedef std::tuple<ll, ll, ll> pl3;
typedef std::map<string, ll> msl;
typedef std::map<char, ll> mcl;
typedef std::map<ll, ll> mll;
typedef std::vector<ll> vl;
typedef std::vector<pl> vpl;
typedef std::priority_queue<ll> pq;
typedef std::priority_queue<ll, vl, greater<ll>> pql; // priority queue taking from the lower value.
typedef std::vector<edge> gr;
const ll MOD = 1e9 + 7;
const ll INF = MOD * MOD;
const long double EPS = 1e-9;
const long double PI = 3.14159265358979323846;

#define rep(i, n) for(ll (i) = 0; (i) < (n) ; (i)++ )
#define revrep(i, n) for(ll (i) = n - 1; (i) >= 0 ; (i)--)
#define For(i, a, b) for(ll (i) = (a); (i) < (b) ; (i)++ )
#define revFor(i, b, a) for(ll (i) = (b) - 1; (i) >= (a) ; (i)--)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define isUpper(c) ('a' - c > 0)
#define toLower(c) char((c)+0x20)
#define toUpper(c) char((c)-0x20)
#define pb push_back
#define mp make_pair
#define pr(a) cout << (a)
#define prl(a) cout << (a) << endl
#define prl2(a,b) cout << (a) << " " << (b) << endl
#define prl3(a,b,c) cout << (a) << " " << (b) << " " << (c) << endl
#define prl4(a,b,c,d) cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl
#define prs(a) cout << (a) << " "
#define yn(condition) if ((condition)) prl("Yes"); else prl("No");
#define YN(condition) if ((condition)) prl("YES"); else prl("NO");
#define rpr(i,n,ans) rep((i),(n)) {if ((i) < n - 1) prs((ans[(i)])); else if ((i) == n - 1) prl((ans[(i)]));}
#define cpr(i,n,ans) rep((i),(n)) prl((ans[(i)]))
#define in1(a) cin >> (a)
#define in2(a,b) cin >> (a) >> (b)
#define in3(a,b,c) cin >> (a) >> (b) >> (c)
#define in4(a,b,c,d) cin >> (a) >> (b) >> (c) >> (d)
#define rin1(i,n,a) rep((i),(n)) cin >> (a[(i)])
#define rin2(i,n,a,b) rep((i),(n)) cin >> (a[(i)]) >> (b[(i)])
#define rin3(i,n,a,b,c) rep((i),(n)) cin >> (a[(i)]) >> (b[(i)]) >> (c[(i)])
#define rin1_m1(i,n,a) rep((i),(n)) {cin >> (a[(i)]); a[(i)]--;}
#define rin2_m1(i,n,a,b) rep((i),(n)) {cin >> (a[(i)]) >> (b[(i)]); a[(i)]--;b[(i)]--;}
#define rin3_m1(i,n,a,b,c) rep((i),(n)) {cin >> (a[(i)]) >> (b[(i)]) >> (c[(i)]) a[(i)]--;b[(i)]--;c[(i)]--;}
#define matin(i,h,j,w,a) rep((i),(h)) rep((j),(w)) cin >> a[i][j]
#define e1 first
#define e2 second
#define ctol(c) ll((c)) - ll('0')
#define ltos(n) to_string((n))
#define items(kv, v) for(auto &(kv): (v))
#define ndig(N, n) ctol(ll(ltos((N))[ll(ltos((N)).length()) - (n)]))
#define beto(a,b,c) (a) < (b) && (b) < (c)
#define betc(a,b,c) (a) <= (b) && (b) <= (c)
#define rsort(a,n) sort(a,a+n,greater<>())
#define Forchar(c,a,z) for(char (c) = (a); (c) <= (z); (c)++)
#define cntchar(s,c) count(all((s)), c)
#define substring(s, start, end) s.substr((start), (end) - (start) + 1)
#define prl_nd(num, digits) cout << fixed << setprecision(digits) << (num) << endl;
#define XOR(a, b) (a)^(b)
#define prl_time(s) prl3("Elapsed Time:", 1000.0 * (clock() - s) / CLOCKS_PER_SEC, "[ms]");
#define char_to_str(c) string(1, (c))

class UnionFind {
    vl parents, set_size;
public:
    UnionFind(){}
    UnionFind(ll n){
        parents = set_size = vl(n);
        rep(i, n) {parents[i] = i; set_size[i] = 1LL;}
    }

    ll root_find(ll x){
        if (parents[x] == x) return x;
        return parents[x] = root_find(parents[x]);
    }

    void unite(ll x, ll y){
        x = root_find(x); y = root_find(y);
        if (x == y) return;
        if (set_size[x] < set_size[y]){
            parents[y] = x; set_size[x] += set_size[y];
        }else{
            parents[x] = y; set_size[y] += set_size[x];
        }
    }

    bool is_same(ll x, ll y) {// connected or not
        return root_find(x) == root_find(y);
    }

    ll size(ll x) {
        return set_size[root_find(x)];
    }
};

struct BIT{
    ll n; vl dat;
    BIT(ll n): dat(n + 1), n(n){};

    //       x: 1001 1010  1100 1011 1101  1111
    // x & - x: 0001 0010  0100 0001 0001  0001
    //      ->: 1010 1100 10000 1100 1100 10000

    ll sum(ll i){
        ll s = 0;
        while(i > 0){s += dat[i]; i -= i & -i;}
        return s;
    }

    ll sum(ll i, ll j){
        return sum(j - 1) - sum(i - 1);
    }

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

ll gcd(ll m, ll n){
    ll a = max(m, n);
    ll b = min(m, n);
    while(b != 1 && b != 0){ a %= b; swap(a, b);}
    return b == 1 ? 1 : a;
}

ll lcm(ll m, ll n){ return m / gcd(m, n) * n;}

ll power_mod(ll a, ll power, ll mod){
    ll value = 1;
    while(power != 0){
        if(power & 1) value = (value * a) % mod;
        a = (a * a) % mod;
        power = power >> 1;
    }
    return value % mod;
}

ll modinv(ll a, ll mod){
    return power_mod(a, mod - 2, mod);
}

ll power_normal(ll a, ll power){
    ll value = 1;
    while(power != 0){
        if(power & 1) value = value * a;
        a = a * a;
        power = power >> 1;
    }
    return value;
}

ll combination(ll n, ll r){
    if (n == r && n == 0) return 1;
    else if (n <= 0 || r < 0 || r > n) return 0;
    ll numerator = 1;
    ll denomenator = 1;
    for(ll i = 0; i < r ; i++){
        (numerator *= ll(n - i)) %= MOD;
        (denomenator *= modinv(i + 1, MOD)) %= MOD;
    }
    return (numerator * denomenator) % MOD;
}

ll bin_search(ll key, ll A[], ll left, ll right){
    // return the index idx where A[idx] = key.
    // A[left] is start and A[right] is end..
    // In other words, A[right], not A[right - 1], must be defined.
    while(right >= left){
        ll mid = left + (right - left) / 2;
        if (A[mid] == key) return mid;
        else if (A[mid] > key) right = mid - 1;
        else if (A[mid] < key) left = mid + 1;
    }
    return -1;
}

ll lower_bound_bin_search_temp(ll key, ll A[], ll left, ll right){
    // Pay attention to the return value -1.
    // For example, sometimes you may want the value right instead of -1.
    if (A[left] >= key) return left;
    ll mid = left + (right - left) / 2;

    while(right >= left){
        mid = left + (right - left) / 2;
        if (mid == left) {
            if (A[left] < key && key <= A[left + 1]) return left + 1;
            else return -1;
        }
        if (A[mid - 1] < key && key <= A[mid]) return mid;
        else if (A[mid - 1] >= key) right = mid - 1;
        else if (A[mid] < key) left = mid;
    }

    return -1; // all the elements < key
}

ll inf_bin_search_temp(ll key, ll A[], ll left, ll right){
    // Pay attention to the return value -1.
    // For example, sometimes you may want the value right instead of -1.
    if (A[left] > key) return left;
    ll mid = left + (right - left) / 2;

    while(right >= left){
        mid = left + (right - left) / 2;
        if (mid == left) {
            if (A[left] <= key && key < A[left + 1]) return left + 1;
            else return -1;
        }
        if (A[mid - 1] <= key && key < A[mid]) return mid;
        else if (A[mid - 1] > key) right = mid - 1;
        else if (A[mid] <= key) left = mid;
    }

    return -1; // all the elements <= key
}

ll upper_bound_bin_search_temp(ll key, ll A[], ll left, ll right){
    // Pay attention to the return value -1.
    // For example, sometimes you may want the value left instead of -1.
    if (A[right] <= key) return right;
    ll mid = left + (right - left) / 2;

    while(right >= left){
        mid = left + (right - left) / 2;
        if (mid == right) {
            if (A[right - 1] <= key && key < A[right]) return right - 1;
            else return -1;
        }
        if (A[mid] <= key && key < A[mid + 1]) return mid;
        else if (A[mid] > key) right = mid;
        else if (A[mid + 1] <= key) left = mid + 1;
    }

    return -1; // all the elements > key
}

ll sup_bin_search_temp(ll key, ll A[], ll left, ll right){
    // Pay attention to the return value -1.
    // For example, sometimes you may want the value left instead of -1.
    if (A[right] < key) return right;
    ll mid = left + (right - left) / 2;

    while(right >= left){
        mid = left + (right - left) / 2;
        if (mid == right) {
            if (A[right - 1] < key && key <= A[right]) return right - 1;
            else return -1;
        }
        if (A[mid] < key && key <= A[mid + 1]) return mid;
        else if (A[mid] >= key) right = mid;
        else if (A[mid + 1] < key) left = mid + 1;
    }

    return -1; // all the elements >= key
}

ll bin_search_vector(ll key, vl v, ll left, ll right){
    // return the index idx where v[idx] = key.
    // v[left] is start and v[right] is end..
    // In other words, v[right], not v[right - 1], must be defined.
    while(right >= left){
        ll mid = left + (right - left) / 2;
        if (v[mid] == key) return mid;
        else if (v[mid] > key) right = mid - 1;
        else if (v[mid] < key) left = mid + 1;
    }
    return -1;
}

ll lower_bound_bin_search_vector(ll key, vl v){
    // the return value N satisfies
    // v[N - 1] < key <= v[N]
    // N == -1 if all the elements < key
    return lower_bound(all(v), key) - v.begin();
}

ll inf_bin_search_vector(ll key, vl v){
    // the return value N satisfies
    // v[N - 1] <= key < v[N] <= key + 1
    // N == -1 if all the elements <= key
    return lower_bound(all(v), key + 1) - v.begin();
}

ll upper_bound_bin_search_vector(ll key, vl v){
    // the return value N satisfies
    // v[N] <= key < v[N + 1]
    // N == -1 if all the elements > key
    return upper_bound(all(v), key) - v.begin() - 1;
}

ll sup_bin_search_vector(ll key, vl v){
    // the return value N satisfies
    // v[N] <= key - 1 < key <= v[N + 1]
    // N == -1 if all the elements >= key
    return upper_bound(all(v), key - 1) - v.begin() - 1;
}

ll fact(ll n){
    if (n == 0) return 1;
    return n * fact(n - 1);
}

bool is_prime(ll n){
    if (n <= 1) return false;
    for(ll i = 2; i * i <= n; i++){
        if (n % i == 0) return false;
    }
    return true;
}

ll bool_sum(ll a1, ll a2){
    if (a1 == 1 || a2 == 1) return 1;
    return 0;
}

mll prime_decomposition(ll n){
    ll i = 2;
    mll table;
    while(i * i <= n){
        while(n % i == 0){
            table[i] ++;
            n /= i;
        }
        i++;
    }
    if (n > 1) table[n] = 1;
    return table;
}

ll char_to_idx(char c){
    ll idx = 0;
    Forchar(cc, 'a', 'z'){
        if (c == cc) return idx;
        else idx ++;
    }
}

/*
const ll N_VERTEX = 310;
ll a, b, t;
ll dist[N_VERTEX][N_VERTEX];

void warshall_floyd(ll n){
    rep(k, n) rep(i, n) rep(j, n) dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}

int main(void){
    in2(n, m);
    rep(i, n) rep(j, n) {if (i != j) dist[i][j] = INF; else dist[i][i] = 0;}
    rep(i, m){
        in3(a, b, t);
        a--; b--;
        dist[a][b] = t;
        dist[b][a] = t;
    }

    warshall_floyd(n);
}

const ll N_VERTEX = 310;
gr G[N_VERTEX];
ll dist[N_VERTEX], a, b, t, n, m;

void dijkstra(ll start, ll n) {
    priority_queue<pl, vector<pl>, greater<pl>> edge_costs;
    fill(dist, dist + n, INF);
    dist[start] = 0;
    edge_costs.push(pl(0, start));

    while (!edge_costs.empty()) {
        pl edge_cost = edge_costs.top();
        edge_costs.pop();
        ll idx = edge_cost.second;
        ll cost = edge_cost.first;
        if (dist[idx] < cost) continue;

        rep(i, G[idx].size()){
            edge e = G[idx][i];
            if (dist[e.to] > dist[idx] + e.cost){
                dist[e.to] = dist[idx] + e.cost;
                edge_costs.push(pl(dist[e.to], e.to));
            }
        }
    }
}

int main(void){
    in2(n, m);
    rep(i, m){
        in3(a, b, t);
        a--; b--;
        G[a].pb({b, t});
        G[b].pb({a, t});
    }

    dijkstra(0, n);
}

*/

/*
    # 1. The usage of map pair
    map<pl, ll> cnt;
    cnt[{i, j}] = 0;
    items(kv, cnt){
        prl2(kv.first, kv.second);
    }

    # 2. The usage of pair sort

    vpl c;
    rep(i, n){
        ll a, b;
        in2(a, b);
        c.pb(mp(a, b));
    }
    sort(all(c));

    # 3. The usage of next_permutation and combination (factorial search)
    ll a[8];
    rep(i, 8) a[i] = i;
    do{
    }while(next_permutation(a, a+n));

    // here, combination
    ll n, r;
    ll i1, i2, ..., ir;
    For(i1, n - r, n) For(i2, n - r - 1, i1) ... For(ir, n - 2 * r + 1, irr){ process;}

    # 4. sort vector in descending order
    sort(rall(v));

    # 5. bit search
    ll n;
    in1(n);
    const ll base = 3;
    ll upper = 1;
    rep(i, n) upper *= base;
    rep(i, upper){
        ll tmp = i;
        rep(j, n){
            rep(k, base) if (tmp % base == k) prl(k);
            tmp /= base;
        }
    }

    # 6. imos method
    // used when we would like to count the number which
    // shows how many times the numbers between l and r belongs to smt.
    // This method is composed of three process.
    ll n, m, s[MAX_M], l, r;
    in2(n, m);
    rep(i, m) s[i] = 0;

    // 1st step
    rep(i, n){
        in3(l, r, c);
        l--; r--; // if l starts from 1.
        s[l] += c; s[r + 1] -= c;
    }

    // 2nd step
    rep(i, m - 1) s[i + 1] += s[i];
    // 3rd step: judgement...

    #7. shakutori method
    // 1. strech right side while the condition is met.
    // 2. renew the answer
    // 3. increments left side
    // 4. Back to 1. (l <= r must be satisfied all the time.)

    ll l = 0; ll r = 0;
    while (l < n){
        r = max(r, l);
        if (l == r) r++;
        while(r < n && cond) r++;
        answer += r - l; l++;
    }
    prl(answer);

    #8. priority queue

    pq q;
    ll answer = 0;
    ll v;

    rep(i, n) q.push(a[i]);
    rep(i, m){
        v = q.top(); q.pop(); // get the top value and dump the value from queue
        v /= 2; q.push(v); // add the new value
    }

    while(!q.empty()){
        answer += q.top();
        q.pop();
    }

    #9. The shortest path between the k-th edge and another edge (Tree)
    ll depth[MAX_N];
    gr tree[MAX_N];

    void dfs(ll now, ll prev, ll all_cost){
        depth[now] = all_cost;
        items(e, tree[now]){
            if (e.to == prev) continue;
            dfs(e.to, now, all_cost + e.cost);
        }
    }

    ll n, k, a, b, c;

    in2(n, k);
    rep(i, n - 1){
        in3(a, b, c);
        a--; b--;
        tree[a].pb({b, c});
        tree[b].pb({a, c});
    }

    k--;
    dfs(k, -1, 0);

    #10. Visiting Subtree using recurrent function (ABC138D)

    gr tree[MAX_N];
    ll c[MAX_N];
    bool visited[MAX_N];

    void dfs(ll source, ll parent, ll val){
        visited[source] = true;
        c[source] += val;
        rep(i, tree[source].size()){rep(i, m){
        ll res = n % match[i].e1;
    }
            ll vertex = tree[source][i].to;
            if (vertex == parent) continue;
            dfs(vertex, source, c[source]);
        }
    }

    #11. bfs ABC146D
    1. first create a tree.
    2. start searching from a node.
    3. do some processes and push nodes connected with a given target node in BFS.
    4. repeat a series of procedure until queue is empty.

    #12. grid to distance matrix (dx, dy)
    ll w, h;
    ll pos_to_idx(ll x, ll y){
        return y * w + x;
    }

    int main(void){
        in2(h, w);
        rep(y, h){
            in1(s);
            rep(x, w){
                if (s[x] == '#') wall[x][y] = true;
                else wall[x][y] = false;
            }
        }

        rep(i1, h * w)rep(i2, h * w) if (i1 == i2) dist[i1][i2] = 0; else dist[i1][i2] = INF;

        rep(x, w)rep(y, h){
            ll idx1 = pos_to_idx(x, y); ll idx2;
            if (wall[x][y]) continue;
            if (x != 0 && !wall[x - 1][y]){
                idx2 = pos_to_idx(x - 1, y);
                // if warshall floyd
                dist[idx1][idx2] = 1;
                // if dijkstra
                // G[idx1].pb({idx2, 1});
            }
            if (x != w - 1 && !wall[x + 1][y]){
                idx2 = pos_to_idx(x + 1, y);
                dist[idx1][idx2] = 1;
                // G[idx1].pb({idx2, 1});
            }
            if (y != 0 && !wall[x][y - 1]){
                idx2 = pos_to_idx(x, y - 1);
                dist[idx1][idx2] = 1;
                // G[idx1].pb({idx2, 1});
            }
            if (y != h - 1 && !wall[x][y + 1]){
                idx2 = pos_to_idx(x, y + 1);
                dist[idx1][idx2] = 1;
                // G[idx1].pb({idx2, 1});
            }
        }
    }

*/

/*
the operators regarding bit

     x1: 0000 0001 0010 0101 0110 0111
     x2: xxxx 0001 0011 0100 0101 1000
x1 & x2: 0000 0001 0010 0100 0100 0000

      x: 1001 1010  1100 1011 1101  1111
x & - x: 0001 0010  0100 0001 0001  0001
    sum: 1010 1100 10000 1100 1100 10000

x << y is x * 2 ** y
x >> y is rep(i, y) x = x // 2
*/

const ll MAX_N = 200005;
bool okay = false;
ll answer = power_normal(10, 18) * 4 + 1;
ll n, k;
vpl xy;

void solve(){
    rep(i, n){
        ll a, b; in2(a, b);
        xy.pb(mp(a, b));
    }

    sort(all(xy));

    rep(i1, n - k + 1) For(i2, i1 + k - 1, n){
        ll ym = INF; ll yM = -INF;
        For(i3, i1, i2 + 1){
            ym = min(ym, xy[i3].e2);
            yM = max(yM, xy[i3].e2);
        }
        ll sp = (yM - ym) * (xy[i2].e1 - xy[i1].e1);
        answer = min(sp, answer);
    }

    prl(answer);
}

int main(void){
    in2(n, k);
    solve();

    return 0;
}

Score : 400 points

Problem Statement

We have N points in a two-dimensional plane.

The coordinates of the i-th point (1 \leq i \leq N) are (x_i,y_i).

Let us consider a rectangle whose sides are parallel to the coordinate axes that contains K or more of the N points in its interior.

Here, points on the sides of the rectangle are considered to be in the interior.

Find the minimum possible area of such a rectangle.

Constraints

2 \leq K \leq N \leq 50

-10^9 \leq x_i,y_i \leq 10^9 (1 \leq i \leq N)

x_i≠x_j (1 \leq i<j \leq N)

y_i≠y_j (1 \leq i<j \leq N)

All input values are integers. (Added at 21:50 JST)

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1
:
x_{N} y_{N}

Output

Print the minimum possible area of a rectangle that satisfies the condition.

Sample Input 1

4 4
1 4
3 3
6 2
8 1

Sample Output 1

21

One rectangle that satisfies the condition with the minimum possible area has the following vertices: (1,1), (8,1), (1,4) and (8,4).

Its area is (8-1) × (4-1) = 21.

Sample Input 2

4 2
0 0
1 1
2 2
3 3

Sample Output 2

1

Sample Input 3

4 3
-1000000000 -1000000000
1000000000 1000000000
-999999999 999999999
999999999 -999999999

Sample Output 3

3999999996000000001

Watch out for integer overflows.


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

int main()
{
	string s;
	cin >> s;
	cout << s.substr(0, s.length()-8) << endl;;
	return 0;
}

Score : 100 points

Problem Statement

Rng is going to a festival.

The name of the festival is given to you as a string S, which ends with FESTIVAL, from input. Answer the question: "Rng is going to a festival of what?" Output the answer.

Here, assume that the name of "a festival of s" is a string obtained by appending FESTIVAL to the end of s.
For example, CODEFESTIVAL is a festival of CODE.

Constraints

9 \leq |S| \leq 50

S consists of uppercase English letters.

S ends with FESTIVAL.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer to the question: "Rng is going to a festival of what?"

Sample Input 1

CODEFESTIVAL

Sample Output 1

CODE

This is the same as the example in the statement.

Sample Input 2

CODEFESTIVALFESTIVAL

Sample Output 2

CODEFESTIVAL

This string is obtained by appending FESTIVAL to the end of CODEFESTIVAL, so it is a festival of CODEFESTIVAL.

Sample Input 3

YAKINIKUFESTIVAL

Sample Output 3

YAKINIKU


#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000

vector<int> G[100010];

int color[100010];

// 頂点を1と-1で塗っていく
bool dfs(int v, int c) {
    color[v] = c;
    for (int i = 0; i < G[v].size(); i++) {
        // 隣接している頂点が同じ色ならfalse
        if (color[G[v][i]] == c) return false;
        // 隣接している頂点がまだ塗られていないなら-cで塗る
        if (color[G[v][i]] == 0 && !dfs(G[v][i], -c)) return false;
    }

    // すべての頂点を塗れたらtrue
    return true;

}
int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M;
    cin >> N >> M;
    rep(i, M) {
        int s, t;
        cin >> s >> t;
        s--; t--;
        G[s].push_back(t);
        G[t].push_back(s);
    }
    if(dfs(0, 1)) {
        int k1 = 0, k2 = 0;
        rep(i, 100010) {
            if (color[i] == 1) k1++;
            else if (color[i] == -1) k2++;
        }
        cout << k1 * k2 - M << endl;
    } else {
        cout << N * (N - 1) / 2 - M << endl;
    }

    return 0;
}

Score : 500 points

Problem Statement

Rng has a connected undirected graph with N vertices.
Currently, there are M edges in the graph, and the i-th edge connects Vertices A_i and B_i.

Rng will add new edges to the graph by repeating the following operation:

Operation: Choose u and v (u \neq v) such that Vertex v can be reached by traversing exactly three edges from Vertex u, and add an edge connecting Vertices u and v. It is not allowed to add an edge if there is already an edge connecting Vertices u and v.

Find the maximum possible number of edges that can be added.

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i,B_i \leq N

The graph has no self-loops or multiple edges.

The graph is connected.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
:
A_M B_M

Output

Find the maximum possible number of edges that can be added.

Sample Input 1

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

Sample Output 1

4

If we add edges as shown below, four edges can be added, and no more.

Sample Input 2

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

Sample Output 2

5

Five edges can be added, for example, as follows:

Add an edge connecting Vertex 5 and Vertex 3.

Add an edge connecting Vertex 5 and Vertex 2.

Add an edge connecting Vertex 4 and Vertex 1.

Add an edge connecting Vertex 4 and Vertex 2.

Add an edge connecting Vertex 4 and Vertex 3.


#include <bits/stdc++.h>
#define For(i, l, r) for(int i = (l), _end_ = (int)(r); i <= _end_; ++i)
#define Fordown(i, r, l) for(int i = (r), _end_ = (int)(l); i >= _end_; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;

bool chkmin(int &a, int b) {return b < a ? a = b, 1 : 0;}
bool chkmax(int &a, int b) {return b > a ? a = b, 1 : 0;}

inline int read() {
 int x = 0, fh = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar() ) if (ch == '-') fh = -1;
    for (; isdigit(ch); ch = getchar() ) x = (x<<1) + (x<<3) + (ch ^ '0');
    return x * fh;
}

void file() {
#ifdef zjp_shadow
	freopen ("c.in", "r", stdin);
	freopen ("c.out", "w", stdout);
#endif
}

const int N = 100100, M = N << 1;
vector<int> G[N];
set<int> S[N];
int n, m;
void input() {
	n = read();
	m = read();
	For (i, 1, m) {
		int u = read(), v = read();
		G[u].push_back(v);
		S[u].insert(v);
		G[v].push_back(u);
		S[v].insert(u);
	}
}

bool vis[N];
set<int> dep[N][3];
typedef set<int>::iterator it;
int ans = 0, pre[N];
#define all(x) x.begin(), x.end()
void dfs (int u) {
	vis[u] = true;
	for (it i = dep[u][2].begin(); i != dep[u][2].end(); ++i) {
		int from = (*i);
		if (u != from && from && S[u].find(from) == S[u].end()) {
			++ ans;
			G[u].push_back(from);
			G[from].push_back(u);
			S[u].insert(from);
			S[from].insert(u);
		}
	}

	for (int i = 0; i < (G[u].size() ) ; ++i) {
		int v = G[u][i];
		if (vis[v]) continue ;
		pre[v] = u;
		dep[u][0].insert(v);
		dep[v][0].insert(u);
		dep[u][2].insert(all(dep[v][1]) );
		dep[v][2].insert(all(dep[u][1]) );
		dep[u][1].insert(all(dep[v][0]) );
		dep[v][1].insert(all(dep[u][0]) );
		dfs (v);
	}
	vis[u] = false;
}

void solve() {
	dfs (1);

}

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

int main () {
	file();
	input();
	solve();
	output();
    return 0;
}


#include<set>
#include<cstdio>
#include<cctype>
#include<string>
inline int Read(){
	char a;
	int b;
	while(!isdigit(a=getchar()));
	b=a^'0';
	while(isdigit(a=getchar()))b=(((b<<2)+b)<<1)+(a^'0');
	return b;
}
int main()
{
	std::multiset<std::string> set;
	for(int i=Read();i;i--)set.insert("a");
	for(int i=Read();i;i--)set.insert("b");
	for(int i=Read();i;i--)set.insert("c");
	while(set.size()>1){
		std::string s=*set.begin(),t=*set.rbegin();
		set.erase(set.lower_bound(s));
		set.erase(set.lower_bound(t));
		set.insert(s+t);
	}
	printf("%s\n",(*set.begin()).c_str());
	return 0;
}

Score : 1600 points

Problem Statement

For a string S, let f(S) be the lexicographically smallest cyclic shift of S.
For example, if S = babca, f(S) = ababc because this is the smallest among all cyclic shifts (babca, abcab, bcaba, cabab, ababc).

You are given three integers X, Y, and Z.
You want to construct a string T that consists of exactly X as, exactly Y bs, and exactly Z cs.
If there are multiple such strings, you want to choose one that maximizes f(T) lexicographically.

Compute the lexicographically largest possible value of f(T).

Constraints

1 \leq X + Y + Z \leq 50

X, Y, Z are non-negative integers.

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the answer.

Sample Input 1

2 2 0

Sample Output 1

abab

T must consist of two as and two bs.

If T = aabb, f(T) = aabb.

If T = abab, f(T) = abab.

If T = abba, f(T) = aabb.

If T = baab, f(T) = aabb.

If T = baba, f(T) = abab.

If T = bbaa, f(T) = aabb.

Thus, the largest possible f(T) is abab.

Sample Input 2

1 1 1

Sample Output 2

acb


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

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

  for (int h = 1; h <= 3500; h++) {
    for (int n = 1; n <= 3500; n++){
      if ((4*h*n-N*n-N*h) == 0) continue;
      if ((N*h*n) % (4*h*n-N*n-N*h) == 0){
        if ((N*h*n) / (4*h*n-N*n-N*h) >= 1){
          cout << h << endl;
          cout << n << endl;
          cout << (N*h*n) / (4*h*n-N*n-N*h) << endl;
          return 0;
        }
      }
    }
  }

  return 0;

}

Score : 300 points

Problem Statement

You are given an integer N.

Find a triple of positive integers h, n and w such that 4/N = 1/h + 1/n + 1/w.

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

Constraints

It is guaranteed that, for the given integer N, there exists a solution such that h,n,w \leq 3500.

Inputs

Input is given from Standard Input in the following format:

N

Outputs

Print a triple of positive integers h, n and w that satisfies the condition, in the following format:

h n w

Sample Input 1

2

Sample Output 1

1 2 2

4/2 = 1/1 + 1/2 + 1/2.

Sample Input 2

3485

Sample Output 2

872 1012974 1539173474040

It is allowed to use an integer exceeding 3500 in a solution.

Sample Input 3

4664

Sample Output 3

3498 3498 3498


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

long double calc (int N,int h,int n){
	long double temp = n * h * N;
	long double timp = 4*h*n - n*N - h*N;
	return temp / timp;
}

int main(){
	int N;
	cin >> N;
	int h,n,w;
	rep(i,1,3500){
		rep(j,1,3500){

			long double temp = calc(N,i,j);
			if(((temp - int(temp)) == 0) && (temp > 0)){
				h = i;
				n = j;
				w = temp;
				cout << h << " " << n << " " << w << endl;
				return 0;
			}
		}
	}
}


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

struct line { double p, d; ll n; };

ll N, A[100000], B[100000], C[100000];

template<typename T> class BinaryIndexedTree {
  vector<T> vec;
  const ll n;

public:
  BinaryIndexedTree(T _n) : vec(_n + 1), n(_n) {}

  T query(ll x) {
    T ret = 0;
    for(ll i = x; i > 0; i -= i & (-i)) ret += vec[i];
    return ret;
  }

  void update(ll x, T k) {
    for(ll i = x; i <= n; i += i & (-i)) vec[i] += k;
  }
};

bool ok(double x, ll k) {
  line l[100000];
  REP(i, 0, N) l[i] = (line) { (double) (C[i] - A[i] * x) / B[i], (double) -A[i] / B[i], 0 };

  sort(l, l + N, [](const line &a, const line &b) { return a.d < b.d; });
  REP(i, 0, N) l[i].n = i + 1;
  sort(l, l + N, [](const line &a, const line &b) { return a.p < b.p; });

  BinaryIndexedTree<ll> bit(100000);
  ll cnt = 0;
  REP(i, 0, N) {
    cnt += i - bit.query(l[i].n);
    bit.update(l[i].n, 1);
  }

  return N * (N - 1) / 2 - cnt < k;
}

int main(void) {
  cin >> N;
  REP(i, 0, N) cin >> A[i] >> B[i] >> C[i];

  ll n = N * (N - 1) / 2;
  ll k = n / 2 + (n % 2 ? 1 : 0);

  double lx = -1e5, hx = 1e5;
  REP(i, 0, 200) {
    double mx = (lx + hx) / 2;
    if(ok(mx, k)) lx = mx;
    else hx = mx;
  }

  REP(i, 0, N) swap(A[i], B[i]);

  double ly = -1e5, hy = 1e5;
  REP(i, 0, 200) {
    double my = (ly + hy) / 2;
    if(ok(my, k)) ly = my;
    else hy = my;
  }

  printf("%.15lf %.15lf\n", hx, hy);
}

Score : 800 points

Problem Statement

There are N lines in the xy-plane. The i-th line is represented by A_ix+B_iy=C_i.
Any two lines among the N+2 lines, the above N lines plus the x-axis and y-axis, cross each other at exactly one point.

For each pair 1 \leq i < j \leq N, there is a car at the cross point of the i-th and j-th lines.
Even where three or more lines intersect at a point, a car is individually placed for each pair of lines.
That is, there will be k(k-1)/2 cars placed at the intersection of k lines.

Those cars are already very old, and can only be moved parallel to the x-axis or y-axis.

Takahashi will hold an exhibition of antique cars at a place on the xy-plane.
In order to avoid damaging the half-broken cars too much, he will select the place of the exhibition so that the total distance covered will be minimized when all the cars are moved to the place.
If such a place is not uniquely determined, among the places that satisfy the condition above, the place with the minimum x-coordinate will be selected.
If the place is still not uniquely determined, among the places that satisfy the two conditions above, the place with the minimum y-coordinate will be selected.

Find the place of the exhibition that will be selected.

Constraints

2 \leq N \leq 4 × 10^4

1 \leq |A_i|,|B_i| \leq 10^4(1 \leq i \leq N)

0 \leq |C_i| \leq 10^4(1 \leq i \leq N)

No two given lines are parallel.

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
A_1 B_1 C_1
:
A_N B_N C_N

Outputs

Print the x-coordinate and y-coordinate of the place of the exhibition that will be selected, in this order, with a space in between. The output will be judged as correct when the absolute or relative error is at most 10^{-9}.

Sample Input 1

3
1 1 1
2 -1 2
-1 2 2

Sample Output 1

1.000000000000000 1.000000000000000

There is a car at each place shown by a blue circle in the figure. The place to be selected is shown by a purple circle.

Sample Input 2

4
1 1 2
1 -1 0
3 -1 -2
1 -3 4

Sample Output 2

-1.000000000000000 -1.000000000000000

Sample Input 3

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

Sample Output 3

-1.722222222222222 1.325000000000000


#include<bits/stdc++.h>
using namespace std;
int main(void){
  int a=0,b;
  char c;
  string s;
 cin >> s;
  for(int i = 0;i < s.size();i++){
    if(s.at(i) == '1'){
      a++;
    }
  }
  cout << a;
}

Score : 100 points

Problem Statement

Snuke prepared 6 problems for a upcoming programming contest.
For each of those problems, Rng judged whether it can be used in the contest or not.

You are given a string S of length 6.
If the i-th character of s is 1, it means that the i-th problem prepared by Snuke is accepted to be used; 0 means that the problem is not accepted.

How many problems prepared by Snuke are accepted to be used in the contest?

Constraints

The length of S is 6.

S consists of 0 and 1.

Inputs

Input is given from Standard Input in the following format:

S

Outputs

Print the number of problems prepared by Snuke that are accepted to be used in the contest.

Sample Input 1

111100

Sample Output 1

4

The first, second, third and fourth problems are accepted, for a total of four.

Sample Input 2

001001

Sample Output 2

2

Sample Input 3

000000

Sample Output 3

0


#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main(void){
    string S;

    for(int i=0;i<6;i++){
        S[i] = '0';
    }

    cin >> S;

    int a = 0;
    for(int i=0;i<6;i++){
        if(S[i] == '1'){
            a = a + 1;
        }
    }
    cout << a << endl;
    return 0;

}


#include <iostream>
using namespace std;

int main()
{
  int n, m, k; cin >> n >> m >> k;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (i*(m-j)+j*(n-i) == k) {
        cout << "Yes" << endl;
        return 0;
      }
    }
  }

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

Score : 200 points

Problem Statement

We have a grid with N rows and M columns of squares. Initially, all the squares are white.

There is a button attached to each row and each column.
When a button attached to a row is pressed, the colors of all the squares in that row are inverted; that is, white squares become black and vice versa.
When a button attached to a column is pressed, the colors of all the squares in that column are inverted.

Takahashi can freely press the buttons any number of times. Determine whether he can have exactly K black squares in the grid.

Constraints

1 \leq N,M \leq 1000

0 \leq K \leq NM

Input

Input is given from Standard Input in the following format:

N M K

Output

If Takahashi can have exactly K black squares in the grid, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

Press the buttons in the order of the first row, the first column.

Sample Input 2

2 2 1

Sample Output 2

No

Sample Input 3

3 5 8

Sample Output 3

Yes

Press the buttons in the order of the first column, third column, second row, fifth column.

Sample Input 4

7 9 20

Sample Output 4

No


#include <bits/stdc++.h>
using namespace std;
int main() {
	int h, w, flag = 0;
	cin >> h >> w;
	vector<int> arr(26, 0);
	char c;
	for(int i = 0; i < h * w; i++) {
		cin >> c;
		arr[c - 'a']++;
	}

	if((h % 2 == 0)&&(w % 2 == 0)) {
		for(int i = 0; i < 26; i++) {
			if(arr[i] % 4 != 0) {
				flag = 1;
				break;
			}
		}
	} else if((h % 2 == 0)||(w % 2 == 0)){
		int odd, even, count;
		if(h % 2) {
			odd = h;
			even = w;
		} else {
			odd = w;
			even = h;
		}
		count = even / 2;
		for(int i = 0; i < 26; i++) {
			if(arr[i] % 4 != 0) {
				if((arr[i] % 2 == 0)&&(count)) {
					count--;
				} else {
					flag = 1;
					break;
				}
			}
		}
	} else if(h == w) {
		int one = 1, count = h - 1;
		for(int i = 0; i < 26; i++) {
			if(arr[i] % 4 != 0) {
				if(arr[i] % 2 == 1) {
					if(one == 0) {
						flag = 1;
						break;
					} else {
						one = 0;
						if((arr[i] - 1) % 4 != 0) {
							if(count != 0) {
								count--;
							} else {
								flag = 1;
								break;
							}
						}
					}
				} else {
					if(count != 0) {
						count--;
					} else {
						flag = 1;
						break;
					}
				}
			}
		}
	} else {
		int l, s, one = 1, count;
		l = max(h, w);
		s = min(h, w);
		count = l / 2;
		for(int i = 0; i < 26; i++) {
			if(arr[i] % 4 != 0) {
				if(arr[i] % 2 == 1) {
					if(one == 1) {
						if(((arr[i] - 1) % 4 != 0)&&(count != 0)) {
							count--;
						} else if(((arr[i] - 1) % 4 != 0)&&(count == 0)) {
							flag = 1;
							break;
						}
						one = 0;
					} else {
						flag = 1;
						break;
					}
				} else if(arr[i] % 2 == 0) {
					if(count == 0) {
						flag = 1;
						break;
					} else {
						count--;
					}
				}
			}
		}
	}
	if(flag) {
		cout << "No" << endl;
	} else {
		cout << "Yes" << endl;
	}

	return 0;
}

Score : 400 points

Problem Statement

We have an H-by-W matrix.
Let a_{ij} be the element at the i-th row from the top and j-th column from the left.
In this matrix, each a_{ij} is a lowercase English letter.

Snuke is creating another H-by-W matrix, A', by freely rearranging the elements in A.
Here, he wants to satisfy the following condition:

Every row and column in A' can be read as a palindrome.

Determine whether he can create a matrix satisfying the condition.

Note

A palindrome is a string that reads the same forward and backward.
For example, a, aa, abba and abcba are all palindromes, while ab, abab and abcda are not.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

H W
a_{11}a_{12}...a_{1W}
:
a_{H1}a_{H2}...a_{HW}

Output

If Snuke can create a matrix satisfying the condition, print Yes; otherwise, print No.

Sample Input 1

3 4
aabb
aabb
aacc

Sample Output 1

Yes

For example, the following matrix satisfies the condition.

abba
acca
abba

Sample Input 2

2 2
aa
bb

Sample Output 2

No

It is not possible to create a matrix satisfying the condition, no matter how we rearrange the elements in A.

Sample Input 3

5 1
t
w
e
e
t

Sample Output 3

Yes

For example, the following matrix satisfies the condition.

t
e
w
e
t

Sample Input 4

2 5
abxba
abyba

Sample Output 4

No

Sample Input 5

1 1
z

Sample Output 5

Yes


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

int main() {
  int n, a;
  cin >> n >> a;
  cout << n * n - a << endl;
}

Score : 100 points

Problem Statement

We have an N \times N square grid.

We will paint each square in the grid either black or white.

If we paint exactly A squares white, how many squares will be painted black?

Constraints

1 \leq N \leq 100

0 \leq A \leq N^2

Inputs

Input is given from Standard Input in the following format:

N
A

Outputs

Print the number of squares that will be painted black.

Sample Input 1

3
4

Sample Output 1

5

There are nine squares in a 3 \times 3 square grid.
Four of them will be painted white, so the remaining five squares will be painted black.

Sample Input 2

19
100

Sample Output 2

261

Sample Input 3

10
0

Sample Output 3

100

As zero squares will be painted white, all the squares will be painted black.


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

int main() {
  int N, A;
  cin >> N >> A;
  cout << N * N - A << endl;
}


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

int main() {
  int n,k,ans;
  cin>>n>>k;
  ans=0;
  rep(i,n){
  int x;
  cin>>x;
  if((k-x)*2>=x*2){
  ans+=x*2;
  }else{
  ans+=(k-x)*2;
  }
  }
  cout<<ans<<endl;
}

Score : 200 points

Problem Statement

There are N balls in the xy-plane. The coordinates of the i-th of them is (x_i, i).
Thus, we have one ball on each of the N lines y = 1, y = 2, ..., y = N.

In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B.
Then, he placed the i-th type-A robot at coordinates (0, i), and the i-th type-B robot at coordinates (K, i).
Thus, now we have one type-A robot and one type-B robot on each of the N lines y = 1, y = 2, ..., y = N.

When activated, each type of robot will operate as follows.

When a type-A robot is activated at coordinates (0, a), it will move to the position of the ball on the line y = a, collect the ball, move back to its original position (0, a) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

When a type-B robot is activated at coordinates (K, b), it will move to the position of the ball on the line y = b, collect the ball, move back to its original position (K, b) and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

Snuke will activate some of the 2N robots to collect all of the balls. Find the minimum possible total distance covered by robots.

Constraints

1 \leq N \leq 100

1 \leq K \leq 100

0 < x_i < K

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
K
x_1 x_2 ... x_N

Outputs

Print the minimum possible total distance covered by robots.

Sample Input 1

1
10
2

Sample Output 1

4

There are just one ball, one type-A robot and one type-B robot.

If the type-A robot is used to collect the ball, the distance from the robot to the ball is 2, and the distance from the ball to the original position of the robot is also 2, for a total distance of 4.

Similarly, if the type-B robot is used, the total distance covered will be 16.

Thus, the total distance covered will be minimized when the type-A robot is used. The output should be 4.

Sample Input 2

2
9
3 6

Sample Output 2

12

The total distance covered will be minimized when the first ball is collected by the type-A robot, and the second ball by the type-B robot.

Sample Input 3

5
20
11 12 9 17 12

Sample Output 3

74


#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
#include <string>
#include <bitset>
#include <queue>
#include <tuple>
#include <cmath>
#include <map>
#include <cstring>

template<typename T> bool chmax( T &a, const T &b ) { if ( a <= b ) { a = b; return ( true ); } else { return ( false ); } }
template<typename T> bool chmin( T &a, const T &b ) { if ( a >= b ) { a = b; return ( true ); } else { return ( false ); } }

using namespace std;

using ll = long long;
using ull = unsigned long long;

using Pint = pair<int, int>;
using Pll  = pair<ll, ll>;
using Pull = pair<ull, ull>;

#define eb emplace_back
#define pb push_back
#define mp make_pair
#define mt make_tuple

#define F first
#define S second

#define rep( i, n ) for ( int i = 0; i < (int)( n ); ++i )
#define reps( i, n ) for ( int i = 1; i <= (int)( n ); ++i )
#define rrep( i, n ) for ( int i = (int)( ( n ) - 1 ); i >= 0; --i )
#define rreps( i, n ) for ( int i = (int)( ( n ) ); i > 0; --i )
#define arep( i, v ) for ( auto &&i : ( v ) )

template<typename T> T gcd( const T a, const T b ) { return ( b ? gcd( b, a % b ) : a ); }

#define ALL( c ) ( c ).begin(), ( c ).end()
#define RALL( c ) ( c ).rbegin(), ( c ).rend()
#define UNIQUE( c ) ( c ).erase( unique( ( c ).begin(), ( c ).end() ), ( c ).end() )

constexpr ll MOD = 1000000007LL;

#define y0 y3487465
#define y1 y8687969
#define j0 j1347829
#define j1 j234892
#define next asdnext
#define prev asdprev

template<typename T = ll> class UnionFind {
public:
	UnionFind( T n ) { par.resize( n ); siz.resize( n ); rep( i, n ) { par[i] = i; siz[i] = 1; } }
	T find( T x ) { if ( x == par[x] ) return ( x ); else return( par[x] = find( par[x] ) ); }
	void unite( T x, T y ) { T xx = find( x ); T yy = find( y ); if ( xx == yy ) return;
		if ( siz[xx] <= siz[yy] ) swap( xx, yy ); par[yy] = xx; siz[xx] += siz[yy]; }
private:
	vector<T> par, siz;
};

template<typename T = ll> class CompareMax {
public:	T operator()( T a, T b ) { return ( max( a, b ) ); }
};
template<typename T = ll> class CompareMin {
public:	T operator()( T a, T b ) { return ( min( a, b ) ); }
};

template<typename T = ll, typename F = CompareMax<T>, T I = 0> class SegTree {
public:
	SegTree( T n ) { N = n; v.resize( 4 * n ); rep( i, 4 * n ) v[i] = I; }
	void update( T i, T x ) { i += N - 1; v[i] = x; while ( i > 0 ) {
			i = ( i - 1 ) / 2; v[i] = func( v[i * 2 + 1], v[i * 2 + 2] ); } }
	T query( T a, T b, T k, T l, T r ) {
		if ( r <= a || b <= l ) return ( I );
		if ( a <= l && r <= b ) return ( v[k] );
		else { T vl = query( a, b, k * 2 + 1, l, ( l + r ) / 2 );
			T vr = query( a, b, k * 2 + 2, ( l + r ) / 2, r );
			return ( func( vl, vr ) ); }
	}
private:
	T N; F func; vector<T> v;
};

template<typename T = ll> T solveLIS( const vector<T> &v ) {
	vector<T> dp( v.size(), numeric_limits<T>::max() );
	rep( i, v.size() ) {
		*lower_bound( ALL( dp ), v[i] ) = v[i];
	}
	return ( distance( dp.begin(), lower_bound( ALL( dp ), numeric_limits<T>::max() ) ) );
}

template<typename T = ll> T power( T a, T b, T m = MOD ) {
	T res = 1;
	while ( b > 0 ) { if ( b & 1 ) res = res * a % m;
		a = a * a % m; b >>= 1; }
	return ( res );
}

/*
constexpr ll MAX = 500010;
ll fact[MAX];
ll inv[MAX];
ll inv_fact[MAX];

template<typename T> void initComb( T n, T m = MOD )
{
	fact[0] = fact[1] = inv_fact[0] = inv_fact[1] = 1;
	inv[1] = 1;
	for ( int i = 2; i < n; i++ ) {
		fact[i] = ( fact[i - 1] * i ) % m;
		inv[i] = m - inv[m % i] * ( m / i ) % m;
		inv_fact[i] = inv_fact[i - 1] * inv[i] % m;
	}
}

template<typename T> T comb( T n, T r, T m = MOD )
{
	if ( n < r ) return ( 0 );
	if ( n < 0 || r < 0 ) return ( 0 );
	return ( fact[n] * ( inv_fact[r] * inv_fact[n - r] % m ) % m );
}
*/

void replace( string &s, string t, string r ) {
	string::size_type p = 0;
	while ( ( p = s.find( t, p ) ) != string::npos ) {
		s.replace( p, t.length(), r );
		p += r.length();
	}
}

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

	int w = 0, s = 0;
	double cons = 0.0;
	reps( i, a )
	{
		rep( j, b + 1 )
		{
			for ( int k = 0; k <= e * ( i + j ); k += c )
			{
				for ( int l = 0; l <= e * ( i + j ) - k; l += d )
				{
					if ( 100 * ( i + j ) + k + l > f )
					{
						break;
					}
					else
					{
						double t = (double)( k + l ) / ( 100 * ( i + j ) + k + l );
						if ( chmax( cons, t ) )
						{
							w = i + j;
							s = k + l;
						}
					}
				}
			}
		}
	}

	cout << ( w * 100 + s ) << " " << s << endl;

	return ( 0 );
}

Score : 300 points

Problem Statement

Snuke is making sugar water in a beaker.
Initially, the beaker is empty. Snuke can perform the following four types of operations any number of times. He may choose not to perform some types of operations.

Operation 1: Pour 100A grams of water into the beaker.

Operation 2: Pour 100B grams of water into the beaker.

Operation 3: Put C grams of sugar into the beaker.

Operation 4: Put D grams of sugar into the beaker.

In our experimental environment, E grams of sugar can dissolve into 100 grams of water.

Snuke will make sugar water with the highest possible density.

The beaker can contain at most F grams of substances (water and sugar combined), and there must not be any undissolved sugar in the beaker.
Find the mass of the sugar water Snuke will make, and the mass of sugar dissolved in it.
If there is more than one candidate, any of them will be accepted.

We remind you that the sugar water that contains a grams of water and b grams of sugar is \frac{100b}{a + b} percent.
Also, in this problem, pure water that does not contain any sugar is regarded as 0 percent density sugar water.

Constraints

1 \leq A < B \leq 30

1 \leq C < D \leq 30

1 \leq E \leq 100

100A \leq F \leq 3 000

A, B, C, D, E and F are all integers.

Inputs

Input is given from Standard Input in the following format:

A B C D E F

Outputs

Print two integers separated by a space.
The first integer should be the mass of the desired sugar water, and the second should be the mass of the sugar dissolved in it.

Sample Input 1

1 2 10 20 15 200

Sample Output 1

110 10

In this environment, 15 grams of sugar can dissolve into 100 grams of water, and the beaker can contain at most 200 grams of substances.

We can make 110 grams of sugar water by performing Operation 1 once and Operation 3 once.
It is not possible to make sugar water with higher density.
For example, the following sequences of operations are infeasible:

If we perform Operation 1 once and Operation 4 once, there will be undissolved sugar in the beaker.

If we perform Operation 2 once and Operation 3 three times, the mass of substances in the beaker will exceed 200 grams.

Sample Input 2

1 2 1 2 100 1000

Sample Output 2

200 100

There are other acceptable outputs, such as:

400 200

However, the output below is not acceptable:

300 150

This is because, in order to make 300 grams of sugar water containing 150 grams of sugar, we need to pour exactly 150 grams of water into the beaker, which is impossible.

Sample Input 3

17 19 22 26 55 2802

Sample Output 3

2634 934


#include <iostream>
#include <cmath>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <list>
#include <stack>
#include <queue>
using namespace std;
typedef long long ll;

int main(void) {
    set<double> swtr, ssgr;
    int i, j, ans_wtr=0, ans_sgr=0;
    double a, b, c, d, e, f, ans=0;
    cin >> a >> b >> c >> d >> e >> f;

    for(i=0; i<=f/(100*a); i++) {
        for(j=0; 100*a*i+100*b*j<=f; j++)
            swtr.insert(100*a*i+100*b*j);
    }

    for(i=0; i<=f/c; i++) {
        for(j=0; c*i+d*j<=f; j++) {
            ssgr.insert(c*i+d*j);
        }
    }

    vector<double>wtr = vector<double>(swtr.begin(), swtr.end());
    vector<double>sgr = vector<double>(ssgr.begin(), ssgr.end());

    for(i=0; i<wtr.size(); i++) {
        for(j=0; j<sgr.size(); j++) {
            if(i==0 && j==0)
                continue;
            if(wtr[i]+sgr[j]<=f && e*wtr[i]>=100*sgr[j]) {
                if(sgr[j]/(wtr[i]+sgr[j])>=ans) {
                    ans=sgr[j]/(wtr[i]+sgr[j]);
                    ans_wtr=wtr[i];
                    ans_sgr=sgr[j];
                }
            }
        }
    }
    cout << ans_wtr << " " << ans_sgr << endl;
}