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

typedef long long ll;
#define N_MAX 100000

int cmp(const void *a, const void *b)
{
	const int *pa, *pb;
	pa=a;
	pb=b;
	if(*pa>*pb) return -1;
	return (*pa<*pb);
}

void combsort(int a[], int n)
{
	int h, t, i, c;

	h=n*10/13;
	for(;;)
	{
		c=0;
		for(i=0;i+h<n;i++)
		{
			if(a[i]<a[i+h])
			{
				t=a[i];
				a[i]=a[i+h];
				a[i+h]=t;
				c=1;
			}
		}
		if(h>1)
		{
			h=h*10/13;
		}
		else
		{
			if(!c) break;
		}
	}
}

int main(void)
{
	int N, i;
	static int a[N_MAX*3];
	ll sum;

	while(scanf("%d", &N)==1)
	{
		for(i=0;i<3*N;i++)
		{
			scanf("%d", &a[i]);
		}
		combsort(a, 3*N);

		sum=0;
		for(i=0;i<N;i++)
		{
			sum+=a[i*2+1];
		}
		printf("%lld\n", sum);
	}

	return 0;
}

Score : 300 points

Problem Statement

There are 3N participants in AtCoder Group Contest.
The strength of the i-th participant is represented by an integer a_i.
They will form N teams, each consisting of three participants.
No participant may belong to multiple teams.

The strength of a team is defined as the second largest strength among its members.
For example, a team of participants of strength 1, 5, 2 has a strength 2, and a team of three participants of strength 3, 2, 3 has a strength 3.

Find the maximum possible sum of the strengths of N teams.

Constraints

1 ≤ N ≤ 10^5

1 ≤ a_i ≤ 10^{9}

a_i are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the answer.

Sample Input 1

2
5 2 8 5 1 5

Sample Output 1

10

The following is one formation of teams that maximizes the sum of the strengths of teams:

Team 1: consists of the first, fourth and fifth participants.

Team 2: consists of the second, third and sixth participants.

Sample Input 2

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 2

10000000000

The sum of the strengths can be quite large.


#include<stdio.h>

int main(){

	int A,B;
	int x;

	scanf("%d", &A);
	scanf("%d", &B);

	x = A+B;

	if (x > 23){

		x = x-24;
		printf("%d", x);
	} else {

		printf("%d",x);
	}

	return 0;
}

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

Score : 100 points

Problem Statement

Dolphin loves programming contests. Today, he will take part in a contest in AtCoder.

In this country, 24-hour clock is used. For example, 9:00 p.m. is referred to as "21 o'clock".

The current time is A o'clock, and a contest will begin in exactly B hours.
When will the contest begin? Answer in 24-hour time.

Constraints

0 \leq A,B \leq 23

A and B are integers.

Input

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

A B

Output

Print the hour of the starting time of the contest in 24-hour time.

Sample Input 1

9 12

Sample Output 1

21

In this input, the current time is 9 o'clock, and 12 hours later it will be 21 o'clock in 24-hour time.

Sample Input 2

19 0

Sample Output 2

19

The contest has just started.

Sample Input 3

23 2

Sample Output 3

1

The contest will begin at 1 o'clock the next day.

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){
    // Here your code !
    int a, b;
    scanf("%d %d %d", &a, &b);
    printf("%d\n", (a + b) % 24);
}


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

int main(void)
{
	long int n, c, ans = 11;
	scanf("%ld", &n);
	for (int i = (int)sqrt(n); i > 0; i++)
	{
		if (n % i == 0)
		{
			ans = (long int)log10((double)i) + 1;
			break;
		}
	}
	printf("%ld", ans);
	return 0;
}

Score : 300 points

Problem Statement

You are given an integer N.

For two positive integers A and B, we will define F(A,B) as the larger of the following: the number of digits in the decimal notation of A, and the number of digits in the decimal notation of B.

For example, F(3,11) = 2 since 3 has one digit and 11 has two digits.

Find the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.

Constraints

1 \leq N \leq 10^{10}

N is an integer.

Input

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

N

Output

Print the minimum value of F(A,B) as (A,B) ranges over all pairs of positive integers such that N = A \times B.

Sample Input 1

10000

Sample Output 1

3

F(A,B) has a minimum value of 3 at (A,B)=(100,100).

Sample Input 2

1000003

Sample Output 2

7

There are two pairs (A,B) that satisfy the condition: (1,1000003) and (1000003,1). For these pairs, F(1,1000003)=F(1000003,1)=7.

Sample Input 3

9876543210

Sample Output 3

6


#include <stdio.h>
#include<string.h>
int main()
{
long long int a=0,b=0,w=0,flag,i,str1=0,str2=0;

scanf("%lld%lld%lld",&w,&a,&b);
if(b>a+w)
{
printf("%lld",b-(a+w));
}
else{
    printf("%lld",(a)-(b+w));
}
    return 0;
}

Score : 200 points

Problem Statement

AtCoDeer the deer found two rectangles lying on the table, each with height 1 and width W.
If we consider the surface of the desk as a two-dimensional plane, the first rectangle covers the vertical range of [0,1] and the horizontal range of [a,a+W], and the second rectangle covers the vertical range of [1,2] and the horizontal range of [b,b+W], as shown in the following figure:

AtCoDeer will move the second rectangle horizontally so that it connects with the first rectangle.
Find the minimum distance it needs to be moved.

Constraints

All input values are integers.

1≤W≤10^5

1≤a,b≤10^5

Input

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

W a b

Output

Print the minimum distance the second rectangle needs to be moved.

Sample Input 1

3 2 6

Sample Output 1

1

This input corresponds to the figure in the statement. In this case, the second rectangle should be moved to the left by a distance of 1.

Sample Input 2

3 1 3

Sample Output 2

0

The rectangles are already connected, and thus no move is needed.

Sample Input 3

5 10 1

Sample Output 3

4


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

int descending_compare(const void *a, const void *b){
    if (*(int*)a > *(int*)b){
        return -1;
    }else if (*(int*)a == *(int*)b){
        return 0;
    }else{
        return 1;
    }
}

int ascending_compare(const void *a, const void *b){
    if (*(int*)a < *(int*)b){
        return -1;
    }else if (*(int*)a == *(int*)b){
        return 0;
    }else{
        return 1;
    }
}

// array pointer = *a, num of element = n, key key
int lower_bound(int *a, int n, int key){
    int ng, mid, ok;
    ng = -1, ok = n-1;
    while (abs(ok - ng) > 1){
        mid = (ok + ng) / 2;
        if (key <= a[mid]){
            ok = mid;
        }else{
            ng = mid;
        }
    }
    if (a[ok] >= key)return ok;
    return n;
}

//greatest common divisor
unsigned long gcd(unsigned long x, unsigned long y){
    if (y == 0){
        return x;
    }else if (x > y){
        return gcd(y, x % y);
    }else{
        return gcd(x, y % x);
    }
}

unsigned long lcm(unsigned long x, unsigned long y){
    unsigned long g = gcd(x, y);
    return x*y/g;

}

long long factorial(int x){
    long long rtn = 1;
    int i;
    for (i = x; i > 1; i--){
        rtn = (rtn*i);
    }
    return rtn;
}

/*unsigned long long pascal[100][100] = {0};
void make_pascal(void){
    for (int i = 0; i < 100; i++){
        pascal[i][0] = 1;
    }
    pascal[1][1] = 1;
    for (int i = 2; i < 100; i++){
        for (int j = 1; j < 100; j++){
           pascal[i][j] = (pascal[i-1][j-1]+pascal[i-1][j]) % mod;
        }
    }
}*/
long long mod = 1000000007;

//x ^ n
long long mod_pow(long long x, long long n){
    long long res = 1;
    for(int i = 0;i < 60; i++){
        if(n >> i & 1) res = res * x % mod;
        x = x * x % mod;
    }
    return res;
}

/*int struct_ascending_compare(const void *p, const void *q) {
    return ((structname*)p)->member - ((strcutname*)q)->member;
}*/

int field[3][3];
int tate[3][2];
int yoko[2][3];
int total;

int dfs(int level){
    int chokudai = 0;
    if (level == 9){
        for (int i = 0; i < 3; i++){
            for (int j = 0; j < 2; j++){
                if (field[i][j] == field[i][j+1]){
                    chokudai += tate[i][j];
                }
            }
        }
        for (int i = 0; i < 2; i++){
            for (int j = 0; j < 3; j++){
                if (field[i][j] == field[i+1][j]){
                    chokudai += yoko[i][j];
                }
            }
        }
        return chokudai;
    }

    //空いているマスにlevelに応じて書き込む
    int tmp;
    int max = -1;
    int min = total + 1;
    for (int i = 0; i < 3; i++){
        for (int j = 0; j < 3; j++){
            if (field[i][j] == 0){
                if (level % 2){
                    field[i][j] = -1;
                    tmp = dfs(level+1);
                    field[i][j] = 0;
                    min = min>tmp?tmp:min;
                }else{
                    field[i][j] = 1;
                    tmp = dfs(level+1);
                    field[i][j] = 0;
                    max = max<tmp?tmp:max;
                }
            }
        }
    }
    if (level % 2){
        return min;
    }else{
        return max;
    }
}

int dp[5005];
int a[5005];
int n, k;

int check(int mid){
    memset(dp, 0, sizeof(dp));
    dp[0] = 1;

    for (int i = 0; i < n; i++){
        for (int j = 5000; j >= 0; j--){
            if (i != mid && j - a[i] >= 0 && dp[j-a[i]]){
                dp[j] = 1;
                if (i != mid && dp[j-a[i]] && j < k && j >= k-a[mid]) return 0;
            }
        }
    }

    return 1;
}

int main(void){
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; i++){
        scanf("%d", &a[i]);
    }

    qsort(a, n, sizeof(int), ascending_compare);

    int ng, mid, ok;
    ok = n, ng = -1;
    while (abs(ok - ng) > 1){
        mid = (ok + ng) / 2;
        if (!(mid >= k) && check(mid)){
            ng = mid;
        }else{
            ok = mid;
        }
    }
}

Score : 600 points

Problem Statement

AtCoDeer the deer has N cards with positive integers written on them. The number on the i-th card (1≤i≤N) is a_i.
Because he loves big numbers, he calls a subset of the cards good when the sum of the numbers written on the cards in the subset, is K or greater.

Then, for each card i, he judges whether it is unnecessary or not, as follows:

If, for any good subset of the cards containing card i, the set that can be obtained by eliminating card i from the subset is also good, card i is unnecessary.

Otherwise, card i is NOT unnecessary.

Find the number of the unnecessary cards. Here, he judges each card independently, and he does not throw away cards that turn out to be unnecessary.

Constraints

All input values are integers.

1≤N≤5000

1≤K≤5000

1≤a_i≤10^9 (1≤i≤N)

Partial Score

300 points will be awarded for passing the test set satisfying N,K≤400.

Input

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

N K
a_1 a_2 ... a_N

Output

Print the number of the unnecessary cards.

Sample Input 1

3 6
1 4 3

Sample Output 1

1

There are two good sets: {2,3} and {1,2,3}.

Card 1 is only contained in {1,2,3}, and this set without card 1, {2,3}, is also good. Thus, card 1 is unnecessary.

For card 2, a good set {2,3} without card 2, {3}, is not good. Thus, card 2 is NOT unnecessary.

Neither is card 3 for a similar reason, hence the answer is 1.

Sample Input 2

5 400
3 1 4 1 5

Sample Output 2

5

In this case, there is no good set. Therefore, all the cards are unnecessary.

Sample Input 3

6 20
10 4 3 10 25 2

Sample Output 3

3


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

int A[100000];

int cmpfunc (const void * a, const void * b){
  return *(int *)a - *(int *)b;
}

int main(){
  int i, n;
  long long int s;
  scanf("%d", &n);
  s=0;
  for(i=0;i<n;i++){
    scanf("%d",A+i);
    s+=A[i];
  }

  qsort(A, n, sizeof(int), cmpfunc);

  s -= A[n-1];
  for(i=n-2;i>=0;i--){
    if(2*s < A[i+1]) break;
    s -= A[i];
  }

  printf("%d\n", n-1-i);

  return 0;
}

Score : 400 points

Problem Statement

Snuke found N strange creatures.
Each creature has a fixed color and size. The color and size of the i-th creature are represented by i and A_i, respectively.

Every creature can absorb another creature whose size is at most twice the size of itself.
When a creature of size A and color B absorbs another creature of size C and color D (C \leq 2 \times A), they will merge into one creature of size A+C and color B.
Here, depending on the sizes of two creatures, it is possible that both of them can absorb the other.

Snuke has been watching these creatures merge over and over and ultimately become one creature.
Find the number of the possible colors of this creature.

Constraints

2 \leq N \leq 100000

1 \leq A_i \leq 10^9

A_i is an integer.

Input

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

N
A_1 A_2 … A_N

Output

Print the number of the possible colors of the last remaining creature after the N creatures repeatedly merge and ultimately become one creature.

Sample Input 1

3
3 1 4

Sample Output 1

2

The possible colors of the last remaining creature are colors 1 and 3.
For example, when the creature of color 3 absorbs the creature of color 2, then the creature of color 1 absorbs the creature of color 3, the color of the last remaining creature will be color 1.

Sample Input 2

5
1 1 1 1 1

Sample Output 2

