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

using ll = long long;
using pii = pair<int, int>;

#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define x first
#define y second

const int MOD = 1e9 + 7;

void setIO() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
}

//ctrl q is comment
//ctrl b is run
//ctrl s is compile
//shift r is search & replace
//shift d is delete a line

bool comp(pii a, pii b){
    return a.x == b.x ? a.y < b.y : a.x < b.x;
}
int main(){
    setIO();
    ll n; cin >> n;
    int p26 = 0;
    ll mult = 26;
    int temp = n;
    while(temp > mult){
        temp -= mult;
        mult *= 26;
        ++p26;
    }
    mult = 1;
    for(int i = 0; i < p26; ++i){
        mult *= 26;
    }
    string ans = "";
    for(int i = p26; i >= 0; --i){
        if(n % mult == 0 && mult != 1){
            ans += (char)(n / mult - 2 + 'a');
            n %= mult;
            n += mult;
        }
        else{
            ans += (char)(n / mult - 1 + 'a');
            n %= mult;
        }
        mult /= 26;
    }
    cout << ans << "\n";
}

Score : 300 points

Problem Statement

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

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

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

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

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

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

and so on.

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

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

Now, Roger asks you:

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

Constraints

N is an integer.

1 \leq N \leq 1000000000000001

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

2

Sample Output 1

b

Sample Input 2

27

Sample Output 2

aa

Sample Input 3

123456789

Sample Output 3

jjddja


#include <stdio.h>

int main()
{

  int x;
  scanf("%d",&x);
  int result=360/x;
  if(x*result==360)
    	printf("%d",result);
 	else
      	printf("%d",result+1);
}

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

Score : 200 points

Problem Statement

Takahashi is standing on a two-dimensional plane, facing north. Find the minimum positive integer K such that Takahashi will be at the starting position again after he does the following action K times:

Go one meter in the direction he is facing. Then, turn X degrees counter-clockwise.

Constraints

1 \leq X \leq 179

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

Print the number of times Takahashi will do the action before he is at the starting position again.

Sample Input 1

90

Sample Output 1

4

Takahashi's path is a square.

Sample Input 2

1

Sample Output 2

360

Submitted source-code file size, in bytes.

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

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


#include<bits/stdc++.h>
#include<vector>
#include<cmath>
#include<math.h>
#include<cstdlib>
#include<assert.h>
#include<string>
#include<sstream>
#include<algorithm>
#include<ctime>
#include<iostream>
#define ll long long
#define ld long double
#define pb push_back
#define rep(i,n) for(i=0;i<n;i++)
#define rep1(j,n) for (j=0;j<=n;j++)
#define PI 3.14159265358979323846
#define mod 1000000007
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL)
using namespace std;
/*void input(vector<ld>& v,ld n)
{
	ld i,io;
	rep(i,n)
	{
		cin>>io;
		v.pb(io);
	}
}*/
int main()
{
	fastio;
	int n;
	cin>>n;
	cout<<(360)/n<<endl;
}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = INT32_MAX;
const ll INFL = INT64_MAX;
const int mod = 1e9 + 7;

template <typename T>
void print_vec(vector<T> v) {
  for (T i : v) cout << i << " ";
  cout << endl;
}

int main() {
  int ans = 0;
  rep(i, 5) {
    int x;
    cin >> x;
    if (x == 0) ans = i + 1;
  }
  cout << ans << endl;
  return 0;
}

Score : 100 points

Problem Statement

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

The variable x_i was initially assigned a value of i.

Snuke chose one of these variables and assigned it 0.

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

Find out which variable Snuke assigned 0.

Constraints

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

Input

Input is given from Standard Input in the following format:

x_1 x_2 x_3 x_4 x_5

Output

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

Sample Input 1

0 2 3 4 5

Sample Output 1

1

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

Sample Input 2

1 2 0 4 5

Sample Output 2

3


#include<bits/stdc++.h>
using namespace std;
int main(){int m,n;if(m<=n&&n<=2*m&&n%2==0)cout<<"Yes"<<endl;else{cout<<"No"<<endl;}}

Score : 200 points

Problem Statement

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

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

Constraints

1 \leq X \leq 100

1 \leq Y \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

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

Sample Input 1

3 8

Sample Output 1

Yes

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

Sample Input 2

2 100

Sample Output 2

No

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

Sample Input 3

1 2

Sample Output 3

Yes

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


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

int main() {

int x,n, r{101};
vector<int>v{};
cin>>x>>n;
if(!n)
  cout << x;
else
{

  int c;
  for(int i = 0; i < n; i++)
  {
    cin >> c;
    v.push_back(abs(c));
  }

  sort(v.begin(), v.end());
  auto it = find(v.begin(),v.end(), x);

  if (it == v.end()){
    cout << x;
    exit(0);
  }

  int z = 1;
  while(true)
  {
    if(*(it-z) != x-z)
    {
      z = x-z;
      break;
    }
    else if (*(it+z) != x+z)
    {
      z = x+z;
      break;
    }
    z++;

  }
  cout << z;

}
return 0;
}

Score : 300 points

Problem Statement

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

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

Constraints

1 \leq X \leq 100

0 \leq N \leq 100

1 \leq p_i \leq 100

p_1, \ldots, p_N are all distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X N
p_1 ... p_N

Output

Print the answer.

Sample Input 1

6 5
4 7 10 6 5

Sample Output 1

8

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

Sample Input 2

10 5
4 7 10 6 5

Sample Output 2

9

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

Sample Input 3

100 0

Sample Output 3

100

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


#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

using namespace std;
#define int long long
#define ld long double
#define all(x) x.begin(),x.end()
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define vi vector<int>
#define vstr vector<string>
#define vbool vector<bool>
#define vvi vector<vector<int> >
#define mii map<int,int>
#define pb push_back
#define pii pair<int,int>
#define vpair vector<pii >
#define mkp make_pair
#define scan(a,n) for(int i =0 ; i<n ; i++) cin>>a[i]
#define print(a,n) for(int i = 0 ; i < n ; i++) {cout<<a[i]<<' ';}cout<<'\n'
#define mem(a,v) memset(a,v,sizeof(a))
#define loop(i,a,b) for (int i = a; i < b; i++)
#define loope(i,a,b) for (int i = a; i <= b; i++)
#define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define PRECISION std::cout.unsetf ( std::ios::fixed );std::cout.precision(9)
#define PI 3.14159265
#define S second
#define F first
#define CHECK cout<<"CHEDEDWB"<<endl
int mod = 1e9+7;
int inf = 1e18;
int m_inf = INT_MIN;

void solve()
{
	int x,n;
	cin>>x>>n;
	if(n==0)
		cout<<x<<endl;
	else
	{
		int p=0,q=0;
		vector<bool> ans(101,false);
		vi arr(n);
		loop(i,0,n)
		{
			cin>>arr[i];
			ans[arr[i]] = true;
		}
		sort(all(arr));
		loop(i,x,101)
		{
			if(ans[i]==false)
				{
					p = i;
					break;
				}
		}
		for(int i = x;i>0;i--)
		{
			if(ans[i]==false)
				{
					q = i;
					break;
				}
		}
		if(abs(x-p)<abs(x-q))
			cout<<p<<endl;
		else
			cout<<q<<endl;
	}
}

int32_t main()
{
	FastIO; PRECISION;
	int t = 1;
	//cin>>t;
	while(t--)
		solve();
	return 0;
}


// clang-format off
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using G = vector<vector<ll>>;
#define G(i, j) i, vector<ll>(j)
#define Gv(i, j, k) i, vector<ll>(j, k)
#define rep(i,n) for(ll i=0, i##_len=(n); i<i##_len; ++i)
#define rep1(i,n) for(ll i=1, i##_len=(n); i<=i##_len; ++i)
#define rrep(i,n) for(ll i=n-1; i>=0; --i)
#define rrep1(i,n) for(ll i=n; i>0; --i)
#define all(x) (x).begin(),(x).end()
#define SORT(x) sort(all(x))
#define SORT_DESC(x) sort((x).rbegin(),(x).rend())
#define pb push_back
#define mp make_pair
#define coutl cout<<fixed<<setprecision(10)
#define ANS(ans) cout<<ans<<endl
#define END(ans) ANS(ans); return
const ll LINF = (1LL << 62) - 1;
const int INF = (1 << 30) - 1;
template<class T>inline bool chmax(T &a, const T &b){if (a<b){a=b;return 1;}return 0;}
template<class T>inline bool chmin(T &a, const T &b){if (b<a){a=b;return 1;}return 0;}
// clang-format on

void solve(long long X, long long N, std::vector<long long> p) {
  ll a, s = INF;
  rep(i, 102) {
    if (find(all(p), i) != p.end())
      continue;
    ll d = abs(X - i);
    if (d < s) {
      a = i;
      s = d;
    }
  }
  ANS(a);
}

// clang-format off
int main() {
  long long X;
  scanf("%lld",&X);
  long long N;
  scanf("%lld",&N);
  std::vector<long long> p(N);
  for(int i = 0 ; i < N ; i++){
    scanf("%lld",&p[i]);
  }
  solve(X, N, std::move(p));
  return 0;
}


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

#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)

using ll = long long;
using ld = long double;
ll INF = LLONG_MAX;

using vi = vector<int>;
using vll = vector<ll>;
using pii = pair<int, int>;

namespace output {
	void pr(int x) { cout << x; }
	void pr(long x) { cout << x; }
	void pr(ll x) { cout << x; }
	void pr(unsigned x) { cout << x; }
	void pr(unsigned long x) { cout << x; }
	void pr(unsigned long long x) { cout << x; }
	void pr(float x) { cout << x; }
	void pr(double x) { cout << x; }
	void pr(ld x) { cout << x; }
	void pr(char x) { cout << x; }
	void pr(const char* x) { cout << x; }
	void pr(const string& x) { cout << x; }
	void pr(bool x) { pr(x ? "true" : "false"); }
	template<class T> void pr(const complex<T>& x) { cout << x; }

	template<class T1, class T2> void pr(const pair<T1,T2>& x);
	template<class T> void pr(const T& x);

