#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <limits.h>

typedef struct {
    int x;
    int y;
} squr_t;

int sort(const void *a, const void *b){
    if ( ((squr_t*)a)->x - ((squr_t*)b)->x >0 ) {
        return 1;
    }
    if ( ((squr_t*)a)->x - ((squr_t*)b)->x <0 ) {
        return -1;
    }
    if ( ((squr_t*)a)->x - ((squr_t*)b)->x ==0 ) {
        if ( ((squr_t*)a)->y - ((squr_t*)b)->y >=0 ) {
            return 1;
        }
        if ( ((squr_t*)a)->y - ((squr_t*)b)->y <0 ) {
            return -1;
        }
    }
    return 0;
}

int main(void){
    int h,w,n,a,b,count=0;
    scanf("%d%d%d",&h,&w,&n);
    squr_t s[n*9];
    for (int i=0; i<n; i++){
        scanf("%d%d",&a,&b);
        a--;
        b--;
        for (int j=-1; j<2; j++){
            if(a+j>=1 && a+j<h-1){
                for (int k=-1; k<2; k++){
                    if (b+k>=1 && b+k<w-1){
                        s[count].x=a+j;
                        s[count].y=b+k;
                        count++;
                    }
                }
            }
        }
    }
    qsort (s,count,sizeof(squr_t),sort);
    long long ans[10],am=0;
    for (int i=0; i<10; i++){
        ans[i]=0;
    }
    for (int i=0; i<count; i++){
        if (i==0){
            am++;
        } else if (s[i].x==s[i-1].x && s[i].y==s[i-1].y){
            am++;
        } else {
            ans[am]++;
            am=1;
        }
        if (i==count-1){
            ans[am]++;
        }
    }
    ans[0]=(h-2)*(w-2);
    for (int i=1; i<10; i++){
        ans[0]-=ans[i];
    }
    for (int i=0; i<10; i++){
        printf("%lld\n",ans[i]);
    }

	return 0 ;
}

Score : 400 points

Problem Statement

We have a grid with H rows and W columns. At first, all cells were painted white.

Snuke painted N of these cells. The i-th ( 1 \leq i \leq N ) cell he painted is the cell at the a_i-th row and b_i-th column.

Compute the following:

For each integer j ( 0 \leq j \leq 9 ), how many subrectangles of size 3×3 of the grid contains exactly j black cells, after Snuke painted N cells?

Constraints

3 \leq H \leq 10^9

3 \leq W \leq 10^9

0 \leq N \leq min(10^5,H×W)

1 \leq a_i \leq H (1 \leq i \leq N)

1 \leq b_i \leq W (1 \leq i \leq N)

(a_i, b_i) \neq (a_j, b_j) (i \neq j)

Input

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

H W N
a_1 b_1
:
a_N b_N

Output

Print 10 lines.
The (j+1)-th ( 0 \leq j \leq 9 ) line should contain the number of the subrectangles of size 3×3 of the grid that contains exactly j black cells.

Sample Input 1

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

Sample Output 1

0
0
0
2
4
0
0
0
0
0

There are six subrectangles of size 3×3. Two of them contain three black cells each, and the remaining four contain four black cells each.

Sample Input 2

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

Sample Output 2

4
26
22
10
2
0
0
0
0
0

Sample Input 3

1000000000 1000000000 0

Sample Output 3

