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

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

using namespace std;

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

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

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

#define F first
#define S second

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

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

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

constexpr ll MOD = 1000000007LL;
template<typename T = ll> constexpr T MAX = numeric_limits<T>::max();

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

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

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

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

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

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

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

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

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

/*
	regex reg( R"(^(dream|dreamer|erase|eraser)+$)" );
	smatch m;

	if ( regex_match( s, m, reg ) )
	{
		cout << "YES" << endl;
	}
	else
	{
		cout << "NO" << endl;
	}
*/
/*
	cout << setprecision( 16 );
	cout << fixed << setprecision( 16 );
*/

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

int main()
{
	int N, A;
	cin >> N >> A;
	vector<int> v( N );
	rep( i, N ) cin >> v[i];
	sort( ALL( v ) );

	const int X = max( v.back(), A );
	vector<vector<vector<int>>> dp( N + 1, vector<vector<int>>( N + 1, vector<int>( N * X + 1, 0 ) ) );
	dp[0][0][0] = 1;

	reps( j, N )
	{
		rep( k, N + 1 )
		{
			rep( s, N * X + 1 )
			{
				if ( s < v[j - 1] ) dp[j][k][s] = dp[j - 1][k][s];
				else if ( k >= 1 ) dp[j][k][s] = dp[j - 1][k][s] + dp[j - 1][k - 1][s - v[j - 1]];
			}
		}
	}

	ll cnt = 0;
	rep( j, N + 1 )
	{
		cnt += dp[N][j][j * A];
	}

	cout << cnt << endl;

	return ( 0 );
}

Score : 300 points

Problem Statement

Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i.
He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A.
In how many ways can he make his selection?

Constraints

1 \leq N \leq 50

1 \leq A \leq 50

1 \leq x_i \leq 50

N,\,A,\,x_i are integers.

Partial Score

200 points will be awarded for passing the test set satisfying 1 \leq N \leq 16.

Input

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

N A
x_1 x_2 ... x_N

Output

Print the number of ways to select cards such that the average of the written integers is exactly A.

Sample Input 1

4 8
7 9 8 9

Sample Output 1

5

The following are the 5 ways to select cards such that the average is 8:

Select the 3-rd card.

Select the 1-st and 2-nd cards.

Select the 1-st and 4-th cards.

Select the 1-st, 2-nd and 3-rd cards.

Select the 1-st, 3-rd and 4-th cards.

Sample Input 2

3 8
6 6 9

Sample Output 2

0

Sample Input 3

8 5
3 6 2 8 7 6 5 9

Sample Output 3

19

Sample Input 4

33 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

Sample Output 4

8589934591

The answer may not fit into a 32-bit integer.


#include <bits/stdc++.h>

#define NREP() for(ll i = 0; i < n; i++)
#define MREP() for(ll j = 0; j < m; j++)
#define REP(i, x, y) for(ll i = x; i < y; i++)
#define ALL(x) (x).begin(), (x).end()
#define MSG(x) cout << x << endl;
#define MSGF(x, n) MSG(fixed << setprecision(n) << x)
#define END(x) cout << x << endl; exit(0);
#define IPT(v, n) REP(i, 0, n){ cin >> v[i]; }
#define YN(x) x ? cout << "YES" << endl : cout << "NO" << endl;
#define Yn(x) x ? cout << "Yes" << endl : cout << "No" << endl;
#define yn(x) x ? cout << "yes" << endl : cout << "no" << endl;

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;

const static ll MOD = 1e9 + 7;
const static ll INF = 1 << 30;

ll a, b, c, d, k, l, m, n, h, w, q, x, y;
string s, t;

ll ans = 0;
ll sum = 0;
ll cnt = 0;
ll tmp = 0;
ll mini = INF;
ll maxi = 0;

double ave = 0.0;

int main() {
    cin >> n >> a;
    vl x(n);
    NREP() {
        cin >> x[i];
    }

    for (ll bit = 0; bit < (1 << n); ++bit) {
        vector<int> s;
        for (int i = 0; i < n; ++i) {
            if (bit & (1 << i)) {
                s.push_back(i);
            }
        }

        sum = 0;
        cnt = 0;
        for (auto &&i : s) {
            sum += x[i];
            cnt++;
        }
        if (((double) sum / cnt) == a) ans++;
    }

    MSG(ans)
}

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

Submitted source-code file size, in bytes.

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

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


/*#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll f (ll b , ll n){
    if (n < b) return n;
    return f(b , n / b) + (n % b);
}
int main()
{
    ios::sync_with_stdio(false);
    ll n , s; cin >> n >> s;
    if (n == s) {
        cout << n + 1 << endl;
        return 0;
    }
    ll ans = -1;
    for (ll i = 2 ; i * i <= n ; i++){
        if (f(i , n) == s) {
            ans = i;
            break;
        }
    }
    if (ans != -1){
        cout << ans << endl;
        return 0;
    }
    for (ll i = 1 ; i * i <= n - s ; i++){
        if (n % i == 0){
            ll b = (n - s) / i + 1;
            if (b <= n / b) continue;
            if (b < 2) continue;
            ll k = n / b , r = n % b;
            if (s != r + k) continue;
            if (ans == -1 || ans > b) ans = b;
        }
    }
    cout << ans << endl;
    return 0;
}*/

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,s;
bool check(ll x){
	if(n/x>=x)return 0;
	if(x<2)return 0;
	ll k=n/x;ll r=n%x;
	if(s==k+r)return 1;
	return 0;
}
ll js(ll x,ll b){
	ll ans=0;
	while(x){
		ans+=x%b;
		x/=b;
	}
	return ans;
}
int main()
{
	//freopen("H:\\c++1\\in.txt","r",stdin);
	//freopen("H:\\c++1\\out.txt","w",stdout);
	cin>>n>>s;
	ll b=1e18;
	if(n==s)b=n+1;
	for(ll t=2;t * t <= n;t++){
		ll cnt=js(n,t);
		if(cnt==s){
			b=min(b,t);
			break;
		}
	}
	if (b != 1e18){
        cout << b << endl;
        return 0;
	}
	if(n>s){
		ll temp=n-s;
		for(ll t=1;t<=sqrt(temp);t++){
			if(temp%t==0){
				ll x=t+1,y=temp/t+1;
				if(check(y))b=min(b,y);
			}
		}
	}
	if(b==1e18)printf("-1\n");
	else printf("%lld\n",b);
	return 0;
}

/*
82914867733
1676425945
*/

Score : 500 points

Problem Statement

For integers b (b \geq 2) and n (n \geq 1), let the function f(b,n) be defined as follows:

f(b,n) = n, when n < b

f(b,n) = f(b,\,{\rm floor}(n / b)) + (n \ {\rm mod} \ b), when n \geq b