	template<class T, class... Ts> void pr(const T& t, const Ts&... ts) {
		pr(t); pr(ts...);
	}
	template<class T1, class T2> void pr(const pair<T1,T2>& x) {
		pr("{",x.f,", ",x.s,"}");
	}
	template<class T> void pr(const T& x) {
		pr("{"); // const iterator needed for vector<bool>
		bool fst = 1; for (const auto& a: x) pr(!fst?", ":"",a), fst = 0;
		pr("}");
	}

	void print() { pr("\n"); } // print w/ spaces
	template<class T, class... Ts> void print(const T& t, const Ts&... ts) {
		pr(t); if (sizeof...(ts)) pr(" "); print(ts...);
	}
}

using namespace output;

using state = tuple<int, int, int, int>;

vi DX = {-1, 1, 0, 0};
vi DY = {0, 0, -1, 1};

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

	int H, W, K; cin >> H >> W >> K;
	int X1, Y1, X2, Y2; cin >> X1 >> Y1 >> X2 >> Y2; --X1; --Y1; --X2; --Y2;
	swap(X1, Y1);
	swap(X2, Y2);

	vector<vector<bool>> grid (H, vector<bool> (W));
	F0R(i, H) {
		F0R(j, W) {
			char c; cin >> c;
			grid[i][j] = c == '@';
		}
	}

	vector<vector<vector<vector<int>>>> dist (H, vector<vector<vector<int>>> (W, vector<vector<int>> (4, vector<int> (K + 1, 1e7))));

	deque<state> q;
	F0R(i, 4) {
		q.push_front(make_tuple(X1, Y1, i, K));
		dist[Y1][X1][i][K] = 1;
	}

	while (!q.empty()) {
		state v = q.front();
		q.pop_front();

		int x = get<0>(v);
		int y = get<1>(v);
		int d = get<2>(v);
		int r = get<3>(v);

		int dis = dist[y][x][d][r];

		// print("SEARCHING", x, y, d, r);

		F0R(i, 4) {
			if (dis + 1 < dist[y][x][i][K]) {
				dist[y][x][i][K] = dis + 1;
				q.push_back(make_tuple(x, y, i, K));
			}
		}

		if (r > 0) {
			int nx = x + DX[d];
			int ny = y + DY[d];
			if (0 <= nx && nx < W && 0 <= ny && ny < H && !grid[ny][nx]) {
				if (dis < dist[ny][nx][d][r-1]) {
					dist[ny][nx][d][r-1] = dis;
					q.push_front(make_tuple(nx, ny, d, r - 1));
				}
			}
		}
	}

	int ans = 1e7;
	F0R(i, 4) F0R(k, K+1) ans = min(ans, dist[Y2][X2][i][k]);
	print((ans < 1e7) ? ans : -1);
}

Score : 600 points

Problem Statement

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

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

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

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

Constraints

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

H \times W \leq 10^6

1 \leq x_1,x_2 \leq H

1 \leq y_1,y_2 \leq W

x_1 \neq x_2 or y_1 \neq y_2.

c_{i,j} is . or @.

c_{x_1,y_1} = .

c_{x_2,y_2} = .

All numbers in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

5

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

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

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

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

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

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

Sample Input 2

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

Sample Output 2

2

Sample Input 3

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

Sample Output 3

-1


#include<bits/stdc++.h>
using namespace std;
int main() {
  int a,v;
  cin >> a >> v;
  int b,w;
  cin >> b >> w;
  int t;
  cin >> t;
  bool fg = false;
  for (int i = 1; i <= t; i++) {
    if (a > b) {
      a = a - v;
      b = b - w;
      if (a == b) {
        fg = true;
      }
    }
    if (a < b) {
      a = a + v;
      b = b + w;
      if (a == b) {
        fg = true;
      }
    }
  }
  if (fg) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
  return 0;
}

Score : 200 points

Problem Statement

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

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

Constraints

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

1 \leq V,W \leq 10^9

1 \leq T \leq 10^9

A \neq B

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A V
B W
T

Output

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

Sample Input 1

1 2
3 1
3

Sample Output 1

YES

Sample Input 2

1 2
3 2
3

Sample Output 2

NO

Sample Input 3

1 2
3 3
3

Sample Output 3

NO


#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <cmath>
#include <numeric>
#include <iomanip>
#include <deque>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <sstream>
#include <stdexcept>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define FOR(i, a, done) for (int i = (a); i < (done); ++i)
#define all(a) (a).begin(), (a).end()
#define x first
#define y second
#define debug(a) cout << (a) << endl
namespace mp = boost::multiprecision;
using cpp_int = mp::cpp_int;
using ll = long long;
using namespace std;
template <class T>
bool chmax(T &a, const T &b)
{
	if (a < b)
	{
		a = b;
		return 1;
	}
	return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
	if (a > b)
	{
		a = b;
		return 1;
	}
	return 0;
}

ll M = 1000000007;

void solve()
{
	int n,k;
	cin>>n>>k;
	vector<int>a(n);
	vector<vector<int>>imos(k+2,vector<int>(n+2,0));
	rep(i,n){
		cin>>a[i];
		int mi=max(i-a[i],0);
		int ma=min(n-1,i+a[i]);
		imos[0][mi]++;
		imos[0][ma+1]--;

	}
	if((ll)n*k>=(ll)1e6 or n<=k){
		rep(i,n){
			cout<<n;
			if(i!=n-1){
				cout<<" ";
			}else{
				cout<<endl;
			}
		}
		return ;
	}
	int cur=0;
	rep(j,min(k,50)){
		cur=0;
		bool fin=true;
		rep(i,n){
			cur+=imos[j][i];
			a[i]=cur;
			int mi=max(i-a[i],0);
			int ma=min(n-1,i+a[i]);
			imos[j+1][mi]++;
			imos[j+1][ma+1]--;
			if(a[i]!=n){
				fin=false;
			}
		}
		if(fin){
			//cout<<j<<endl;
			rep(i,n){
				cout<<n;
				if(i!=n-1){
					cout<<" ";
				}else{
					cout<<endl;
				}
			}
			return ;
		}
	}
	rep(i,n){
		cout<<a[i];
		if(i!=n-1){
			cout<<" ";
		}else{
			cout<<endl;
		}
	}
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	solve();
	return 0;
}

Score : 500 points

Problem Statement

We have N bulbs arranged on a number line, numbered 1 to N from left to right.
Bulb i is at coordinate i.

Each bulb has a non-negative integer parameter called intensity.
When there is a bulb of intensity d at coordinate x, the bulb illuminates the segment from coordinate x-d-0.5 to x+d+0.5.
Initially, the intensity of Bulb i is A_i. We will now do the following operation K times in a row:

For each integer i between 1 and N (inclusive), let B_i be the number of bulbs illuminating coordinate i. Then, change the intensity of each bulb i to B_i.

Find the intensity of each bulb after the K operations.

Constraints

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

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

0 \leq A_i \leq N

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \ldots A_N

Output