5

There may be multiple creatures of the same size.

Sample Input 3

6
40 1 30 2 7 20

Sample Output 3

4


#include <stdio.h>
int main(void){
  int i,j,N;
  long long p;
  p=1;
  scanf("%d",&N);
  for(i=1; i<=N; i++){
    p*=i;\
    p%=1000000007;
  }
  printf("%d",(int)p);
  return 0;
}

Score : 200 points

Problem Statement

Snuke loves working out. He is now exercising N times.

Before he starts exercising, his power is 1. After he exercises for the i-th time, his power gets multiplied by i.

Find Snuke's power after he exercises N times. Since the answer can be extremely large, print the answer modulo 10^{9}+7.

Constraints

1 ≤ N ≤ 10^{5}

Input

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

N

Output

Print the answer modulo 10^{9}+7.

Sample Input 1

3

Sample Output 1

6

After Snuke exercises for the first time, his power gets multiplied by 1 and becomes 1.

After Snuke exercises for the second time, his power gets multiplied by 2 and becomes 2.

After Snuke exercises for the third time, his power gets multiplied by 3 and becomes 6.

Sample Input 2

10

Sample Output 2

3628800

Sample Input 3

100000

Sample Output 3

457992974

Print the answer modulo 10^{9}+7.


#include<stdio.h>