Here, {\rm floor}(n / b) denotes the largest integer not exceeding n / b,
and n \ {\rm mod} \ b denotes the remainder of n divided by b.

Less formally, f(b,n) is equal to the sum of the digits of n written in base b.
For example, the following hold:

f(10,\,87654)=8+7+6+5+4=30

f(100,\,87654)=8+76+54=138

You are given integers n and s.
Determine if there exists an integer b (b \geq 2) such that f(b,n)=s.
If the answer is positive, also find the smallest such b.

Constraints

1 \leq n \leq 10^{11}

1 \leq s \leq 10^{11}

n,\,s are integers.

Input

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

n
s

Output

If there exists an integer b (b \geq 2) such that f(b,n)=s, print the smallest such b.
If such b does not exist, print -1 instead.

Sample Input 1

87654
30

Sample Output 1

10

Sample Input 2

87654
138

Sample Output 2

100

Sample Input 3

87654
45678

Sample Output 3

-1

Sample Input 4

31415926535
1

Sample Output 4

31415926535

Sample Input 5

1
31415926535

Sample Output 5

-1


#include<bits/stdc++.h>
using namespace std;
string a;
int n,m,x,y;
int main(){
    cin>>a;
    for(int i=0;i<a.size();i++){
        if(a[i]=='N') n++;
        if(a[i]=='S') m++;
        if(a[i]=='W') x++;
        if(a[i]=='E') y++;
    }
    if(!n && m || n && !m) cout<<"No"<<endl;
    else if(!x && y || x && !y) cout<<"No"<<endl;
    else cout<<"Yes"<<endl;
    return 0;
}

Score : 200 points

Problem Statement

Snuke lives on an infinite two-dimensional plane. He is going on an N-day trip.
At the beginning of Day 1, he is at home. His plan is described in a string S of length N.
On Day i(1 ≦ i ≦ N), he will travel a positive distance in the following direction:

North if the i-th letter of S is N

West if the i-th letter of S is W

South if the i-th letter of S is S

East if the i-th letter of S is E

He has not decided each day's travel distance. Determine whether it is possible to set each day's travel distance so that he will be back at home at the end of Day N.

Constraints

1 ≦ | S | ≦ 1000

S consists of the letters N, W, S, E.

Input

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

S

Output

Print Yes if it is possible to set each day's travel distance so that he will be back at home at the end of Day N. Otherwise, print No.

Sample Input 1

SENW

Sample Output 1

Yes

If Snuke travels a distance of 1 on each day, he will be back at home at the end of day 4.

Sample Input 2

NSNNSNSN

Sample Output 2

Yes

Sample Input 3

NNEW

Sample Output 3

No

Sample Input 4

W

Sample Output 4

No


#include <bits/stdc++.h>
using namespace std;
int n,a[100005];
long long ans;
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    	cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        ans+=a[i]/2;
        a[i]%=2;
        if(i<n&&a[i]&&a[i+1])
        {
            ans++;
            a[i]--;
            a[i+1]--;
        }
    }
    cout<<ans;
    return 0;
}

Score : 400 points

Problem Statement

Snuke has a large collection of cards. Each card has an integer between 1 and N, inclusive, written on it.
He has A_i cards with an integer i.

Two cards can form a pair if the absolute value of the difference of the integers written on them is at most 1.

Snuke wants to create the maximum number of pairs from his cards, on the condition that no card should be used in multiple pairs. Find the maximum number of pairs that he can create.

Constraints

1 ≦ N ≦ 10^5

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

All input values are integers.

Input

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

N
A_1
:
A_N

Output

Print the maximum number of pairs that Snuke can create.

Sample Input 1

4
4
0
3
2

Sample Output 1

4

For example, Snuke can create the following four pairs: (1,1),(1,1),(3,4),(3,4).

Sample Input 2

8
2
0
1
6
0
8
2
1

Sample Output 2

9


#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<string>
#include<iomanip>
#include<cstdio>

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define MOD 1000000007
#define MAXS(x,y) (x = max(x,y))
#define MINS(x,y) (x = min(x,y))

using namespace std;

typedef long long int ll;

const ll INF=(ll)1e18;

vector<int> pre, in, post;

int N;

vector<ll> a;

int main(){
  cin >> N;

  a.resize(N+1);
  REP(i,N)cin >> a[i];
  a[N] = 0;

  ll ans = 0;

  REP(i,N){
    if(a[i] == 0)continue;

    if(a[i]%2==0){
      ans += a[i]/2;
    }else{
      ans += a[i]/2;
      if(a[i+1]>0){
        ans += 1;
        a[i+1] -= 1;
      }
    }
  }

  cout << ans << endl;

}


#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN=1e5+10;
const int N=4000;
struct node{
	ll x,y;
};
node a[MAXN];
ll prime[MAXN],s[MAXN],s1[MAXN];
bool p[MAXN];
int n,len;
map<ll,int>mp;
void init()
{
	for (int i=2;i<=N;i++) {
		if (!p[i]) {
			p[i]=1; prime[++len]=i;
		}
		for (int j=1;j<=len&&prime[j]*i<=N;j++) {
			p[prime[j]*i]=1;
			if (i%prime[j]==0) break;
		}
	}
}
ll sqr(ll x){
	return x*x;
}
void pan(ll x,int now){
	ll ans1=1;
	ll ans2=1;
	for (register int i=1;i<=len&&prime[i]*prime[i]<=x;i++)
	if (x%prime[i]==0)
	{
        int sum=0;
        while (x%prime[i]==0) {
        	x/=prime[i]; sum++;
        }
        sum=(sum%3);
        for (int j=1;j<=sum;j++) ans1*=(ll)prime[i];
        for (int j=1;j<=(3-sum)%3;j++) ans2*=(ll)prime[i];
	}
    if (x>1) {
    	ans1*=x;
    	if (sqr(floor(sqrt(x)))==x) {
    		ans2*=floor(sqrt(x));
    	}
    	else if (sqr(ceil(sqrt(x)))==x) {
    		ans2*=ceil(sqrt(x));
    	}
    	else {
    		if (x>MAXN) ans2=0; else ans2*=(x*x);
     	}
    }
    a[now].x=ans1; a[now].y=ans2;
}
bool cmp(node x,node y)
{
	return x.x<y.x;
}
int main()
{
	scanf("%d",&n);
	init();
	for (int i=1;i<=n;i++) {
		scanf("%lld",&s[i]);
		pan(s[i],i);
		mp[a[i].x]++;
	}
	sort(a+1,a+n+1,cmp);
	int ans=0;
	for (int i=1;i<=n;i++)
		if (a[i].x!=1)
			if (a[i].x!=a[i-1].x) {
				ans+=max(mp[a[i].x],mp[a[i].y]);
				mp[a[i].x]=0; mp[a[i].y]=0;
			}
	if (a[1].x==1) ans++;
	cout << ans << endl;
}