Print the intensity A{'}_i of each bulb i after the K operations to Standard Output in the following format:

A{'}_1 A{'}_2 \ldots A{'}_N

Sample Input 1

5 1
1 0 0 1 0

Sample Output 1

1 2 2 1 2

Initially, only Bulb 1 illuminates coordinate 1, so the intensity of Bulb 1 becomes 1 after the operation.
Similarly, the bulbs initially illuminating coordinate 2 are Bulb 1 and 2, so the intensity of Bulb 2 becomes 2.

Sample Input 2

5 2
1 0 0 1 0

Sample Output 2

3 3 4 4 3


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

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

    while (K--) {
        vector<ll> inout(N+1, 0);
        for (int i = 0; i < N; i++) {
            inout[max((ll)0, i - A[i])]++;
            inout[min(N, i+A[i]+1)]--;
        }
        A[0] = inout[0];
        for (int i = 1; i < N; i++) {
            A[i] = A[i-1] + inout[i];
        }
        bool f = true;
        for (int i = 0; i < N; i++) {
            f = f && (A[i] == N);
        }
        if (f) break;
    }
    for (int i = 0; i < N; i++) {
        cout << A[i] << ' ';
    }
    cout << endl;
}


#include <bits/stdc++.h>
typedef long long LL;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

using namespace std;

int main(void)
{
  int n;
  cin >> n;
  vector<LL> value(1<<18), weight(1<<18);
  FOR(i,1,n+1) cin >> value[i] >> weight[i];
  vector<vector<pair<LL, LL>>> hanzen(1<<11); // weight, value
  hanzen[0].emplace_back(0,0);
  FOR(bits,1,1<<11) {
    for(pair<LL,LL> p:hanzen[bits/2]) {
      pair<LL,LL> added = make_pair(p.first+weight[bits], p.second+value[bits]);
      hanzen[bits].push_back(p);
      hanzen[bits].push_back(added);
    }
    vector<pair<LL,LL>> tmp(hanzen[bits].begin(),hanzen[bits].end());
    sort(tmp.begin(),tmp.end());
    hanzen[bits].clear();
    hanzen[bits].emplace_back(0,0);
    for(pair<LL,LL> p:tmp) {
      if(hanzen[bits].back().second < p.second) hanzen[bits].push_back(p);
    }
  }
  int q;
  cin >> q;
  while(q--) {
    LL v, l;
    cin >> v >> l;
    vector<pair<LL,LL>> comb;
    comb.emplace_back(0,0);
    while(v>=(1<<11)) {
      vector<pair<LL,LL>> tmp(comb.begin(), comb.end());
      for(pair<LL,LL> p:tmp) {
        pair<LL,LL> added = make_pair(p.first+weight[v], p.second+value[v]);
        comb.push_back(added);
      }
      v/=2;
    }
    LL answer = 0;
    for(pair<LL,LL> p: comb) {
      LL cw,cv;
      tie(cw,cv) = p;
      if(cw>l) continue;
      auto ite = lower_bound(hanzen[v].begin(),hanzen[v].end(), make_pair(l-cw+1,0ll));
      --ite;
      answer=max(answer, cv+ite->second);
    }
    cout << answer << endl;
  }

  return 0;
}

Score : 700 points

Problem Statement

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

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

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

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

Constraints

All values in input are integers.

1 \leq N < 2^{18}

1 \leq Q \leq 10^5

1 \leq V_i \leq 10^5

1 \leq W_i \leq 10^5

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

Input

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

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

Output

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

Sample Input 1

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

Sample Output 1

0
3
3

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

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

Sample Input 2

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

Sample Output 2

256
255
250
247
255
259
223
253


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll,ll> pa;

#define fst first
#define snd second
#define pb push_back

const int maxn=500005,maxm=512;
const double pi=acos(-1.0);
//const ll mod=1000000007;
const ll mod=998244353;
const double eps=1e-8;

int dig(int xx)
{
    for(int i=0;;i++)
    {
        if(xx>>i&1) return i;
    }
}

struct dat
{
    int sta;
    ll v,w;
    dat(ll v=0,ll w=0):sta(sta),v(v),w(w){}
};

int n,q;
int v[maxn]={0},w[maxn]={0};

ll ve[maxm][maxm][2]={0};

vector<pa> ans1[maxm];

vector<pa> tmp;

ll cl2(int vv,int ww)
{
    int lb=0,ub=ans1[vv].size();
    while(ub-lb>1)
    {
        int mid=(ub+lb)/2;
        if(ans1[vv][mid].fst<=ww) lb=mid;
        else ub=mid;
    }
    return ans1[vv][lb].snd;
}

void solve(int vv,int L)
{
    int i,j;
    if(vv<512)
    {
        for(j=0;;j++) {if((vv<<j)>=maxm/2&&(vv<<j)<maxm) break;}
        ll ans=0;
        for(i=1;i<(1<<(j+1));i++)
        {
            if(ve[vv<<j][i][1]<=L) ans=max(ans,ve[vv<<j][i][0]);
        }
        printf("%lld\n",ans);return;
    }
    tmp.clear();tmp.pb(pa(0,0));if(w[vv]<=L) tmp.pb(pa(w[vv],v[vv]));
    while(1)
    {
        vv/=2;
        if(vv>=maxm/2&&vv<maxm) break;
        int nn=tmp.size();
        for(i=0;i<nn;i++)
        {
            if(tmp[i].fst+w[vv]<=L) {tmp.pb(pa(tmp[i].fst+w[vv],tmp[i].snd+v[vv]));}
        }
    }
    ll ans=0;
    for(i=0;i<tmp.size();i++)
    {
        ll www=tmp[i].fst,vvv=tmp[i].snd;
        ans=max(ans,cl2(vv,L-www)+vvv);
    }
    printf("%lld\n",ans);
}

bool cmp(pa a,pa b)
{
    if(a.fst!=b.fst) return a.fst<b.fst;
    return a.snd<b.snd;
}

int tp[maxn];

int main()
{
	int i,j;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
    {
        scanf("%d %d",v+i,w+i);
    }
    n=max(n,maxm-1);
    for(i=maxm/2;i<maxm;i++)
    {
        for(j=1;j<maxm;j++)
        {
            int jj=j&(-j);jj=j-jj;
            ve[i][j][0]=ve[i][jj][0]+v[i>>(8-dig(j))];
            ve[i][j][1]=ve[i][jj][1]+w[i>>(8-dig(j))];
        }
    }
    for(i=maxm/2;i<maxm;i++)
    {
        for(j=0;j<maxm;j++) ans1[i].pb(pa(ve[i][j][1],ve[i][j][0]));
        sort(ans1[i].begin(),ans1[i].end(),cmp);
    }

    scanf("%d",&q);
    for(i=0;i<q;i++)
    {
        int vv,L;
        scanf("%d %d",&vv,&L);
        solve(vv,L);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ll Z=0;
string s;
cin>>s;
ll rui=0;
ll a=0;
ll f=0;
ll s1=s.size();
ll b[s1+1];
ll c[s1+1];
for (ll i=0;i<s1;i++){
if(s.at(i)=='1'){
a++;}
else{
a--;}
b[i]=a;
c[i]=a;
Z=max(Z,a);}
for (ll i=s1-2;i>=0;i--){
b[i]=max(b[i],b[i+1]);}
for (ll i=0;i<s1;i++){
if(s.at(i)=='1'){
rui++;}
else if(s.at(i)=='0'){
rui--;}
else{
if(rui-c[i-1]+b[i]+2<=Z){
rui++;}
else{
rui--;}}
f=min(f,rui);}
ll Z1=Z+1;
ll f1=0;
rui=0;
for (ll i=0;i<s1;i++){
if(s.at(i)=='1'){
rui++;}
else if(s.at(i)=='0'){
rui--;}
else{
if(rui+b[i]-c[i-1]+2<=Z1){
rui++;}
else{
rui--;}}
f1=min(f1,rui);}
ll ans=min(Z-f,Z1-f1);
if(s.at(0)=='?'){
ans=1;}
cout<<ans<<endl;}

Score : 800 points

Problem Statement

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

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

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

Find the minimum possible unbalancedness of S'.

Constraints

1 \leq |S| \leq 10^6

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

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum possible unbalancedness of S'.

Sample Input 1

0??

Sample Output 1

1

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

Sample Input 2

0??0

Sample Output 2

2

Sample Input 3

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

Sample Output 3

4


Score : 800 points

Problem Statement

Snuke has a string x of length N.
Initially, every character in x is 0.

Snuke can do the following two operations any number of times in any order:

Choose A consecutive characters in x and replace each of them with 0.

Choose B consecutive characters in x and replace each of them with 1.

Find the number of different strings that x can be after Snuke finishes doing operations.
This count can be enormous, so compute it modulo (10^9+7).

Constraints

1 \leq N \leq 5000

1 \leq A,B \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of different strings that x can be after Snuke finishes doing operations, modulo (10^9+7).

Sample Input 1

4 2 3

Sample Output 1

11

For example, x can be 0011 or 1111 in the end, but cannot be 0110.

Sample Input 2

10 7 2

Sample Output 2

533

Sample Input 3

1000 100 10

Sample Output 3

828178524


#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

int n,m;
int main()
{
    cin>>n>>m;
    cout<<n*m<<endl;
    return 0;
}

Score : 100 points

Problem Statement

Compute A \times B.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the value A \times B as an integer.

Sample Input 1

2 5

Sample Output 1

10

We have 2 \times 5 = 10.

Sample Input 2

100 100

Sample Output 2

10000


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <string>
#include <numeric>
#include <algorithm>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <ctime>
#define ll long long
using namespace std;

int main(){
	int a,b;
	while(scanf("%d%d",&a,&b)==2)
    {
        printf("%d\n",a*b);
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> P;
const int maxn = 500050;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const ll linf = 7e18;
ll power(ll x, ll y)
{
    ll ans = 1;
    while (y)
    {
        if (y & 1)
            ans = ans * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return ans;
}

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

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    cout << n * m << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

    int A[n], B[n];
    for(int i=0; i<n; i++) cin >> A[i] >> B[i];

    sort(A, A+n);
    sort(B, B+n);

    int left, right;
    if(n%2 == 0) {
        left = A[n/2-1] + A[n/2];
        right = B[n/2-1] + B[n/2];
    } else {
        left = A[n/2];
        right = B[n/2];
    }

    cout << right - left +1 << endl;

    return 0;
}

Score : 500 points

Problem Statement

There are N integers X_1, X_2, \cdots, X_N, and we know that A_i \leq X_i \leq B_i.
Find the number of different values that the median of X_1, X_2, \cdots, X_N can take.

Notes

The median of X_1, X_2, \cdots, X_N is defined as follows. Let x_1, x_2, \cdots, x_N be the result of sorting X_1, X_2, \cdots, X_N in ascending order.

If N is odd, the median is x_{(N+1)/2};

if N is even, the median is (x_{N/2} + x_{N/2+1}) / 2.

Constraints

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

1 \leq A_i \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
A_2 B_2
:
A_N B_N

Output

Print the answer.

Sample Input 1

2
1 2
2 3

Sample Output 1

3

If X_1 = 1 and X_2 = 2, the median is \frac{3}{2};

if X_1 = 1 and X_2 = 3, the median is 2;

if X_1 = 2 and X_2 = 2, the median is 2;

if X_1 = 2 and X_2 = 3, the median is \frac{5}{2}.

Thus, the median can take three values: \frac{3}{2}, 2, and \frac{5}{2}.

Sample Input 2

3
100 100
10 10000
1 1000000000

Sample Output 2

9991


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

int main() {
	int N;
	cin >> N;
	vector< pair<int, int>> AB(N);
	for (int i = 0; i < N; i++) cin >> AB[i].first >> AB[i].second;

	if (N % 2 == 0) {
		sort(AB.begin(), AB.end());
		double Amin = AB[N / 2 - 1].first;
		double Amax = min(AB[N / 2 - 1].second, AB[N / 2].second);
		sort(AB.begin(), AB.end(), [](auto& a, auto& b) {return a.second > b.second; });
		double Bmin = max(AB[N / 2].first, AB[N / 2 - 1].first);
		double Bmax = AB[N / 2 - 1].second;
		cout << ((Amax + Bmax) / 2 - (Amin + Bmin) / 2) * 2 + 1 << endl;
	}
	else {
		sort(AB.begin(), AB.end());
		double Amin = AB[(N - 1) / 2].first;
		double Amax = min(AB[(N - 1) / 2].second, AB[(N + 1) / 2].second);
		sort(AB.begin(), AB.end(), [](auto& a, auto& b) {return a.second > b.second; });
		double Bmin = max(AB[(N - 1) / 2].first, AB[(N + 1) / 2].first);
		double Bmax = AB[(N - 1) / 2].second;
		if (Amax < Bmin) cout << (Amax - Amin) + 1 + (Bmax - Bmin) + 1;
		else cout << Bmax - Amin + 1;
	}

}


 #include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
#define pi 3.1415926535
#define endl '\n'
using namespace std;
struct custom {
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }
    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
//using namespace __gnu_pbds;
//typedef tree<ll,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
const ll inf=1e19;
/*ll power(ll x,ll y)
{
    ll res=1;
    while(y>0)
    {
        if(y%2==1)
            res=(res*x)%mod;
        x=(x*x)%mod;
        y=(y>>1);
    }
    return res;
}*/
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll a,b,c,d,x,y,r,k;
    cin>>a>>b>>c>>d>>k;
    x=a*60+b;
    y=c*60+d;
    x=abs(x-y);
    cout<<max(0LL,x-k)<<endl;
}

Score : 100 points

Problem Statement

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

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

Constraints

0 \le H_1, H_2 \le 23

0 \le M_1, M_2 \le 59

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

K \ge 1

Takahashi is up for at least K minutes.

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

Input

Input is given from Standard Input in the following format:

H_1 M_1 H_2 M_2 K

Output

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

Sample Input 1

10 0 15 0 30

Sample Output 1

270

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

Sample Input 2

10 0 12 0 120

Sample Output 2

0

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


#include <bits/stdc++.h>

#define int         long long
#define uint        unsigned int
#define ld          long double
#define showoff     ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb          push_back
#define pii         pair<int,int>
#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 f           first
#define se          second
#define maxn        200005
#define all(v)      v.begin(),v.end()
#define sz(x)       (int)x.size()
#define mod         998244353
#define pqueue      priority_queue<pii>
#define pdqueue     priority_queue< int,vector<int> ,greater< int >>

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int power(int a,int n)
{
    a %= mod;
    if(n == 1)return a;
    if(n == 0)return 1;
    if(n%2)return (a*(power((a*a)%mod,n/2)%mod))%mod;
    return power((a*a)%mod,n/2)%mod;
}

const int inf = (int) 1e18;

int inverse(int x){
    return power(x,mod-2)%mod;//little fermat....
}

void solve(){
    int h1,m1,h2,m2,k;
    cin >> h1 >> m1 >> h2 >> m2 >> k;
    int tot = 0;
    if(h2 > h1)tot = m2+(h2-h1-1)*60+60-m1;
    else tot = m2-m1;
    cout << max(0LL,tot-k) << "\n";
}

signed main()
{
    showoff;
    int T = 1;
    //cin >> T;
    FOR(t,1,T+1){
        solve();
        cout << "\n";
    }
    return 0;
}
//*->for large size of matrix take int not long long if possible......
//*->always take maximum as inf for safer side ...


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

int main() {
  string t;
  cin >> t;
  int i;
  for(i=0;i<(int)t.size();i++){
    if(t.at(i)=='?'){
      t.at(i)='D';
    }
  }
  cout << t << endl;
}

Score : 200 points

Problem Statement

For a string S consisting of the uppercase English letters P and D, let the doctoral and postdoctoral quotient of S be the total number of occurrences of D and PD in S as contiguous substrings. For example, if S = PPDDP, it contains two occurrences of D and one occurrence of PD as contiguous substrings, so the doctoral and postdoctoral quotient of S is 3.

We have a string T consisting of P, D, and ?.

Among the strings that can be obtained by replacing each ? in T with P or D, find one with the maximum possible doctoral and postdoctoral quotient.

Constraints

1 \leq |T| \leq 2 \times 10^5

T consists of P, D, and ?.

Input

Input is given from Standard Input in the following format:

T

Output

Print one string with the maximum possible doctoral and postdoctoral quotient among the strings that can be obtained by replacing each ? in T with P or D.
If there are multiple such strings, you may print any of them.

Sample Input 1

PD?D??P

Sample Output 1

PDPDPDP

This string contains three occurrences of D and three occurrences of PD as contiguous substrings, so its doctoral and postdoctoral quotient is 6, which is the maximum doctoral and postdoctoral quotient of a string obtained by replacing each ? in T with P or D.

Sample Input 2

P?P?

Sample Output 2

PDPD


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

int main()
{
	string s;
    cin >> s;
    int l =s.length();
   for(int i=0;i<l;i++)
   {
       if(s[i]=='?')
       {
           if(i==l-1 && i-1>=0 && s[i-1]=='P')
           {
				s[i]='D';
           }
           else if(i-1>=0 && s[i-1]=='P' && i+1<l && s[i+1]!='D')
           {
               s[i]='D';
           }
           else
          	 s[i]='P';
       }
   }
    cout << s << endl;

	return 0;
}


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

int main() {
    string T;
    cin >> T;
    int len = T.size();
    string n = "";
    for (int i=0; i < len; i++) {
        if (T[i] == '?') {
            if (i == (len -1)) {
                n += "D";
            } else if (i > 0 && T[i-1] == 'P') {
                n += "D";
            } else if (i < (len - 1) && T[i+1] == 'P') {
                n += "D";
            }
            else {
                n += "P";
            }
        } else {
            n += T[i];
        }
    }

    cout << n << endl;
    return 0;
}


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

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back

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

char alpha[26] = {'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'};

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

int main(){
    string t; cin >> t;
    REP(i, 0, t.size()){
        if(t[i] == '?'){
            cout << "D";
        }else{
            cout << t[i];
        }
    }
    cout << endl;
}


#include <bits/stdc++.h>

using namespace std;

#include <vector>
#include <map>
#include <set>
#include <queue>
#include <math.h>
#include <iostream>
#include <stdio.h>

typedef long long ll;
typedef vector<ll> vll;
typedef pair<ll,ll> pairll;

#define sll(x) scanf("%lld",&x);
#define READ freopen("in.txt","r",stdin)
#define WRITE freopen("out.txt","w",stdout)

#define ff first
#define ss second

inline ll readInt()
{
    ll in; scanf("%lld",&in); return in;
}

inline ll ADD(ll a, ll b, ll mod)
{
    return (a + b + mod + mod)%mod;
}

inline ll SUBS(ll a, ll b, ll mod)
{
    return (a-b+mod + mod) %mod;
}

inline ll MUL(ll a, ll b, ll mod)
{
    return (a*b + mod)%mod;
}

ll bigMod(ll base, ll power, ll mod)
{
    if(!power) return 1;
    if(!base) return 0;

    ll temp = bigMod(base, power/2, mod);

    if(power%2 == 0)
    {
        return (temp*temp)%mod;
    }
    else return (((temp*temp)%mod)*base)%mod;
}

inline ll modInverse(ll a, ll modp)
{
    return bigMod(a, modp -2, modp);
}

inline ll modInverseExtended(ll a, ll m)
{
    ll m0 = m;
    ll y = 0, x = 1;

    if (m == 1)
      return 0;

    while (a > 1)
    {
        // q is quotient
        ll q = a / m;
        ll t = m;

        // m is remainder now, process same as
        // Euclid's algo
        m = a % m, a = t;
        t = y;

        // Update y and x
        y = x - q * y;
        x = t;
    }

    // Make x positive
    if (x < 0)
       x += m0;

    return x;
}

void autoinput()
{
    #ifndef ONLINE_JUDGE
    READ;
    //WRITE;

    #endif
}

int main()
{
    autoinput();

    ll N;

    sll(N);

    vector<ll> leaves(N+1), freedom(N+1);

    freedom[0] = 1;

    for(ll i = 0; i <= N; i++)
    {
        sll(leaves[i]);
    }

    for(ll i = 0; i <= N; i++)
    {
        if(!i)
        {
            freedom[i] -= leaves[i];
        }
        else
        {
            if(freedom[i-1] < INT64_MAX/10) freedom[i] = 2*freedom[i-1] - leaves[i];
            else freedom[i] = freedom[i-1];
        }

        if(freedom[i] < 0)
        {
            puts("-1"); return 0;
        }

    }

    vll tree(N+1);

    tree[N] = leaves[N];

    ll sum = tree[N];

    for(ll i = N-1; i >= 0; i--)
    {
        tree[i] = min(freedom[i],tree[i+1]);
        tree[i] += leaves[i];

        sum += tree[i];

    }

    printf("%lld\n",sum);

}

Score : 600 points

Problem Statement

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

Notes

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

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

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

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

Constraints

0 \leq N \leq 10^5

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

A_N \geq 1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_0 A_1 A_2 \cdots A_N

Output

Print the answer as an integer.

Sample Input 1

3
0 1 1 2

Sample Output 1

7

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

Sample Input 2

4
0 0 1 0 2

Sample Output 2

10

Sample Input 3

2
0 3 1

Sample Output 3

-1

Sample Input 4

1
1 1

Sample Output 4

-1

Sample Input 5

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

Sample Output 5

264


#pragma GCC optimize ("O3")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int CM = 1 << 17, CL = 12;
char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct;
const ll ma0 = 1157442765409226768;
const ll ma1 = 1085102592571150095;
const ll ma2 = 71777214294589695;
const ll ma3 = 281470681808895;
const ll ma4 = 4294967295;
inline int getint() {
	if (ci - owa > 0) {
		memcpy(cn, owa, CL);
		ci -= CM;
		fread(cn + CL, 1, CM, stdin);
	}
	ll tmp = *(ll*)ci;
	int dig = 68 - __builtin_ctzll((tmp & ma0) ^ ma0);
	tmp = tmp << dig & ma1;
	tmp = tmp * 10 + (tmp >> 8) & ma2;
	tmp = tmp * 100 + (tmp >> 16) & ma3;
	tmp = tmp * 10000 + (tmp >> 32) & ma4;
	ci += 72 - dig >> 3;
	return tmp;
}

unsigned char D[252004];
bool E[252004];
int Q[252004];
int main() {
	//cin.tie(0);
	//ios::sync_with_stdio(false);

	int N = getint();
	int n = N + 2;
	rep1(i, N) {
		int ij = n * i + 1;
		unsigned char kari = min(i - 1, N - i);
		unsigned char kari2 = 3;

		rep(j, kari) {
			D[ij++] = kari2++;
		}
		rep(j, N - kari - kari) {
			D[ij++] = kari + 3;
		}
		rep(j, kari) {
			D[ij++] = --kari2;
		}
	}

	int kotae = 0;
	rep(k, N * N) {
		int ij = getint();
		ij += (ij - 1) / N * 2 + N + 2;

		kotae += D[ij];
		E[ij] = 1;

		int p = 0, q = 0;
		Q[p++] = ij;

		while (p > q) {
			int ij = Q[q++];
			int d =	D[ij] - E[ij] + 1;

			if (D[ij - n] > d) {
				D[ij - n] = d;
				Q[p++] = ij - n;
			}
			if (D[ij + n] > d) {
				D[ij + n] = d;
				Q[p++] = ij + n;
			}
			if (D[ij - 1] > d) {
				D[ij - 1] = d;
				Q[p++] = ij - 1;
			}
			if (D[ij + 1] > d) {
				D[ij + 1] = d;
				Q[p++] = ij + 1;
			}
		}
	}

	printf("%d\n", kotae - 3 * N * N);

	Would you please return 0;
}

Score : 700 points

Problem Statement

Tonight, in your favourite cinema they are giving the movie Joker and all seats are occupied. In the cinema there are N rows with N seats each, forming an N\times N square. We denote with 1, 2,\dots, N the viewers in the first row (from left to right); with N+1, \dots, 2N the viewers in the second row (from left to right); and so on until the last row, whose viewers are denoted by N^2-N+1,\dots, N^2.

At the end of the movie, the viewers go out of the cinema in a certain order: the i-th viewer leaving her seat is the one denoted by the number P_i. The viewer P_{i+1} waits until viewer P_i has left the cinema before leaving her seat.
To exit from the cinema, a viewer must move from seat to seat until she exits the square of seats (any side of the square is a valid exit). A viewer can move from a seat to one of its 4 adjacent seats (same row or same column).
While leaving the cinema, it might be that a certain viewer x goes through a seat currently occupied by viewer y; in that case viewer y will hate viewer x forever. Each viewer chooses the way that minimizes the number of viewers that will hate her forever.

Compute the number of pairs of viewers (x, y) such that y will hate x forever.

Constraints

2 \le N \le 500

The sequence P_1, P_2, \dots, P_{N^2} is a permutation of \{1, 2, \dots, N^2\}.

Input

The input is given from Standard Input in the format

N
P_1 P_2 \cdots P_{N^2}

Output

If ans is the number of pairs of viewers described in the statement, you should print on Standard Output

ans

Sample Input 1

3
1 3 7 9 5 4 8 6 2

Sample Output 1

1

Before the end of the movie, the viewers are arranged in the cinema as follows:

1 2 3
4 5 6
7 8 9

The first four viewers leaving the cinema (1, 3, 7, 9) can leave the cinema without going through any seat, so they will not be hated by anybody.

Then, viewer 5 must go through one of the seats where viewers 2, 4, 6, 8 are currently seated while leaving the cinema; hence he will be hated by at least one of those viewers.

Finally the remaining viewers can leave the cinema (in the order 4, 8, 6, 2) without going through any occupied seat (actually, they can leave the cinema without going through any seat at all).

Sample Input 2

4
6 7 1 4 13 16 10 9 5 11 12 14 15 2 3 8

Sample Output 2

3

Sample Input 3

6
11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10 29 28 33 12 6 5 30

Sample Output 3

11


#include <bits/stdc++.h>

#define ll long long

using namespace std;

const int MAXN = 250010;
const int MAXM = 10010;
const int MAXINT = 2147483647;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

int n, m, k;
int ans, tot, cnt;

int a[MAXM][MAXM];
int vis[MAXM][MAXM];

string s;

inline int read() {
	int s = 0, f = 1;
	char ch = getchar();
	while ('0' > ch || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
	while ('0' <= ch && ch <= '9') {s = (s << 3) + (s << 1) + ch - 48; ch = getchar();}
	return s * f;
}

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

void Bfs(int s) {
	queue<int> qx, qy;
	queue<int> q1;
	qx.push((s - 1) / n + 1);
	qy.push((s - 1) % n + 1);
//	printf("%d %d\n", qx.front(), qy.front());
	q1.push(0);
	memset(vis, 0, sizeof(vis));
//	vis[z] = 0;
	int res = INF;
	while (qx.size()) {
		int x = qx.front(), y = qy.front(), t = q1.front();
		qx.pop(); qy.pop();
		q1.pop();
		vis[x][y] = 1;
//		if (x % n == 1 || x <= n || x > n * n - n || x % n == 0)
		if (x == 1 || x == n || y == 1 || y == n) {
			res = min(res, t);
//			printf("finish: %d\n", t);
			continue;
		}
		for (int i = 0; i < 4; i++) {
			int xx = x + dx[i];
			int yy = y + dy[i];
			if (1 <= xx && xx <= n && 1 <= yy && yy <= n && !vis[xx][yy]) {
				qx.push(xx);
				qy.push(yy);
				q1.push(t + a[xx][yy]);
			}
//			if (x / n * n < x + d[i] && x + d[i] <= x / n * n + n && !vis[x + d[i]]) {
//				q.push(x + d[i]);
//				q1.push(t + e[x][i]);
//			}
		}
//		for (int i = 2; i < 4; i++) {
//			if (0 < x + d[i] && x + d[i] <= n * n && !vis[x + d[i]]) {
//				q.push(x + d[i]);
//				q1.push(t + e[x][i]);
//			}
//		}
	}
	ans += res;
	return;
}

int p[MAXN];

int main(){
	int T;
	scanf("%d", &n);
	for (int i = 1; i <= n * n; i++) {
		scanf("%d", &p[i]);
	}
//	for (int i = 1; i <= n * n; i++) {
//		int c = i;
//		e[c][1] = e[c][2] = e[c][3] = e[c][0] = 1;
//	}
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			a[i][j] = 1;
		}
	}

	for (int i = 1; i <= n * n; i++) {
		Bfs(p[i]);
		a[(p[i] - 1) / n + 1][(p[i] - 1) % n + 1] = 0;
//		int c = n * (p[i] - 1) + p[i];
////		e[c][1] = e[c][2] = e[c][3] = e[c][4] = 0;
//		if (c > n) e[c - n][2] = 0;
//		if (c < n * n - n + 1) e[c + n][3] = 0;
//		e[c - 1][0] = e[c + 1][1] = 0;
//		print();
	}
	printf("%d\n", ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair< ll, ll > Pi;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<=(n);i++)
#define rep3(i,i0,n) for(int i=i0;i<(n);i++)
#define pb push_back
#define mod 1000000007
const ll INF = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
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;}
#define all(x) x.begin(), x.end()
#define mp make_pair
bool compare(Pi a, Pi b) {
    if(a.first != b.first){
        return a.first < b.first;

    }else{
        return a.second < b.second;
    }
}

bool In_map(ll y,ll x,ll h,ll w){
    if(y<0 || x<0 || y>=h || x>=w){
        return 0;
    }else{
        return 1;
    }
}
const vector<ll> dx{1,0,-1,0};
const vector<ll> dy{0,1,0,-1};

int main() {
    ll N;
    cin >>N;
    ll x = N%10;
    if(x==3){
        cout<<"bon"<<endl;
    }else if(x==0 || x==1 || x==6 || x==8){
        cout<<"pon"<<endl;
    }else{
        cout<<"hon"<<endl;
    }

    return 0;

}

Score: 100 points

Problem Statement

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

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

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

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

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

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

Constraints

N is a positive integer not exceeding 999.

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

16

Sample Output 1

pon

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

Sample Input 2

2

Sample Output 2

hon

Sample Input 3

183

Sample Output 3

bon


/*
URL_HERE
*/
/*

*/
#ifdef _WIN32
	#pragma warning(disable:4996)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>

#define ten5p1 100001
#define ten6p1 1000001
#define ten8p1 100000001
#define ten9p1 1000000001
#define ll long long
#define rep(var,n) for(int var=0;var<n;++var)
#define repi(n) rep(i,n)
#define repj(n) rep(j,n)
#define repi1(n) for(int i=1;i<n;++i)
#define _min(a,b) a<b?a:b
#define _max(a,b) a>b?a:b
#define zeroi(data,n) memset(data,0,sizeof(int)*n);
#define zeroll(data,n) memset(data,0,sizeof(long long)*n);
#define one(data,n) {for(int _ONEI=0;_ONEI<n;++_ONEI) data[_ONEI]=1;}
#define readc(var) fscanf(_fin,"%c",var)
#define readi(var) fscanf(_fin,"%d",var)
#define readll(var) fscanf(_fin,"%lld",var)
#define repreadi(data,n) rep(_repreadi, n) readi(&data[_repreadi])
#define writec(var) fprintf(_fout,"%c",var)
#define writecsp(var) fprintf(_fout,"%c ",var)
#define writecln(var) fprintf(_fout,"%c\n",var)
#define writei(var) fprintf(_fout,"%d",var)
#define writeisp(var) fprintf(_fout,"%d ",var)
#define writellsp(var) fprintf(_fout,"%lld ",var)
#define writeiln(var) fprintf(_fout,"%d\n",var)
#define writellln(var) fprintf(_fout,"%lld\n",var)
#define writefln(var) fprintf(_fout,"%f\n",var)
#define writes(str) fprintf(_fout,"%s",str)
#define writesp() fprintf(_fout," ")
#define writeln() fprintf(_fout,"\n")
#define iseven(x) ((x&1)==0 ? 1:0)
#define RUN_LOCAL(testfilename) {_fin = fopen(testfilename, "r"); if(_fin==NULL) _fin=stdin;}
#define swap(type,a, b) {type t=a;a=b;b=t;}
#define swapxor(a,b) {a^=b;b^=a;a^=b;}
#define sort(data,n) std::sort(data,data+n)
#define mod(a,b) b==0 ? 0 : a==0 ? 0 :(a>0 ? a%b : b + a % b)
ll gcd(ll a, ll b) { if (b == 0) return a; return gcd(b, a % b); }

FILE* _fin = stdin;
FILE* _fout = stdout;
void reversei(int* data, int n) { int k = n >> 1; repi(k) { int tmp = data[i]; data[i] = data[n - i - 1]; data[n - i - 1] = tmp; } }
int bsearch(int val, int* data, int n) { if (data[0] > val) return -1; if (data[n - 1] < val) return n; int l = 0; int r = n - 1; for (; l < r;) { int mid = (l + r + 1) >> 1; if (data[mid] <= val) l = mid; else r = mid - 1; }if (data[r] != val) ++r; return r; }
int reads(char* str, int maxsize)
{
	for (;;)
	{
		if(fgets(str, maxsize, _fin)==NULL) break;
		if (str[0] != '\n' && str[0] != '\r') break;
	}
	int slen = strlen(str);
	if (slen == 0) return 0;
	if (str[slen - 1] == '\n' || str[slen - 1] == '\r') str[--slen] = 0;
	return slen;
}

#define _Vec(type, name) \
struct name { type* data; int size; int n; };\
void init(name* t, int size) { t->data = (type*)malloc(sizeof(type) * size); t->size = size; t->n = 0; }\
void resize(name* t) { int ns = t->size * 1.2f; t->data = (type*)realloc(t->data, sizeof(type) * ns); t->size = ns; }\
void add(name* t, type val) { if (t->n >= t->size) resize(t); int k = t->n; t->data[k] = val; t->n = k + 1; }\
void free(name* t) { free(t->data);}
_Vec(int, Veci)
_Vec(long long, Vecll)
_Vec(char*, Vecs)

#define _ispal(type, name) int name(type* a, type* b, int n){repi(n){if (a[i] != b[n - i - 1]){return 0;}}return 1;}
_ispal(int, ispali)
_ispal(char, ispalc)

#define _Pair(type, name) struct name{type x, y;}; \
int cmp_##name(const void* _a, const void* _b){name* a = (name*)_a;name* b = (name*)_b;if (a->x == b->x){if (a->y < b->y) return -1;else return 1;}if (a->x < b->x) return -1;else return 1;}\
void sort_##name(name* d, int n){qsort(d, n, sizeof(name), cmp_##name);}
	_Pair(int, Pairi)

	int cmp_Str(const void* _a, const void* _b) { char* a = *((char**)_a); char* b = *((char**)_b); return strcmp(a, b); }
void sort_Str(char** str, int n) { qsort(str, n, sizeof(char*), cmp_Str); }

int main()
{
	RUN_LOCAL("dataabc168B.txt");

	int k; readi(&k);
	char s[102];
	int len = reads(s, 102);

	repi(k) writec(s[i]);
	if(k<=len) writes("...\n");
	return 0;
}

Score: 200 points

Problem Statement

We have a string S consisting of lowercase English letters.

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

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

Constraints

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

S is a string consisting of lowercase English letters.

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

Input

Input is given from Standard Input in the following format:

K
S

Output

Print a string as stated in Problem Statement.

Sample Input 1

7
nikoandsolstice

Sample Output 1

nikoand...

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

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

Sample Input 2

40
ferelibenterhominesidquodvoluntcredunt

Sample Output 2

ferelibenterhominesidquodvoluntcredunt

The famous quote from Gaius Julius Caesar.


#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define rando mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define fi first
#define se second
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define debugii(x) cerr << " - " << #x << ": " << x.fi<<","<<x.se << endl;
#define sep() cerr << "--------------------" << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ld long double
#define ll long long
//#define int ll
#define ii pair<int,int>
#define v vector<int>
#define vii vector<ii>
#define vv vector<vector<int> >
#define mp make_pair
#define INF 1000000000
#define pb push_back
#define EPS 1e-9
const int MOD = 1000000007; // 998244353
int32_t main(){
	boost;
	//freopen(".in","r",stdin);
	int k;
	string s;
	cin>>k>>s;
	if(sz(s)<=k)cout<<s<<"\n";
	else{
		cout<<s.substr(0,k)<<"...\n";
	}
	return 0;
}

//do smth instead of nothing and stay organized
//long long
//array bounds
//special cases
//binary search


#include <bits/stdc++.h>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;

#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define PI pair<int,int>

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = (ll)(1e9 + 7);
const int N = 1000010;

int A, B, H, M;

int main() {
    while (cin >> A >> B >> H >> M) {
        double e1 = -(1.0 * (H * 60 + M) / 360 * pi - pi / 2);
        double e2 = -(1.0 * M / 30 * pi - pi / 2);
        double a = A * cos(e1), b = A * sin(e1);
        double c = B * cos(e2), d = B * sin(e2);
        double dis = sqrt((a - c) * (a - c) + (b - d) * (b - d));
        cout << setprecision(14) << dis << endl;
    }
    return 0;
}

Score: 300 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

1 \leq A, B \leq 1000

0 \leq H \leq 11

0 \leq M \leq 59

Input

Input is given from Standard Input in the following format:

A B H M

Output

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

Sample Input 1

3 4 9 0

Sample Output 1

5.00000000000000000000

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

Sample Input 2

3 4 10 40

Sample Output 2

4.56425719433005567605

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


#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int h, m, hl, ml;
double angle, radian, ans;
int main()
{
	scanf("%d%d%d%d", &hl, &ml, &h, &m);
	angle = fabs(0.5 *( h * 60 + m) - 6 * m * 0.1 * 10);
	//cout<<angle<<endl;
	if(angle > 180) angle -= 180;
	if(angle == 0)
	{
		printf("%d", abs(hl - ml));
		return 0;
	}
	radian = angle * 3.14159265358979323846 / 180 * 1e-9 *1e9;
	//cout<<radian<<endl;
	ans = hl * hl + ml *ml - 2 * hl * ml * cos(angle * M_PI/180);
	printf("%.20f", sqrt(hl * hl + ml *ml - 2 * hl * ml * cos(angle * M_PI/180)));
	return 0;
}


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

int main()
{
    int A,B,H,M;
    float PI = 3.14159265358979323846f;
    float ans = 0;
    cin >> A >> B >> H >> M;
    float a,b,c;
    a = (60.0f*H + M) * 6.0f;
    b = (60.0f*H + M) * 0.5f;
    c = min(abs(b - a),abs(360.0f - b + a));
    ans = sqrtl(A*A+B*B-2.0f*A*B*cos(PI * c / 180.0f));
    cout << fixed << setprecision(15) << ans << endl;

}


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

#define REP(i, n) for(int64_t i = 0; i < n; i++)
#define REPR(i, n) for(int64_t i = n; i >= 0; i--)
#define FOR(i, m, n) for(int64_t i = m; i < n; i++)
#define FORR(i, m, n) for(int64_t i = m; i >= n; i--)
#define ALL(v) v.begin(), v.end()
typedef long long ll;
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    fill( (T*)array, (T*)(array+N), val );
}
template<class T>
inline bool chmax(T &a, T b) {if(a < b) {a = b;return true;}return false;}
template<class T>
inline bool chmin(T &a, T b) {if(a > b) {a = b;return true;}return false;}
const ll INF= 1ll << 60;
const ll MOD = 1000000007;
double EPS = 1e-10;
//----------------------------------------------------------

double a,b,h,m;
int main(){
    cin >> a >> b >> h >> m;
    double ans = 0;
    double mk = M_PI/1800.0;
    double hk = M_PI/21600.0;
    double mr = ((mk*(h*3600+m*60)-h*2*M_PI)>=0?mk*(h*3600+m*60)-h*2*M_PI:mk*(h*3600+m*60));
    double hr = hk*(h*3600+m*60);
    double dr = ((mr-hr)>=0?mr-hr:hr-mr);
    if(dr>M_PI)
    {
        double t1 = ((2*M_PI-hr)-mr>=0?(2*M_PI-hr)-mr:mr-(2*M_PI-hr));
        double t2 = ((2*M_PI-mr)-hr>=0?(2*M_PI-mr)-hr:hr-(2*M_PI-mr));
        dr=(t1>M_PI?t2:t1);
    }
    ans = sqrt(a*a+b*b-2*a*b*cos(dr));
    cout << fixed << setprecision(11) << ans << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
double PI = acos(-1.0);
#define all(v) v.begin(),v.end()

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

    int n,m;
    cin >> n >> m;

    vector<int> adj[n+1];

    for(int i= 0 ; i < m ; ++i ){
        int a,b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    for( int i = 1 ; i < n + 1 ; ++i ){
        for( int x : adj[i] ) cout << x <<" ";
        cout << endl;
    }
    vector<int> ans( n - 1 , -1 );
    bool ok = true;
    unordered_set<int> S;

    for( int i = 2 ; i <= n ; ++i ){
        sort(all(adj[i]));
        bool ga = binary_search(all(adj[i]), 1);
        if(ga){
            ans[i-2] = 1;
            S.insert(i);
        }
    }

    for( int x : S ){
        for( int y : adj[x] ){
            if( ans[y-2] == -1 ) {
                ans[y-2] = x;
            }
        }
    }

    for( int i = 0 ; i < ans.size() ; ++i ){
        if( ans[i] == -1 ){
            ok = 0;
            break;
        }
    }
    if( ok ){
        cout << "Yes\n";
        for( int x : ans ) cout << x <<'\n';
    } else cout << "No\n";

    return 0;
}

Score: 400 points

Problem Statement

There is a cave.

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

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

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

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

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

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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

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

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

One can travel between any two rooms by traversing passages.

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
:
A_M B_M

Output

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

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

Sample Input 1

4 4
1 2
2 3
3 4
4 2

Sample Output 1

Yes
1
2
2

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

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

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

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

Thus, the objective is satisfied.

Sample Input 2

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

Sample Output 2

Yes
6
5
5
1
1

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


#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define INF 0x3f3f3f3f
#define MAX (1<<20)
#define MOD 1000000007
#define i64 long long
#define all(x) (x).begin() , (x).end()
#define sz(x) (int)(x).size()
#define ii pair<i64, i64>
#define fs first
#define sc second
#define eb emplace_back
#define vi vector<i64>
#define vvi vector<vi>
#define vii vector<ii>
#define vvii vector<vii>
#define lsb(x) ((x) & (-x))
#define gcd(x,y) __gcd((x),(y))

using namespace std;

map<ii, i64> f;
vii dir;
vi pot2;
i64 n, xz, yz, zz, a, b, ans, fator;

int main(){_
    cin >> n;

    pot2.eb(1);
    for (int i=n; i--;){
        pot2.eb((2*pot2.back())%MOD);
    }

    dir = vii(n);
    for (auto &[x, y] : dir){
        cin >> x >> y;

        if (!x) {
            if (!y){
                ++zz;
                continue;
            }else{
                ++xz;
            }
        }else if (!y){
            ++yz;
        }

        if (x<0){
            x = -x; y = -y;
        }

        i64 mdc = abs(gcd(x, y));
        x /= mdc; y/=mdc;

        ++f[{x,y}];
    }

    ans = 1LL;

    fator = (pot2[xz] + pot2[yz] - 1LL + MOD)%MOD;
    ans = (ans * fator) % MOD;

    for (auto [p, c] : f){
        i64 x = p.fs, y=p.sc;

        // cout << x << " " << y << " >> " << c << endl;

        if (!x || !y || !c) continue;

        a = (y > 0 ? y : -y);
        b = (y > 0 ? -x : x);

        fator = (pot2[c] + pot2[f[{a,b}]] - 1LL + MOD) % MOD;
        ans = (ans * fator) % MOD;

        f[{a,b}] *= !(f[{a,b}]);
    }

    cout << (ans-1LL+zz)%MOD << endl;

    return 0;
}

Score: 500 points

Problem Statement

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

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

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

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

Constraints

All values in input are integers.

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

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

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the count modulo 1000000007.

Sample Input 1

3
1 2
-1 1
2 -1

Sample Output 1

5

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

The 1-st

The 1-st and 2-nd

The 2-nd

The 2-nd and 3-rd

The 3-rd

Sample Input 2

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

Sample Output 2

479


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

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

#define p_ary(ary,a,b) do { cout << "["; for (int count = (a);count < (b);++count) cout << ary[count] << ((b)-1 == count ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

template<typename T1,typename T2>ostream& operator<<(ostream& os,const pair<T1,T2>& a) {os << "(" << a.first << ", " << a.second << ")";return os;}

const char newl = '\n';

const ll mod = 1000000007;
//const ll mod = 998244353;

const int MAX_N = 200010;
ll fact[MAX_N],fact_inv[MAX_N],inv[MAX_N];

ll pow_mod(ll a,ll b) {
    ll ret;
    if (b < 0) ret = pow_mod(a,mod+b-1);
    else if (b == 0) ret = 1;
    else if (b == 1) ret = a;
    else {
        ll c = pow_mod(a,b/2);
        if (b%2) ret = (c*c)%mod*a%mod;
        else ret = c*c%mod;
    }
    return ret;
}

void init(int x) {
    fact[0] = 1;fact[1] = 1;
    for (int i = 2;i <= x;++i) fact[i] = fact[i-1]*i%mod;
    fact_inv[x] = pow_mod(fact[x],-1);
    for (int i = x;i > 0;--i) fact_inv[i-1] = fact_inv[i]*i%mod;
    for (int i = 1;i <= x;++i) inv[i] = fact_inv[i]*fact[i-1]%mod;
}

ll combi(ll a, ll b) {
    return fact[a]*fact_inv[b]%mod*fact_inv[a-b]%mod;
}

int main() {
    int n,m = 0;
    cin >> n;
    init(n);
    map<P,int> mp;
    set<P> st;
    for (int i = 0;i < n;++i) {
        ll a,b;
        scanf("%lld%lld",&a,&b);
        if (a == 0 && b == 0) {
            n--;
            m++;
            continue;
        }
        ll g = gcd(abs(a),abs(b));
        a /= g;
        b /= g;
        if (a < 0 || (a == 0 && b < 0)) {
            a *= -1;
            b *= -1;
        }
        mp[make_pair(a,b)]++;
    }
    ll ans = pow_mod(2,n)-1,s = 1;
    for (auto p : mp) {
        P q = make_pair(p.first.second,-p.first.first);
        if (q.first < 0 || (q.first == 0 && q.second < 0)) {
            q.first *= -1;
            q.second *= -1;
        }
        if (mp.find(q) == mp.end() || st.find(p.first) != st.end()) continue;
        n -= p.second+mp[q];
        (ans -= (pow_mod(2,p.second)-1)*(pow_mod(2,mp[q])-1)%mod*(pow_mod(2,n)*s%mod)%mod) %= mod;
        st.insert(p.first);
        st.insert(q);
        (s *= (pow_mod(2,p.second+mp[q])-(pow_mod(2,p.second)-1)*(pow_mod(2,mp[q])-1)%mod)%mod) %= mod;
    }
    cout << (ans+m+mod)%mod << endl;
}


#include <bits/stdc++.h>
#define ALL(a)  (a).begin(),(a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define PI 3.141592653589793
#define endl "\n"

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string S, T;
  cin >> S >> T;
  bool flag = false;
  for(int i = 0; i < 26; i++){
    string str = S + (char)(i + 'a');
    if(str == T) flag = true;
  }
  if(flag) cout << "Yes" << endl;
  else cout << "No" << endl;
}

Score : 100 points

Problem Statement

Takahashi wants to be a member of some web service.

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

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

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

Constraints

S and T are strings consisting of lowercase English letters.

1 \leq |S| \leq 10

|T| = |S| + 1

Input

Input is given from Standard Input in the following format:

S
T

Output

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

Sample Input 1

chokudai
chokudaiz

Sample Output 1

Yes

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

Sample Input 2

snuke
snekee

Sample Output 2

No

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

Sample Input 3

a
aa

Sample Output 3

Yes


#include<bits/stdc++.h>
using namespace std;
int main(){
	long long a,b,c,k;
	scanf("%lld %lld %lld %lld",&a,&b,&c,&k);
	long long ans = min(a,k);
	k-=a;
	k = k-b;
	if(k){
		ans -= k;
	}
	printf("%lld\n", ans);
	return 0;
}

Score : 200 points

Problem Statement

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

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

Constraints

All values in input are integers.

0 \leq A, B, C

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

Input

Input is given from Standard Input in the following format:

A B C K

Output

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

Sample Input 1

2 1 1 3

Sample Output 1

2

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

Sample Input 2

1 2 3 4

Sample Output 2

0

Sample Input 3

2000000000 0 0 2000000000

Sample Output 3

2000000000


#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0;i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;
int main() {
  ll a,b,c;
  ll k;
  cin >> a >> b >> c >> k;
  ll ans;
  if(k - a - b > 0){
    ans = a - (k - a - b);
  }else if(k < a){
    ans = k;
  }else if(k - a - b < 0){
    ans = a;
  }
  cout << ans << endl;
  return 0;
}


#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <iomanip>
#include <stack>
#include <list>
#include <map>
#include <unordered_map>
#include <chrono>
#include <numeric>
using namespace std;
using ll = long long;
const ll INF = (ll)1e18+1;
const ll DIV = 1000000007;
//#define TEST
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
#ifdef TEST
    chrono::system_clock::time_point start, end;
    start = chrono::system_clock::now();
#endif
    long A,B,C,K;
    cin >> A >> B >> C >> K;
    if(K <= A) cout << K << endl;
    else if(K > A && K <= A+B) cout << A << endl;
    else cout << A - (K - (A + B)) << endl;
#ifdef TEST
    end = chrono::system_clock::now();
    cerr << static_cast<double>(chrono::duration_cast<chrono::microseconds>(end - start).count() / 1000.0) << "[ms]" << endl;
#endif
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int mi=INT_MAX;
bool xx=false;
void recursrion(int index ,int currSum,int K,vector < pair< int , vector<int> > > &arr,vector<int> Books,int N,int M)
{
    if(index>=N){return ;}
    recursrion(index+1,currSum,K,arr,Books,N,M);
    int cost=arr[index].first;
    currSum+=cost;
    vector<int> x=arr[index].second;
    for(int i=0;i<M;i++)
    {
        Books[i]+=x[i];
    }
    int FLAG=0;
    for(int i=0;i<M;i++)
    {
        if(Books[i]<K)
        {
            FLAG=1;
            break;
        }
    }
    if(FLAG==0)
    {
        xx=true;
        mi=min(mi,currSum);
    }
    recursrion(index+1,currSum,K,arr,Books,N,M);
    return ;
}
int main()
{
    // freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
    int N,M,K;
    cin>>N>>M>>K;
    vector < pair < int , vector<int> > > arr;
    for(int i=0;i<N;i++)
    {
        int x;
        cin>>x;
        vector<int> S(M);
        for(auto &it:S)
        {
            cin>>it;
        }
        arr.push_back(make_pair(x,S));
    }
    vector<int> Books(M,0);
    recursrion(0,0,K,arr,Books,N,M);
    if(xx==true)
    {
        cout<<mi;
    }
    else
    {
        cout<<-1;
    }

}

Score : 300 points

Problem

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

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

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

Constraints

All values in input are integers.

1\leq N, M\leq 12

1\leq X\leq 10^5

1\leq C_i \leq 10^5

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

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

120

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

Sample Input 2

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

Sample Output 2

-1

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

Sample Input 3

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

Sample Output 3

1067


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

#define fast_io ios_base::sync_with_stdio(false); cin.tie(nullptr)
#define rep(i, x, y) for(decltype(y) i = (x) - ((x) > (y)); i != (y) - ((x) > (y)); i += 1 - 2 * ((x) > (y)))
#define trav(i,a) for(auto& (i) : (a))
#define all(a) (a).begin(),(a).end()
#define sz(a) int((a).size())
#define clr(a,x) memset((a),(x),sizeof(a)) // array (0,-1)
#define dbg(x) cerr << "STDERR => " << __LINE__ << ": " << #x << " = " << (x) << "  :(\n";
#define pb push_back
#define mp make_pair
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline void cpuTime() { cerr << "cpu time : " << double(clock())/CLOCKS_PER_SEC << "\n"; return; }
using ll = long long;
using ull = unsigned long long;
using F = float;
using D = double;
using vi = vector<int>;
using pii = pair<int,int>;

void solve(){

    int n, m, x;
    cin >> n >> m >> x;

    vector<vi> c(n, vi(m+1, 0));
    rep(i,0,n){
        rep(j,0,m+1){
            cin >> c[i][j];
        }
    }

    vector<bool> a(n,0), b(n,0);
    ll cost = 0, ans = LLONG_MAX;
    vi sum(m, 0);

    rep(i,0,n){
        a[n-1-i] = 1;
        b = a;

        do{
            rep(j,0,m) sum[j] = 0;
            cost = 0;
            rep(j,0,n){
                if(b[j]){
                    cost += c[j][0];
                    rep(k,0,m){
                        sum[k] += c[j][k+1];
                    }
                }
            }

            rep(k,0,m){
                if(sum[k] < x) break;
                if(k==(m-1)) ans = min(ans, cost);
            }
        }while(next_permutation(all(b)));
    }

    if(ans==LLONG_MAX) cout << "-1\n";
    else cout << ans << "\n";

    return;
}

/*
 read the question carefully ( what're the exact constraints? )
 look out for SPECIAL CASES (n=1?) and OVERFLOW (ll vs int?) ARRAY OUT OF BOUNDS
 Check datatypes
*/
int main(){

    fast_io;
    int t=1;
    // cin >> t;

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

    cpuTime();
return 0;
}


#include <bits/stdc++.h>

/*==========type============*/
typedef long long ll;
typedef std::pair<int,int> pii;
typedef std::pair<ll,ll> pll;
typedef std::pair<std::string,ll> psl;

/*==========constant============*/
#define INF INT_MAX
#define INF_L LONG_MAX
#define INF_LL LLONG_MAX
#define MOD 1000000007

/*==========function============*/
#define REP(ii,jj,nn) for (ll ii=jj;ii<(nn);ii++)
#define RREP(ii,nn,jj) for (ll ii = nn; jj<ii;ii--)
#define each(i,...) for (auto&& i:__VA_ARGS__)
#define ALL(vec) (vec).begin(),(vec).end()
#define sum(...) accumulate(ALL(__VA_ARGS__),0LL)
#define dsum(...) accumulate(ALL(__VA_ARGS__),0.0)
#define vec(type,name,...) vector<type> name(__VA_ARGS__)
template<class T> inline auto max(const T& a){ return *max_element(ALL(a)); }
template<class T> inline auto min(const T& a){ return *min_element(ALL(a)); }
inline ll gcd(ll a,ll b){if(b == 0) return a;return  gcd(b,a%b);}
inline ll lcm(ll a,ll b){ll g = gcd(a,b);return a / g * b;}

using namespace std;

ll n,m,x;
vector<ll> c(12);
vector<ll> a[12];

bool eval(vector<ll> aa){
    REP(i,0,aa.size()){
        if(aa[i] < x)return false;
    }
    return true;
}

ll dfs(ll index, ll cost,vector<ll>& aa){
    if(eval(aa))return cost;
    if(index >= n)return INF;

    ll ret = INF;

    vector<ll> aaa = aa;
    REP(i,0,aaa.size())aaa[i] += a[index][i];
    ret = min(ret,dfs(index+1,cost+c[index],aaa));

    ret = min(ret,dfs(index+1,cost,aa));

    return ret;
}

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

    cin >> n >> m >> x;
    REP(i,0,n){
        cin >> c[i];
        REP(j,0,m){
            ll aa;
            cin >> aa;
            a[i].push_back(aa);
        }
    }

    vector<ll> aaa(m,0);
    ll ans = dfs(0,0,aaa);
    if(ans == INF)ans = -1;

    cout << ans << endl;

    return 0;
}


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

  if(S == "ABC") cout << "ARC" << endl;
  else cout << "ABC" << endl;

  return 0;
}

