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

int main()
{
    int k, s,z, count=0;
    scanf("%d %d\n",&k,&s) ;
    for(int x=0; x<=k; x++)
    {
        for(int y=0; y<=k; y++)
        {
            z=s-x-y;
            if(z>=0&&z<=k)
            {
                count++;
            }

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

    return 0;
}


#include<iostream>
using namespace std;

int main(){
  int K,S;
  cin >> K >> S;
  int x=0,y,z,expected,ans=0;
  while(x<=K){
    y=0;
    while(y<=K){
      expected = S - x - y;
      for(z=0;z<=K;z++){
        ans += expected == z;
      }
      y++;
    }
    x++;
  }
  cout << ans << endl;
}

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

Score : 200 points

Problem Statement

You are given two integers K and S.

Three variable X, Y and Z takes integer values satisfying 0≤X,Y,Z≤K.

How many different assignments of values to X, Y and Z are there such that X + Y + Z = S?

Constraints

2≤K≤2500

0≤S≤3K

K and S are integers.

Input

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

K S

Output

Print the number of the triples of X, Y and Z that satisfy the condition.

Sample Input 1

2 2

Sample Output 1

6

There are six triples of X, Y and Z that satisfy the condition:

X = 0, Y = 0, Z = 2

X = 0, Y = 2, Z = 0

X = 2, Y = 0, Z = 0

X = 0, Y = 1, Z = 1

X = 1, Y = 0, Z = 1

X = 1, Y = 1, Z = 0

Sample Input 2

5 15

Sample Output 2

1

The maximum value of X + Y + Z is 15, achieved by one triple of X, Y and Z.

Submitted source-code file size, in bytes.

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

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


#include <iostream>
using namespace std;

int k,s;
int jsq=0;
int x,y,z;

int main()
{
	cin>>k>>s;
	for(x=0;x<=s;x++)
	{
		for(y=0;y<=s;y++)
		{
			for(z=0;z<=s;z++)
			{
				if(x+y+z==s&&x<=k&&y<=k&&z<=k)
				{
					jsq++;
				}
			}
		}
	}
	cout<<jsq;
	return 0;
}


#include <iostream>
using namespace std;

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

  int ans = 0;
  for (int x = 0; x <= K; x++) {
    for (int y = 0; y <= K; y++) {
      int z = S - x - y;
      if (0 <= z && z <= K)
        ans++;
    }
  }

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


#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <functional>
#include <stack>
#include <queue>
#include <string>
#include <map>
#include <unordered_map>
#include <bitset>
#include <numeric>
#include <vector>
#include <unordered_set>
#include <bitset>
#include <limits>
#include <random>
#include <complex>
#include <numeric>
#include <cstdlib>

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

const ll mod=1000000007ll;
const ll inf=1000000000000000ll;

int main(){
    ll k,s,ans;
    cin>>k>>s;
    ans=0;
    for(ll i=0;i<=k;i++){
        for(ll j=0;j<=k;j++){
                if(s-(i+j)<=k&&0<=s-(i+j))ans++;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include <iostream>
using namespace std;

int main () {
	int k, s, cnt = 0;
	cin >> k >> s;

	for (int i = 0; i <= k; i++) {
		for (int j = 0; j <= k; j++) {
			int l = s - i - j;
			if (0 <= l && l <= k) cnt++;
		}
	}
	cout << cnt << endl;

	return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main() {
	int k, s, Ans = 0;
	cin >> k >> s;
	for (int i = 0; i <= k; i++) {
		for (int j = 0; i + j <= k; j++) {
			int Tmp = s - (i + j);
			if (i + j + Tmp == s) {
				Ans++;
			}
		}
	}
	cout << Ans << endl;
	return 0;
}


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

int main(){
	int k,s,count=0;
  	cin >> k >> s;
  	for (int x=0;x<=k;x++){
    	for(int y=0;y<=k;y++){
        	for(int z=0;z<=k;z++){
            	if (x+y+z==s) count++;
            }
        }
    }
  	cout << count << endl;
}


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

long long k, s, dp[5][2505*3];

int dfs(int now,int sum)
{
	if(now>3) return !sum;
	if(dp[now][sum]!=-1) return dp[now][sum];
	int i,res=0;
	for(i=0;i<=sum;i++) res+=dfs(now+1,sum-i);
	return dp[now][sum]=res;
}

int main ()
{
	memset(dp,-1,sizeof(dp));
	cin >> k >> s;
	cout<<dfs(1,s);
}


#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <functional>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <list>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL<<29;
const ll mod = 1e9+7;
#define rep(i,n) for(int (i)=0;(i)<(ll)(n);++(i))
#define repd(i,n,d) for(ll (i)=0;(i)<(ll)(n);(i)+=(d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset((m),(v),sizeof(m))
#define chmin(x,y) (x=min(x,y))
#define chmax(x,y) (x=max(x,y))
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)),(x).end())
template<class T> ostream &operator<<(ostream &os, const vector<T> &v){int n=v.size();rep(i,n)os<<v[i]<<(i==n-1?"":" ");return os;}

int main(){
	ll k, s;
  cin>>k>>s;
  ll res = 0;
  for(ll x = 0; x <= k; x++){
	  ll t = s-x;
    if(t>2*k) continue;
    if(t<=k) res += t+1;
    else res += k-(t-k)+1;
  }
  cout<<res<<endl;
	return 0;
}


#include <cstdio>
#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>

using namespace std;

int main(void) {
  int k, s;
  scanf("%d %d", &k, &s);

  int count = 0;

  for(int i = 0; i <= k; i++) {
    for(int j = i; j <= k; j++) {
      for(int l = j; l <= k; l++) {
	if(i + j + l == s) {
	  if( i == j && j == l) {
	    count++;
	  }
	  else if(i == j || j == l || l == i) {
	    count += 3;
	  }
	  else {
	    count += 6;
	  }
	}
      }
    }
  }
  printf("%d\n", count);
}


#include <bits/stdc++.h>

// macro
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define MOD 1000000007
#define ll unsigned long long
#define FOR(i, m, n) for(ll i = m;i < n;i++)

using namespace std;

// Factorial
// need init
ll factorial_memo[5001];
ll factorial(ll n) {
  factorial_memo[0] = 1;
  if(factorial_memo[n] != -1) return factorial_memo[n];
  factorial_memo[n] = (factorial(n-1) * n) % MOD;
  return factorial_memo[n];
}

// Combination(nCr)
// need init
ll nCr_memo[5001][5001];
ll nCr(ll n, ll r) {
  if(r*2 > n) r = n-r;
  if(r == 1) return n;
  if(r == 0) return 1;
  if(nCr_memo[n][r] != -1) return nCr_memo[n][r];
  nCr_memo[n][r] = (nCr(n-1, r) + nCr(n-1, r-1)) % MOD;
  return nCr_memo[n][r];
}

void solve() {
  int K,S;
  cin >> K >> S;
  int cnt = 0;
  REP(i, K+1) {
    REP(j, K+1) {
      if(0 <= S-i-j && S-i-j <= K) cnt++;
    }
  }
  cout << cnt << "\n";
}

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


#pragma region ヘッダー
#define _CRT_SECURE_NO_WARNINGS

#ifdef _____MY_DEBUG_____
#define VS_ENDPAUSE()	(system("pause"))
#define DEBUG_OUT(...)  (printf(__VA_ARGS__))
#else
#define VS_ENDPAUSE()
#define DEBUG_OUT(...)
#endif

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <cmath>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;
typedef signed char int8;
typedef signed short int16;
typedef signed int int32;
typedef signed long long int64;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
#define int int64

#ifndef INT8_MIN
#define INT8_MIN	(-127)
#endif
#ifndef INT8_MAX
#define INT8_MAX	( 128)
#endif
#ifndef UINT8_MAX
#define UINT8_MAX	( 255)
#endif
#ifndef INT16_MIN
#define INT16_MIN	(-32768)
#endif
#ifndef INT16_MAX
#define INT16_MAX	( 32767)
#endif
#ifndef UINT16_MAX
#define UINT16_MAX	( 65535)
#endif
#ifndef INT32_MIN
#define INT32_MIN	(-2147483648)	// -2 * 10^9
#endif
#ifndef INT32_MAX
#define INT32_MAX	( 2147483647)	//  2 * 10^9
#endif
#ifndef UINT32_MAX
#define UINT32_MAX	( 4294967295)	//  4 * 10^9
#endif
#ifndef INT64_MIN
#define INT64_MIN	(-9223372036854775808)	// -9 * 10^18
#endif
#ifndef INT64_MAX
#define INT64_MAX	( 9223372036854775807)	//  9 * 10^18
#endif
#ifndef UINT64_MAX
#define UINT64_MAX	( 18446744073709551615)	// 18 * 10^18
#endif

void func(void);
#pragma endregion

int32 main(void)
{
    func();
    VS_ENDPAUSE();
    return 0;
}

void func(void)
{
//DEBUG_OUT("----\n");
    uint32 K, S;
    cin >> K >> S;

    uint32 ans = 0;
    for(uint32 x=0; x<=K; x++){
        if(x+K+K < S){ continue; }
        for(uint32 y=0; y<=(S-x) && y<=K; y++){
            if(x+y+K < S){ continue; }
            for(uint32 z=0; z<=(S-x-y) & z<=K; z++){
                if(x+y+z == S){
                    DEBUG_OUT("x=%d y=%d z=%d\n", x, y, z);
                    ans ++;
                }
            }
        }
    }

    cout << ans << endl;

    return;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int k,s;
    cin>>k>>s;
    int ans=0;
    for(int i=0;i<=k;i++){
        for(int j=0;j<=k;j++){
            if(0<=s-i-j&&s-i-j<=k){
                ans++;
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

int main()
{
    int k,s;
    scanf("%d%d",&k,&s);
    int sum=0;
    for(int x=0;x<=k;++x)
    {
        for(int y=0;y<=k;++y)
        {
            for(int z=0;z<=k;++z)
            {
                if(z+y+x==s)
                    sum++;
            }
        }
    }
    printf("%d\n",sum);
    return 0;
}


Score : 300 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Another string T is initially empty.
Determine whether it is possible to obtain S = T by performing the following operation an arbitrary number of times:

Append one of the following at the end of T: dream, dreamer, erase and eraser.

Constraints

1≦|S|≦10^5

S consists of lowercase English letters.

Input

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

S

Output

If it is possible to obtain S = T, print YES. Otherwise, print NO.

Sample Input 1

erasedream

Sample Output 1

YES

Append erase and dream at the end of T in this order, to obtain S = T.

Sample Input 2

dreameraser

Sample Output 2

YES

Append dream and eraser at the end of T in this order, to obtain S = T.

Sample Input 3

dreamerer

Sample Output 3

NO


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i=0, i##_len=(n); i<i##_len; ++i)
#define REPR(i,n) for(ll i = n;i >= 0;--i)
#define FOR(i,m,n) for(ll i = m, i##_len=(n);i <i##_len; ++i)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
const int INF = 1e9;
const ll LLINF = 1e16;

ll modinv(ll a, ll m) {
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

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

    string t = "";
    vector<string> component;
    component.push_back("dream");
    component.push_back("dreamer");
    component.push_back("erase");
    component.push_back("eraser");

    for (int i = s.size() - 1; i >= 0;){
        bool flag = false;
        REP(j,component.size()){

            if ( (int)(i - component[j].size() + 1) < 0)
            {
                continue;
            }

            if(s.substr(i-component[j].size() + 1,component[j].size()) == component[j]){
                flag = true;
                i -= component[j].size();
                t = component[j] + t;
                break;
            }
        }
        if(!flag){
            break;
        }
    }

    if (s == t)
    {
        cout << "YES" << endl;
    }
    else{
        cout << "NO" << endl;
    }

    return 0;
}


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

string divide[4] = {"dream", "dreamer", "erase", "eraser"};
bool dp[100010];

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

    dp[0] = 1;
    for(int i = 0; i < S.size(); ++i){
        if(!dp[i])continue; // そこまでで矛盾があったらとりあえず無視
        for(string s : divide){
            if(S.substr(i, s.size()) == s){ // うまく切れたら先に進む
                dp[i + s.size()] = 1;
            }
        }
    }
    cout << (dp[S.size()] ? "YES" : "NO") << endl;
    return 0;
}


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

int main() {
    string s; cin >> s;
    int n = s.length();

    vector<int> dp(n + 1, 0);
    dp[0] = 1;
    for (int i = 0; i < n; i++) if (dp[i]) {
        if (i + 5 <= n and s.substr(i, 5) == "dream")   dp[i + 5] = 1;
        if (i + 7 <= n and s.substr(i, 7) == "dreamer") dp[i + 7] = 1;
        if (i + 5 <= n and s.substr(i, 5) == "erase")   dp[i + 5] = 1;
        if (i + 6 <= n and s.substr(i, 6) == "eraser")  dp[i + 6] = 1;
    }

    puts(dp[n] ? "YES" : "NO");

    return 0;
}


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

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

  while(S!=""){
  	if (S.substr(0,6)=="dreamer") S=S.substr(7);
    else if (S.substr(0,4)=="dream") S=S.substr(5);
    else if (S.substr(0,5)=="eraser") S=S.substr(6);
    else if (S.substr(0,4)=="erase") S=S.substr(5);
    else break;
  }

  if (S.size()) cout << "NO" << endl;
  else cout << "YES" << endl;
}


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

using namespace std;

//typedef
//------------------------------------------
typedef long long LL;
typedef vector<int> VI;
typedef vector<bool> VB;
typedef vector<char> VC;
typedef vector<double> VD;
typedef vector<LL> VLL;
typedef vector<VI> VVI;
typedef vector<VB> VVB;
typedef vector<string> VS;
typedef vector<VLL> VVLL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef pair<int, string> PIS;
typedef pair<string, int> PSI;
typedef pair<string, string> PSS;

//数値・文字列
//------------------------------------------
inline int toInt(string s) {
    int v;
    istringstream sin(s);
    sin >> v;
    return v;
}

inline LL toLongLong(string s) {
    LL v;
    istringstream sin(s);
    sin >> v;
    return v;
}

template<class T>
inline string toString(T x) {
    ostringstream sout;
    sout << x;
    return sout.str();
}

inline VC toVC(string s) {
    VC data(s.begin(), s.end());
    return data;
}

template<typename List>
void SPRIT(const std::string &s, const std::string &delim, List &result) {
    result.clear();
    string::size_type pos = 0;
    while (pos != string::npos) {
        string::size_type p = s.find(delim, pos);
        if (p == string::npos) {
            result.push_back(s.substr(pos));
            break;
        } else {
            result.push_back(s.substr(pos, p - pos));
        }
        pos = p + delim.size();
    }
}

string TRIM(const string &str, const char *trimCharacterList = " \t\v\r\n") {
    string result;
    string::size_type left = str.find_first_not_of(trimCharacterList);
    if (left != string::npos) {
        string::size_type right = str.find_last_not_of(trimCharacterList);
        result = str.substr(left, right - left + 1);
    }
    return result;
}

template<typename T>
bool VECTOR_EXISTS(vector<T> vec, T data) {
    auto itr = std::find(vec.begin(), vec.end(), data);
    size_t index = distance(vec.begin(), itr);
    if (index != vec.size()) {
        return true;
    } else {
        return 0;
    }
}

#define UPPER(s) transform((s).begin(), (s).end(), (s).begin(), ::toupper)
#define LOWER(s) transform((s).begin(), (s).end(), (s).begin(), ::tolower)

//四捨五入 nLen=小数点第N位にする
//------------------------------------------

//四捨五入
double ceil_n(double dIn, int nLen) {
    double dOut;
    dOut = dIn * pow(10.0, nLen);
    dOut = (double) (int) (dOut + 0.9);
    return dOut * pow(10.0, -nLen);
}

//切り捨て
double floor_n(double dIn, int nLen) {
    double dOut;
    dOut = dIn * pow(10.0, nLen);
    dOut = (double) (int) (dOut);
    return dOut * pow(10.0, -nLen);
}

//切り上げ
double round_n(double dIn, int nLen) {
    double dOut;
    dOut = dIn * pow(10.0, nLen);
    dOut = (double) (int) (dOut + 0.5);
    return dOut * pow(10.0, -nLen);
}

//n桁目の数の取得
int take_a_n(int num, int n) {
    string str = toString(num);
    return str[str.length() - n] - '0';
}

//進数
//------------------------------------------

//"1111011" → 123
int strbase_2to10(const std::string &s) {
    int out = 0;
    for (int i = 0, size = s.size(); i < size; ++i) {
        out *= 2;
        out += ((int) s[i] == 49) ? 1 : 0;
    }
    return out;
}

//"123" → 1111011
int strbase_10to2(const std::string &s) {
    int binary = toInt(s);
    int out = 0;
    for (int i = 0; binary > 0; i++) {
        out = out + (binary % 2) * pow(static_cast<int>(10), i);
        binary = binary / 2;
    }
    return out;
}

//"ABC" 2748
int strbase_16to10(const std::string &s) {
    int out = stoi(s, 0, 16);
    return out;
}

//1111011 → 123
int intbase_2to10(int in) {
    string str = toString(in);
    return strbase_2to10(str);
}

//123 → 1111011
int intbase_10to2(int in) {
    string str = toString(in);
    return strbase_10to2(str);
}

int intbase_16to10(int in) {
    string str = toString(in);
    return strbase_16to10(str);
}

//123→ "7B"
string intbase_10to16(unsigned int val, bool lower = true) {
    if (!val)
        return std::string("0");
    std::string str;
    const char hc = lower ? 'a' : 'A';     // 小文字 or 大文字表記
    while (val != 0) {
        int d = val & 15;     // 16進数一桁を取得
        if (d < 10)
            str.insert(str.begin(), d + '0');  //  10未満の場合
        else //  10以上の場合
            str.insert(str.begin(), d - 10 + hc);
        val >>= 4;
    }
    return str;
}

//整数を2進数表記したときの1の個数を返す
LL bitcount64(LL bits) {
    bits = (bits & 0x5555555555555555) + (bits >> 1 & 0x5555555555555555);
    bits = (bits & 0x3333333333333333) + (bits >> 2 & 0x3333333333333333);
    bits = (bits & 0x0f0f0f0f0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f0f0f0f0f);
    bits = (bits & 0x00ff00ff00ff00ff) + (bits >> 8 & 0x00ff00ff00ff00ff);
    bits = (bits & 0x0000ffff0000ffff) + (bits >> 16 & 0x0000ffff0000ffff);
    return (bits & 0x00000000ffffffff) + (bits >> 32 & 0x00000000ffffffff);
}

//comparison
//------------------------------------------
#define C_MAX(a, b) ((a)>(b)?(a):(b))
#define C_MIN(a, b) ((a)<(b)?(a):(b))
#define C_ABS(a, b) ((a)<(b)?(b)-(a):(a)-(b))

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define EACH(i, c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s, e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define SUMI(obj) accumulate((obj).begin(), (obj).end(), 0)
#define SUMD(obj) accumulate((obj).begin(), (obj).end(), 0.)
#define SUML(obj) accumulate((obj).begin(), (obj).end(), 0LL)
#define UB(obj, n) upper_bound((obj).begin(), (obj).end(), n)
#define LB(obj, n) lower_bound((obj).begin(), (obj).end(), n)
#define BS(v, n) binary_search(ALL(v), (n))
#define PB push_back
#define MP make_pair

//input output
//------------------------------------------
#define GL(s) getline(cin, (s))
#define INIT std::ios::sync_with_stdio(false);std::cin.tie(0);
#define OUT(d) std::cout<<(d);
#define OUT_L(d) std::cout<<(d)<<endl;
#define FOUT(n, d) std::cout<<std::fixed<<std::setprecision(n)<<(d);
#define EL() std::cout << "\n";
#define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";}

//repetition
//------------------------------------------
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RFOR(i, a, b) for(int i=(b)-1;i>=(a);--i)
#define REP(i, n)  FOR(i,0,n)
#define RREP(i, n) for(int i = n;i >= 0;i--)
#define FORLL(i, a, b) for(LL i=LL(a);i<LL(b);++i)
#define RFORLL(i, a, b) for(LL i=LL(b)-1;i>=LL(a);--i)
#define REPLL(i, n) for(LL i=0;i<LL(n);++i)
#define RREPLL(i, n) for(LL i=LL(n)-1;i>=0;--i)
#define FOREACH(x, v) for(auto &(x) : (v))
#define FORITER(x, v) for(auto (x) = (v).begin(); (x) != (v).end(); ++(x))

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

//math
//--------------------------------------------

//min <= aim <= max
template<typename T>
inline bool BETWEEN(const T aim, const T min, const T max) {
    if (min <= aim && aim <= max) {
        return true;
    } else {
        return false;
    }
}

template<class T>
inline T SQR(const T x) { return x * x; }

template<class T1, class T2>
inline T1 POW(const T1 x, const T2 y) {
    if (!y)return 1;
    else if ((y & 1) == 0) {
        return SQR(POW(x, y >> 1));
    } else return POW(x, y ^ 1) * x;
}

template<typename T>
constexpr T ABS(T x) {
    static_assert(is_signed<T>::value, "ABS(): argument must be signed");
    return x < 0 ? -x : x;
}

//partial_permutation nPr 順列
//first・・最初の数
//middle・・r(取り出す数)
//last・・n(全体数)
template<class BidirectionalIterator>
bool next_partial_permutation(BidirectionalIterator first, BidirectionalIterator middle, BidirectionalIterator last) {
    reverse(middle, last);
    return next_permutation(first, last);
}

//combination nCr 組み合わせ
//first1・・最初の数
//last1==first2・・r(取り出す数)
//last2・・n(全体数)
template<class BidirectionalIterator>
bool next_combination(BidirectionalIterator first1, BidirectionalIterator last1, BidirectionalIterator first2,
                      BidirectionalIterator last2) {
    if ((first1 == last1) || (first2 == last2)) {
        return false;
    }
    BidirectionalIterator m1 = last1;
    BidirectionalIterator m2 = last2;
    --m2;
    while (--m1 != first1 && !(*m1 < *m2)) {
    }
    bool result = (m1 == first1) && !(*first1 < *m2);
    if (!result) {
        while (first2 != m2 && !(*m1 < *first2)) {
            ++first2;
        }
        first1 = m1;
        std::iter_swap(first1, first2);
        ++first1;
        ++first2;
    }
    if ((first1 != last1) && (first2 != last2)) {
        m1 = last1;
        m2 = first2;
        while ((m1 != first1) && (m2 != last2)) {
            std::iter_swap(--m1, m2);
            ++m2;
        }
        std::reverse(first1, m1);
        std::reverse(first1, last1);
        std::reverse(m2, last2);
        std::reverse(first2, last2);
    }
    return !result;
}

//numeric_law
//--------------------------------------------

template<typename T>
constexpr bool ODD(T x) {
    return x % 2 != 0;
}

template<typename T>
constexpr bool EVEN(T x) {
    return x % 2 == 0;
}

//最大公約数
template<class T>
inline T GCD(const T x, const T y) {
    if (x < 0)return GCD(-x, y);
    if (y < 0)return GCD(x, -y);
    return (!y) ? x : GCD(y, x % y);
}

//最小公倍数
template<class T>
inline T LCM(const T x, const T y) {
    if (x < 0)return LCM(-x, y);
    if (y < 0)return LCM(x, -y);
    return x * (y / GCD(x, y));
}

//ax + by = 1
//x,yが変数に格納される
template<class T>
inline T EXTGCD(const T a, const T b, T &x, T &y) {
    if (a < 0) {
        T d = EXTGCD(-a, b, x, y);
        x = -x;
        return d;
    }
    if (b < 0) {
        T d = EXTGCD(a, -b, x, y);
        y = -y;
        return d;
    }
    if (!b) {
        x = 1;
        y = 0;
        return a;
    } else {
        T d = EXTGCD(b, a % b, x, y);
        T t = x;
        x = y;
        y = t - (a / b) * y;
        return d;
    }
}

//素数
template<class T>
inline bool ISPRIME(const T x) {
    if (x <= 1)return false;
    for (T i = 2; SQR(i) <= x; i++)if (x % i == 0)return false;
    return true;
}

//素数をtrueとして返す
template<class T>
VB ERATOSTHENES(const T n) {
    VB arr(n, true);
    for (int i = 2; i < SQR(n); i++) {
        if (arr[i]) {
            for (int j = 0; i * (j + 2) < n; j++) {
                arr[i * (j + 2)] = false;
            }
        }
    }
    return arr;
}

// a <= x < b の素数を返す
template<typename T>
VB ERATOSTHENES(const T a, const T b) {
    VB small = ERATOSTHENES(b);
    VB prime(b - a, true);

    for (int i = 2; (T) (SQR(i)) < b; i++) {
        if (small[i]) {
            for (T j = max(2, (a + i - 1) / i) * i; j < b; j += i) {
                prime[j - a] = false;
            }
        }
    }

    return prime;
}

//約数
template<class T>
vector<T> DIVISOR(T n) {
    vector<T> v;
    for (int i = 1; i * i <= n; ++i) {
        if (n % i == 0) {
            v.push_back(i);
            if (i != n / i) {
                v.push_back(n / i);
            }
        }
    }
    sort(v.begin(), v.end());
    return v;
}

//組み合わせ個数
template<typename T>
T NCR(T n, T r) {
    T ans = 1;
    for (T i = n; i > n - r; --i) {
        ans = ans * i;
    }
    for (T i = 1; i < r + 1; ++i) {
        ans = ans / i;
    }
    return ans;
}

//行列
int MATRIZ_CHAIN(VI &p, VVI &s) {
    const static int INF = 1 << 20;
    const int n = p.size() - 1;
    VVI X(n, VI(n, INF));
    s.resize(n, VI(n));
    for (int i = 0; i < n; ++i) X[i][i] = 0;
    for (int w = 1; w < n; ++w)
        for (int i = 0, j; j = i + w, j < n; ++i)
            for (int k = i; k < j; ++k) {
                int f = p[i] * p[k + 1] * p[j + 1];
                if (X[i][k] + X[k + 1][j] + f < X[i][j]) {
                    X[i][j] = X[i][k] + X[k + 1][j] + f;
                    s[i][j] = k;
                }
            }
    return X[0][n - 1];
}

//最長増加部分列
VI LIS(const VI &a) {
    const static int INF = 99999999;
    const int n = a.size();
    VI A(n, INF);
    VI id(n);
    for (int i = 0; i < n; ++i) {
        id[i] = distance(A.begin(), lower_bound(A.begin(), A.end(), a[i]));
        A[id[i]] = a[i];
    }
    int m = *max_element(id.begin(), id.end());
    VI b(m + 1);
    for (int i = n - 1; i >= 0; --i)
        if (id[i] == m) b[m--] = a[i];
    return b;
}

//最長共通部分列 string->toVC
template<typename T>
vector<T> LCS(const vector<T> &a, const vector<T> &b) {
    const int n = a.size(), m = b.size();
    vector<VI> X(n + 1, VI(m + 1));
    vector<VI> Y(n + 1, VI(m + 1));
    REP(i, n) {
        REP(j, m) {
            if (a[i] == b[j]) {
                X[i + 1][j + 1] = X[i][j] + 1;
                Y[i + 1][j + 1] = 0;
            } else if (X[i + 1][j] < X[i][j + 1]) {
                X[i + 1][j + 1] = X[i][j + 1];
                Y[i + 1][j + 1] = +1;
            } else {
                X[i + 1][j + 1] = X[i + 1][j];
                Y[i + 1][j + 1] = -1;
            }
        }
    }
    vector<T> c;
    for (int i = n, j = m; i > 0 && j > 0;) {
        if (Y[i][j] > 0) --i;
        else if (Y[i][j] < 0) --j;
        else {
            c.PB(a[i - 1]);
            --i;
            --j;
        }
    }
    REVERSE(c);
    return c;
}

//コイン C総額 cs使用できるコインの種類
VI money_change(int C, VI &cs) {
    const int INF = 99999999;
    int n = cs.size();
    VI xs(C + 1, INF);
    VI ys(C + 1);
    xs[0] = 0;
    for (int i = 0; i < n; ++i) {
        for (int c = 0; c + cs[i] <= C; ++c) {
            if (xs[c + cs[i]] > xs[c] + 1) {
                xs[c + cs[i]] = xs[c] + 1;
                ys[c + cs[i]] = c;
            }
        }
    }
    VI zs;
    for (int c = C; c > 0; c = ys[c]) {
        zs.push_back(c - ys[c]);
    }
    return zs;
}

//confirmation
//--------------------------------------------

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

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

/*
 *
 *
 *   ~~~~Below My Answer~~~~
 *
 *
 * */

int main() {

    string S;
    cin >> S;
    REVERSE(S);

    VS str_s = {"dream", "dreamer", "erase", "eraser"};
    REP(i, SZ(str_s)){
        REVERSE(str_s[i]);
    }

    bool good = true;
    for(int i=0;i<SZ(S);){
        bool tmp = false;
        REP(j, SZ(str_s)){
            if(S.substr(i, SZ(str_s[j]))==str_s[j]){
                tmp = true;
                i += SZ(str_s[j]);
                break;
            }
        }
        if(!tmp){
            good =false;
            break;
        }
    }

    if(good){
        OUT_L("YES");
    }else{
        OUT_L("NO");
    }

    return 0;
}


#include <bits/stdc++.h>

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

#define INF 1 << 29
#define REP(i,n) for(ll i=0; i<(int)(n); i++)
#define FOR(i,k,n) for(ll i=(k);i<(int)(n);i++)

vector<bool> gen_sosuu(vector<bool> sosuu){
  int size = sosuu.size();
  REP(i,size){
    sosuu[i] = true;
  }
  sosuu[0] = false;
  sosuu[1] = false;
  sosuu[2] = true;
  FOR(i,2,sqrt(size)+1){
    if(sosuu[i] == false) continue;
    for(int j = 2; i*j<size;j++){
      sosuu[i*j] =false;
    }
  }
  return sosuu;
}

string t[4] = {"dreamer","eraser","erase","dream"};
string h[4] = {"dream","eraser","erase","dreamer"};

string s;

bool match(int i, int k){
  REP(j,t[k].size()){
    if(i>=s.size()) return false;
    if(s[i] != t[k][j]) return false;
    i++;
  }
  return true;
}

bool dfs(int i){
  if(i>=s.size()) return true;
  REP(j,4){
    if(match(i,j)){
      if(dfs(i+t[j].size())) return true;
    }
  }
  return false;
}

int main(){
  cin >> s;
  if(dfs(0)){
    cout << "YES" <<endl;
  }else{
    cout <<"NO"<<endl;
  }
}


#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 unsigned long long ull;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<ull> vul;
typedef vector<vector<ull>> vvul;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;

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

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

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

double ave = 0.0;

bool can_make = false;

void dfs(string str) {
    if (str != s.substr(0, str.length())) return;

    if (str.length() >= s.length()) {
        if (str == s && !can_make) {
            can_make = true;
        }
        return;
    }

    if (s.at(str.size()) == 'd') {
        dfs(str + "dream");
        dfs(str + "dreamer");
    } else {
        dfs(str + "erase");
        dfs(str + "eraser");
    }
}

int main() {
    cin >> s;
    reverse(ALL(s));
    ll idx = 0;
    while (true) {
        if (s.substr(idx,  5) == "maerd" ||
            s.substr(idx,  5) == "esare") {
            idx += 5;
        } else if (s.substr(idx,  6) == "resare") {
            idx += 6;
        } else if (s.substr(idx,  7) == "remaerd") {
            idx += 7;
        } else {
            END("NO")
        }

        if (idx == s.size()) {
            END("YES")
        }
    }
}


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

int main(){
    string str;
    cin >> str;
    reverse(str.begin(),str.end());
    cout << str << endl;
    for(int i=0;i<str.length();){
        if(str.substr(i,5) == "maerd" || str.substr(i,5) == "esare"){
            i += 5;
        }else if(str.substr(i,3) == "rem"){
            string s = str.substr(i,min(7,(int)str.length()-i));
            i += 7;
        }else if(str.substr(i,3) == "res"){
            string s = str.substr(i,min(6,(int)str.length()-i));
            i += 6;
        }else{
            cout << "NO" << endl;
            return 0;
        }
    }
    cout << "YES" << endl;
    return 0;
}


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

using namespace std;

//const int maxn=;
const int INF=10000;
bool mark[4];
int last=INF;

bool ok(char x,int p)
{
	bool ok0=false;
	string a[4]={"maerd","remaerd","esare","resare"};
	int len[4]={5,7,5,6};
	for(int i=0;i<4;i++)
	{
		if(a[i][p%last]==x) ok0=true;
		if(p%last==len[i]-1 && a[i][p%last]==x) last=p+1;
	}
	return ok0;
}

int main()
{
	string s;
	cin>>s;
	int p=0;
	int len=s.length();
	for(int i=len-1;i>=0;i--)
	{
		if(!ok(s[i],p++)){
			cout<<"NO";
			return 0;
		}
	}

	return 0;
}


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

int main()
{
	bool flag = true;
	string s;
	cin >> s;
	for (int i = s.size() - 1; i >= 0; i--) {
		if (i >= 6 && s.substr(i - 6, 7) == "dreamer") {
			i -= 6;
		}
		else if (i >= 4 && s.substr(i - 4, 5) == "dream") {
			i -= 4;
		}
		else if (i >= 5 && s.substr(i - 5, 6) == "eraser") {
			i -= 5;
		}
		else if (i >= 4 && s.substr(i - 4, 5) == "erase") {
			i -= 4;
		}
		else {
			cout << "NO" << endl;
			flag = false;
			break;
		}
	}
	if (flag) {
		cout << "YES" << endl;
	}
	return 0;
}


#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<vector>
#include<array>
#include<string>
#include<stack>
#include<queue>
#include<algorithm>
#include<cassert>
#include<functional>
#include<random>
#include<complex>
#include<bitset>
#include<chrono>
//#include<boost/multiprecision/cpp_int.hpp>
#define int int64_t
#define uint uint64_t
#define REP(i, a, b) for (int64_t i = (int64_t)(a); i < (int64_t)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i: a)
#define ITR(x, a) for (auto x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())
#define Min(x) *min_element(ALL(x))
#define Max(x) *max_element(ALL(x))
#define Unique(L) (L.erase(unique(ALL(L)), L.end()))
#define intmax (std::numeric_limits<int64_t>::max() / 4)
#define doublemax (std::numeric_limits<double>::max() / 4)
using namespace std;
//typedef boost::multiprecision::cpp_int bigint;
const double EPS = 1e-9;
const double PI = acos(-1.0);

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

	string S;
	cin >> S;
	vector<string> a= { "dreamer","dream","eraser","erase" };
	int cursor = 0;
	while (cursor < S.size()) {
		bool ff = false;
		rep(i, a.size())if (cursor + a[i].size() <= S.size()) {
			bool flag = true;
			rep(j, a[i].size())if (a[i][j] != S[cursor + j]) { flag = false; break; }
			if (flag == true) {
				ff = true;
				cursor += a[i].size();
				break;
			}
		}
		if (ff == false) {
			cout << "NO" << endl;
			return 0;
		}
	}
	cout << "YES" << endl;
	return 0;
}


#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"

using namespace std;

//const long long int MOD = 1000000007;
const int MOD = 1000000007;
//const int MOD = 998244353;

long long int N, M, K, H, W, L, R;
//int N, M, K, H, W, L, R;

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

	vector<double>v(6);
	vector<double>w(6);
	for (auto &i : v) {
		cin >> i;
		i /= 100;
	}
	for (auto &i : w) {
		cin >> i;
		i /= 100;
	}
	vector < double >box;
	box.push_back(0);
	box.push_back(1);
	for (int i = 0; i < 6; i++) {
		box.push_back((double)w[i] / (v[i] + w[i]));
	}
	double ans = 1;
	for (auto i : box) {
		double bag = i;
		for (int j = 0; j < 6; j++) {
			if (i*(v[j] + w[j]) <= w[j]) {
				bag += w[j] * (1 - i) - v[j] * i;
			}
		}
		//cout << bag << endl;
		ans = min(ans, bag);
	}
	cout <<setprecision(20)<< ans << endl;
	return 0;
}

Score : 1000 points

Problem Statement

#nck {
width: 30px;
height: auto;
}

There are two (6-sided) dice: a red die and a blue die.
When a red die is rolled, it shows i with probability p_i percents, and when a blue die is rolled, it shows j with probability q_j percents.

Petr and tourist are playing the following game.
Both players know the probabilistic distributions of the two dice.
First, Petr chooses a die in his will (without showing it to tourist), rolls it, and tells tourist the number it shows.
Then, tourist guesses the color of the chosen die.
If he guesses the color correctly, tourist wins. Otherwise Petr wins.

If both players play optimally, what is the probability that tourist wins the game?

Constraints

0 ≤ p_i, q_i ≤ 100

p_1 + ... + p_6 = q_1 + ... + q_6 = 100

All values in the input are integers.

Input

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

p_1 p_2 p_3 p_4 p_5 p_6
q_1 q_2 q_3 q_4 q_5 q_6

Output

Print the probability that tourist wins.
The absolute error or the relative error must be at most 10^{-9}.

Sample Input 1

25 25 25 25 0 0
0 0 0 0 50 50

Sample Output 1

1.000000000000

tourist can always win the game: If the number is at most 4, the color is definitely red. Otherwise the color is definitely blue.

Sample Input 2

10 20 20 10 20 20
20 20 20 10 10 20

Sample Output 2

0.550000000000


#include <bits/stdc++.h>
using namespace std;
int a[6], b[6];
double solve(double p, int m = 0) {
	double ra = 0.0, rb = 0.0;
	for(int i = 0; i < 6; i++) {
		if(abs(p * a[i] - (1 - p) * b[i]) <= 1.0e-6) ra += a[i] * 0.5, rb += b[i] * 0.5;
		else if(p * a[i] > (1 - p) * b[i]) ra += a[i];
		else rb += b[i];
	}
	if(m == 1) return ra;
	return ra - rb;
}
int main() {
	for(int i = 0; i < 6; i++) cin >> a[i];
	for(int i = 0; i < 6; i++) cin >> b[i];
	double l = 0.0, r = 1.0;
	for(int i = 0; i <= 100; i++) {
		double m = (l + r) * 0.5;
		if(solve(m) > 0.0) r = m;
		else l = m;
	}
	printf("%.12lf\n", solve(l, 1) * 0.01);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;

const int N = 100010, mod = 1e9 + 7;

int tt = -1;
string s;
char stk[N];

int main()
{
    cin >> s;
    for(int i = 0; i < s.size(); i++)
        if(tt == -1 || stk[tt] != s[i])
            stk[++tt] = s[i];

    cout << tt << endl;
    return 0;
}

Score : 300 points

Problem Statement

Two foxes Jiro and Saburo are playing a game called 1D Reversi. This game is played on a board, using black and white stones. On the board, stones are placed in a row, and each player places a new stone to either end of the row. Similarly to the original game of Reversi, when a white stone is placed, all black stones between the new white stone and another white stone, turn into white stones, and vice versa.

In the middle of a game, something came up and Saburo has to leave the game. The state of the board at this point is described by a string S. There are |S| (the length of S) stones on the board, and each character in S represents the color of the i-th (1 ≦ i ≦ |S|) stone from the left. If the i-th character in S is B, it means that the color of the corresponding stone on the board is black. Similarly, if the i-th character in S is W, it means that the color of the corresponding stone is white.

Jiro wants all stones on the board to be of the same color. For this purpose, he will place new stones on the board according to the rules. Find the minimum number of new stones that he needs to place.

Constraints

1 ≦ |S| ≦ 10^5

Each character in S is B or W.

Input

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

S

Output

Print the minimum number of new stones that Jiro needs to place for his purpose.

Sample Input 1

BBBWW

Sample Output 1

1

By placing a new black stone to the right end of the row of stones, all white stones will become black. Also, by placing a new white stone to the left end of the row of stones, all black stones will become white.

In either way, Jiro's purpose can be achieved by placing one stone.

Sample Input 2

WWWWWW

Sample Output 2

0

If all stones are already of the same color, no new stone is necessary.

Sample Input 3

WBWBWBWBWB

Sample Output 3

9


#include<bits/stdc++.h>
using namespace std;
string a;
long long b;
int main()
{
    cin>>a;
    for(int i=1;i<a.length();i++)
        if(a[i-1]!=a[i])
			b++;
    cout<<b;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	int res = 1;
	bool flag;
	cin >> s;
	int len = s.size();
	if (s[0] == 'B')
		flag = 0;
	else
		flag = 1;
	for (int i = 1; i < len; ++i)
	{
		if ((s[i] == 'B') && (!flag))
			continue;
		else if ((s[i] == 'W') && (flag))
			continue;
		else
		{
			res++;
			flag = (s[i]=='B')?0:1;
		}
	}
	cout << res - 1;
return 0;
}


#include <iostream>
#include <cstdio>

using namespace std;

char c;
string s;
int cnt = 0;

int main()
{
    cin>>s;
    c = s[0];
    for(int i = 1; s[i]; i++)
    {
        if(c != s[i])
        {
            c = s[i];
            cnt++;
        }
    }
    printf("%d", cnt);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,N) for(int i=0;i<int(N);++i)
#define rep1(i,N) for(int i=1;i<int(N);++i)
#define all(a) (a).begin(),(a).end()
#define bit(k) (1LL<<(k))
#define SUM(v) accumulate(all(v), 0LL)

typedef pair<int, int> i_i;
typedef pair<ll, ll> l_l;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
struct fast_ios{ fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; }fast_ios_;

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

#define TOSTRING(x) string(#x)
template <typename T> istream &operator>>(istream &is, vector<T> &vec) { for (T &x : vec) is >> x; return is; }
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os  << "["; for(auto _: v) os << _ << ", "; os << "]"; return os; };
template <typename T> ostream &operator<<(ostream &os, set<T> &st) { os << "("; for(auto _: st) { os << _ << ", "; } os << ")";return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair< T, U >& p){os << "{" <<p.first << ", " << p.second << "}";return os; }
template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &mp){ os << "["; for(auto _: mp){ os << _ << ", "; } os << "]" << endl; return os; }

#define DUMPOUT cerr
void dump_func(){ DUMPOUT << endl; }
template <class Head, class... Tail> void dump_func(Head &&head, Tail &&... tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); }

#ifdef DEBUG
#define dbg(...) dump_func(__VA_ARGS__)
#define dump(...) DUMPOUT << string(#__VA_ARGS__) << ": "; dump_func(__VA_ARGS__)
#else
#define dbg(...)
#define dump(...)
#endif

const int INF = (ll)1e9;
const ll INFLL = (ll)1e18+1;
const ll MOD = 1000000007;
// const ll MOD = 998244353;
const long double PI = acos(-1.0);

/*
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const string dir = "DRUL";
*/

int main() {
    int N,L;
    cin >> N >> L;
    vector<string> S(N);
    cin >> S;
    sort(all(S));
    rep(i,N){
        cout << S[i];
    }
    cout << endl;
}

Score : 200 points

Problem Statement

Iroha has a sequence of N strings S_1, S_2, ..., S_N. The length of each string is L.

She will concatenate all of the strings in some order, to produce a long string.

Among all strings that she can produce in this way, find the lexicographically smallest one.

Here, a string s=s_1s_2s_3...s_n is lexicographically smaller than another string t=t_1t_2t_3...t_m if and only if one of the following holds:

There exists an index i(1≦i≦min(n,m)), such that s_j = t_j for all indices j(1≦j<i), and s_i<t_i.

s_i = t_i for all integers i(1≦i≦min(n,m)), and n<m.

Constraints

1 ≦ N, L ≦ 100

For each i, the length of S_i equals L.

For each i, S_i consists of lowercase letters.

Input

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

N L
S_1
S_2
:
S_N

Output

Print the lexicographically smallest string that Iroha can produce.

Sample Input 1

3 3
dxx
axx
cxx

Sample Output 1

axxcxxdxx

The following order should be used: axx, cxx, dxx.


#include<bits/stdc++.h>
using namespace std;
string a[101];int n,i;
int main(){
    scanf("%d%*d",&n);
    for(i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    for(i=0;i<n;i++)
        cout<<a[i];
    return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <map>

#define rep(i, N) for (int i = 0; i < (int)N; i++)
using namespace std;
typedef long long ll;
const ll LLINF = 9223372036854775807;
const int MOD = 1000000007;

int main() {
  int N, L; cin >> N >> L;
  string S[N]; rep(i,N) cin >> S[i];
  sort(S,S+N);

  rep(i,N) cout << S[i];
  cout << endl;
  return 0;
}


#pragma GCC optimize(3)
#pragma GCC target("avx")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
string a[101];
int main(){
	int n,i,l;
	cin>>n>>l;
	for(i=0;i<n;i++){
		cin>>a[i];
	}
    sort(a,a+n);
	for(i=0;i<n;i++)
	cout<<a[i];
	cout<<endl;
}


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

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

  string S[110];
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }

  sort(S, S+N);

  string ans;
  for (int i = 0; i < N; i++) {
    ans += S[i];
  }

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