Score : 1100 points

Problem Statement

Snuke got positive integers s_1,...,s_N from his mother, as a birthday present. There may be duplicate elements.

He will circle some of these N integers. Since he dislikes cubic numbers, he wants to ensure that if both s_i and s_j (i ≠ j) are circled, the product s_is_j is not cubic. For example, when s_1=1,s_2=1,s_3=2,s_4=4, it is not possible to circle both s_1 and s_2 at the same time. It is not possible to circle both s_3 and s_4 at the same time, either.

Find the maximum number of integers that Snuke can circle.

Constraints

1 ≦ N ≦ 10^5

1 ≦ s_i ≦ 10^{10}

All input values are integers.

Input

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

N
s_1
:
s_N

Output

Print the maximum number of integers that Snuke can circle.

Sample Input 1

8
1
2
3
4
5
6
7
8

Sample Output 1

6

Snuke can circle 1,2,3,5,6,7.

Sample Input 2

6
2
4
8
16
32
64

Sample Output 2

3

Sample Input 3

10
1
10
100
1000000007
10000000000
1000000009
999999999
999
999
999

Sample Output 3

9


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>

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

#include <functional>
#include <cassert>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007 //1e9+7(prime number)
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define SIZE 100010

const int MAX_P = 4010; //max
bool prime[MAX_P+1];

void Eratosthenes(void){
  for(int i=2;i<=MAX_P;i++)
    prime[i]=true;

  for(int i=2;i*i<=MAX_P;i++)
    if(prime[i])
      for(int j=i;i*j<=MAX_P;j++)
        prime[i*j]=false;
}

int main(){
  int n;
  ll in;
  vector<ll> s;
  vector<ll> prime_v,prime_v3;
  map<ll,int> mm;
  map<ll,ll> sq;

  Eratosthenes();

  for(ll i=2;i<=100010;i++){
    sq[i*i] = i;
  }

  for(ll i=2;i<4010;i++){
    if(prime[i]) prime_v.push_back(i);
    if(prime[i]) prime_v3.push_back(i*i*i);
  }

  scanf("%d",&n);

  for(int i=0;i<n;i++){
    scanf("%lld",&in);
    s.push_back(in);
  }

  for(int i=0;i<n;i++){
    ll t = s[i];

    for(int j=0;j<prime_v3.size();j++){
      if(t < prime_v3[j]) break;

      while(t%prime_v3[j]==0){
        t /= prime_v3[j];
      }
    }

    s[i] = t;
  }

  for(int i=0;i<n;i++){
    mm[s[i]]++;
  }

  sort(s.begin(),s.end());
  s.erase(unique(s.begin(),s.end()),s.end());

  n = s.size();

  int ans = 0;

  for(int i=0;i<n;i++){
    int f[1000] = {};

    ll t = s[i];

    for(int j=0;j<prime_v.size();j++){
      if(t%(prime_v[j]*prime_v[j])==0){
        t/=(prime_v[j]*prime_v[j]);
        f[j] = 2;
      }else if(t%prime_v[j]==0){
        t /= prime_v[j];
        f[j] = 1;
      }else{
        f[j] = 0;
      }
    }

    ll q = 1;

    auto it = sq.find(t);

    if(it != sq.end()){
      q = it->second;
    }else if(t > 1){
      if(t > 100000){
        ans += mm[s[i]];
        continue;
      }

      q = t*t;
    }

    for(int j=0;j<prime_v.size();j++){
      if(f[j]==1)
        q *= (ll) prime_v[j] * prime_v[j];
      else if(f[j]==2)
        q *= prime_v[j];
    }

    if(mm[q]==0){
      ans += mm[s[i]];
    }else if(s[i] > q){
      ans += max(mm[s[i]],mm[q]);
    }

  }

  if(mm[1] > 0) ans ++;

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

  return 0;
}


#include<bits/stdc++.h>
#define fr(i,n) for(int i=0;i<(n);++i)
#define foor(i,a,b) for(int i=(a);i<=(b);++i)
#define rf(i,n) for(int i=(n);i--;)
#define roof(i,b,a) for(int i=(b);i>=(a);--i)
#define elsif else if
#define all(x) x.begin(),x.end()
#define Sort(x) sort(all(x))
#define Reverse(x) reverse(all(x))
#define PQ priority_queue
#define NP(x) next_permutation(all(x))
#define M_PI 3.14159265358979323846
#define popcount __builtin_popcount
using namespace std;            typedef vector<bool> vb; typedef vector<vb>  vvb;
                                typedef vector<int>  vi; typedef vector<vi>  vvi;