Score : 100 points

Problem Statement

AtCoder Inc. holds a contest every Saturday.

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

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

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

Constraints

S is ABC or ARC.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

ABC

Sample Output 1

ARC

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


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c;
    cin>>a>>b;
    vector<int>m(a+1,0);
    m[0]=1;
    for(int i=0; i<b; i++)
    {
        cin>>c;
        vector<int>n(c);
        for(int j=0; j<c; j++)
        {
            cin>>n[i];
            m[n[i]]++;
        }
    }
    int p=0;
    for(int k=0; k<a+1; k++)
    {
        if(m[k]==0)
            p++;
    }
    cout<<p<<endl;
}

Score : 200 points

Problem Statement

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq K \leq 100

1 \leq d_i \leq N

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

Input

Input is given from Standard Input in the following format:

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

Output

Print the answer.

Sample Input 1

3 2
2
1 3
1
3

Sample Output 1

1

Snuke 1 has Snack 1.

Snuke 2 has no snacks.

Snuke 3 has Snack 1 and 2.

Thus, there will be one victim: Snuke 2.

Sample Input 2

3 3
1
3
1
3
1
3

Sample Output 2

2


#define dbg(...) "amit_dwivedi"
#define db(...)  "Too OP"
#include "bits/stdc++.h"
using namespace std;using ll=long long;
ll n,m,L,R;
const int mod = 1e9+7;