int main()
{
	int snum,cnum,ans;
	scanf("%d %d",&snum,&cnum);
	if(snum>=cnum*2) ans=snum;
	if(snum<cnum*2) ans=(int)((snum-cnum*2)/3)+snum;

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

Score : 300 points

Problem Statement

Snuke loves puzzles.

Today, he is working on a puzzle using S- and c-shaped pieces.
In this puzzle, you can combine two c-shaped pieces into one S-shaped piece, as shown in the figure below:

Snuke decided to create as many Scc groups as possible by putting together one S-shaped piece and two c-shaped pieces.

Find the maximum number of Scc groups that can be created when Snuke has N S-shaped pieces and M c-shaped pieces.

Constraints

1 ≤ N,M ≤ 10^{12}

Input

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

N M

Output

Print the answer.

Sample Input 1

1 6

Sample Output 1

2

Two Scc groups can be created as follows:

Combine two c-shaped pieces into one S-shaped piece

Create two Scc groups, each from one S-shaped piece and two c-shaped pieces

Sample Input 2

12345 678901

Sample Output 2

175897


Score : 500 points

Problem Statement

Snuke, who loves animals, built a zoo.

There are N animals in this zoo. They are conveniently numbered 1 through N, and arranged in a circle.
The animal numbered i (2≤i≤N-1) is adjacent to the animals numbered i-1 and i+1. Also, the animal numbered 1 is adjacent to the animals numbered 2 and N, and the animal numbered N is adjacent to the animals numbered N-1 and 1.

There are two kinds of animals in this zoo: honest sheep that only speak the truth, and lying wolves that only tell lies.

Snuke cannot tell the difference between these two species, and asked each animal the following question: "Are your neighbors of the same species?" The animal numbered i answered s_i. Here, if s_i is o, the animal said that the two neighboring animals are of the same species, and if s_i is x, the animal said that the two neighboring animals are of different species.

More formally, a sheep answered o if the two neighboring animals are both sheep or both wolves, and answered x otherwise.
Similarly, a wolf answered x if the two neighboring animals are both sheep or both wolves, and answered o otherwise.

Snuke is wondering whether there is a valid assignment of species to the animals that is consistent with these responses. If there is such an assignment, show one such assignment. Otherwise, print -1.

Constraints

3 ≤ N ≤ 10^{5}

s is a string of length N consisting of o and x.

Input

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

N
s

Output

If there does not exist an valid assignment that is consistent with s, print -1.
Otherwise, print an string t in the following format. The output is considered correct if the assignment described by t is consistent with s.

t is a string of length N consisting of S and W.

If t_i is S, it indicates that the animal numbered i is a sheep. If t_i is W, it indicates that the animal numbered i is a wolf.

Sample Input 1

6
ooxoox

Sample Output 1

SSSWWS

For example, if the animals numbered 1, 2, 3, 4, 5 and 6 are respectively a sheep, sheep, sheep, wolf, wolf, and sheep, it is consistent with their responses. Besides, there is another valid assignment of species: a wolf, sheep, wolf, sheep, wolf and wolf.

Let us remind you: if the neiboring animals are of the same species, a sheep answers o and a wolf answers x. If the neiboring animals are of different species, a sheep answers x and a wolf answers o.

Sample Input 2

3
oox

Sample Output 2

-1

Print -1 if there is no valid assignment of species.

Sample Input 3

10
oxooxoxoox

Sample Output 3

SSWWSSSWWS


Score : 200 points

Problem Statement

You are given an image A composed of N rows and N columns of pixels, and a template image B composed of M rows and M columns of pixels.

A pixel is the smallest element of an image, and in this problem it is a square of size 1×1.

Also, the given images are binary images, and the color of each pixel is either white or black.

In the input, every pixel is represented by a character: . corresponds to a white pixel, and # corresponds to a black pixel.

The image A is given as N strings A_1,...,A_N.

The j-th character in the string A_i corresponds to the pixel at the i-th row and j-th column of the image A (1≦i,j≦N).

Similarly, the template image B is given as M strings B_1,...,B_M.

The j-th character in the string B_i corresponds to the pixel at the i-th row and j-th column of the template image B (1≦i,j≦M).

Determine whether the template image B is contained in the image A when only parallel shifts can be applied to the images.

Constraints

1≦M≦N≦50

A_i is a string of length N consisting of # and ..

B_i is a string of length M consisting of # and ..

Input

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

N M
A_1
A_2
:
A_N
B_1
B_2
:
B_M

Output

Print Yes if the template image B is contained in the image A. Print No otherwise.

Sample Input 1

3 2
#.#
.#.
#.#
#.
.#

Sample Output 1

Yes

The template image B is identical to the upper-left 2 × 2 subimage and the lower-right 2 × 2 subimage of A. Thus, the output should be Yes.

Sample Input 2

4 1
....
....
....
....
#

Sample Output 2

No

The template image B, composed of a black pixel, is not contained in the image A composed of white pixels.


#include<stdio.h>

#define N 8
#define M (N*(N-1)/2)

int n, m, a[M], b[M];

int search(int noArray, int *isStoraged, int *seq, int *cnt){
  if(noArray==n){
//    for(int j=0; j<n; j++) { printf("%d",seq[j]); } printf("\n");
    *cnt = *cnt + 1;
  }
  else{
    for(int i=0; i<m; i++){
      if(a[i]==seq[noArray-1] && isStoraged[b[i]-1]==0){
        isStoraged[b[i]-1] = 1;
        seq[noArray] = b[i];
        search(noArray+1, isStoraged, seq, cnt);
        isStoraged[b[i]-1] = 0;
      }
      if(b[i]==seq[noArray-1] && isStoraged[a[i]-1]==0){
         isStoraged[a[i]-1] = 1;
         seq[noArray] = a[i];
         search(noArray+1, isStoraged, seq, cnt);
         isStoraged[a[i]-1] = 0;
      }
    }
  }
  return 0;
}

int main(){
  scanf("%d %d", &n, &m);
  for(int i=0; i<m; i++){
    scanf("%d %d", &a[i], &b[i]);
  }

  int isPass[N]={0}, orderPassed[N]={0}, cnt=0;
  isPass[0]=1;  // node 1 is Passed initially
  orderPassed[0]=1; //  node 1 is Storaged
  search(1, isPass, orderPassed, &cnt);

  printf("%d",cnt);

  return 0;
}

Score : 300 points

Problem Statement

You are given an undirected unweighted graph with N vertices and M edges that contains neither self-loops nor double edges.

Here, a self-loop is an edge where a_i = b_i (1≤i≤M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≤i<j≤M).

How many different paths start from vertex 1 and visit all the vertices exactly once?

Here, the endpoints of a path are considered visited.

For example, let us assume that the following undirected graph shown in Figure 1 is given.

Figure 1: an example of an undirected graph

The following path shown in Figure 2 satisfies the condition.

Figure 2: an example of a path that satisfies the condition

However, the following path shown in Figure 3 does not satisfy the condition, because it does not visit all the vertices.

Figure 3: an example of a path that does not satisfy the condition

Neither the following path shown in Figure 4, because it does not start from vertex 1.

Figure 4: another example of a path that does not satisfy the condition

Constraints

2≦N≦8

0≦M≦N(N-1)/2

1≦a_i<b_i≦N

The given graph contains neither self-loops nor double edges.

Input

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

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the number of the different paths that start from vertex 1 and visit all the vertices exactly once.

Sample Input 1

3 3
1 2
1 3
2 3

Sample Output 1

2

The given graph is shown in the following figure:

The following two paths satisfy the condition:

Sample Input 2

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

Sample Output 2

1

This test case is the same as the one described in the problem statement.


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

typedef struct {
	int a;
	int b;
} pass_t;

static int Count;

static void route(pass_t *pass, int M, int N, char history[8], int target);

int main(int argc, char *argv[])
{
	int i, N, M;
	pass_t pass[28];
	char history[8] = {0};

	scanf("%d %d", &N, &M);
	for ( i = 0; i < M; i++ ){
		scanf("%d %d", &pass[i].a, &pass[i].b);
	}
	route(pass, M, N, history, 1);
	printf("%d\n", Count);
	return 0;
}

static void route(pass_t *pass, int M, int N, char history[8], int target)
{
	char h[8];
	int i;

	if ( history[ target - 1 ]  == 1 ){
		return;
	}
	memcpy(h, history, 8);
	h[ target - 1] = 1;

	for ( i = 0; i < N; i++ ){
		if ( h[i] != 1 ){
			break;
		}
	}
	if ( i == N ){
		Count++;
	}

	for ( i = 0; i < M; i++ ){
		if ( pass[i].a == target ){
			route(pass, M, N, h, pass[i].b);
		} else if (pass[i].b == target ){
			route(pass, M, N, h, pass[i].a);
		}
	}
}


#include <stdio.h>
#define MAX_N 40
#define MAX_A 10
#define MAX_B 10
#define INF 1000000007

int min(int x, int y) {
    return (x < y) ? x : y;
}

int main() {
    int n, ma, mb;
    scanf("%d %d %d", &n, &ma, &mb);

    int dp[MAX_N * MAX_A + 1][MAX_N * MAX_B + 1];
    for (int i = 0; i <= MAX_N * MAX_A; i++) {
        for (int j = 0; j <= MAX_N * MAX_B; j++) {
            dp[i][j] = INF;
        }
    }
    dp[0][0] = 0;
    for (int k = 0; k < n; k++) {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        for (int i = MAX_N * MAX_A; i >= a; i--) {
            for (int j = MAX_N * MAX_B; j >= b; j--) {
                dp[i][j] = min(dp[i][j], dp[i - a][j - b] + c);
            }
        }
    }
    int ans = INF;
    for (int ia = ma, ib = mb; ia <= MAX_N * MAX_A && ib <= MAX_N * MAX_B; ia += ma, ib += mb) {
        ans = min(dp[ia][ib], ans);
    }
    printf("%d\n", (ans == INF) ? -1 : ans);
    return 0;
}

Score : 400 points

Problem Statement

Dolphin is planning to generate a small amount of a certain chemical substance C.

In order to generate the substance C, he must prepare a solution which is a mixture of two substances A and B in the ratio of M_a:M_b.

He does not have any stock of chemicals, however, so he will purchase some chemicals at a local pharmacy.

The pharmacy sells N kinds of chemicals. For each kind of chemical, there is exactly one package of that chemical in stock.

The package of chemical i contains a_i grams of the substance A and b_i grams of the substance B, and is sold for c_i yen (the currency of Japan).

Dolphin will purchase some of these packages. For some reason, he must use all contents of the purchased packages to generate the substance C.

Find the minimum amount of money required to generate the substance C.

If it is not possible to generate the substance C by purchasing any combination of packages at the pharmacy, report that fact.

Constraints

1≦N≦40

1≦a_i,b_i≦10

1≦c_i≦100

1≦M_a,M_b≦10

gcd(M_a,M_b)=1

a_i, b_i, c_i, M_a and M_b are integers.

Input

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

N M_a M_b
a_1 b_1 c_1
a_2 b_2 c_2
:
a_N b_N c_N

Output

Print the minimum amount of money required to generate the substance C. If it is not possible to generate the substance C, print -1 instead.

Sample Input 1

3 1 1
1 2 1
2 1 2
3 3 10

Sample Output 1

3

The amount of money spent will be minimized by purchasing the packages of chemicals 1 and 2.

In this case, the mixture of the purchased chemicals will contain 3 grams of the substance A and 3 grams of the substance B, which are in the desired ratio: 3:3=1:1.

The total price of these packages is 3 yen.

Sample Input 2

1 1 10
10 10 10

Sample Output 2

-1

The ratio 1:10 of the two substances A and B cannot be satisfied by purchasing any combination of the packages. Thus, the output should be -1.


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

int main(void) {
    char buf[1000];
    int N;
    char* a;
    int Odd = 0;

    fgets(buf, sizeof(buf), stdin);
    sscanf(buf, "%d\n", &N);
    fgets(buf, sizeof(buf), stdin);
    a = strtok(buf, " ");
    if (atoi(a) % 2)
        Odd++;
    while ((a = strtok(NULL, " ")) != NULL) {
        if (atoi(a) % 2)
            Odd++;
    }
  if(Odd%2!=1||N==1)
    printf("Yes");
  else
    printf("No");

    return 0;
}

Score : 300 points

Problem Statement

There are N integers written on a blackboard. The i-th integer is A_i.

Takahashi will repeatedly perform the following operation on these numbers:

Select a pair of integers, A_i and A_j, that have the same parity (that is, both are even or both are odd) and erase them.

Then, write a new integer on the blackboard that is equal to the sum of those integers, A_i+A_j.

Determine whether it is possible to have only one integer on the blackboard.

Constraints

2 ≦ N ≦ 10^5

1 ≦ A_i ≦ 10^9

A_i is an integer.

Input

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

N
A_1 A_2 … A_N

Output

If it is possible to have only one integer on the blackboard, print YES. Otherwise, print NO.

Sample Input 1

3
1 2 3

Sample Output 1

YES

It is possible to have only one integer on the blackboard, as follows:

Erase 1 and 3 from the blackboard, then write 4. Now, there are two integers on the blackboard: 2 and 4.

Erase 2 and 4 from the blackboard, then write 6. Now, there is only one integer on the blackboard: 6.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

NO


#include <stdio.h>

int main(void){
	long int N, r=0, ans=0,i,j;
	scanf("%ld",&N);
	long int a[N];

	for(i = 0; i < N; i++){
		scanf("%ld",&a[i]);

		for(j = 0; j < i; j++){
			if(a[i] == a[j]){
				a[i] = 0;
				r++;
			}
		}
	}
	for(i = 0; i < N; i++){
		if(a[i] > 0) ans++;
	}
	printf("%ld",ans - r % 2);
	return 0;
}

Score : 400 points

Problem Statement

Snuke has decided to play a game using cards.
He has a deck consisting of N cards. On the i-th card from the top, an integer A_i is written.

He will perform the operation described below zero or more times, so that the values written on the remaining cards will be pairwise distinct. Find the maximum possible number of remaining cards. Here, N is odd, which guarantees that at least one card can be kept.

Operation: Take out three arbitrary cards from the deck. Among those three cards, eat two: one with the largest value, and another with the smallest value. Then, return the remaining one card to the deck.

Constraints

3 ≦ N ≦ 10^{5}

N is odd.

1 ≦ A_i ≦ 10^{5}

A_i is an integer.

Input

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

N
A_1 A_2 A_3 ... A_{N}

Output

Print the answer.

Sample Input 1

5
1 2 1 3 7

Sample Output 1

3

One optimal solution is to perform the operation once, taking out two cards with 1 and one card with 2. One card with 1 and another with 2 will be eaten, and the remaining card with 1 will be returned to deck. Then, the values written on the remaining cards in the deck will be pairwise distinct: 1, 3 and 7.

Sample Input 2

15
1 3 5 2 1 3 2 8 8 6 2 6 11 1 1

Sample Output 2

7


#include <stdio.h>
int main() {
    int a,b,c,d;
    scanf("%d",&a);
    scanf("%d",&b);
    scanf("%d",&c);
    scanf("%d",&d);
    if(a*b < c*d) {
        printf("%d",c*d);
    }else{
        printf("%d", a*b);
    }
}

Score : 100 points

Problem Statement

There are two rectangles.
The lengths of the vertical sides of the first rectangle are A, and the lengths of the horizontal sides of the first rectangle are B.
The lengths of the vertical sides of the second rectangle are C, and the lengths of the horizontal sides of the second rectangle are D.

Print the area of the rectangle with the larger area.
If the two rectangles have equal areas, print that area.

Constraints

All input values are integers.

1≤A≤10^4

1≤B≤10^4

1≤C≤10^4

1≤D≤10^4

Input

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

A B C D

Output

Print the area of the rectangle with the larger area.
If the two rectangles have equal areas, print that area.

Sample Input 1

3 5 2 7

Sample Output 1

15

The first rectangle has an area of 3×5=15, and the second rectangle has an area of 2×7=14.
Thus, the output should be 15, the larger area.

Sample Input 2

100 600 200 300

Sample Output 2

60000


#include <stdio.h>

int main(void) {
  char s[20];
  scanf("%s", s);

  for (int i=0; s[i]!='\0'; i++) {
    if (s[i]==',') s[i]=' ';
  }

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

Score : 100 points

Problem Statement

As a New Year's gift, Dolphin received a string s of length 19.

The string s has the following format: [five lowercase English letters],[seven lowercase English letters],[five lowercase English letters].

Dolphin wants to convert the comma-separated string s into a space-separated string.

Write a program to perform the conversion for him.

Constraints

The length of s is 19.

The sixth and fourteenth characters in s are ,.

The other characters in s are lowercase English letters.

Input

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

s

Output

Print the string after the conversion.

Sample Input 1

happy,newyear,enjoy

Sample Output 1

happy newyear enjoy

Replace all the commas in happy,newyear,enjoy with spaces to obtain happy newyear enjoy.

Sample Input 2

haiku,atcoder,tasks

Sample Output 2

haiku atcoder tasks

Sample Input 3

abcde,fghihgf,edcba

Sample Output 3

abcde fghihgf edcba


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

int main(){

    char line[256];

    fgets(line,sizeof(line),stdin);

    line[5] = ' ';
    line[13] = ' ';

    printf("%s",line);

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#define MAX(X, Y) (((X) > (Y))? (X) : (Y))
#define MIN(X, Y) (((X) < (Y))? (X) : (Y))
int compare_int(const void *a, const void *b){
  return *(long int*)b - *(long int*)a; // 大きい順
}
// min
int main(void){
  int sx, sy, tx, ty;
  int h = 0, w = 0;

  scanf("%d%d%d%d", &sx, &sy, &tx, &ty);

  if(sx >= 0 && tx >= 0){
    w = tx - sx;
  }else if(sx < 0 && tx >= 0){
    w = (-1) * sx + tx;
  }else if(sx < 0 && tx < 0){
    w = (-1) * sx - (-1) * tx;
  }

  if(sy >= 0 && ty >= 0){
    h = ty - sy;
  }else if(sy < 0 && ty >= 0){
    h = (-1) * sy + ty;
  }else if(sy < 0 && ty < 0){
    h = (-1) * sy - (-1) * ty;
  }

  //  printf("h = %d, w = %d\n", h, w);
  // 内側
  // (tx - sx) * R
  for(int i = 0; i < w; i++){
    printf("R");
  }
  // (ty - sy) * U
  for(int i = 0; i < h; i++){
    printf("U");
  }
  // (tx - sx) * L
  for(int i = 0; i < w; i++){
    printf("L");
  }
  // (ty - sy) * D
  for(int i = 0; i < h; i++){
    printf("D");
  }

  // 外側
  // D, ((tx - sx) + 1) * R
  printf("D");
  for(int i = 0; i < w + 1; i++){
    printf("R");
  }
  // ((ty - sy) + 1) *  U, L
  for(int i = 0; i < h + 1; i++){
    printf("U");
  }
  printf("L");
  // U, ((tx - sx) + 1) * L
  printf("U");
  for(int i = 0; i < w + 1; i++){
    printf("L");
  }
  // ((ty - sy) + 1) * D, R
  for(int i = 0; i < h + 1; i++){
    printf("D");
  }
  printf("R\n");

  return 0;
}

Score : 300 points

Problem Statement

Dolphin resides in two-dimensional Cartesian plane, with the positive x-axis pointing right and the positive y-axis pointing up.

Currently, he is located at the point (sx,sy). In each second, he can move up, down, left or right by a distance of 1.

Here, both the x- and y-coordinates before and after each movement must be integers.

He will first visit the point (tx,ty) where sx < tx and sy < ty, then go back to the point (sx,sy), then visit the point (tx,ty) again, and lastly go back to the point (sx,sy).

Here, during the whole travel, he is not allowed to pass through the same point more than once, except the points (sx,sy) and (tx,ty).

Under this condition, find a shortest path for him.

Constraints

-1000 ≤ sx < tx ≤ 1000

-1000 ≤ sy < ty ≤ 1000

sx,sy,tx and ty are integers.

Input

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

sx sy tx ty

Output

Print a string S that represents a shortest path for Dolphin.

The i-th character in S should correspond to his i-th movement.

The directions of the movements should be indicated by the following characters:

U: Up

D: Down

L: Left

R: Right

If there exist multiple shortest paths under the condition, print any of them.

Sample Input 1

0 0 1 2

Sample Output 1

UURDDLLUUURRDRDDDLLU

One possible shortest path is:

Going from (sx,sy) to (tx,ty) for the first time: (0,0) → (0,1) → (0,2) → (1,2)

Going from (tx,ty) to (sx,sy) for the first time: (1,2) → (1,1) → (1,0) → (0,0)

Going from (sx,sy) to (tx,ty) for the second time: (0,0) → (-1,0) → (-1,1) → (-1,2) → (-1,3) → (0,3) → (1,3) → (1,2)

Going from (tx,ty) to (sx,sy) for the second time: (1,2) → (2,2) → (2,1) → (2,0) → (2,-1) → (1,-1) → (0,-1) → (0,0)

Sample Input 2

-2 -2 1 1

Sample Output 2

UURRURRDDDLLDLLULUUURRURRDDDLLDL


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

#define INF 114514810

struct iset_t {
	int data;
	struct iset_t* next[2];
};

struct iset_t* generateSet(int data, struct iset_t* next1, struct iset_t* next2) {
	struct iset_t* generated = malloc(sizeof(struct iset_t));
	generated->data = data;
	generated->next[0] = next1;
	generated->next[1] = next2;
	return generated;
}

int N, M;
int cost[128][128];
struct iset_t* usedEdges[128][128];

char edgeUsed[1024];

void traverseSet(struct iset_t* set) {
	if (set == NULL || set->data < 0) return;
	if (set->data > 0) edgeUsed[set->data] = 1;
	traverseSet(set->next[0]);
	traverseSet(set->next[1]);
}

int main(void) {
	int i, j, k;
	int count;
	if (scanf("%d%d", &N, &M) != 2) return 1;
	for (i = 1; i <= N; i++) {
		for (j = 1; j <= N; j++) {
			cost[i][j] = INF;
			usedEdges[i][j] = NULL;
		}
		cost[i][i] = 0;
	}
	for (i = 1; i <= M; i++) {
		int a, b, c;
		if (scanf("%d%d%d", &a, &b, &c) != 3) return 1;
		cost[a][b] = cost[b][a] = c;
		usedEdges[a][b] = usedEdges[b][a] = generateSet(i, NULL, NULL);
	}
	for (k = 1; k <= N; k++) {
		for (i = 1; i <= N; i++) {
			for (j = 1; j <= N; j++) {
				int current = cost[i][j];
				int candidate = cost[i][k] + cost[k][j];
				if (candidate < current) {
					cost[i][j] = candidate;
					usedEdges[i][j] = generateSet(0, usedEdges[i][k], usedEdges[k][j]);
				} else if (candidate == current) {
					usedEdges[i][j] = generateSet(0, usedEdges[i][j],
						generateSet(0, usedEdges[i][k], usedEdges[k][j]));
				}
			}
		}
	}
	for (i = 1; i <= N; i++) {
		for (j = 1; j <= N; j++) {
			traverseSet(usedEdges[i][j]);
		}
	}
	count = 0;
	for (i = 1; i <= M; i++) {
		if (!edgeUsed[i]) count++;
	}
	printf("%d\n", count);
	return 0;
}

Score : 400 points

Problem Statement

You are given an undirected connected weighted graph with N vertices and M edges that contains neither self-loops nor double edges.

The i-th (1≤i≤M) edge connects vertex a_i and vertex b_i with a distance of c_i.

Here, a self-loop is an edge where a_i = b_i (1≤i≤M), and double edges are two edges where (a_i,b_i)=(a_j,b_j) or (a_i,b_i)=(b_j,a_j) (1≤i<j≤M).

A connected graph is a graph where there is a path between every pair of different vertices.

Find the number of the edges that are not contained in any shortest path between any pair of different vertices.

Constraints

2≤N≤100

N-1≤M≤min(N(N-1)/2,1000)

1≤a_i,b_i≤N

1≤c_i≤1000

c_i is an integer.

The given graph contains neither self-loops nor double edges.

The given graph is connected.

Input

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

N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

Output

Print the number of the edges in the graph that are not contained in any shortest path between any pair of different vertices.

Sample Input 1

3 3
1 2 1
1 3 1
2 3 3

Sample Output 1

1

In the given graph, the shortest paths between all pairs of different vertices are as follows:

The shortest path from vertex 1 to vertex 2 is: vertex 1 → vertex 2, with the length of 1.

The shortest path from vertex 1 to vertex 3 is: vertex 1 → vertex 3, with the length of 1.

The shortest path from vertex 2 to vertex 1 is: vertex 2 → vertex 1, with the length of 1.

The shortest path from vertex 2 to vertex 3 is: vertex 2 → vertex 1 → vertex 3, with the length of 2.

The shortest path from vertex 3 to vertex 1 is: vertex 3 → vertex 1, with the length of 1.

The shortest path from vertex 3 to vertex 2 is: vertex 3 → vertex 1 → vertex 2, with the length of 2.

Thus, the only edge that is not contained in any shortest path, is the edge of length 3 connecting vertex 2 and vertex 3, hence the output should be 1.

Sample Input 2

3 2
1 2 1
2 3 1

Sample Output 2

0

Every edge is contained in some shortest path between some pair of different vertices.


#include <stdio.h>
#define INF 1 << 29

int min(int a,int b){
  if(a < b){
    return a;
  }
  return b;
}

int main(void){
  int N,M;
  scanf("%d%d",&N,&M);

  int a[M],b[M],c[M],dist[N][N];

  for(int i = 0;i < N;i++){
    for(int j = 0;j < N;j++){
      if(i == j)
        dist[i][j] = 0;
      else
        dist[i][j] = INF;
    }
  }

  for(int i = 0;i < M;i++){
    scanf("%d%d%d",&a[i],&b[i],&c[i]);
    a[i]--; b[i]--;
    dist[a[i]][b[i]] = dist[b[i]][a[i]] = c[i];
  }

  for(int k = 0;k < N;k++){
    for(int i = 0;i < N;i++){
      for(int j = 0;j < N;j++){
        dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
      }
    }
  }

  int ans = 0;
  for(int i = 0;i < M;i++){
    if(dist[a[i]][b[i]] < c[i]) ans++;
  }

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

  return 0;
}


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

int main(void) {
	int x, y, z;
	scanf("%d%d", &x, &y);
	if ((x>0 && y>0 && x<=y) || (x<0 && y<0 && x<=y)) {
		z = y - x;
	} else if ((x>0 && y>0) || (x<0 && y<0)) {
		z = x - y + 2;
	} else if (x == 0 && y>0) {
		z = y;
	} else {
		z = abs(abs(y)-abs(x)) + 1;
	}

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

Score : 300 points

Problem Statement

Snuke has a calculator. It has a display and two buttons.

Initially, the display shows an integer x.
Snuke wants to change this value into another integer y, by pressing the following two buttons some number of times in arbitrary order:

Button A: When pressed, the value on the display is incremented by 1.

Button B: When pressed, the sign of the value on the display is reversed.

Find the minimum number of times Snuke needs to press the buttons to achieve his objective.
It can be shown that the objective is always achievable regardless of the values of the integers x and y.

Constraints

x and y are integers.

|x|, |y| ≤ 10^9

x and y are different.

Input

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

x y

Output

Print the minimum number of times Snuke needs to press the buttons to achieve his objective.

Sample Input 1

10 20

Sample Output 1

10

Press button A ten times.

Sample Input 2

10 -10

Sample Output 2

1

Press button B once.

Sample Input 3

-10 -20

Sample Output 3

12

Press the buttons as follows:

Press button B once.

Press button A ten times.

Press button B once.


Score : 600 points

Problem Statement

A tetromino is a figure formed by joining four squares edge to edge.
We will refer to the following seven kinds of tetromino as I-, O-, T-, J-, L-, S- and Z-tetrominos, respectively:

Snuke has many tetrominos. The number of I-, O-, T-, J-, L-, S- and Z-tetrominos in his possession are a_I, a_O, a_T, a_J, a_L, a_S and a_Z, respectively.
Snuke will join K of his tetrominos to form a rectangle that is two squares tall and 2K squares wide.
Here, the following rules must be followed:

When placing each tetromino, rotation is allowed, but reflection is not.

Each square in the rectangle must be covered by exactly one tetromino.

No part of each tetromino may be outside the rectangle.

Snuke wants to form as large a rectangle as possible.
Find the maximum possible value of K.

Constraints

0≤a_I,a_O,a_T,a_J,a_L,a_S,a_Z≤10^9

a_I+a_O+a_T+a_J+a_L+a_S+a_Z≥1

Input

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

a_I a_O a_T a_J a_L a_S a_Z

Output

Print the maximum possible value of K. If no rectangle can be formed, print 0.

Sample Input 1

2 1 1 0 0 0 0

Sample Output 1

3

One possible way to form the largest rectangle is shown in the following figure:

Sample Input 2

0 0 10 0 0 0 0

Sample Output 2

0

No rectangle can be formed.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#include <assert.h>
#define FOR(i,a,n) for(i=(a);i<(n);i++)
#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 MOD 1000000007
#define SIZE 100005
#define LOCAL
#ifdef LOCAL
    #define DEBUG(...) printf(__VA_ARGS__)
#else
    #define DEBUG(...)
#endif
int comp(const void* a,const void* b){
	return *(int*)a-*(int*)b;
}
ll a[7];
int main(void)
{
	int i;
	ll res;
	FOR(i,0,7) scanf("%lld",a+i);
	res=a[0]/2*2+a[1]+a[3]/2*2+a[4]/2*2;
	if(a[0]%2&&a[3]%2&&a[4]%2) res+=3;
	printf("%lld\n",res);
	return 0;
}


#include<stdio.h>
int main()
{
    int a,b,r;
    char op;
    scanf("%d %c %d",&a,&op,&b);
    switch(op)
    {
    case '+':
        {
          r=a+b;
          printf("%d\n",r);
          break;
        }
    case '-':
        {
            r=a-b;
            printf("%d\n",r);
            break;
        }

    }
    return 0;

}

Score : 100 points

Problem Statement

Joisino wants to evaluate the formula "A op B".
Here, A and B are integers, and the binary operator op is either + or -.
Your task is to evaluate the formula instead of her.

Constraints

1≦A,B≦10^9

op is either + or -.

Input

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

A op B

Output

Evaluate the formula and print the result.

Sample Input 1

1 + 2

Sample Output 1

3

Since 1 + 2 = 3, the output should be 3.

Sample Input 2

5 - 7

Sample Output 2

-2


#include<stdio.h>
int main()
{
	int a;
	char s;
	int b;
	scanf(" %d",&a);
	scanf(" %c",&s);
	scanf(" %d",&b);
	if(s=='+')
	printf("%d\n",a+b);
	else
	printf("%d\n",a-b);
}


#include<stdio.h>
int main()
{
    char vc;
    scanf("%c",&vc);
    if(vc=='a' || vc=='e' || vc=='i' || vc=='o' || vc=='u')
    {
        printf("vowel\n");
    }
    else
    {
        printf("consonant\n");
    }
    return 0;
}

Score : 100 points

Problem Statement

Given a lowercase English letter c, determine whether it is a vowel. Here, there are five vowels in the English alphabet: a, e, i, o and u.

Constraints

c is a lowercase English letter.

Input

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

c

Output

If c is a vowel, print vowel. Otherwise, print consonant.

Sample Input 1

a

Sample Output 1

vowel

Since a is a vowel, print vowel.

Sample Input 2

z

Sample Output 2

consonant

Sample Input 3

s

Sample Output 3

consonant


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

#define _CRT_SECURE_NO_WARNINGS
#define TLong long long

// fact(n)
int fact(int n){
	if(n == 1)	return 1;
	else return (n * fact(n - 1));
}

// combination(n,r)
int combination(int n, int r){

	if(r == 0 || r == n)	return 1;
	else if(r == 1)	return n;
	return (combination(n - 1, r - 1) + combination(n - 1, r));
}

// gcd(a,b)
int gcd(int a,int b){
	int ret;
	ret = (b == 0) ? a : gcd(b, a % b);
	return ret;
}

// lcm(a.b)
int lcm(int a,int b){
	return (a * b) / gcd(a,b);
}

// qsort(array, size, sizeof(int),comp);
int comp(const int *a,const int *b){
	return (*a - *b);
}

// append(array,*size,addNum)
void append(int *array,int *size,int num){
	realloc(array, sizeof(int) * (*size + 1));
	array[(*size)] = num;
	++(*size);
}

// pop(array,*size) & nongetpop(array,*size)
int pop(int *array,int *size){
	int temp = array[(*size - 1)];
	realloc(array,sizeof(int) * (*size - 1));
	--(*size);
	return temp;
}

void nongetpop(int *array,int *size){
	realloc(array,sizeof(int) * (*size - 1));
	--(*size);
}

int main(int argc, char const *argv[]){
	char string[] = "aiueo";
	char ch;
	scanf("%c",ch);
	for (int i = 0; i < 5; ++i)
	{
		if(ch == string[i]){
			puts("Yes");
			return 0;
		}
	}
	puts("No");
}


#include<stdio.h>
int main()
{
    int no1,no2,i;
    scanf("%d%d",&no1,&no2);
    char s[100][100];
    getchar();
    for(i=0; i<no1; i++)
    {
        gets(s[i]);
    }
    for(i=0; i<no1; i++)
    {
        puts(s[i]);
        puts(s[i]);
    }
    return 0;
}


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

#define ll long long
#define fr(i,l,r) for(i=(l);i<(r);i++)
#define min(p,q)((p)<(q)?(p):(q))

static int par[200010];//親
static int rank[200010];//木の深さ

//n要素で初期化
void init(int n){
	int i;
	for(i=0;i<n;i++){
		par[i]=i;
		rank[i]=0;
	}
}

//木の根を求める
int find(int x){
	if(par[x]==x){
		return x;
	}else{
		par[x]=find(par[x]);
		return par[x];
	}
}

//xとyの属する集合を併合
void unite(int x, int y){
	x=find(x);
	y=find(y);
	if(x==y) return;
	if(rank[x]<rank[y]){
		par[x]=y;
	}else{
		par[y]=x;
		if(rank[x]==rank[y])rank[x]++;
	}
}

static int par2[200010];//親
static int rank2[200010];//木の深さ

//n要素で初期化
void init2(int n){
	int i;
	for(i=0;i<n;i++){
		par2[i]=i;
		rank2[i]=0;
	}
}

//木の根を求める
int find2(int x){
	if(par2[x]==x){
		return x;
	}else{
		par2[x]=find2(par2[x]);
		return par2[x];
	}
}

//xとyの属する集合を併合
void unite2(int x, int y){
	x=find2(x);
	y=find2(y);
	if(x==y) return;
	if(rank2[x]<rank2[y]){
		par2[x]=y;
	}else{
		par2[y]=x;
		if(rank2[x]==rank2[y])rank2[x]++;
	}
}

struct tosi_t{
	int idnum;
	ll num;
}

;int compare_tosi(const void *a, const void *b) {
  struct tosi_t *A = (struct tosi_t *)a;
  struct tosi_t *B = (struct tosi_t *)b;
  return A->num - B->num;
}

int main(void)
{
	//変数の宣言
	int n;
	//int k,l;
	int p,q,r,s;
	static struct tosi_t tosi[200010];
	int pr[200010];

	//よく使う変数
	int i,j,k,l;
	int flag=0;
	int ans=0;
	int count=0;
	int temp,temp1,temp2;
	int max,min;
	int len;
	//データの読み込み

	init(200010);
	init2(200010);

	scanf("%d %d %d",&n,&k,&l);
	for(i=0;i<k;i++){
		scanf("%d %d",&p,&q);
		unite(p,q);
	}
	for(i=0;i<l;i++){
		scanf("%d %d",&r,&s);
		unite2(r,s);
	}

//	printf("nは%dです\n", n);
//	printf("データの読み込み終了\n");
	//実際の処理

	for(i=1;i<=n;i++){
		tosi[i].num=par[i]*1000000+par2[i];
		tosi[i].idnum=i;
	}

//	for(i=1;i<=n;i++){
//		printf("par[%d]=%d,par2[%d]=%d num=%d\n",i,par[i],i,par2[i],tosi[i].num);
//	}

	qsort(tosi, n, sizeof(struct tosi_t), compare_tosi);

//	for(i=1;i<=n;i++){
//		printf("par[%d]=%d,par2[%d]=%d num=%d\n",i,par[i],i,par2[i],tosi[i].num);
//	}

	i=1;
	j=1;
	while(j<n+1){
//		printf("i=%d,j=%d\n",i,j);
		if(tosi[i].num==tosi[j].num){
			i++;
			flag=1;
		}else{
			if(flag==1) count=i-j;
			pr[tosi[j].idnum]=count;
//			printf("tosi[j].idnum=%d,count=%d\n",tosi[j].idnum,count);
			j++;
			flag=0;
		}
	}

	for(i=1;i<=n;i++){
		if(i!=1) printf(" ");
		printf("%d",pr[i]);
	}

//	printf("計算部分終了\n");
	//出力

//	printf("結果の出力終了\n");

	return 0;

}

Score : 400 points

Problem Statement

There are N cities. There are also K roads and L railways, extending between the cities.
The i-th road bidirectionally connects the p_i-th and q_i-th cities, and the i-th railway bidirectionally connects the r_i-th and s_i-th cities.
No two roads connect the same pair of cities. Similarly, no two railways connect the same pair of cities.

We will say city A and B are connected by roads if city B is reachable from city A by traversing some number of roads. Here, any city is considered to be connected to itself by roads.
We will also define connectivity by railways similarly.

For each city, find the number of the cities connected to that city by both roads and railways.

Constraints

2 ≦ N ≦ 2*10^5

1 ≦ K, L≦ 10^5

1 ≦ p_i, q_i, r_i, s_i ≦ N

p_i < q_i

r_i < s_i

When i ≠ j, (p_i, q_i) ≠ (p_j, q_j)

When i ≠ j, (r_i, s_i) ≠ (r_j, s_j)

Input

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

N K L
p_1 q_1
:
p_K q_K
r_1 s_1
:
r_L s_L

Output

Print N integers. The i-th of them should represent the number of the cities connected to the i-th city by both roads and railways.

Sample Input 1

4 3 1
1 2
2 3
3 4
2 3

Sample Output 1

1 2 2 1

All the four cities are connected to each other by roads.

By railways, only the second and third cities are connected. Thus, the answers for the cities are 1, 2, 2 and 1, respectively.

Sample Input 2

4 2 2
1 2
2 3
1 4
2 3

Sample Output 2

1 2 2 1

Sample Input 3

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

Sample Output 3

1 1 2 1 2 2 2


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

#define EMPTY    0
#define NOTEMPTY 1
#define Q 10007

typedef struct node{
	int n;
	struct node *next;
}node;

typedef struct hashnode{
	int a;
	int b;
	int n;
	struct hashnode *next;
}hnode;

int empty(node *q);
int dequeue(node *q);
void enqueue(node *q, int n);
void check_connect(char *g, int *r, const int n, const int t);
int hash(int, int);
int counting(int, int, hnode *);
int hash_search(int, int, hnode *);

hnode *hashtable;

int main(void){
	int n, k, l;
	int p, q;
	int i, j;
	int *ar, *br, ans;
	char *graph;

	scanf("%d %d %d", &n, &k, &l);
	graph = (char *)malloc(sizeof(char)*(n*n));
	for(i=0; i<n; i++){
		for(j=0; j<n; j++){
			graph[i*n+j] = 0;
		}
	}
	ar = (int *)malloc(sizeof(int)*n);
	br = (int *)malloc(sizeof(int)*n);
	for(i=0; i<n; i++){
		ar[i] = -1;
		br[i] = -1;
	}
	for(i=0; i<k; i++){
		scanf("%d %d", &p, &q);
		graph[(p-1)*n+(q-1)] = 1;
		//printf("conncted %d and %d with path.\n", p ,q);
	}
	//printf("checking connect with path\n");
	check_connect(graph, ar, n, 1);

	for(i=0; i<l; i++){
		scanf("%d %d", &p, &q);
		graph[(p-1)*n+(q-1)] += 2;
		//printf("connected %d and %d with train.\n", p, q);
	}

	hashtable = (hnode *)malloc(sizeof(hnode)*Q);
	for(i=0; i<Q; i++){
		(hashtable+i)->next = NULL;
		(hashtable+i)->a = -1;
		(hashtable+i)->b = -1;
	}

	//printf("checking connect with train\n");
	check_connect(graph, br, n, 2);

	//printf("start counting\n");
	for(i=0; i<n; i++){
		counting(ar[i], br[i], hashtable);
	}
	//printf("start print result\n");
	for(i=0; i<n; i++){
		ans = hash_search(ar[i], br[i], hashtable);
		printf("%d ", ans);
	}
	printf("\n");

	return 0;
}

int hash_search(int a, int b, hnode *ht){
	int h;
	hnode *p;

	h = hash(a, b);
	p = ht+h;
	while(p->next != NULL){
		if(p->a == a && p->b == b) return p->n;
		p = p->next;
	}
	if(p->a == a && p->b == b) return p->n;

	return -1;
}

int counting(int a, int b, hnode *ht){
	int h;
	hnode *p, *new;

	h = hash(a, b);
	p = ht + h;
	while(p->next != NULL){
		if(p->a == a && p->b == b){
			p->n++;
			return 0;
		}
		p = p->next;
	}
	if(p->a == a && p->b == b){
			p->n++;
	}else{
		new = (hnode *)malloc(sizeof(hnode));
		new->a = a;
		new->b = b;
		new->n = 1;
		new->next = NULL;
		p->next = new;
	}
	return 0;
}

int hash(int a, int b){
	int h;

	h = (a*a+b)%Q;
	//printf("%d %d %d\n", a, b, h);

	return h;
}

int empty(node *q){
	if(q->next == NULL) return EMPTY;

	return NOTEMPTY;
}

node* init_queue(){
	node *np;

	np = (node *)malloc(sizeof(node));
	np->next = NULL;

	return np;
}

int dequeue(node *q){
	node *np;
	int n;

	if(empty(q) == EMPTY) return 0;
	np = q->next;
	q->next = q->next->next;
	n = np->n;
	free(np);

	return n;
}

void enqueue(node *q, int n){
	node *np;

	np = (node*)malloc(sizeof(node));
	np->n = n;
	np->next = NULL;
	while(q->next != NULL) q = q->next;
	q->next = np;
}

void check_connect(char *g, int *r, const int n, const int t){
	int cnum;
	node *set;
	int i, j;

	set = init_queue();

	for(i=0; i<n; i++){
		if(r[i] != -1)continue;
		enqueue(set, i);
		r[i] = i;
		while(empty(set) != EMPTY){
			cnum=dequeue(set);
			//printf("checking connection with %d.\n", cnum);
			for(j=0; j<n; j++){
				if(g[cnum*n+j] >= t){
					if(r[j] == -1){
						enqueue(set, j);
						r[j] = i;
						//printf("%d is connected.\n", j);
					}
				}
			}
		}
	}
}


#include<stdio.h>
 int main() {
 	int a,b,x;
 	scanf("%d %d %d",&a,&b,&x);
 	if(a==b){ if(a%x==0)
 	{printf("1");}else{printf("0");}}
 	else {printf("%d",(1+(b/x)-(a/x)));}
 	return 0;}

Score : 200 points

Problem Statement

You are given nonnegative integers a and b (a ≤ b), and a positive integer x.
Among the integers between a and b, inclusive, how many are divisible by x?

Constraints

0 ≤ a ≤ b ≤ 10^{18}

1 ≤ x ≤ 10^{18}

Input

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

a b x

Output

Print the number of the integers between a and b, inclusive, that are divisible by x.

Sample Input 1

4 8 2

Sample Output 1

3

There are three integers between 4 and 8, inclusive, that are divisible by 2: 4, 6 and 8.

Sample Input 2

0 5 1

Sample Output 2

6

There are six integers between 0 and 5, inclusive, that are divisible by 1: 0, 1, 2, 3, 4 and 5.

Sample Input 3

9 9 2

Sample Output 3

0

There are no integer between 9 and 9, inclusive, that is divisible by 2.

Sample Input 4

1 1000000000000000000 3

Sample Output 4

333333333333333333

Watch out for integer overflows.


#include <stdio.h>

int main(void)
{
	long long int a, b, x;
	scanf("%lld%lld%lld", &a, &b, &x);
	if (a%x == 0)
		printf("%lld\n", (b - a) / x + 1);
	else
		printf("%lld\n", (b - a) / x);
	getchar();

	return 0;
}


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

#define lli long long int
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)<0?-(a):(a))
#define alloc(type,size) calloc(sizeof(type),size)

#define N 1020

typedef struct {
	double x;
	double y;
	double r;
} circle;

typedef struct node {
	struct branch {
		struct branch *next;
		struct node *below;
		double cost;
	} *branch;
	bool bl;
	double val;
} node;
typedef struct branch branch;

typedef struct heap {
	node *nd;
	int val;
} hp;

circle point[N];
int n;

void push_back(node *a,node *b,branch *c){
	c->next = a->branch;
	c->below = b;
	a->branch = c;
	return;
}
void hp_push(hp x,hp *heap,int *heap_size){
	int i = (*heap_size)++;
	while(i>0){
		int p = (i-1)/2;
		if(heap[p].val < x.val) break;
		heap[i] = heap[p];
		i = p;
	}
	heap[i] = x;
}

hp hp_pop(hp *heap,int *heap_size){
	hp ret = heap[0];
	hp x = heap[--*heap_size];
	int i = 0;
	while(i*2+1 < *heap_size){
		int a = i*2+1;
		int b = i*2+2;
		if(b < *heap_size && heap[b].val < heap[a].val)a = b;
		if(heap[a].val >= x.val)break;
		heap[i] = heap[a];
		i = a;
	}
	heap[i] = x;
	return ret;
}

void dijkstra(node *start,hp *heap,int *heap_size){
	branch *br;
	if(start->bl){
		if(*heap_size) return dijkstra(hp_pop(heap,heap_size).nd,heap,heap_size);
		else return;
	}
	start->bl = true;
	for(br = start->branch;br;br=br->next){
		if(br->below->bl) continue;
		if(br->below->val > start->val+br->cost){
			br->below->val = start->val+br->cost;
			hp x;
			x.nd = br->below;
			x.val = br->below->val;
			hp_push(x,heap,heap_size);
		}else if(br->below->val == 0){
			br->below->val = start->val+br->cost;
			hp x;
			x.nd = br->below;
			x.val = br->below->val;
			hp_push(x,heap,heap_size);
		}
	}
	if(*heap_size){
		return dijkstra(hp_pop(heap,heap_size).nd,heap,heap_size);
	}
}
void init(node *nd,int n){
	int i;
	for(i=1;i<=n;i++){
		nd[i].bl = false;
		nd[i].val = 0;
	}
}

double dist(circle a,circle b){
	double x = a.x-b.x;
	double y = a.y-b.y;
	double d = sqrt(x*x+y*y)-a.r-b.r;
	return d<0.0?0.0:d;
}

int main(void){
	int i,j;
	node *nd;
	hp *heap;
	branch *br;
	int heap_size=0;

	scanf("%lf%lf",&point[0].x,&point[0].y);
	scanf("%lf%lf",&point[1].x,&point[1].y);
	scanf("%d",&n);
	n+=2;
	nd = alloc(node,n+n);
	nd--;//1origin
	br = alloc(branch,n*n+n);
	heap = alloc(hp,n*n+n);
	for(i=2;i<n;i++){
		scanf("%lf%lf%lf",&point[i].x,&point[i].y,&point[i].r);
	}
	return 0;
	for(i=0;i<n;i++){
		for(j=i+1;j<n;j++){
			br->cost = dist(point[i],point[j]);
			push_back(&nd[i],&nd[j],br++);
			br->cost = dist(point[i],point[j]);
			push_back(&nd[j],&nd[i],br++);
		}
	}
	dijkstra(&nd[0],heap,&heap_size);
	printf("%.10lf\n", nd[1].val);

    return 0;
}

Score : 600 points

Problem Statement

On the xy-plane, Snuke is going to travel from the point (x_s, y_s) to the point (x_t, y_t).
He can move in arbitrary directions with speed 1.
Here, we will consider him as a point without size.

There are N circular barriers deployed on the plane.
The center and the radius of the i-th barrier are (x_i, y_i) and r_i, respectively.
The barriers may overlap or contain each other.

A point on the plane is exposed to cosmic rays if the point is not within any of the barriers.

Snuke wants to avoid exposure to cosmic rays as much as possible during the travel.
Find the minimum possible duration of time he is exposed to cosmic rays during the travel.

Constraints

All input values are integers.

-10^9 ≤ x_s, y_s, x_t, y_t ≤ 10^9

(x_s, y_s) ≠ (x_t, y_t)

1≤N≤1,000

-10^9 ≤ x_i, y_i ≤ 10^9

1 ≤ r_i ≤ 10^9

Input

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

x_s y_s x_t y_t
N
x_1 y_1 r_1
x_2 y_2 r_2
:
x_N y_N r_N

Output

Print the minimum possible duration of time Snuke is exposed to cosmic rays during the travel.
The output is considered correct if the absolute or relative error is at most 10^{-9}.

Sample Input 1

-2 -2 2 2
1
0 0 1

Sample Output 1

3.6568542495

An optimal route is as follows:

Sample Input 2

-2 0 2 0
2
-1 0 2
1 0 2

Sample Output 2

0.0000000000

An optimal route is as follows:

Sample Input 3

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

Sample Output 3

4.0000000000

An optimal route is as follows:


Score : 100 points

Problem Statement

You have N cups and 1 ball.

The cups are arranged in a row, from left to right.

You turned down all the cups, then inserted the ball into the leftmost cup.

Then, you will perform the following Q operations:

The i-th operation: swap the positions of the A_i-th and B_i-th cups from the left. If one of these cups contains the ball, the ball will also move.

Since you are a magician, you can cast a magic described below:

Magic: When the ball is contained in the i-th cup from the left, teleport the ball into the adjacent cup (that is, the (i-1)-th or (i+1)-th cup, if they exist).

The magic can be cast before the first operation, between two operations, or after the last operation, but you are allowed to cast it at most once during the whole process.

Find the number of cups with a possibility of containing the ball after all the operations and possibly casting the magic.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

1 \leq A_i < B_i \leq N

Input

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

N Q
A_1 B_1
A_2 B_2
:
A_Q B_Q

Output

Print the number of cups with a possibility of eventually containing the ball.

Sample Input 1

10 3
1 3
2 4
4 5

Sample Output 1

4

Sample Input 2

20 3
1 7
8 20
1 19

Sample Output 2

5


/*
cat <<EOF >mistaken-paste
*/

#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
#define _USE_MATH_DEFINES

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>

#define BIG 2000000007
#define VERYBIG 200000000000007LL

#define MOD 1000000007
typedef uint64_t ull;
typedef  int64_t sll;

#define N_MAX 200
#define M_MAX 200000

#ifdef __cplusplus
#include <queue>
#include <stack>
// #include <tuple>
#include <set>
#include <map>

// using namespace std; // HELL
using std::queue;
using std::priority_queue;
using std::stack;
// using std::tuple;
using std::set;
using std::map;
using std::vector;
using std::greater;
using std::pair;

#endif

typedef struct {
	int32_t a;
	int32_t b;
} hw;

typedef struct {
	sll a;
	sll b;
} hwll;

typedef struct {
	hwll a;
	hwll b;
} linell;

typedef struct {
	ull s;
	ull t;
	int32_t c;
} struct_a;

typedef struct {
	int32_t from;
	int32_t to;
	sll cost;
} struct_b;

const hw vector8[8] = {
	{-1, -1},
	{-1,  0},
	{-1, +1},
	{ 0, -1},
	{ 0, +1},
	{+1, -1},
	{+1,  0},
	{+1, +1}
};

ull n, m;
ull h, w;
ull k;
ull q;
ull vua, vub, vuc, vud, vue, vuf;
sll vsa, vsb, vsc, vsd, vse, vsf;
long double vra, vrb, vrc;
double vda, vdb, vdc;
size_t slen;
size_t tlen;
char ch, dh;

ull umin (ull x, ull y) {
	return (x < y) ? x : y;
}

ull umax (ull x, ull y) {
	return (x > y) ? x : y;
}

sll smin (sll x, sll y) {
	return (x < y) ? x : y;
}

sll smax (sll x, sll y) {
	return (x > y) ? x : y;
}

ull gcd (ull x, ull y) {
	if (x < y) {
		return gcd(y, x);
	} else if (y == 0) {
		return x;
	} else {
		return gcd(y, x % y);
	}
}

ull bitpow (ull a, ull x, ull modulo) {
	ull result = 1;
	while (x) {
		if (x & 1) {
			result *= a;
			result %= modulo;
		}
		x /= 2;
		a = (a * a) % modulo;
	}
	return result;
}

ull divide (ull a, ull b, ull modulo) {
	return (a * bitpow(b, modulo - 2, modulo)) % modulo;
}

ull udiff (ull a, ull b) {
	if (a >= b) {
		return a - b;
	} else {
		return b - a;
	}
}

sll sdiff (sll a, sll b) {
	if (a >= b) {
		return a - b;
	} else {
		return b - a;
	}
}

void printUquotient (ull left, ull right) {
	const int32_t digits = 20;

	printf("%llu.", left / right);
	left %= right;
	for (int32_t i = 0; i < digits; i++) {
		left *= 10;
		printf("%1d", left / right);
		left %= right;
	}
	puts("");

	return;
}

void printSquotient (sll left, sll right) {
	if (left * right < 0) putchar('-');
	printUquotient(sdiff(left, 0), sdiff(right, 0));

	return;
}

int bitcount (ull n) {
	int result = 0;
	while (n) {
		if (n & 1) result++;
		n /= 2;
	}
	return result;
}

#ifdef __cplusplus

bool setfind (set<ull> s, ull x) {
	return (s.find(x) != s.end());
}

#endif

// double distance (sll x1, sll y1, sll x2, sll y2) {
// 	double xdist2, ydist2, origindist, dist;

// 	xdist2 = (x1 - x2) * (x1 - x2);
// 	ydist2 = (y1 - y2) * (y1 - y2);
// 	return sqrt(xdist2 + ydist2);
// }

int32_t pullcomp (const void *left, const void *right) {
	ull l = *(ull*)left;
	ull r = *(ull*)right;
	if (l < r) {
		return -1;
	}
	if (l > r) {
		return +1;
	}
	return 0;
}

int32_t psllcomp (const void *left, const void *right) {
	ull l = *(sll*)left;
	ull r = *(sll*)right;
	if (l < r) {
		return -1;
	}
	if (l > r) {
		return +1;
	}
	return 0;
}

int32_t pcharcomp (const void *left, const void *right) {
	char l = *(char*)left;
	char r = *(char*)right;
	if (l < r) {
		return -1;
	}
	if (l > r) {
		return +1;
	}
	return 0;
}

int32_t pstrcomp (const void *left, const void *right) {
	char* l = *(char**)left;
	char* r = *(char**)right;

	return strcmp(l, r);
}

int32_t phwllABcomp (const void *left, const void *right) {
	hwll l = *(hwll*)left;
	hwll r = *(hwll*)right;
	if (l.a < r.a) {
		return -1;
	}
	if (l.a > r.a) {
		return +1;
	}
	if (l.b < r.b) {
		return -1;
	}
	if (l.b > r.b) {
		return +1;
	}
	return 0;
}

bool isinrange (sll left, sll x, sll right) {
	return (left <= x && x <= right);
}

ull mergeteq (ull a[], ull n) {
	ull l = n / 2, r = n - l;
	if (n == 1) return 0;
	ull ret = mergeteq(a, l) + mergeteq(a + l, r);

	ull tmp[n];
	sll i = 0, j = 0;
	while (i < l || j < r) {
		if (i == l) {
			tmp[i + j] = a[i + j];
			j++;
			continue;
		}
		if (j == r) {
			tmp[i + j] = a[i];
			i++;
			continue;
		}

		if (a[i] > a[l + j]) {
			tmp[i + j] = a[l + j];
			ret += l - i;
			j++;
		} else {
			tmp[i + j] = a[i];
			i++;
		}
	}

	for (i = 0; i < n; i++) {
		a[i] = tmp[i];
	}
	return ret;
}

sll a[N_MAX];

ull dp[N_MAX][N_MAX][256];

ull solve () {
	sll i, j, ki, li;
	ull result = 0;
	// sll result = 0;
	double dresult = 0;
	// ull maybe = 0;
	sll maybe = 0;
	// ull sum = 0;
	sll sum = 0;
	sll item;
	ull *dpcell;

	dp[0][0][0] = 1;

	for (i = 0; i < n; i++) {
		for (j = 0; j <= n; j++) {
			for (ki = 0; !(ki >> 8); ki++) {
				dp[i + 1][j][ki] = (dp[i][j][ki] + (j ? dp[i][j - 1][ki ^ a[i]] : 0)) % MOD;
			}
		}
	}

	ull frac = 1;
	for (i = 0; i <= n; i++) {
		result += dp[n][i][k] * frac;
		result %= MOD;

		frac *= (i + 1);
		frac %= MOD;
	}

	printf("%llu\n", result);
	// printf("%.12lf\n", dresult);
	// puts(s);

	return 0;

	success:
	// puts("YES");
	// puts("Yes");
	// printf("%llu\n", result);
	// puts("0");
	puts("Takahashi");
	return 0;

	fail:
	// puts("NO");
	// puts("No");
	// puts("0");
	puts("-1");
	// puts("-1 -1 -1");
	// puts("Aoki");
	return 1;
}

int32_t main (void) {
	int32_t i, j;
	int32_t x, y;

	// scanf("%lf%lf", &vda, &vdb, &vdc);
	// scanf("%lld%lld%lld%lld", &vsa, &vsb, &vsc, &vsd);
	// scanf("%llu%llu%llu", &vua, &vub, &vuc, &vud);
	// scanf("%llu%llu", &h, &w);
	// scanf("%*llu%*llu");
	scanf("%llu", &n, &m);
	scanf("%llu", &k, &n);
	// scanf("%llu%llu", &vua, &vub, &vuc, &vud, &vue, &vuf);
	// scanf("%lld%lld", &vsa, &vsb, &vsc);
	// scanf("%s", s);
	// scanf("%s", t);
	// scanf("%llu", &m);
	// scanf("%llu", &q);
	for (i = 0; i < n; i++) {
		scanf("%lld", &a[i]);
		// scanf("%lld", &b[i]);
		// scanf("%llu", &c[i]);
		// a[i]--;
		// b[i]--;
	}
	// for (i = 0; i < m; i++) {
	// 	scanf("%lld", &a[n + i]);
	// 	scanf("%lld", &b[n + i]);
	// }
	// for (i = 0; i < n; i++) {
	// 	scanf("%llu", &b[i]);
	// }
	// for (i = 0; i < h; i++) {
	// 	for (j = 0; j < w; j++) {
	// 		scanf("%llu", &a[i][j]);
	// 	}
	// }
	// for (i = 0; i < h; i++) {
	// 	scanf("%s", &s[i]);
	// }

	// scanf("%llu", &k);
	// scanf("%llu", &q);
	solve();

	return 0;
}

Max Score: $400$ Points

Problem Statement

Sample testcase 3 has a mistake, so we erased this case and rejudged all solutions of this problem. (21:01)

Snuke got a sequence $a$ of length $n$ from AtCoder company. All elements in $a$ are distinct.

He made a sequence $b$, but actually, he is not remembered it.

However, he is remembered a few things about sequence $b$.

All elements in $b$ are distinct.

All elements in $b$ is in $a$.

$b_1 \oplus b_2 \oplus \cdots \oplus b_r = k$. ($r$ is length of sequence $b$) [$\oplus$ means XOR]

For example, if $a = { 1, 2, 3 }$ and $k = 1$, he can make $b = { 1 }, { 2, 3 }, { 3, 2 }$.

He wants to restore sequence $b$, but he says that there are too many ways and he can't restore it.
Please calculate the ways to make $b$ and help him.

Since the answer can be large, print the answer modulo $1,000,000,007$.

Input

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

$n \ k$
$a_1 \ a_2 \ \cdots \ a_n$

Output

Print the number of ways to make sequence $b$.

Print the answer modulo $1,000,000,007$.

Constraints

$1 \le n \le 100$

$1 \le a_i, k \le 255$

$i \neq j \Rightarrow a_i \neq a_j$

Subtasks

Subtask 1 [ $50$ points ]

$1 \le n \le 4$

Subtask 2 [ $170$ points ]

$1 \le n \le 20$

Subtask 3 [ $180$ points ]

There are no additional constraints.

Sample Input 1

3 1
1 2 3

Sample Output 1

3

You can make 3 patterns: $b = \{ 1 \}, \{ 2, 3 \}, \{ 3, 2 \}$

Sample Input 2

3 10
8 7 5

Sample Output 2

6

You can make 6 patterns: $b = \{ 5, 7, 8 \}, \{ 5, 8, 7 \}, \{ 7, 5, 8 \}, \{ 7, 8, 5 \}, \{ 8, 5, 7 \}, \{ 8, 7, 5 \}$.

Sample Input 4

25 127
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125

Sample Output 4

235924722

Please output answer mod $1,000,000,007$.

writer: E869120


#include <stdio.h>

int main(){

    int a,b,c,answer,max,sub;

    scanf("%d",&a);
    scanf("%d",&b);
    scanf("%d",&c);
    max=0;
    if(max<a){
        max=a;

    }
    if(max<b){
        max=b;

    }
    if(max<c){
        max=c;

    }

    if(max==(a+b+c)/2){
        printf("Yes");
    }
    else
    {
        printf("No");
    }
    return 0;

}

Score : 100 points

Problem Statement

Two students of AtCoder Kindergarten are fighting over candy packs.

There are three candy packs, each of which contains a, b, and c candies, respectively.

Teacher Evi is trying to distribute the packs between the two students so that each student gets the same number of candies. Determine whether it is possible.

Note that Evi cannot take candies out of the packs, and the whole contents of each pack must be given to one of the students.

Constraints

1 ≦ a, b, c ≦ 100

Input

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

a b c

Output

If it is possible to distribute the packs so that each student gets the same number of candies, print Yes. Otherwise, print No.

Sample Input 1

10 30 20

Sample Output 1

Yes

Give the pack with 30 candies to one student, and give the two packs with 10 and 20 candies to the other. Then, each gets 30 candies.

Sample Input 2

30 30 100

Sample Output 2

No

In this case, the student who gets the pack with 100 candies always has more candies than the other.

Note that every pack must be given to one of them.

Sample Input 3

56 25 31

Sample Output 3

Yes


#include <stdio.h>

int main(void)
{
        int a,b,c;
        scanf("%d %d %d",&a,&b,&c);
                if(a == b+c || b == c+a || c == a+b)
                        {printf("%s","Yes");}
                else{printf("%s","No");}
        return 0;
}


#include<stdio.h>
int main()
{
    int a,b,c,d,e,f;
    scanf("%d%d%d",&a,&b,&c);
    d=a+b;
    e=a+c;
    f=b+c;
    if(d==c)
    {
        printf("YES");
    }

    else if(e==b)
    {
        printf("YES");
    }
    else if(f==a)
    {
        printf("YES");
    }
    else
    {
        printf("NO");
    }
    return 0;
}


Score : 200 points

Problem Statement

There is a rectangle in the xy-plane, with its lower left corner at (0, 0) and its upper right corner at (W, H). Each of its sides is parallel to the x-axis or y-axis. Initially, the whole region within the rectangle is painted white.

Snuke plotted N points into the rectangle. The coordinate of the i-th (1 ≦ i ≦ N) point was (x_i, y_i).

Then, he created an integer sequence a of length N, and for each 1 ≦ i ≦ N, he painted some region within the rectangle black, as follows:

If a_i = 1, he painted the region satisfying x < x_i within the rectangle.

If a_i = 2, he painted the region satisfying x > x_i within the rectangle.

If a_i = 3, he painted the region satisfying y < y_i within the rectangle.

If a_i = 4, he painted the region satisfying y > y_i within the rectangle.

Find the area of the white region within the rectangle after he finished painting.

Constraints

1 ≦ W, H ≦ 100

1 ≦ N ≦ 100

0 ≦ x_i ≦ W (1 ≦ i ≦ N)

0 ≦ y_i ≦ H (1 ≦ i ≦ N)

W, H (21:32, added), x_i and y_i are integers.

a_i (1 ≦ i ≦ N) is 1, 2, 3 or 4.

Input

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

W H N
x_1 y_1 a_1
x_2 y_2 a_2
:
x_N y_N a_N

Output

Print the area of the white region within the rectangle after Snuke finished painting.

Sample Input 1

5 4 2
2 1 1
3 3 4

Sample Output 1

9

The figure below shows the rectangle before Snuke starts painting.

First, as (x_1, y_1) = (2, 1) and a_1 = 1, he paints the region satisfying x < 2 within the rectangle:

Then, as (x_2, y_2) = (3, 3) and a_2 = 4, he paints the region satisfying y > 3 within the rectangle:

Now, the area of the white region within the rectangle is 9.

Sample Input 2

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

Sample Output 2

0

It is possible that the whole region within the rectangle is painted black.

Sample Input 3

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

Sample Output 3

64


#include <stdio.h>

int main(void)
{
  int w, h, n, x, y, a, rx, ry, lx, ly;
  scanf("%d %d %d", &w, &h, &n);

  rx = w; ry = h; lx = 0; ly = 0;
  while(n--){
    scanf("%d %d %d", &x, &y, &a);
    if(a == 1){
      if(x >= lx) lx = x;
    }else if(a == 2){
      if(x <= lx){
      printf("0\n");
      return 0;
      }else rx = x;
    }
    if(a == 3){
      if(y >= ry){
	printf("0\n");
	return 0;
      }else ly = y;
    }else if(a == 4){
      if(y <= ry) ry = y;
    }
  }
  printf("%d\n", (ry-ly)*(rx-lx));
  return 0;
}


#include<stdio.h>
int main()
{
    int N, T;
	int A[10000];
    scanf("%d %d", &N, &T);
	int i;
	for(i=0;i<N;i++) {
		scanf("%d", &A[i]);
	}
	int min = A[0];
	int maxv = 0;
	int count = 1;
	for (i = 0; i < N; i++) {
		if(min > A[i]) min = A[i];
		if(maxv == (A[i] - min) ) count++;
		else if(maxv < A[i] - min) {
			maxv = A[i] - min;
			count = 1;
		}
	}
	printf("%d", count);

}

Score : 400 points

Problem Statement

There are N towns located in a line, conveniently numbered 1 through N. Takahashi the merchant is going on a travel from town 1 to town N, buying and selling apples.

Takahashi will begin the travel at town 1, with no apple in his possession. The actions that can be performed during the travel are as follows:

Move: When at town i (i < N), move to town i + 1.

Merchandise: Buy or sell an arbitrary number of apples at the current town. Here, it is assumed that one apple can always be bought and sold for A_i yen (the currency of Japan) at town i (1 ≦ i ≦ N), where A_i are distinct integers. Also, you can assume that he has an infinite supply of money.

For some reason, there is a constraint on merchandising apple during the travel: the sum of the number of apples bought and the number of apples sold during the whole travel, must be at most T. (Note that a single apple can be counted in both.)

During the travel, Takahashi will perform actions so that the profit of the travel is maximized. Here, the profit of the travel is the amount of money that is gained by selling apples, minus the amount of money that is spent on buying apples. Note that we are not interested in apples in his possession at the end of the travel.

Aoki, a business rival of Takahashi, wants to trouble Takahashi by manipulating the market price of apples. Prior to the beginning of Takahashi's travel, Aoki can change A_i into another arbitrary non-negative integer A_i' for any town i, any number of times. The cost of performing this operation is |A_i - A_i'|. After performing this operation, different towns may have equal values of A_i.

Aoki's objective is to decrease Takahashi's expected profit by at least 1 yen. Find the minimum total cost to achieve it. You may assume that Takahashi's expected profit is initially at least 1 yen.

Constraints

1 ≦ N ≦ 10^5

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

A_i are distinct.

2 ≦ T ≦ 10^9

In the initial state, Takahashi's expected profit is at least 1 yen.

Input

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

N T
A_1 A_2 ... A_N

Output

Print the minimum total cost to decrease Takahashi's expected profit by at least 1 yen.

Sample Input 1

3 2
100 50 200

Sample Output 1

1

In the initial state, Takahashi can achieve the maximum profit of 150 yen as follows:

Move from town 1 to town 2.

Buy one apple for 50 yen at town 2.

Move from town 2 to town 3.

Sell one apple for 200 yen at town 3.

If, for example, Aoki changes the price of an apple at town 2 from 50 yen to 51 yen, Takahashi will not be able to achieve the profit of 150 yen. The cost of performing this operation is 1, thus the answer is 1.

There are other ways to decrease Takahashi's expected profit, such as changing the price of an apple at town 3 from 200 yen to 199 yen.

Sample Input 2

5 8
50 30 40 10 20

Sample Output 2

2

Sample Input 3

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

Sample Output 3

2


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

int main(void) {
	char s[999999];

	scanf("%s", s);

	int l=strlen(s),wcnt=0, i;

	for(i=1;s[i];i++){
		if(s[i-1]-s[i]){
			wcnt++;
		}
	}

	printf("%d\n", wcnt);
	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 <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define ll long long
#define rep(i,n) for(ll i=0;i<(n);i++)
#define max(p,q) ((p)>(q)?(p):(q))
#define min(p,q) ((p)<(q)?(p):(q))
#define chmax(a,b) ((a)=(a)>(b)?(a):(b))
#define chmin(a,b) ((a)=(a)<(b)?(a):(b))
#define abs(p) ((p)>=(0)?(p):(-(p)))
#define MOD 1000000007
ll powll(ll a,ll b){ll r=1;rep(i,b){r*=a;}return r;}
#define swap(a,b) do{ll w=(a);(a)=(b);(b)=w;}while(0)
#define swapd(a,b) do{double w=(a);(a)=(b);(b)=w}while(0)
#define in(a) scanf("%lld", &(a))
#define in2(a,b) scanf("%lld %lld",&(a),&(b))
#define in3(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c))
#define ind(a) scanf("%lf", &(a))
#define ins(a) scanf("%s", (a))
#define inc(a) scanf("%c", &(a))
#define put(a) printf("%lld\n", (a))
#define putd(a) printf("%.15f\n", (a))
//      puts(a) printf("%s\n", a) 文字はこっち
//your code here!
int main(void){
    ll N;
    char s[105], t[105];
    in(N); ins(s); ins(t);
    ll ans=N;
    rep(i, N){
        if(strncmp(s+i, t, N-i)) ans++;
        else break;
    }
    put(ans);
    return 0;
}

Score : 200 points

Problem Statement

Snuke is interested in strings that satisfy the following conditions:

The length of the string is at least N.

The first N characters equal to the string s.

The last N characters equal to the string t.

Find the length of the shortest string that satisfies the conditions.

Constraints

1≤N≤100

The lengths of s and t are both N.

s and t consist of lowercase English letters.

Input

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

N
s
t

Output

Print the length of the shortest string that satisfies the conditions.

Sample Input 1

3
abc
cde

Sample Output 1

5

The shortest string is abcde.

Sample Input 2

1
a
z

Sample Output 2

2

The shortest string is az.

Sample Input 3

4
expr
expr

Sample Output 3

4

The shortest string is expr.


#include <stdio.h>

int main(void) {
	int n, i, c=0;
	char s[111], t[111];

	scanf("%d", &n);

	scanf("%s\n%s", s, t);

	for(i=0;i<n;i++){
		if(s[n-1-i]==t[i])
		c++;
	}

	printf("%d\n", 2*n-c);
	return 0;
}


Score : 1300 points

Problem Statement

We have a pyramid with N steps, built with blocks.
The steps are numbered 1 through N from top to bottom.
For each 1≤i≤N, step i consists of 2i-1 blocks aligned horizontally.
The pyramid is built so that the blocks at the centers of the steps are aligned vertically.

A pyramid with N=4 steps

Snuke wrote a permutation of (1, 2, ..., 2N-1) into the blocks of step N.
Then, he wrote integers into all remaining blocks, under the following rule:

The integer written into a block b must be equal to the median of the three integers written into the three blocks directly under b, or to the lower left or lower right of b.

Writing integers into the blocks

Afterwards, he erased all integers written into the blocks.
Now, he only remembers that the permutation written into the blocks of step N was (a_1, a_2, ..., a_{2N-1}).

Find the integer written into the block of step 1.

Constraints

2≤N≤10^5

(a_1, a_2, ..., a_{2N-1}) is a permutation of (1, 2, ..., 2N-1).

Input

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

N
a_1 a_2 ... a_{2N-1}

Output

Print the integer written into the block of step 1.

Sample Input 1

4
1 6 3 7 4 5 2

Sample Output 1

4

This case corresponds to the figure in the problem statement.

Sample Input 2

2
1 2 3

Sample Output 2

2


#include<stdio.h>

int main()

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

    if(a==b && a==c )
        printf("One different colors: %d.",a);

    else if(a==b && a!=c)
        printf("Two different colors: %d and %d.",a,c);

    else if(a==c && a!=b)
        printf("Two different colors: %d and %d.",a,b);

    else if(b==c && b!=a)
        printf("Two different colors: %d and %d.",a,b);

    else if(a!=b && a!=c)
        printf("Three different colors: %d, %d, and &d.",a,b,c);

    return 0;

}

Score : 100 points

Problem Statement

AtCoDeer the deer recently bought three paint cans.
The color of the one he bought two days ago is a, the color of the one he bought yesterday is b, and the color of the one he bought today is c.
Here, the color of each paint can is represented by an integer between 1 and 100, inclusive.

Since he is forgetful, he might have bought more than one paint can in the same color.
Count the number of different kinds of colors of these paint cans and tell him.

Constraints

1≦a,b,c≦100

Input

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

a b c

Output

Print the number of different kinds of colors of the paint cans.

Sample Input 1

3 1 4

Sample Output 1

3

Three different colors: 1, 3, and 4.

Sample Input 2

3 3 33

Sample Output 2

2

Two different colors: 3 and 33.


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

Score : 200 points

Problem Statement

There are N balls placed in a row.
AtCoDeer the deer is painting each of these in one of the K colors of his paint cans.
For aesthetic reasons, any two adjacent balls must be painted in different colors.

Find the number of the possible ways to paint the balls.

Constraints

1≦N≦1000

2≦K≦1000

The correct answer is at most 2^{31}-1.

Input

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

N K

Output

Print the number of the possible ways to paint the balls.

Sample Input 1

2 2

Sample Output 1

2

We will denote the colors by 0 and 1. There are two possible ways: we can either paint the left ball in color 0 and the right ball in color 1, or paint the left in color 1 and the right in color 0.

Sample Input 2

1 10

Sample Output 2

10

Since there is only one ball, we can use any of the ten colors to paint it. Thus, the answer is ten.


#include <stdio.h>

int main() {
	int n,k;
	long int i = 0;
	i = scanf("%d", &n);
	i = scanf("%d", &k);
        i = k;
	for(int z = 1;n>z;z++){
        i *= k-1;

}
        printf("%ld",i);
	return 0;

}


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

int main(void){

    int N;

    scanf("%d", &N);

    long long T[N], A[N];

    for(int i = 0; i < N; i++){
        scanf("%lld%lld", &T[i], &A[i]);
    }

    long long l = T[0], r = A[0];

    for(int i = 0; i < N; i++){
        long long t = (l + r) / (T[i] + A[i]);
        while(l > T[i] * t || r > A[i] * t){
            t++;
        }
        l = T[i] * t;
        r = A[i] * t;
    }

    printf("%lld\n", l + r);

    return 0;
}

Score : 300 points

Problem Statement

AtCoDeer the deer is seeing a quick report of election results on TV.
Two candidates are standing for the election: Takahashi and Aoki.
The report shows the ratio of the current numbers of votes the two candidates have obtained, but not the actual numbers of votes.
AtCoDeer has checked the report N times, and when he checked it for the i-th (1≦i≦N) time, the ratio was T_i:A_i.
It is known that each candidate had at least one vote when he checked the report for the first time.

Find the minimum possible total number of votes obtained by the two candidates when he checked the report for the N-th time.
It can be assumed that the number of votes obtained by each candidate never decreases.

Constraints

1≦N≦1000

1≦T_i,A_i≦1000 (1≦i≦N)

T_i and A_i (1≦i≦N) are coprime.

It is guaranteed that the correct answer is at most 10^{18}.

Input

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

N
T_1 A_1
T_2 A_2
:
T_N A_N

Output

Print the minimum possible total number of votes obtained by Takahashi and Aoki when AtCoDeer checked the report for the N-th time.

Sample Input 1

3
2 3
1 1
3 2

Sample Output 1

10

When the numbers of votes obtained by the two candidates change as 2,3 → 3,3 → 6,4, the total number of votes at the end is 10, which is the minimum possible number.

Sample Input 2

4
1 1
1 1
1 5
1 100

Sample Output 2

101

It is possible that neither candidate obtained a vote between the moment when he checked the report, and the moment when he checked it for the next time.

Sample Input 3

5
3 10
48 17
31 199
231 23
3 2

Sample Output 3

6930


Score : 300 points

Problem Statement

AtCoDeer the deer and his friend TopCoDeer is playing a game.
The game consists of N turns.
In each turn, each player plays one of the two gestures, Rock and Paper, as in Rock-paper-scissors, under the following condition:

(※) After each turn, (the number of times the player has played Paper)≦(the number of times the player has played Rock).

Each player's score is calculated by (the number of turns where the player wins) - (the number of turns where the player loses), where the outcome of each turn is determined by the rules of Rock-paper-scissors.

(For those who are not familiar with Rock-paper-scissors: If one player plays Rock and the other plays Paper, the latter player will win and the former player will lose. If both players play the same gesture, the round is a tie and neither player will win nor lose.)

With his supernatural power, AtCoDeer was able to foresee the gesture that TopCoDeer will play in each of the N turns, before the game starts.
Plan AtCoDeer's gesture in each turn to maximize AtCoDeer's score.

The gesture that TopCoDeer will play in each turn is given by a string s. If the i-th (1≦i≦N) character in s is g, TopCoDeer will play Rock in the i-th turn. Similarly, if the i-th (1≦i≦N) character of s in p, TopCoDeer will play Paper in the i-th turn.

Constraints

1≦N≦10^5

N=|s|

Each character in s is g or p.

The gestures represented by s satisfy the condition (※).

Input

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

s

Output

Print the AtCoDeer's maximum possible score.

Sample Input 1

gpg

Sample Output 1

0

Playing the same gesture as the opponent in each turn results in the score of 0, which is the maximum possible score.

Sample Input 2

ggppgggpgg

Sample Output 2

2

For example, consider playing gestures in the following order: Rock, Paper, Rock, Paper, Rock, Rock, Paper, Paper, Rock, Paper. This strategy earns three victories and suffers one defeat, resulting in the score of 2, which is the maximum possible score.


#include<stdio.h>
int main(){
  char s[20];
  int i;
  scanf("%s",s);
  for(i=0;s[i];i++){
    if(i==4)printf(" ");
    printf("%c\n",s[i]);
  }
  return 0;
}

Score : 100 points

Problem Statement

This contest is CODE FESTIVAL.
However, Mr. Takahashi always writes it CODEFESTIVAL, omitting the single space between CODE and FESTIVAL.

So he has decided to make a program that puts the single space he omitted.

You are given a string s with 12 letters.
Output the string putting a single space between the first 4 letters and last 8 letters in the string s.

Constraints

s contains exactly 12 letters.

All letters in s are uppercase English letters.

Input

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

s

Output

Print the string putting a single space between the first 4 letters and last 8 letters in the string s.
Put a line break at the end.

Sample Input 1

CODEFESTIVAL

Sample Output 1

CODE FESTIVAL

Putting a single space between the first 4 letters and last 8 letters in CODEFESTIVAL makes it CODE FESTIVAL.

Sample Input 2

POSTGRADUATE

Sample Output 2

POST GRADUATE

Sample Input 3

ABCDEFGHIJKL

Sample Output 3

ABCD EFGHIJKL


void approach_a( char* c, int* remain ) {
	// a...z = 97...122

	int distance = 'z' + 1 - *c;

	if( *remain >= distance ) {
		*c      =  'a';
		*remain -= distance;
	}
}

void nextalphabet( char* c, int* remain ) {
	while( *remain > 0 ){
		if( *c == 'z' ) {*c = 'a';}
		else            {*c += 1; }
		*remain -= 1;
	}
}

main() {
	char s[100000]; scanf( "%s", &s );
	int  K;         scanf( "%d", &K );

	int i;
	int r = K;

	for( i=0; s[i]!='\0'; i++ ) approach_a( &s[i], &r );

	r %= 'z' + 1 - 'a';
	nextalphabet( &s[i-1], &r );

	puts( s );
	return 0;
}

Score : 400 points

Problem Statement

Mr. Takahashi has a string s consisting of lowercase English letters.
He repeats the following operation on s exactly K times.

Choose an arbitrary letter on s and change that letter to the next alphabet. Note that the next letter of z is a.

For example, if you perform an operation for the second letter on aaz, aaz becomes abz.
If you then perform an operation for the third letter on abz, abz becomes aba.

Mr. Takahashi wants to have the lexicographically smallest string after performing exactly K operations on s.
Find the such string.

Constraints

1≤|s|≤10^5

All letters in s are lowercase English letters.

1≤K≤10^9

Input

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

s
K

Output

Print the lexicographically smallest string after performing exactly K operations on s.

Sample Input 1

xyz
4

Sample Output 1

aya

For example, you can perform the following operations: xyz, yyz, zyz, ayz, aya.

Sample Input 2

a
25

Sample Output 2

z

You have to perform exactly K operations.

Sample Input 3

codefestival
100

Sample Output 3

aaaafeaaivap


#if 0
cat <<EOF >mistaken-paste
#endif
// thanks for @rsk0315_h4x

#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
#define _USE_MATH_DEFINES

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>

typedef uint64_t ull;
typedef  int64_t sll;

#define N_MAX 1000000

typedef struct {
	sll a;
	sll b;
} hwll;

typedef struct {
	sll a;
	sll b;
	sll c;
} hwllc;

ull n;

int32_t phwllABcomp (const void *left, const void *right) {
	hwll l = *(hwll*)left;
	hwll r = *(hwll*)right;
	if (l.a < r.a) {
		return -1;
	}
	if (l.a > r.a) {
		return +1;
	}
	if (l.b < r.b) {
		return -1;
	}
	if (l.b > r.b) {
		return +1;
	}
	return 0;
}

int32_t ptriplecomp (const void *left, const void *right) {
	hwllc l = *(hwllc*)left;
	hwllc r = *(hwllc*)right;

	if (l.a < r.a) {
		return -1;
	}
	if (l.a > r.a) {
		return +1;
	}
	if (l.b < r.b) {
		return -1;
	}
	if (l.b > r.b) {
		return +1;
	}
	if (l.c < r.c) {
		return -1;
	}
	if (l.c > r.c) {
		return +1;
	}
	return 0;
}

sll a[N_MAX];
sll b[N_MAX];
sll c[N_MAX];
hwll xy[N_MAX];

hwllc g1[N_MAX], g2[N_MAX];
ull glen1 = 0, glen2 = 0;
ull gin1[N_MAX], gin2[N_MAX];
bool check1[N_MAX], check2[N_MAX];
sll c1[N_MAX], c2[N_MAX];
sll min1 = 0, min2 = 0;

bool dfs1 (sll v) {
	check1[v] = true;

	for (sll i = gin1[v]; i < gin1[v + 1]; i++) {
		sll u = g1[i].b;

		if (check1[u]) {
			if (c1[v] + g1[i].c != c1[u]) return false;
		} else {
			c1[u] = c1[v] + g1[i].c;
			if (c1[u] < min1) min1 = c1[u];
			if (!dfs1(u)) return false;
		}
	}
	return true;
}
bool dfs2 (sll v) {
	check2[v] = true;

	for (sll i = gin2[v]; i < gin2[v + 1]; i++) {
		sll u = g2[i].b;

		if (check2[u]) {
			if (c2[v] + g2[i].c != c2[u]) return false;
		} else {
			c2[u] = c2[v] + g2[i].c;
			if (c2[u] < min2) min2 = c2[u];
			if (!dfs2(u)) return false;
		}
	}
	return true;
}

ull solve () {
	sll i, j;

	for (i = 0; i < n; i++) {
		xy[i].a = a[i];
		xy[i].b = i;
	}
	qsort(xy, n, sizeof(hwll), phwllABcomp);
	for (i = 1; i < n; i++) {
		if (xy[i - 1].a != xy[i].a) continue;

		ull v = xy[i - 1].b, u = xy[i].b;

		hwllc xl = {b[v], b[u], c[u] - c[v]};
		hwllc xr = {b[u], b[v], c[v] - c[u]};
		g1[glen1++] = xl;
		g1[glen1++] = xr;
	}
	qsort(g1, glen1, sizeof(hwllc), ptriplecomp);

	for (i = 0; i < n; i++) {
		xy[i].a = b[i];
		xy[i].b = i;
	}
	qsort(xy, n, sizeof(hwll), phwllABcomp);
	for (i = 1; i < n; i++) {
		if (xy[i - 1].a != xy[i].a) continue;

		ull v = xy[i - 1].b, u = xy[i].b;

		hwllc xl = {a[v], a[u], c[u] - c[v]};
		hwllc xr = {a[u], a[v], c[v] - c[u]};
		g2[glen2++] = xl;
		g2[glen2++] = xr;
	}
	qsort(g2, glen2, sizeof(hwllc), ptriplecomp);

	i = j = 0;
	while (i < n) {
		gin1[i] = j;
		while (j < glen1 && g1[i].a == g1[j].a) j++;
		i++;
	}
	gin1[n] = glen1;
	i = j = 0;
	while (i < n) {
		gin2[i] = j;
		while (j < glen2 && g2[i].a == g2[j].a) j++;
		i++;
	}
	gin2[n] = glen2;

	if (!glen1) goto success;

	if (!dfs1(g1[0].a)) goto fail;
	if (!dfs2(g2[0].a)) goto fail;

	for (i = 0; i < n; i++) {
		if (check1[a[i]]) {
			if (c[i] - c1[b[i]] + min1 < 0) goto fail;
		}
		if (check2[b[i]]) {
			if (c[i] - c2[a[i]] + min2 < 0) goto fail;
		}
	}

	success:
	puts("Yes");
	return 0;

	fail:
	puts("No");
	return 1;
}

int32_t main (void) {
	scanf("%*llu%*llu");
	scanf("%llu", &n);
	for (int i = 0; i < n; i++) {
		scanf("%lld", &a[i]);
		scanf("%lld", &b[i]);
		scanf("%lld", &c[i]);
		a[i]--;
		b[i]--;
	}

	solve();

	return 0;
}

Score : 800 points

Problem Statement

There is a grid with R rows and C columns.
We call the cell in the r-th row and c-th column (r，c).

Mr. Takahashi wrote non-negative integers into N of the cells, that is, he wrote a non-negative integer a_i into (r_i，c_i) for each i (1≤i≤N).
After that he fell asleep.

Mr. Aoki found the grid and tries to surprise Mr. Takahashi by writing integers into all remaining cells.
The grid must meet the following conditions to really surprise Mr. Takahashi.

Condition 1: Each cell contains a non-negative integer.

Condition 2: For any 2×2 square formed by cells on the grid, the sum of the top left and bottom right integers must always equal to the sum of the top right and bottom left integers.

Determine whether it is possible to meet those conditions by properly writing integers into all remaining cells.

Constraints

2≤R，C≤10^5

1≤N≤10^5

1≤r_i≤R

1≤c_i≤C

(r_i，c_i) ≠ (r_j，c_j) (i≠j)

a_i is an integer.

0≤a_i≤10^9

Input

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

R C
N
r_1 c_1 a_1
r_2 c_2 a_2
:
r_N c_N a_N

Output

Print Yes if it is possible to meet the conditions by properly writing integers into all remaining cells.
Otherwise, print No.

Sample Input 1

2 2
3
1 1 0
1 2 10
2 1 20

Sample Output 1

Yes

You can write integers as follows.

Sample Input 2

2 3
5
1 1 0
1 2 10
1 3 20
2 1 30
2 3 40

Sample Output 2

No

There are two 2×2 squares on the grid, formed by the following cells:

Cells (1，1), (1，2), (2，1) and (2，2)

Cells (1，2), (1，3), (2，2) and (2，3)

You have to write 40 into the empty cell to meet the condition on the left square, but then it does not satisfy the condition on the right square.

Sample Input 3

2 2
3
1 1 20
1 2 10
2 1 0

Sample Output 3

No

You have to write -10 into the empty cell to meet condition 2, but then it does not satisfy condition 1.

Sample Input 4

3 3
4
1 1 0
1 3 10
3 1 10
3 3 20

Sample Output 4

Yes

You can write integers as follows.

Sample Input 5

2 2
4
1 1 0
1 2 10
2 1 30
2 2 20

Sample Output 5

No

All cells already contain a integer and condition 2 is not satisfied.


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

int main(void) {
	int i;
	int a, b, h;
	char buf[128], *ch;

	fgets(buf, sizeof(buf), stdin);
	ch = strtok(buf, " ");
	a = atoi(ch);
	fgets(buf, sizeof(buf), stdin);
	ch = strtok(buf, " ");
	b = atoi(ch);
	fgets(buf, sizeof(buf), stdin);
	ch = strtok(buf, " ");
	h = atoi(ch);

	printf("%d\n", ((a + b)*h)/2);
	return 0;
}

Score : 100 points

Problem Statement

You are given a trapezoid. The lengths of its upper base, lower base, and height are a, b, and h, respectively.

An example of a trapezoid

Find the area of this trapezoid.

Constraints

1≦a≦100

1≦b≦100

1≦h≦100

All input values are integers.

h is even.

Input

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

a
b
h

Output

Print the area of the given trapezoid. It is guaranteed that the area is an integer.

Sample Input 1

3
4
2

Sample Output 1

7

When the lengths of the upper base, lower base, and height are 3, 4, and 2, respectively, the area of the trapezoid is (3+4)×2/2 = 7.

Sample Input 2

4
4
4

Sample Output 2

16

In this case, a parallelogram is given, which is also a trapezoid.