typedef long long ll;           typedef vector< ll>  vl; typedef vector<vl>  vvl;
typedef unsigned long long ull; typedef vector<ull>  vu; typedef vector<vu>  vvu;
typedef double dbl;             typedef vector<dbl>  vd; typedef vector<vd>  vvd;
typedef string str;             typedef vector<str>  vs; typedef vector<vs>  vvs;
typedef pair<int,int>pii;       typedef vector<pii>vpii; typedef map<int,int>mii;
typedef pair< ll, ll>pll;       typedef vector<pll>vpll; typedef map< ll, ll>mll;
typedef pair<dbl,dbl>pdd;       typedef vector<pdd>vpdd; typedef map<dbl,dbl>mdd;
typedef pair<str,str>pss;       typedef vector<pss>vpss; typedef map<str,str>mss;
typedef pair<int, ll>pil;       typedef vector<pil>vpil; typedef map<int, ll>mil;
typedef pair< ll,int>pli;       typedef vector<pli>vpli; typedef map< ll,int>mli;
typedef pair<dbl,int>pdi;       typedef vector<pdi>vpdi; typedef map<dbl,int>mdi;
template<typename T>vector<T>&operator<<(vector<T>&v,const T t){v.push_back(t);return v;}
template<typename T>multiset<T>&operator<<(multiset<T>&m,const T t){m.insert(t);return m;}
template<typename T>set<T>&operator<<(set<T>&s,const T t){s.insert(t);return s;}
template<typename T>stack<T>&operator<<(stack<T>&s,const T t){s.push(t);return s;}
template<typename T>stack<T>&operator>>(stack<T>&s,T&t){t=s.top();s.pop();return s;}
template<typename T>queue<T>&operator<<(queue<T>&q,const T t){q.push(t);return q;}
template<typename T>queue<T>&operator>>(queue<T>&q,T&t){t=q.front();q.pop();return q;}
template<typename T,typename U>PQ<T,vector<T>,U>&operator<<(PQ<T,vector<T>,U>&q,const T t){q.push(t);return q;}
template<typename T,typename U>PQ<T,vector<T>,U>&operator>>(PQ<T,vector<T>,U>&q,T&t){t=q.top();q.pop();return q;}
template<typename T,typename U>istream&operator>>(istream&s,pair<T,U>&p){return s>>p.first>>p.second;}
template<typename T>istream&operator>>(istream&s,vector<T>&v){fr(i,v.size()){s>>v[i];}return s;}
template<typename T,typename U>ostream&operator<<(ostream&s,const pair<T,U>p){return s<<p.first<<" "<<p.second;}
//template<typename T>ostream&operator<<(ostream&s,const vector<T>v){for(auto a:v){s<<a<<endl;}return s;}
template<typename T>ostream&operator<<(ostream&s,const vector<T>v){fr(i,v.size()){i?s<<" "<<v[i]:s<<v[i];}return s;}
template<typename T>ostream&operator<<(ostream&s,const deque<T>d){fr(i,d.size()){i?s<<" "<<d[i]:s<<d[i];}return s;}
template<typename T>_Bit_reference operator&=(_Bit_reference b,T t){return b=b&t;}
template<typename T>_Bit_reference operator^=(_Bit_reference b,T t){return b=b^t;}
template<typename T>_Bit_reference operator|=(_Bit_reference b,T t){return b=b|t;}
template<typename T,typename U>pair<T,U>operator+(pair<T,U>a,pair<T,U>b){return {a.first+b.first,a.second+b.second};}
template<typename T,typename U>pair<T,U>operator-(pair<T,U>a,pair<T,U>b){return {a.first-b.first,a.second-b.second};}
void print(void){cout<<endl;}
template<typename T>void print(T t){cout<<t<<endl;}
template<typename T,typename...U>void print(T&&t,U&&...u){cout<<t<<" ";print(forward<U>(u)...);}
bool YN(bool b){print(b?"YES":"NO");return b;}bool PI(bool b){print(b?"POSSIBLE":"IMPOSSIBLE");return b;}
bool Yn(bool b){print(b?"Yes":"No");return b;}bool Pi(bool b){print(b?"Possible":"Impossible");return b;}
bool yn(bool b){print(b?"yes":"no");return b;}bool pi(bool b){print(b?"possible":"impossible");return b;}
const int MD=1e9+7;
template<typename T>str to_string(const T&n){ostringstream s;s<<n;return s.str();}
template<typename T>T&chmax(T&a,T b){return a=max(a,b);}
template<typename T>T&chmin(T&a,T b){return a=min(a,b);}
template<typename T,typename U>vector<pair<T,U>>dijkstra(const vector<vector<pair<T,U>>>&E,const U s,const T inf){using P=pair<T,U>;vector<P>d;fr(i,E.size()){d<<P{inf,i};}PQ<P,vector<P>,greater<P>>pq;pq<<(d[s]=P{0,s});while(pq.size()){P a=pq.top();pq.pop();U v=a.second;if(d[v].first>=a.first){for(P e:E[v]){if(d[v].first+e.first<d[e.second].first){d[e.second]=P{d[v].first+e.first,v};pq<<P{d[v].first+e.first,e.second};}}}}return d;}
template<typename T,typename U>map<U,pair<T,U>>dijkstra(map<U,vector<pair<T,U>>>E,const U s,const T inf){using P=pair<T,U>;map<U,P>d;for(pair<U,vector<P>>e:E){d[e.first]=P{inf,e.first};}PQ<P,vector<P>,greater<P>>pq;pq<<(d[s]=P{0,s});while(pq.size()){P a=pq.top();pq.pop();U v=a.second;if(d[v].first>=a.first){for(P e:E[v]){if(d[v].first+e.first<d[e.second].first){d[e.second]=P{d[v].first+e.first,v};pq<<P{d[v].first+e.first,e.second};}}}}return d;}
ll maxflow(vector<mil>&E,int s,int t){ll z=0;vi b(E.size(),-1);for(int i=0;;++i){static auto dfs=[&](int v,ll f,auto&dfs)->ll{if(v==t)return f;b[v]=i;for(auto&p:E[v]){if(b[p.first]<i&&p.second){if(ll r=dfs(p.first,min(f,p.second),dfs)){p.second-=r;E[p.first][v]+=r;return r;}}}return 0;};ll x=dfs(s,ll(1e18),dfs);z+=x;if(x==0)return z;}}
template<typename T>T distsq(pair<T,T>a,pair<T,T>b){return (a.first-b.first)*(a.first-b.first)+(a.second-b.second)*(a.second-b.second);}
template<typename T>T max(const vector<T>a){T m=a[0];for(T e:a){m=max(m,e);}return m;}
template<typename T>T min(const vector<T>a){T m=a[0];for(T e:a){m=min(m,e);}return m;}
template<typename T>T gcd(const T a,const T b){return a?gcd(b%a,a):b;}
template<typename T>T gcd(const vector<T>a){T g=a[0];for(T e:a){g=gcd(g,e);}return g;}
template<typename T>vector<T>LIS(const vector<T>A){vector<T>B;for(T a:A){auto it=lower_bound(all(B),a);if(it==B.end()){B<<a;}else{*it=a;}}return B;}
template<typename T>vector<T>LCS(vector<T>A,vector<T>B){int N=A.size(),M=B.size();vector<vector<pair<int,pii>>>d(N+1,vector<pair<int,pii>>(M+1));fr(i,N){fr(j,M){if(A[i]==B[j]){d[i+1][j+1]={d[i][j].first+1,{i,j}};}else{d[i+1][j+1]=max(d[i][j+1],d[i+1][j]);}}}vector<T>r;for(pii p={N,M};d[p.first][p.second].first;p=d[p.first][p.second].second){r<<A[d[p.first][p.second].second.first];}Reverse(r);return r;}
str LCS(str S,str T){vector<char>s=LCS(vector<char>(S.begin(),S.end()),vector<char>(T.begin(),T.end()));return str(s.begin(),s.end());}
template<typename T>vector<pair<T,T>>ConvexHull(vector<pair<T,T>>V){if(V.size()<=3){return V;}Sort(V);rf(i,V.size()-1)V<<V[i];vector<pair<T,T>>r;for(pair<T,T>p:V){int s=r.size();while(s>=2&&(p.second-r[s-1].second)*(p.first-r[s-2].first)<(p.second-r[s-2].second)*(p.first-r[s-1].first)){r.pop_back();--s;}r<<p;}r.pop_back();return r;}
class UnionFind{vi p,s;void extend(int N){foor(i,p.size(),N){p<<i;s<<1;}}public:UnionFind(void){}UnionFind(int N){extend(N-1);}int find(int i){extend(i);return p[i]=p[i]==i?i:find(p[i]);}void unite(int a,int b){extend(a);extend(b);if((a=find(a))!=(b=find(b))){if(s[a]>s[b]){swap(a,b);}s[b]+=s[a];p[a]=b;}}void unite(pii p){return unite(p.first,p.second);}bool same(int a,int b){extend(a);extend(b);return find(a)==find(b);}bool same(pii p){return same(p.first,p.second);}int size(int x){extend(x);return s[find(x)];}};
ll MST(vector<pair<ll,pii>>&E){Sort(E);UnionFind uf;ll z=0;for(auto&e:E){if(!uf.same(e.second)){z+=e.first;uf.unite(e.second);}}return z;}
ll strmod(const str&s,const int m){ll x=0;fr(i,s.size()){x=(x*10+s[i]-48)%m;}return x;}
vvl mul(const vvl&A,const vvl&B,const int m){vvl C;fr(y,A.size()){C<<vl(B[y].size());}fr(y,C.size()){fr(x,C[y].size()){fr(i,A[0].size()){(C[y][x]+=A[y][i]*B[i][x])%=m;}}}return C;}
vvl pow(const vvl&A,const ll n,const int m){vvl B;fr(y,A.size()){B<<vl(A.size());}if(n==0){fr(i,B.size()){B[i][i]=1;}}elsif(n%2){B=mul(A,pow(A,n-1,m),m);}else{vvl C=pow(A,n/2,m);B=mul(C,C,m);}return B;}
ll pow(const ll a,const ll n,const int m){ll t;return n?(n&1?a>=0?a%m:(m-(-a%m))%m:1)*(t=pow(a,n>>1,m),t*t%m)%m:!!a;}
ll inv(const ll x,const int p){return pow(x,p-2,p);}
ll inv(const ll x){return inv(x,MD);}
vpll fact(const int n,const int p){vpll v(n+1);v[0].first=1;foor(i,1,n){v[i].first=v[i-1].first*i%p;}v[n].second=inv(v[n].first,p);roof(i,n,1){v[i-1].second=v[i].second*i%p;}return v;}
class Combination{const vpll f;const int M;public:Combination(int n,int m):f(fact(n,m)),M(m){}Combination(int n):Combination(n,MD){}ll P(int n,int k){return n<0||k<0||n<k?0ll:f[n].first*f[n-k].second%M;}ll C(int n,int k){return P(n,k)*f[k].second%M;}ll H(int n,int k){return n==0&&k==0?1ll:C(n+k-1,k);}};
ll C2(const int n){return(ll)n*~-n/2;}
ll sum(const vi a){ll s=0;for(int e:a){s+=e;}return s;}
ll sum(const vl a){ll s=0;for(ll e:a){s+=e;}return s;}
template<typename T>int MSB(T N){int r=-1;for(;N>0;N/=2){++r;}return r;}
template<typename T>class SegmentTree{vector<T>S;T(*const op)(T a,T b);const T zero;const int B;public:SegmentTree(int N,T(*f)(T a,T b),const T zero):S(1<<MSB(N-1)+2,zero),op(f),zero(zero),B(1<<MSB(N-1)+1){}SegmentTree(vector<T>v,T(*f)(T a,T b),const T zero):SegmentTree(v.size(),f,zero){fr(i,v.size()){S[S.size()/2+i]=v[i];}roof(i,S.size()/2-1,1){S[i]=op(S[i*2],S[i*2+1]);}}T calc(int l,int r){l+=B;r+=B;if(l>r){return zero;}if(l==r){return S[l];}T L=S[l],R=S[r];for(;l/2<r/2;l/=2,r/=2){if(l%2==0){L=op(L,S[l+1]);}if(r%2==1){R=op(S[r-1],R);}}return op(L,R);}void replace(int i,T x){for(S[i+=B]=x;i!=1;i/=2){if(i%2){S[i/2]=op(S[i-1],S[i]);}else{S[i/2]=op(S[i],S[i+1]);}}}void add(int i,T x){replace(i,op(S[B+i],x));}T top(){return S[1];}};
ll BITsum(vl&B,int i){ll z=0;while(i>0){z+=B[i];i-=i&-i;}return z;}
void BITadd(vl&B,int i,ll x){while(i<B.size()){B[i]+=x;i+=i&-i;}}
ll fib(const ll n,const int m){ll a,b,c,d,A,B,C,D;a=1;b=0;c=0;d=1;rf(i,63){A=a*a+b*c;B=a*b+b*d;C=c*a+d*c;D=c*b+d*d;if(n>>i&1){a=A;b=B;c=C;d=D;A=a+b;B=a;C=c+d;D=c;}a=A%m;b=B%m;c=C%m;d=D%m;}return b;}
vi primes(int n){vb b(n+1);vi p;foor(i,2,n){if(!b[i]){p<<i;for(int j=2*i;j<=n;j+=i){b[j]=true;}}}return p;}
vb isprime(const int n){vb v(n+1,true);v[0]=v[1]=false;foor(i,2,n){if(v[i]){for(int j=2*i;j<=n;j+=i){v[j]=false;}}}return v;}