void sol(){
    int n,k;
    cin>>n>>k;
    bool snuke[n+1] = {0};
    for(int i = 0,temp; i <k;i++){
        cin>>temp;
        for(int j=0,l; j<temp; j++) {
            cin>>l;
            snuke[l] = true;
        }
    }
    int ans = 0;
    for(int i=1; i<=n; i++) {
        ans += (!snuke[i]);
    }
    cout << ans << endl;

}
int main(){
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    // int t;cin>>t;while(t--)
    sol();
}


#include<iostream>
#include<vector>
using namespace std;
int main() {
	int n, k, d,numofnusuke,count=0;
	cin >> n >> k;
	vector<bool> nusuke(n,false);
	for (int i = 0; i < k; i++) {
		cin >> d;
		for (int j = 0; j < d; j++) {
			cin >> numofnusuke;
			nusuke[numofnusuke-1] = true;
		}
	}
	for (int i = 0; i < n; i++) {
		if (nusuke[i] == false)
			count++;
	}
	cout << count;
}


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

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

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

    int A,B;
    vector<int> bad(N,0);
    for(int i=0;i<M;i++){
        cin >> A >> B;
        if(H[A-1]<=H[B-1]){
            bad[A-1] = 1; // 悪である
        }
        if(H[A-1]>=H[B-1]){
            bad[B-1] = 1; // 悪である
        }
    }

    int count=0;
    for(int i=0;i<N;i++){
        if(bad[i]==0){
            count++;
        }
    }

    cout << count << endl;

    return 0;
}