999999996000000004
0
0
0
0
0
0
0
0
0


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#include<math.h>
#include<assert.h>
#define inf 1072114514
#define llinf 4154118101919364364
#define mod 1000000007
#define pi 3.1415926535897932384
int max(int a,int b){if(a>b){return a;}return b;}
int min(int a,int b){if(a<b){return a;}return b;}
int zt(int a,int b){return max(a,b)-min(a,b);}
int gcd(int a,int b){int c;while(b!=0){c=a%b;a=b;b=c;}return a;}
int lcm(int a,int b){int c=gcd(a,b);a/=c;return a*b;}
int nCr(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
int nHr(int a,int b){return nCr(a+b-1,b);}
int fact(int a){int i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
int dsum(int x){int r=0;while(x){r+=(x%10);x/=10;}return r;}
int dsumb(int x,int b){int r=0;while(x){r+=(x%b);x/=b;}return r;}
int sankaku(int x){return ((1+x)*x)/2;}
void swap(int *a,int *b){int c;c=(*a);(*a)=(*b);(*b)=c;}
//小さい順
int compareInt(const void* a, const void* b){int aNum = *(int*)a;int bNum = *(int*)b;return aNum - bNum;}

int main(void){
    int N,K,X,Y;
    scanf("%d%d%d%d",&N,&K,&X,&Y);
    if(N<=K){
        printf("%d",N*X);
        return 0;
    }else{
        printf("%d",K*X+(N-K)*Y);
        return 0;
    }
}

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

Score : 100 points

Problem Statement

There is a hotel with the following accommodation fee:

X yen (the currency of Japan) per night, for the first K nights

Y yen per night, for the (K+1)-th and subsequent nights

Tak is staying at this hotel for N consecutive nights.
Find his total accommodation fee.

Constraints

1 \leq N, K \leq 10000

1 \leq Y < X \leq 10000

N,\,K,\,X,\,Y are integers.

Input

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

N
K
X
Y

Output

Print Tak's total accommodation fee.

Sample Input 1

5
3
10000
9000

Sample Output 1

48000

The accommodation fee is as follows:

10000 yen for the 1-st night

10000 yen for the 2-nd night

10000 yen for the 3-rd night

9000 yen for the 4-th night

9000 yen for the 5-th night

Thus, the total is 48000 yen.

Sample Input 2

2
3
10000
9000

Sample Output 2

20000

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 <stdio.h>

int main(void) {
  int n, k, x, y;
  scanf("%d %d %d %d", &n, &k, &x, &y);

  printf("%d\n", n<=k ? x*n: k*x+(n-k)*y);
  return 0;
}


#include<stdio.h>
int main()
{
    int day,rank,x,y,sum,r,r2;
    scanf("%d %d",&day,&rank);
    scanf("%d %d",&x,&y);
    if(day<rank){
        printf("%d",day*x);
    }
    else {
    sum=rank*x;
    r=day-rank;
    r2=r*y;
    printf("%d",sum+r2);
    }

}


#include<stdio.h>
int main(void){
int n,k,x,y=0;
scanf("%d",&n);
scanf("%d",&k);
scanf("%d",&x);
scanf("%d",&y);
if(n>k)printf("%d",k*x+(n-k)*y);
printf("%d",n*x);
return 0;
}


#include<stdio.h>
#include<string.h>

int main(void)
{
	char w[101];
	char check[101];
	int i,j,count=1;
	int flag=0;

	for(i=0; i <= 100; i++) w[i] = '\0';
	scanf("%s",w);

	for(i=0; i < 100; i++){
		if(w[i] == 'Z') flag = 1;
		for(j=i+1; j < 100; j++){
			if(w[i] == w[j]){
					if(flag == 1){
						 break;
					}
					count++;
					w[j] = 'Z';
			}

			if( (count % 2) == 0){
				check[i] = 'O';
			}else if((count % 2 != 0) && flag != 1){
				check[i] = 'N';
			}else{
			}

		}

		flag = 0;
		count = 1;
	}

	flag = 0;
	for(i=0; i < 100; i++){
		if(check[i] == 'N') flag = 1;
	}

	if(flag == 0){
		printf("Yes\n");
	}else{
		printf("No\n");
	}

	return 0;
}

Score : 200 points

Problem Statement

Let w be a string consisting of lowercase letters.
We will call w beautiful if the following condition is satisfied:

Each lowercase letter of the English alphabet occurs even number of times in w.

You are given the string w. Determine if w is beautiful.

Constraints

1 \leq |w| \leq 100

w consists of lowercase letters (a-z).

Input

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

w

Output

Print Yes if w is beautiful. Print No otherwise.

Sample Input 1

abaccaba

Sample Output 1

Yes

a occurs four times, b occurs twice, c occurs twice and the other letters occur zero times.

Sample Input 2

hthth

Sample Output 2

No


#include<stdio.h>
#include<string.h>

int main() {
	int loop;
	char w[101];
	int sum[26];
	int i = 0;
	int len;
	int loop1, loop2;

	scanf("%s", &w);
	len = strlen(w);
	for (loop = 0; loop < 26; loop++)
		sum[loop] = 0;

	for (loop1 = 0; loop1 < len; loop1++)
		for (loop2 = 0; loop2 < 26; loop2++) {
			if (w[loop1] == loop2 + 97) sum[loop2]++;
		}

	for (loop = 0; loop < 26; loop++)
		if (sum[loop] % 2 != 0) i++;

	if (i == 0) printf("Yes");
	else printf("No");

}


#include <stdio.h>
typedef long long ll;

ll n, a;
ll x[55];
ll dp[55][55][55 * 55];
ll solve(ll sum, ll card, ll i);

int main()
{
	scanf("%lld %lld", &n, &a);
	for (ll i = 0; i < n; i++) {
		scanf("%lld", &x[i]);
	}

	for (ll i = 0; i < 55; i++) {
		for (ll j = 0; j < 55; j++) {
			for (ll k = 0; k < 55 * 55; k++) {
				dp[i][j][k] = -1;
			}
		}
	}

	printf("%lld\n", solve(0, 0, 0));

	return 0;
}

ll solve(ll sum, ll card, ll i)
{
	if (dp[i][card][sum] != -1) {
		return dp[i][card][sum];
	}

	if (i >= n && sum == card * a && card > 0) {
		return dp[i][card][sum] = 1;
	} else if (i >= n) {
		return dp[i][card][sum] = 0;
	}

	return dp[i][card][sum] = solve(sum + x[i], card + 1, i + 1) +
		solve(sum, card, i + 1);
}

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<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

#define INF 10000000000
#define M 1000000007

#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))

#define B 310

int main() {
	long N, L, Q;
	long x[100001];
	long i, j, k;
	long a, b, tmp;
	long move1day[100001];
	long moveBday[100001] = {0};
	long right;
	long ans[100001] = {0};

	scanf("%ld", &N);
	for(i=1;i<=N;i++) scanf("%ld", &x[i]);
	scanf("%ld", &L);

	right = 2;
	for(i=1;i<=N;i++) {
		for(j=right;j<=N;j++) {
			if(x[j] - x[i] > L) {
				j--;
				break;
			}
		}
		move1day[i] = j;
		right = j;
	}

	for(i=1;i<=N;i++) {
		k = i;
		for(j=0;j<B;j++) {
			if(k >= N) break;
			k = move1day[k];
		}
		if(j == B) moveBday[i] = k;
	}

	scanf("%ld", &Q);
	for(i=1;i<=Q;i++) {
		scanf("%ld %ld", &a, &b);

		if(a > b) { tmp = a; a = b; b = tmp; }

		j = a;
		while(moveBday[j] > 0 && moveBday[j] < b) {
			j = moveBday[j];
			ans[i] += B;
		}

		while(j < b) {
			j = move1day[j];
			ans[i]++;
		}
	}

	for(i=1;i<=Q;i++) printf("%ld\n", ans[i]);

	return 0;
}

Score : 700 points

Problem Statement

N hotels are located on a straight line. The coordinate of the i-th hotel (1 \leq i \leq N) is x_i.

Tak the traveler has the following two personal principles:

He never travels a distance of more than L in a single day.

He never sleeps in the open. That is, he must stay at a hotel at the end of a day.

You are given Q queries. The j-th (1 \leq j \leq Q) query is described by two distinct integers a_j and b_j.
For each query, find the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel following his principles.
It is guaranteed that he can always travel from the a_j-th hotel to the b_j-th hotel, in any given input.

Constraints

2 \leq N \leq 10^5

1 \leq L \leq 10^9

1 \leq Q \leq 10^5

1 \leq x_i < x_2 < ... < x_N \leq 10^9

x_{i+1} - x_i \leq L

1 \leq a_j,b_j \leq N

a_j \neq b_j

N,\,L,\,Q,\,x_i,\,a_j,\,b_j are integers.

Partial Score

200 points will be awarded for passing the test set satisfying N \leq 10^3 and Q \leq 10^3.

Input

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

N
x_1 x_2 ... x_N
L
Q
a_1 b_1
a_2 b_2
:
a_Q b_Q

Output

Print Q lines.
The j-th line (1 \leq j \leq Q) should contain the minimum number of days that Tak needs to travel from the a_j-th hotel to the b_j-th hotel.

Sample Input 1

9
1 3 6 13 15 18 19 29 31
10
4
1 8
7 3
6 7
8 5

Sample Output 1

4
2
1
2

For the 1-st query, he can travel from the 1-st hotel to the 8-th hotel in 4 days, as follows:

Day 1: Travel from the 1-st hotel to the 2-nd hotel. The distance traveled is 2.

Day 2: Travel from the 2-nd hotel to the 4-th hotel. The distance traveled is 10.

Day 3: Travel from the 4-th hotel to the 7-th hotel. The distance traveled is 6.

Day 4: Travel from the 7-th hotel to the 8-th hotel. The distance traveled is 10.


#include<stdio.h>

int main() {
	char s[1001];
	int loop;
	int flag;
	int n = 0, w = 0, S = 0, e = 0;

	scanf("%s", &s);

	for (loop = 0; s[loop] != '\0'; loop++) {
		if (s[loop] == 'N')
			n++;
		else if (s[loop] == 'W')
			w++;
		else if (s[loop] == 'S')
			S++;
		else if (s[loop] == 'E')
			e++;
	}

	if (n != 0 && S == 0) printf("No");
	else if (n == 0 && S != 0) printf("No");
	else if (e == 0 && w != 0)printf("No");
	else if (e != 0 && w == 0) printf("No");
	else printf("Yes");

	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


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<stdio.h>
int main()
{
	int N,i,sum=0;
	scanf("%d",&N);
	for(i=1;i<=N;i++)
	{
		sum=i+sum;
	}
	printf("%d",sum);
	return 0;
}

Score : 100 points

Problem Statement

There are N children in AtCoder Kindergarten. Mr. Evi will arrange the children in a line, then give 1 candy to the first child in the line, 2 candies to the second child, ..., N candies to the N-th child. How many candies will be necessary in total?

Constraints

1≦N≦100

Input

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

N

Output

Print the necessary number of candies in total.

Sample Input 1

3

Sample Output 1

6

The answer is 1+2+3=6.

Sample Input 2

10

Sample Output 2

55

The sum of the integers from 1 to 10 is 55.

Sample Input 3

1

Sample Output 3

1

Only one child. The answer is 1 in this case.


#include<stdio.h>
int main(){
  int n;
  scanf("%d",&n);
  printf("%d",(n+1)*n/2);
  return 0;
}


#include<stdio.h>
int main(){
  int a,i,n;
  scanf("%d",&a);
  for(i=0;i<a;i++){
    n=n+i;
  }
  printf("%d",n);
  return 0;
}


#include<stdio.h>
#include <string.h>

void main(){
  char str[256];
  int i,len,array[10],ind;
  ind=0;
  scanf("%s",str);
  len=strlen(str);

  // 入力
  for(i=0;i<len;i++){
    if(str[i]==48){
      array[ind++]=0;
    }else if(str[i]==49){
      array[ind++]=1;
    }else{
      ind--;
    }
  }

  //出力
  for(i=0;i<ind;i++){
    printf("%d",array[i]);
  }
}

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.


//set many funcs template
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define inf 1072114514
#define llinf 4154118101919364364
#define mod 1000000007

int max(int a,int b){if(a>b){return a;}return b;}
int min(int a,int b){if(a<b){return a;}return b;}
int zt(int a,int b){return max(a,b)-min(a,b);}
int round(int a,int b){if((a%b)*2 >= b){return (a/b)+1;}return a/b;}
int ceil(int a,int b){if(a%b==0){return a/b;}return (a/b)+1;}
int gcd(int a,int b){int c;while(b!=0){c=a%b;a=b;b=c;}return a;}
int lcm(int a,int b){int c=gcd(a,b);a/=c;return a*b;}
int nCr(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
int fact(int a){int i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
int pow(int a,int b){int i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}
long long llmax(long long a,long long b){if(a>b){return a;}return b;}
long long llmin(long long a,long long b){if(a<b){return a;}return b;}
long long llzt(long long a,long long b){return llmax(a,b)-llmin(a,b);}
long long llround(long long a,long long b){if((a%b)*2 >= b){return (a/b)+1;}return a/b;}
long long llceil(long long a,long long b){if(a%b==0){return a/b;}return (a/b)+1;}
long long llgcd(long long a,long long b){long long c;while(b!=0){c=a%b;a=b;b=c;}return a;}
long long lllcm(long long a,long long b){long long c=llgcd(a,b);a/=c;return a*b;}
long long llnCr(long long a,long long b){long long i,r=1;for(i=1;i<=b;i++){r*=(a+1-i);r/=i;}return r;}
long long llfact(long long a){long long i,r=1;for(i=1;i<=a;i++){r*=i;}return r;}
long long llpow(long long a,long long b){long long i,r=1;for(i=1;i<=b;i++){r*=a;}return r;}
double dbmax(double a,double b){if(a>b){return a;}return b;}
double dbmin(double a,double b){if(a<b){return a;}return b;}
double dbzt(double a,double b){return dbmax(a,b)-dbmin(a,b);}
int sortfncsj(const void *a,const void *b){if(*(int *)a>*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;}
int sortfnckj(const void *a,const void *b){if(*(int *)a<*(int *)b){return 1;}if(*(int *)a==*(int *)b){return 0;}return -1;}
int llsortfncsj(const void *a,const void *b){if(*(long long *)a>*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}
int llsortfnckj(const void *a,const void *b){if(*(long long *)a<*(long long *)b){return 1;}if(*(long long *)a==*(long long *)b){return 0;}return -1;}
int dbsortfncsj(const void *a,const void *b){if(*(double *)a>*(double *)b){return 1;}if(*(double *)a==*(double *)b){return 0;}return -1;}
int dbsortfnckj(const void *a,const void *b){if(*(double *)a<*(double *)b){return 1;}if(*(double *)a==*(double *)b){return 0;}return -1;}

int main(void){
    int n,k,i,a[131072],p=0;
    char s[16],t[16]={0};
    scanf("%s",s);
    for(i=0;i<strlen(s);i++){
        if(s[i]=='B'){p=max(0,p-1);t[p]=0;}
        else{t[p]=s[i];p++;}
    }
    printf("%s\n",t);
    return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#define MAX(a,b) (a > b) ? a : b
#define MIN(a,b) (a < b) ? a : b
const int inf = 1000000000; // 10^9

int Amax(int *p,int size){
   int _max = 0;
   for(int i = 0;i < size;i++)
      if(p[i] > _max)_max = p[i];
   return _max;
}
int Amin(int *p,int size){
   int _min = 0;
   for(int i = 0;i < size;i++)
      if(p[i] < _min)_min = p[i];
   return _min;
}
int main(){
   int n;scanf("%d",&n);
   int a[100];
   for(int i = 0;i < n;i++)scanf("%d",&a[i]);
   int ans = 100;
   for(int i = Amin(a,n);i < Amax(a,n);i++){
      int _ans = 0;
      for(int j = 0;j < n;j++){
         _ans += pow(abs(a[j] - i),2);
      }
      //printf("MIN(%d,%d)\n",ans,_ans);
      ans = MIN(ans,_ans);
      //printf("%d:%d\n",i,ans);
   }
   printf("%d\n",ans);
}

Score : 200 points

Problem Statement

Evi has N integers a_1,a_2,..,a_N. His objective is to have N equal integers by transforming some of them.

He may transform each integer at most once. Transforming an integer x into another integer y costs him (x-y)^2 dollars. Even if a_i=a_j (i≠j), he has to pay the cost separately for transforming each of them (See Sample 2).

Find the minimum total cost to achieve his objective.

Constraints

1≦N≦100

-100≦a_i≦100

Input

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

N
a_1 a_2 ... a_N

Output

Print the minimum total cost to achieve Evi's objective.

Sample Input 1

2
4 8

Sample Output 1

8

Transforming the both into 6s will cost (4-6)^2+(8-6)^2=8 dollars, which is the minimum.

Sample Input 2

3
1 1 3

Sample Output 2

3

Transforming the all into 2s will cost (1-2)^2+(1-2)^2+(3-2)^2=3 dollars. Note that Evi has to pay (1-2)^2 dollar separately for transforming each of the two 1s.

Sample Input 3

3
4 2 5

Sample Output 3

5

Leaving the 4 as it is and transforming the 2 and the 5 into 4s will achieve the total cost of (2-4)^2+(5-4)^2=5 dollars, which is the minimum.

Sample Input 4

4
-100 -100 -100 -100

Sample Output 4

0

Without transforming anything, Evi's objective is already achieved. Thus, the necessary cost is 0.


#include <stdio.h>
#include <math.h>

int main(void){
	int n,i,ave,res=0;
	double sum,a[i];
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%lf",&a[i]);
		sum += a[i];
	}
	ave = (int)round(sum/(double)n);
	for(i=0;i<n;i++){
		res += (a[i] - ave) * (a[i] - ave);
	}
	printf("%d\n",res);
}


#include <stdio.h>

int main(void)
{
  int n, a[100], ave, cost;
  int i;

  scanf("%d", &n);

  ave = 0;
  for (i = 0; i < n; i++){
    scanf("%d", &a[i]);
    ave += a[i];
  }
  if (ave % 2 != 0){
    ave++;
  }

  ave /= n;
  cost = 0;
  for (i = 0; i < n; i++){
    cost += (a[i] - ave) * (a[i] - ave);
  }
  printf("%d\n", cost);

  return (0);
}


#include <stdio.h>

int main(void)
{
    int n;
    int a[128];
    int mini;
    int tmp;
    int i, j;

    scanf("%d", &n);
    for (i = 0; i < n; ++i){
        scanf("%d", a + i);
    }

    mini = (int)1e9;
    for (i = -100; i <= 100; ++i){
        tmp = 0;
        for (j = 0; j < n; ++j){
            tmp += (a[j] - i) * (a[j] - i);
        }
        if (tmp < mini){
            mini = tmp;
        }
    }

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

    return (0);
}


#include<stdio.h>
int main()
{
int n,s,l,a[1000],i;
scanf("%d%d",&n,&l);
for(i=0;i<n;i++)
{scanf("%d",&a[i]);
s=s+a[i];
}
if(s<l)
printf("IMPOSSIBLE");
else
printf("POSSIBLE");
return 0;
}

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.


Score : 100 points

Problem Statement

Iroha loves Haiku. Haiku is a short form of Japanese poetry. A Haiku consists of three phrases with 5, 7 and 5 syllables, in this order.

To create a Haiku, Iroha has come up with three different phrases. These phrases have A, B and C syllables, respectively. Determine whether she can construct a Haiku by using each of the phrases once, in some order.

Constraints

1≦A,B,C≦10

Input

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

A B C

Output

If it is possible to construct a Haiku by using each of the phrases once, print YES (case-sensitive). Otherwise, print NO.

Sample Input 1

5 5 7

Sample Output 1

YES

Using three phrases of length 5, 5 and 7, it is possible to construct a Haiku.

Sample Input 2

7 7 5

Sample Output 2

NO


#include<stdio.h>
int main(){
  int a, b, c;
  scanf("%d %d %d", &a, &b, &c);
  if ((a + b + c) == 17) {
    if ((a + b) ==12 || (b + c) == 12) {
      printf("YES");
    } else {
      printf("NO");
    }
  } else {
    printf("NO");
  }
  return 0;
}


#include <stdio.h>

int main(void) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);

    if (a == 5 || a == 7) {
      if (b == 5 || b == 7) {
        if (c == 5 || c == 7) {
          int d = a * b * c;
          if (d == 175) {
            printf("YES");
          }
          else printf("NO");
        }
        else printf("NO");
      }
      else printf("NO");
    }
    else printf("NO");

    return 0;
}


// A - 和風いろはちゃんイージー / Iroha and Haiku (ABC Edition)

#include <stdio.h>

int main(void) {
  int n[3], n5 = 0, n7 = 0;
  scanf("%d %d %d", &n[0], &n[1], &n[2]);
  for (int i = 0; i < 3; i++) {
    if (n[i] == 5) {
      n5++;
    } else if (n[i] == 7) {
      n7++;
    }
  }
  if((n5 == 2) && (n7 == 1)){
    printf("YES\n");
  }else{
    printf("NO\n");
  }
  //printf("%d %d ",n5,n7);
  return 0;
}


#include <stdio.h>
#include <string.h>
void minimize(int * N, int digit, int min){
    int i;
    for(i = 0; i <= digit; i++){
        N[i] = min;
    }
}

void calc(int * N, int K, int * D, int digit, int digit_max, int * moveup, int min, int max){
    if(digit > 0 && N[digit] <= max) calc(N, K, D, digit-1, digit_max, moveup, min, max);
    N[digit] += *moveup;
    if(digit == digit_max && N[digit] == 0) return;
    *moveup = 0;
    if(N[digit] == 10){
        *moveup = 1;
        minimize(N, digit, min);
        return;
    }

    while(1){
        int D_loop = 0;
        for(; D_loop < K; D_loop++){
            if(N[digit] == D[D_loop]) break;
        }
        if(D_loop == K) return;

        N[digit]++;
        if(N[digit] == 10){
            *moveup = 1;
            minimize(N, digit, min);
            return;
        }
    }
}

int main(void){
    // Your code here!
    int N_orig;
    int N_q = -1;
    int N[5] = {0, 0, 0, 0, 0};
    int K;
    int D[9] = {};
    int i;
    int min = 0;
    int max = 9;
    int N_ans = 0;

    scanf("%d %d", &N_orig, &K);
    for(i = 0; i < K; i++) scanf("%d", &D[i]);

    for(i = 0; i < K; i++){
        if(D[i] == min) min++;
        if(D[K-(i+1)] == max) max--;
    }

    N_q = N_orig;
    for(i = 0; N_q != 0; i++){
        N[i] = N_q % 10;
        N_q /= 10;
    }

    int moveup = 0;
    calc(N, K, D, i, i, &moveup, min, max);

    for(i = 4; i >= 0; i--){
        N_ans *= 10;
        N_ans += N[i];
    }
    printf("%d", N_ans);
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
  int N=0,K=0;
  int D_num[10]={0};
  char D[10]={'\0'};
  char N_str[8]={'\0'};
  int flag=0;

  scanf("%d %d\n",&N,&K);
  for(int i=0;i<K;i++) scanf("%d",&D_num[i]);
  for(int i=0;i<K;i++) sprintf(&D[i],"%c",('0'+D_num[i]));
  for(int i=N;i<100001;i++){
    sprintf(N_str,"%d",i);
    flag=0;
    for(int j=0;N_str[j]!='\0';j++){
      for(int k=0;k<K;k++){
        if(N_str[j]==D[k]){
          flag=1;
          break;
        }
      }
      if(flag==1) break;
    }
    if(flag==0) break;
  }
  printf("%s\n",N_str);

  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<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<stdbool.h>
typedef long long ll;
typedef long double ld;
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define rrep(i,l,r)for(ll i=(l);i>=(r);i--)
#define INF (1LL<<60)
#define MOD1 1000000007
#define MOD2 998244353
#define MAX_N (1 << 17)
#define YES printf("Yes\n")
#define NO printf("No\n")
#define PN printf("\n")

void swap(ll *a, ll *b){ll c;c=*b;*b=*a;*a=c;}
ll max2(ll a,ll b){return a>=b?a:b;}
ll min2(ll a,ll b){return a>=b?b:a;}
ll min3(ll a, ll b, ll c){return (a<=b && a<=c) ? a : b<=c ? b : c;}
ll max3(ll a, ll b, ll c){return (a>=b && a>=c) ? a : b>=c ? b : c;}
ll minn(ll n, ll a[n]){ll b=INF;rep(i,0,n) b=min2(b,a[i]);return b;}
ll maxn(ll n, ll a[n]){ll b=-INF;rep(i,0,n) b=max2(b,a[i]);return b;}
ll ABS(ll a){return a>=0?a:(-a);}
ll POW(ll a, ll b){ll c=1;rep(i,0,b) c*=a;return c;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll mod(ll n){return n%=MOD1;}
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}

typedef struct{ ll a , b;}fr;
int cmp1( const void *p, const void *q ) { return ((fr*)p) ->a - ((fr*)q)->a;}
int cmp2( const void *p, const void *q ) { return ((fr*)q) ->a - ((fr*)p)->a;}
void strsortup(fr*a,int n){qsort(a,n,sizeof(fr),cmp1);}
void strsortdown(fr*a,int n){qsort(a,n,sizeof(fr),cmp2);}

/*aのb進数時の桁数*/
ll digits(ll a, ll b){return a/b?1+digits(a/b,b):1;}
/*nをa進数にしたときの、i桁目の数*/
ll a_base2(ll n, ll a, ll i){return i==1?n%a:a_base2(n/a,a,i-1);}

int main(void){
    // char ;
    ll n,k;
    scanf("%lld%lld",&n,&k);
    ll d[k],dig=digits(n,10);
    rep(i,0,k){
        scanf("%lld",&d[i]);
    }

    rep(i,0,dig){
        while(1){
            ll a=a_base2(n,10,i+1),f=0;
            rep(j,0,k){
                if(a==d[j]) break;
                f++;
            }
            if(f==k) break;
            n+=POW(10,i);
        }
    }

    printf("%lld\n",n);
    return 0;
}


Score : 400 points

Problem Statement

We have a large square grid with H rows and W columns.
Iroha is now standing in the top-left cell.
She will repeat going right or down to the adjacent cell, until she reaches the bottom-right cell.

However, she cannot enter the cells in the intersection of the bottom A rows and the leftmost B columns. (That is, there are A×B forbidden cells.) There is no restriction on entering the other cells.

Find the number of ways she can travel to the bottom-right cell.

Since this number can be extremely large, print the number modulo 10^9+7.

Constraints

1 ≦ H, W ≦ 100,000

1 ≦ A < H

1 ≦ B < W

Input

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

H W A B

Output

Print the number of ways she can travel to the bottom-right cell, modulo 10^9+7.

Sample Input 1

2 3 1 1

Sample Output 1

2

We have a 2×3 grid, but entering the bottom-left cell is forbidden. The number of ways to travel is two: "Right, Right, Down" and "Right, Down, Right".

Sample Input 2

10 7 3 4

Sample Output 2

3570

There are 12 forbidden cells.

Sample Input 3

100000 100000 99999 99999

Sample Output 3

1

Sample Input 4

100000 100000 44444 55555

Sample Output 4

738162020


#include <stdio.h>

int main(void) {
	int N, X, sum = 0;

	scanf("%d%d", &N, &X);

	if (N % 2 == 0 && N == 2 * X)sum = N + X;
	if (N > 2 * X)sum = (N - 2 * X) * 3 + N + 2 * X;
	if (N < 2 * X)sum = X + (N - X)*(N + 1);

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

	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<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long long max(long long a,long long b){
    if(a>b){
        return a;
    }else{
        return b;
    }
}
int main()
{
    long long n,x;
    scanf("%lld %lld",&n,&x);
    long long counter=0;
    long long len1,len2,val;
    counter+=n;
    len1=n-max(x,n-x);
    len2=max(x,n-x);
    while(1)
    {if(len2%len1== 0)
    {counter +=(len2/len1)*2*len1 - len1;
    break;}
    else
    {counter+=(len2/len1)*2*len1;
    val=len1;
    len1=len2-(len2/len1)*len1;
    len2=val;}}
    printf("%lld\n",counter);
    return 0;
}

Score : 500 points

Problem Statement

Snuke is conducting an optical experiment using mirrors and his new invention, the rifle of Mysterious Light.

Three mirrors of length N are set so that they form an equilateral triangle.
Let the vertices of the triangle be a, b and c.

Inside the triangle, the rifle is placed at the point p on segment ab such that ap = X.
(The size of the rifle is negligible.)
Now, the rifle is about to fire a ray of Mysterious Light in the direction of bc.

The ray of Mysterious Light will travel in a straight line, and will be reflected by mirrors, in the same ways as "ordinary" light.
There is one major difference, though: it will be also reflected by its own trajectory as if it is a mirror!
When the ray comes back to the rifle, the ray will be absorbed.

The following image shows the ray's trajectory where N = 5 and X = 2.

It can be shown that the ray eventually comes back to the rifle and is absorbed, regardless of the values of N and X.
Find the total length of the ray's trajectory.

Constraints

2≦N≦10^{12}

1≦X≦N-1

N and X are integers.

Partial Points

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

Another 200 points will be awarded for passing the test set without additional constraints.

Input

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

N X

Output

Print the total length of the ray's trajectory.

Sample Input 1

5 2

Sample Output 1

12

Refer to the image in the Problem Statement section.
The total length of the trajectory is 2+3+2+2+1+1+1 = 12.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#define swap(type,a,b) do{type t=a;a=b;b=t;}while(0);
#define MAX(a,b) (((a)>(b))?(a):(b))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define ll long long
#define INF 100000000
#define FOR(i,a,n) for(i=(a);i<(n);i++)
int comp(const void* a,const void* b){
	return *(int*)a-*(int*)b;
}
void fill(int a[],int b,int c){
	int i;
	FOR(i,0,b) a[i]=c;
	return;
}
ll dfs(ll a,ll b){
	if(a>b) swap(ll,a,b);
	if(b%a) return 2*a*((b-1)/a)+dfs(a,b%a);
	else return 2*a*((b-1)/a)+1;
}
int main(void)
{
	ll n,x;
	scanf("%lld%lld",&n,&x);
	printf("%lld\n",n+dfs(x,n-x));
	return 0;
}


#include<stdio.h>

int main()
{
	int n, x;

	scanf("%d%d", &n, &x);

	if(n% x || x== 1)
		printf("%d\n", (n- 1)* 3);
	else
		printf("%d\n", (n- x)* 3);

	return 0;
}