int main(){cin.tie(0);ios::sync_with_stdio(false);
	int N;str S;cin>>N>>S;
	vvl d(N+1,vl(N+1));
	d[0][0]=1;
	fr(i,N){
		(d[i+1][1]+=2*d[i][0])%=MD;
		(d[i+1][0]+=d[i][0])%=MD;
		foor(j,1,i){
			(d[i+1][j+1]+=2*d[i][j])%=MD;
			(d[i+1][j-1]+=d[i][j])%=MD;
		}
	}
	print(d[N][S.size()]*inv(pow(2,S.size(),MD))%MD);
	return 0;
}

Score : 800 points

Problem Statement

Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard only has 3 keys on it: the 0 key, the 1 key and the backspace key.

To begin with, he is using a plain text editor with this keyboard. This editor always displays one string (possibly empty). Just after the editor is launched, this string is empty. When each key on the keyboard is pressed, the following changes occur to the string:

The 0 key: a letter 0 will be inserted to the right of the string.

The 1 key: a letter 1 will be inserted to the right of the string.

The backspace key: if the string is empty, nothing happens. Otherwise, the rightmost letter of the string is deleted.

Sig has launched the editor, and pressed these keys N times in total. As a result, the editor displays a string s. Find the number of such ways to press the keys, modulo 10^9 + 7.

Constraints

1 ≦ N ≦ 5000

1 ≦ |s| ≦ N

s consists of the letters 0 and 1.

Partial Score

400 points will be awarded for passing the test set satisfying 1 ≦ N ≦ 300.