Score : 300 points

Problem Statement

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

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

How many good observatories are there?

Constraints

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq H_i \leq 10^9

1 \leq A_i,B_i \leq N

A_i \neq B_i

Multiple roads may connect the same pair of observatories.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

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

Output

Print the number of good observatories.

Sample Input 1

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

Sample Output 1

2

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

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

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

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

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

Sample Input 2

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

Sample Output 2

3


/**
 * Dont raise your voice, improve your argument.
 * --Desmond Tutu
 */

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

vector<string> vec_splitter(string s) {
    for(char& c: s) c = c == ','?  ' ': c;
    stringstream ss; ss << s;
    vector<string> res;
    for(string z; ss >> z; res.push_back(z));
    return res;
}

void debug_out(vector<string> __attribute__ ((unused)) args, __attribute__ ((unused)) int idx) { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(vector<string> args, int idx, Head H, Tail... T) {
    if(idx > 0) cerr << ", ";
    stringstream ss; ss << H;
    cerr << args[idx] << " = " << ss.str();
    debug_out(args, idx + 1, T...);
}

#ifdef XOX
#define debug(...) debug_out(vec_splitter(#__VA_ARGS__), 0, __VA_ARGS__)
#else
#define debug(...) 42
#endif

const double PI = acos(-1);
typedef long long ll;
#define int ll
#define fori(n) for(int i=0; i<int(n); i++)

#define cins(s) string s; cin>>s;
#define cini(i) int i; cin>>i;
#define cind(d) double d; cin>>d;
#define cinai(a, n) vi a(n); fori(n) { cin>>a[i]; }
#define cinas(s, n) vs s(n); fori(n) { cin>>s[i]; }
#define cinad(a, n) vd a(n); fori(n) { cin>>a[i]; }

typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<string> vs;

void solve() {
    cini(n);
    cini(m);
    cinai(h,n);
    vvi adj(n);
    for(int i=0; i<m; i++) {
        cini(a);
        cini(b);
        a--;
        b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    vb ans(n, true);

    for(int i=0; i<n; i++) {
        for(int j : adj[i])
            if(h[i]<=h[j])
                ans[i]=false;
    }

    int cnt=0;
    for(int i=0; i<n; i++)
        if(ans[i])
            cnt++;
    cout<<cnt<<endl;

}

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


// myfirstatcoder.cpp : アプリケーションのエントリ ポイントを定義します。

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

int main()
{
	int x;
	cin >> x;
	llrep(i, -200, 200)
		llrep(j, -200, 200)
		if (i*i*i*i*i - j * j*j*j*j == x) {
			cout << i << " " << j << endl;
			return 0;
		}
	//cout << a << b << endl;
}

/*
ライブラリ
・string
・vector<int>
::::vector<vector<int>> A(N,vector<int>(M));
::::A[i][j] = i;
・map<string,int>

関数
・rep(i,n) … ループ(i=0;i<N;++i)
・rep_b(i,n) … ループ(i=1;i<=N;++i)
・gcd(int, int) … 最大公約数 int
・find_string(str, str) … 文字列検索 bool
*/

Score : 400 points

Problem Statement

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

Constraints

1 \leq X \leq 10^9

X is an integer.

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

Input

Input is given from Standard Input in the following format:

X

Output

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

A B

Sample Input 1

33

Sample Output 1

2 -1

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

Sample Input 2

1

Sample Output 2

0 -1


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream>
#include <iomanip>
#include <limits>
#include <cmath>
using namespace std;

int main(){
    signed long X;
    cin>>X;
    signed long A,B;
    vector<signed long> ALL;
    for(int i=0;i<10;i++){
        long tmp=pow(i,5);
        ALL.push_back(tmp);
        cout<<tmp<<endl;
    }
    bool ok=false;
    for(int i=-9;i<10;i++){
        signed long tmp;
        if(i<0){
            tmp=X-ALL[abs(i)];
        }else{
            tmp=X+ALL[i];

        }
        if(tmp<0){
            for(int j=0;j<ALL.size();j++){
            if(ALL[j]==abs(tmp)){
                A=-j;
                B=i;
                ok=true;
                break;
            }
        }
        }else{
            for(int j=0;j<ALL.size();j++){
                if(ALL[j]==tmp){
                    A=j;
                    B=i;
                    ok=true;
                    break;
                }
            }
        }

        if(ok)break;
    }
    cout<<A<<" "<<B;

}