Input

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

N
s

Output

Print the number of the ways to press the keys N times in total such that the editor displays the string s in the end, modulo 10^9+7.

Sample Input 1

3
0

Sample Output 1

5

We will denote the backspace key by B. The following 5 ways to press the keys will cause the editor to display the string 0 in the end: 00B, 01B, 0B0, 1B0, BB0. In the last way, nothing will happen when the backspace key is pressed.

Sample Input 2

300
1100100

Sample Output 2

519054663

Sample Input 3

5000
01000001011101000100001101101111011001000110010101110010000

Sample Output 3

500886057


#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#define MOD 1000000007LL
using namespace std;

typedef long long ll;

ll mod_pow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1)res=res*x%MOD;
		x=x*x%MOD;
		n>>=1;
	}
	return res;
}

int n;
string s;
ll dp[5001][5001];

int main(void){
	scanf("%d",&n);
	cin >> s;
	dp[0][0]=1;
	for(int i=0;i<n;i++){
		for(int j=0;j<=i;j++){
			dp[i+1][j+1]=(dp[i+1][j+1]+(dp[i][j]*2LL)%MOD)%MOD;
			dp[i+1][max(j-1,0)]=(dp[i+1][max(j-1,0)]+dp[i][j])%MOD;
		}
	}
	int len=s.size();
	ll res=dp[n][len];
	res=res*mod_pow((MOD+1LL)/2LL,len)%MOD;
	printf("%lld\n",res);
	return 0;
}


#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
typedef  long long ll;
ll gcd(ll x,ll y){
   if(y==0) return x;
   else return gcd(y,x%y);
}
ll lcm(ll x,ll y){
   return x/gcd(x,y)*y;
}
using namespace std;

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

   rep(i,s.size()){
      if(s[i]=='0') ans+='0';
      else if(s[i]=='1') ans+='1';
      else {
         if(ans.size()==0) continue;
         else ans.pop_back();
      }
   }
   cout<<ans;

return 0;
}

Score : 200 points

Problem Statement

Sig has built his own keyboard. Designed for ultimate simplicity, this keyboard only has 3 keys on it: the 0 key, the 1 key and the backspace key.

To begin with, he is using a plain text editor with this keyboard. This editor always displays one string (possibly empty). Just after the editor is launched, this string is empty. When each key on the keyboard is pressed, the following changes occur to the string:

The 0 key: a letter 0 will be inserted to the right of the string.

The 1 key: a letter 1 will be inserted to the right of the string.

The backspace key: if the string is empty, nothing happens. Otherwise, the rightmost letter of the string is deleted.

Sig has launched the editor, and pressed these keys several times. You are given a string s, which is a record of his keystrokes in order. In this string, the letter 0 stands for the 0 key, the letter 1 stands for the 1 key and the letter B stands for the backspace key. What string is displayed in the editor now?

Constraints

1 ≦ |s| ≦ 10 (|s| denotes the length of s)

s consists of the letters 0, 1 and B.

The correct answer is not an empty string.

Input

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

s

Output

Print the string displayed in the editor in the end.

Sample Input 1

01B0

Sample Output 1

00

Each time the key is pressed, the string in the editor will change as follows: 0, 01, 0, 00.

Sample Input 2

0BB1

Sample Output 2

1

Each time the key is pressed, the string in the editor will change as follows: 0, (empty), (empty), 1.


#include<bits/stdc++.h>
using namespace std;
vector<char> con;
int main()
{
	string a;
	cin>>a;
	for(int i=0;i<a.size();i++)
	{
		if(a[i]=='0')
		{
			con.push_back('0');
			continue;
		}
	    if(a[i]=='1')
	    {
	    	con.push_back('1');
	    	continue;
	    }
	    if(con.size()!=0&&a[i]=='B')
	        con.pop_back();
	}
	for(int i=0;i<con.size();i++)
	    cout<<con[i];
	cout<<endl;
	return 0;
}


#include <bits/stdc++.h>

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rrep(i, n) for(int i = ((int)(n)-1); i >= 0; i--)
#define all(x) (x).begin(),(x).end()

using namespace std;
using ll = long long;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> P;

const int INF = 1e9;
//const ll INF = 1e18;
const double EPS = 1e-10;
const int MOD = 1e9+7;
const double PI = acos(-1.0);

int main()
{
	string s;
	cin >> s;
	if(s.size() == 2 && s[0] == s[1]){
		cout << "1 2" << endl;
		return 0;
	}
	if(s.size() >= 3 && s[0] == s[1]){
		cout << "1 3" << endl;
		return 0;
	}
	for(int i = 2; i < s.size(); i++){
		if(s[i-1] == s[i]){
			cout << i-1 << ' ' << i+1 << endl;
			return 0;
		}
	}
	for(int i = 1; i < s.size()-1; i++){
		if(s[i-1] == s[i+1]){
			cout << i << ' ' << i+2 << endl;
			return 0;
		}
	}
	cout << "-1 -1" << endl;
	return 0;
}

Score : 400 points

Problem Statement

Given a string t, we will call it unbalanced if and only if the length of t is at least 2, and more than half of the letters in t are the same. For example, both voodoo and melee are unbalanced, while neither noon nor a is.

You are given a string s consisting of lowercase letters. Determine if there exists a (contiguous) substring of s that is unbalanced. If the answer is positive, show a position where such a substring occurs in s.

Constraints

2 ≦ |s| ≦ 10^5

s consists of lowercase letters.

Partial Score

200 points will be awarded for passing the test set satisfying 2 ≦ N ≦ 100.

Input

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

s

Output

If there exists no unbalanced substring of s, print -1 -1.

If there exists an unbalanced substring of s, let one such substring be s_a s_{a+1} ... s_{b} (1 ≦ a < b ≦ |s|), and print a b. If there exists more than one such substring, any of them will be accepted.

Sample Input 1

needed

Sample Output 1

2 5

The string s_2 s_3 s_4 s_5 = eede is unbalanced. There are also other unbalanced substrings. For example, the output 2 6 will also be accepted.

Sample Input 2

atcoder

Sample Output 2

-1 -1

The string atcoder contains no unbalanced substring.


#include <bits/stdc++.h>
#define int long long
#define mod (int)(1e9+7)
#define inf (int)(3e18+7)
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<n;i++)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(v) v.begin(),v.end()
#define mkp make_pair
#define mkt make_tuple
#define prique(T) priority_queue<T,vector<T>,greater<T>>
#define vecunique(vec) sort(vec.begin(), vec.end());decltype(vec)::iterator result = std::unique(vec.begin(), vec.end());vec.erase(result, vec.end())
using namespace std;

bool prime(int x) {
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0)return false;
	}
	return x > 1;
}
int gcd(int x, int y) {
	if (y == 0)return x;
	return gcd(y, x % y);
}
int lcm(int x, int y) {
	return x / gcd(x, y) * y;
}
int kai(int x, int y) {
	int res = 1;
	for (int i = x - y + 1; i <= x; i++) {
		res *= i; res %= mod;
	}
	return res;
}
int mod_pow(int x, int y) {
	int res = 1;
	while (y > 0) {
		if (y & 1) {
			res = res * x % mod;
		}
		x = x * x % mod;
		y >>= 1;
	}
	return res;
}
int comb(int x, int y) {
	if (y > x)return 0;
	return kai(x, y) * mod_pow(kai(y, y), mod - 2) % mod;
}
/*--------Library Zone!--------*/

int n, a[114514], l;
signed main() {
	cin >> n >> l;
	rep(i, n)cin >> a[i];
	int memo;
	rep(i, n - 1) {
		if (a[i] + a[i + 1] >= l) {
			memo = i;
			goto ok;
		}
	}
	cout << "Impossible" << endl;
	return 0;
ok:;
	cout << "Possible" << endl;
	rep(i, memo)cout << i + 1 << endl;
	for (int i = n - 2; i > memo; i--)cout << i + 1 << endl;
	cout << memo + 1 << endl;
}

Problem Statement

We have N pieces of ropes, numbered 1 through N. The length of piece i is a_i.

At first, for each i (1≤i≤N-1), piece i and piece i+1 are tied at the ends, forming one long rope with N-1 knots. Snuke will try to untie all of the knots by performing the following operation repeatedly:

Choose a (connected) rope with a total length of at least L, then untie one of its knots.

Is it possible to untie all of the N-1 knots by properly applying this operation? If the answer is positive, find one possible order to untie the knots.

Constraints

2≤N≤10^5

1≤L≤10^9

1≤a_i≤10^9

All input values are integers.

Input

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

N L
a_1 a_2 ... a_n

Output

If it is not possible to untie all of the N-1 knots, print Impossible.

If it is possible to untie all of the knots, print Possible, then print another N-1 lines that describe a possible order to untie the knots. The j-th of those N-1 lines should contain the index of the knot that is untied in the j-th operation. Here, the index of the knot connecting piece i and piece i+1 is i.

If there is more than one solution, output any.

Sample Input 1

3 50
30 20 10

Sample Output 1

Possible
2
1

If the knot 1 is untied first, the knot 2 will become impossible to untie.

Sample Input 2

2 21
10 10

Sample Output 2

Impossible

Sample Input 3

5 50
10 20 30 40 50

Sample Output 3

Possible
1
2
3
4

Another example of a possible solution is 3, 4, 1, 2.


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

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n; cin >> n;
  V<> a(n); for (auto&& e : a) cin >> e;
  sort(rbegin(a), rend(a));
  a.push_back(0);
  int x = 0;
  while (true) {
    int y = upper_bound(begin(a), end(a), a[x], greater<int>()) - begin(a);
    if (a[y] - 1 <= x and x < a[x]) break;
    ++x;
  }
  bool res;
  if (x >= a[x + 1]) {
    res = !(a[x] - x & 1);
  } else if (x == a[x + 1] - 1) {
    bool f = !(a[x] - x & 1);
    int y = upper_bound(begin(a), end(a), a[x + 1], greater<int>()) - begin(a);
    bool g = !(y - x & 1);
    res = f or g;
  } else {
    int y = upper_bound(begin(a), end(a), a[x], greater<int>()) - begin(a);
    res = !(y - x & 1);
  }
  cout << (res ? "First" : "Second") << '\n';
}

Problem Statement

There are N piles of candies on the table. The piles are numbered 1 through N. At first, pile i contains a_i candies.

Snuke and Ciel are playing a game. They take alternating turns. Snuke goes first. In each turn, the current player must perform one of the following two operations:

Choose a pile with the largest number of candies remaining, then eat all candies of that pile.

From each pile with one or more candies remaining, eat one candy.

The player who eats the last candy on the table, loses the game. Determine which player will win if both players play the game optimally.

Constraints

1≤N≤10^5

1≤a_i≤10^9

Input

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

N
a_1 a_2 … a_N

Output

If Snuke will win, print First. If Ciel will win, print Second.

Sample Input 1

2
1 3

Sample Output 1

First

At the beginning of the game, pile 2 contains the most candies. If Snuke eats all candies of this pile, Ciel has no choice but to eat the last candy.

Sample Input 2

3
1 2 1

Sample Output 2

First

If Snuke eats one candy from each pile, Ciel is again left with the last candy.

Sample Input 3

3
1 2 3

Sample Output 3

Second


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

bool a[11];

bool f(int n){
  int m = n;
  int ind;
  while(m > 0){
    ind = m % 10;
    m /= 10;
    if(!a[ind]) return true;
  }
  return false;
}

int main(void){
  for(int i = 0; i < 11; i++) a[i] = true;
  int n, k, temp;
  cin >> n >> k;
  for(int i = 0; i < k; i++){
    cin >> temp;
    a[temp] = false;
  }
  bool flag = true;
  temp = n - 1;
  while(flag){
    temp++;
    flag = f(temp);
  }
  cout << temp << endl;
  return 0;
}

Score : 300 points

Problem Statement

Iroha is very particular about numbers. There are K digits that she dislikes: D_1, D_2, ..., D_K.

She is shopping, and now paying at the cashier.
Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change).

However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money.

Find the amount of money that she will hand to the cashier.

Constraints

1 ≦ N < 10000

1 ≦ K < 10

0 ≦ D_1 < D_2 < … < D_K≦9

\{D_1,D_2,...,D_K\} ≠ \{1,2,3,4,5,6,7,8,9\}

Input

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

N K
D_1 D_2 … D_K

Output

Print the amount of money that Iroha will hand to the cashier.

Sample Input 1

1000 8
1 3 4 5 6 7 8 9

Sample Output 1

2000

She dislikes all digits except 0 and 2.

The smallest integer equal to or greater than N=1000 whose decimal notation contains only 0 and 2, is 2000.

Sample Input 2

9999 1
0

Sample Output 2

9999


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

const int MAXN = 300 + 21;
int a[MAXN];
long long ans;
int main() {
 int n;
  cin >> n;
  for (int i = 0; i < 2 * n; i++)
  		cin >> a[i];
  sort(a, a + (2 * n));
  for (int i = 0; i < 2 * n; i++) {
   	ans += a[i];
  }
  cout << ans << '\n';
  return 0;
}

Score : 200 points

Problem Statement

Snuke is having a barbeque party.

At the party, he will make N servings of Skewer Meal.

Example of a serving of Skewer Meal

He has a stock of 2N skewers, all of which will be used in Skewer Meal. The length of the i-th skewer is L_i.
Also, he has an infinite supply of ingredients.

To make a serving of Skewer Meal, he picks 2 skewers and threads ingredients onto those skewers.
Let the length of the shorter skewer be x, then the serving can hold the maximum of x ingredients.

What is the maximum total number of ingredients that his N servings of Skewer Meal can hold, if he uses the skewers optimally?

Constraints

1≦N≦100

1≦L_i≦100

For each i, L_i is an integer.

Input

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

N
L_1 L_2 ... L_{2N}

Output

Print the maximum total number of ingredients that Snuke's N servings of Skewer Meal can hold.

Sample Input 1

2
1 3 1 2

Sample Output 1

3

If he makes a serving using the first and third skewers, and another using the second and fourth skewers, each serving will hold 1 and 2 ingredients, for the total of 3.

Sample Input 2

5
100 1 2 3 14 15 58 58 58 29

Sample Output 2

135


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t,i,j;
    while(cin >> t){
            int arr[110];
        for( i =0; i<2*t; i++){
            cin >> arr[i];
        }
        sort(arr,arr+i);
        int s=0;
        for(j=0; j<2*t; j++){
            if(j%2==0)
                s+=arr[j];
        }
        cout << s<<endl;
    }
    return 0;
}


#ifdef CLION
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
//#define int long long

using namespace __gnu_pbds;
using namespace std;

using ll = long long;
#define int ll
template<typename T>
using oset = tree<
        T,
        null_type,
        less<T>,
        rb_tree_tag,
        tree_order_statistics_node_update>;

#define rep(i, n) for (int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define chkmx(a, b) (a) = max((a), (b))
#define chkmn(a, b) (a) = min((a), (b))

constexpr int MAXN = 3e3;

vector<int> g[MAXN];

int cnt = 0;
int max_deg = 0;
int k;
void dfs(int v, int p, int dep) {
    cnt++;
    if (dep == (k) / 2 && k % 2 == 0) return;
    if (dep == (k) / 2) {
        max_deg = max(max_deg, (int)g[v].size() - (p != v));
        return;
    }
    for (auto u : g[v]) {
        if (u != p)
            dfs(u, v, dep + 1);
    }
}

void run() {
    int n;
    cin >> n >> k;
    if (k == 1) {
        cout << n - 2 << '\n';
    }
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    int ans = n;
    for (int i = 0; i < n; i++) {
        cnt = max_deg = 0;
        dfs(i, i, 0);
        ans = min(ans, n - cnt - max_deg);
    }
    cout << ans << '\n';
}

signed main() {

#ifdef CLION
    freopen("input.txt", "r", stdin);
#else
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif
    int t = 1;
    //cin >> t;
    while (t--)
        run();
    return 0;
}

Score : 600 points

Problem Statement

Given an undirected tree, let the distance between vertices u and v be the
number of edges on the simple path from u to v.
The diameter of a tree is the maximum among the distances between any two vertices.
We will call a tree good if and only if its diameter is at most K.

You are given an undirected tree with N vertices numbered 1 through N.
For each i (1≦i≦N-1), there is an edge connecting vertices A_i and B_i.

You want to remove zero or more vertices from the tree, so that the resulting tree is good.
When a vertex is removed, all incident edges will also be removed.
The resulting graph must be connected.

Find the minimum number of vertices that you need to remove in order to produce a good tree.

Constraints

2≦N≦2000

1≦K≦N-1

1≦A_i≦N, 1≦B_i≦N

The graph defined by A_i and B_i is a tree.

Input

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

N K
A_1 B_1
A_2 B_2
:
A_{N-1} B_{N-1}

Output

Print the minimum number of vertices that you need to remove in order to produce a good tree.

Sample Input 1

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

Sample Output 1

2

The tree is shown below. Removing vertices 5 and 6 will result in a good tree with the diameter of 2.

Sample Input 2

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

Sample Output 2

0

Since the given tree is already good, you do not need to remove any vertex.


#include<bits/stdc++.h>

#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define pf push_front
#define pb2 pop_back
#define pf2 pop_front
#define line printf("\n")
#define pq priority_queue
#define rep(k,i,j) for(int k = (int)i;k<(int)j;k++)
#define repd(k,i,j) for(int k = (int)i;k>=(int)j;k--)
#define ll long long
#define ALL(a) a.begin(),a.end()

using namespace std;

double EPS = 1e-9;
int INF = 1e9+7;;
long long INFLL = 1e17;
double pi = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};

clock_t first_attempt = clock();
inline void cek_time(){
	clock_t cur = clock()- first_attempt;
	cerr<<"TIME : "<<(double) cur/CLOCKS_PER_SEC<<endl;
}
inline void OPEN (string s) {
  freopen ((s + ".in").c_str (), "r", stdin);
  freopen ((s + ".out").c_str (), "w", stdout);
}

//end of template

const int maxn = 5e5+5;
int n,K;

int pos[maxn],ans[maxn];
int main(){
	scanf("%d%d",&n,&K);

	rep(k,1,n+1){
		int a;
		scanf("%d",&a);
		pos[a] = k;
	}

	while(1){
		int x = 0;
		rep(k,1,n){
			if(pos[k]<pos[k+1])continue;
			if(abs(pos[k]-pos[k+1])>=K)swap(pos[k],pos[k+1]),x++;
		}
		if(!x)break;
	}

	rep(k,1,n+1)ans[pos[k]] = k;

	rep(k,1,n+1)printf("%d\n",ans[k]);
	return 0;
}

Score : 2000 points

Problem Statement

You are given a permutation P_1 ... P_N of the set {1, 2, ..., N}.

You can apply the following operation to this permutation, any number of times (possibly zero):

Choose two indices i,j (1 ≦ i < j ≦ N), such that j - i ≧ K and |P_i - P_j| = 1.
Then, swap the values of P_i and P_j.

Among all permutations that can be obtained by applying this operation to the given permutation, find the lexicographically smallest one.

Constraints

2≦N≦500,000

1≦K≦N-1

P is a permutation of the set {1, 2, ..., N}.

Input

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

N K
P_1 P_2 ... P_N

Output

Print the lexicographically smallest permutation that can be obtained.

Sample Input 1

4 2
4 2 3 1

Sample Output 1

2
1
4
3

One possible way to obtain the lexicographically smallest permutation is shown below:

4 2 3 1

4 1 3 2

3 1 4 2

2 1 4 3

Sample Input 2

5 1
5 4 3 2 1

Sample Output 2

1
2
3
4
5

Sample Input 3

8 3
4 5 7 8 3 1 2 6

Sample Output 3

1
2
6
7
5
3
4
8