#include <stdio.h>

int main(void) {
  int a, b;
  scanf("%d %d", &a, &b);
  if(a <= b) {
    printf("%d\n", a);
  }
  else {
    printf("%d\n", a - 1);
  }
  return 0;
}

Score: 100 points

Problem Statement

In AtCoder Kingdom, Gregorian calendar is used, and dates are written in the "year-month-day" order, or the "month-day" order without the year.

For example, May 3, 2018 is written as 2018-5-3, or 5-3 without the year.

In this country, a date is called Takahashi when the month and the day are equal as numbers. For example, 5-5 is Takahashi.

How many days from 2018-1-1 through 2018-a-b are Takahashi?

Constraints

a is an integer between 1 and 12 (inclusive).

b is an integer between 1 and 31 (inclusive).

2018-a-b is a valid date in Gregorian calendar.

Input

Input is given from Standard Input in the following format:

a b

Output

Print the number of days from 2018-1-1 through 2018-a-b that are Takahashi.

Sample Input 1

5 5

Sample Output 1

5

There are five days that are Takahashi: 1-1, 2-2, 3-3, 4-4 and 5-5.

Sample Input 2

2 1

Sample Output 2

1

There is only one day that is Takahashi: 1-1.

Sample Input 3

11 30

Sample Output 3

11

There are eleven days that are Takahashi: 1-1, 2-2, 3-3, 4-4, 5-5, 6-6, 7-7, 8-8, 9-9, 10-10 and 11-11.


#include <stdio.h>

int main(void)
{
	int a,b;

	scanf("%d%d",&a,&b);

	if(a<=b){
		printf("%d\n",a);
	}else{
		printf("%d\n",a-1);
	}

	return 0;

}

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

Submitted source-code file size, in bytes.

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

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


#include <stdio.h>

int a,b ;

int main(void){

    scanf ("%d%d",&a,&b) ;

    if ( a<=b ){
        printf ("%d",a) ;
    }
    else {
        printf ("%d",a-1) ;
    }

    return 0 ;

}


#include <stdio.h>
int main(void){
    int a,b,day;
    scanf("%d %d",&a,&b);/*aは月、ｂは日付*/
    day=a-1;
    if(a<=b){
        day++;
    }
    printf("%d",day);
}


#include<stdio.h>

int main()
{
  int a,b;
  a=b=1;
  int takahashi=0;
  int i,j;

  if(scanf("%d%d",&a,&b)==1)
    {
  for(i=1;i<=a;i++)
    {
      for(j=1;j<=b;j++)
	{
	  if(i==j)
	    {
	      takahashi++;
	    }
	}
    }
    }
  printf("%d\n",takahashi);
  return 0;
}


Score: 300 points

Problem Statement

We have a canvas divided into a grid with H rows and W columns. The square at the i-th row from the top and the j-th column from the left is represented as (i, j).

Initially, all the squares are white. square1001 wants to draw a picture with black paint. His specific objective is to make Square (i, j) black when s_{i, j}= #, and to make Square (i, j) white when s_{i, j}= ..

However, since he is not a good painter, he can only choose two squares that are horizontally or vertically adjacent and paint those squares black, for some number of times (possibly zero). He may choose squares that are already painted black, in which case the color of those squares remain black.

Determine if square1001 can achieve his objective.

Constraints

H is an integer between 1 and 50 (inclusive).

W is an integer between 1 and 50 (inclusive).

For every (i, j) (1 \leq i \leq H, 1 \leq j \leq W), s_{i, j} is # or ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1} s_{1, 2} s_{1, 3} ... s_{1, W}
s_{2, 1} s_{2, 2} s_{2, 3} ... s_{2, W}
: :
s_{H, 1} s_{H, 2} s_{H, 3} ... s_{H, W}

Output

If square1001 can achieve his objective, print Yes; if he cannot, print No.

Sample Input 1

3 3
.#.
###
.#.

Sample Output 1

Yes

One possible way to achieve the objective is shown in the figure below. Here, the squares being painted are marked by stars.

Sample Input 2

5 5
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#

Sample Output 2

No

square1001 cannot achieve his objective here.

Sample Input 3

11 11
...#####...
.##.....##.
#..##.##..#
#..##.##..#
#.........#
#...###...#
.#########.
.#.#.#.#.#.
##.#.#.#.##
..##.#.##..
.##..#..##.

Sample Output 3

Yes


#include <stdio.h>

main()
{
	char s[4];
	scanf("%s", s);
	int price = 700;
	for(int i=0; i!=4; ++i){
		if(s[i]=='o') price += 100;
	}
	printf(price);
	return 0;
}

Score : 100 points

Problem Statement

In "Takahashi-ya", a ramen restaurant, a bowl of ramen costs 700 yen (the currency of Japan), plus 100 yen for each kind of topping (boiled egg, sliced pork, green onions).

A customer ordered a bowl of ramen and told which toppings to put on his ramen to a clerk. The clerk took a memo of the order as a string S. S is three characters long, and if the first character in S is o, it means the ramen should be topped with boiled egg; if that character is x, it means the ramen should not be topped with boiled egg. Similarly, the second and third characters in S mean the presence or absence of sliced pork and green onions on top of the ramen.

Write a program that, when S is given, prints the price of the corresponding bowl of ramen.

Constraints

S is a string of length 3.

Each character in S is o or x.

Input

Input is given from Standard Input in the following format:

S

Output

Print the price of the bowl of ramen corresponding to S.

Sample Input 1

oxo

Sample Output 1

900

The price of a ramen topped with two kinds of toppings, boiled egg and green onions, is 700 + 100 \times 2 = 900 yen.

Sample Input 2

ooo

Sample Output 2

1000

The price of a ramen topped with all three kinds of toppings is 700 + 100 \times 3 = 1000 yen.

Sample Input 3

xxx

Sample Output 3

700

The price of a ramen without any toppings is 700 yen.


#include<stdio.h>
int main(void)
{
	char str1[4]={'o','o','o'};
	char str2[4]={'o','o','x'};
	char str3[4]={'o','x','o'};
	char str4[4]={'o','x','x'};
	char str5[4]={'x','o','o'};
	char str6[4]={'x','x','o'};
	char str7[4]={'x','x','x'};
	char str8[4]={'x','o','x'};
	char str[5];
	scanf("%5s",str);
	if(strcmp(str1,str)==0){
		printf("1000");
	}
	else if(strcmp(str2,str)==0){
		printf("900");
	}
	else if(strcmp(str3,str)==0){
		printf("900");
	}
	else if(strcmp(str4,str)==0){
		printf("800");
	}
	else if(strcmp(str5,str)==0){
		printf("900");
	}
	else if(strcmp(str6,str)==0){
		printf("800");
	}
	else if(strcmp(str7,str)==0){
		printf("700");
	}
	else if(strcmp(str8,str)==0){
		printf("800");
	}
	return 0;
}


#include<stdio.h>

int main(void)
{
	int n,x,m,min,i;
	scanf("%d%d%d",&n,&x,&m);
	min=m;
	x-=m;
	for(i=0;i<n-1;i++){
		scanf("%d",&m);
		if(m<min)
			min=m;
		x-=m;
	}

	printf("%d",n+x/min);
	return 0;
}

Score : 200 points

Problem Statement

Akaki, a patissier, can make N kinds of doughnut using only a certain powder called "Okashi no Moto" (literally "material of pastry", simply called Moto below) as ingredient. These doughnuts are called Doughnut 1, Doughnut 2, ..., Doughnut N. In order to make one Doughnut i (1 ≤ i ≤ N), she needs to consume m_i grams of Moto. She cannot make a non-integer number of doughnuts, such as 0.5 doughnuts.

Now, she has X grams of Moto. She decides to make as many doughnuts as possible for a party tonight. However, since the tastes of the guests differ, she will obey the following condition:

For each of the N kinds of doughnuts, make at least one doughnut of that kind.

At most how many doughnuts can be made here? She does not necessarily need to consume all of her Moto. Also, under the constraints of this problem, it is always possible to obey the condition.

Constraints

2 ≤ N ≤ 100

1 ≤ m_i ≤ 1000

m_1 + m_2 + ... + m_N ≤ X ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
m_1
m_2
:
m_N

Output

Print the maximum number of doughnuts that can be made under the condition.

Sample Input 1

3 1000
120
100
140

Sample Output 1

9

She has 1000 grams of Moto and can make three kinds of doughnuts. If she makes one doughnut for each of the three kinds, she consumes 120 + 100 + 140 = 360 grams of Moto. From the 640 grams of Moto that remains here, she can make additional six Doughnuts 2. This is how she can made a total of nine doughnuts, which is the maximum.

Sample Input 2

4 360
90
90
90
90

Sample Output 2

4

Making one doughnut for each of the four kinds consumes all of her Moto.

Sample Input 3

5 3000
150
130
150
130
110

Sample Output 3

26


#include <stdio.h>

int main(int argc, char* argv[]) {

	int a, b, c, x, y;
	int sum = 0;

	scanf("%d %d %d %d %d", &a, &b, &c, &x, &y);

	if(2 * c < a + b) {
		if(x <= y) {
			sum += 2 * x * c;
			sum += b * (y - x);
		} else {
			sum += 2 * y * c;
			sum += a * (x - y);
		}
	} else {
		sum = a * x + b * y;
	}
	printf("%d", sum);

	return 0;
}

Score : 300 points

Problem Statement

"Pizza At", a fast food chain, offers three kinds of pizza: "A-pizza", "B-pizza" and "AB-pizza". A-pizza and B-pizza are completely different pizzas, and AB-pizza is one half of A-pizza and one half of B-pizza combined together. The prices of one A-pizza, B-pizza and AB-pizza are A yen, B yen and C yen (yen is the currency of Japan), respectively.

Nakahashi needs to prepare X A-pizzas and Y B-pizzas for a party tonight. He can only obtain these pizzas by directly buying A-pizzas and B-pizzas, or buying two AB-pizzas and then rearrange them into one A-pizza and one B-pizza. At least how much money does he need for this? It is fine to have more pizzas than necessary by rearranging pizzas.

Constraints

1 ≤ A, B, C ≤ 5000

1 ≤ X, Y ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C X Y

Output

Print the minimum amount of money required to prepare X A-pizzas and Y B-pizzas.

Sample Input 1

1500 2000 1600 3 2

Sample Output 1

7900

It is optimal to buy four AB-pizzas and rearrange them into two A-pizzas and two B-pizzas, then buy additional one A-pizza.

Sample Input 2

1500 2000 1900 3 2

Sample Output 2

8500

It is optimal to directly buy three A-pizzas and two B-pizzas.

Sample Input 3

1500 2000 500 90000 100000

Sample Output 3

100000000

It is optimal to buy 200000 AB-pizzas and rearrange them into 100000 A-pizzas and 100000 B-pizzas. We will have 10000 more A-pizzas than necessary, but that is fine.


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

#define SIZE 100000

long int N,C;
long int XV[2][SIZE];
long int gainmax[2][SIZE];
long int gain[2][SIZE];
long int gaintotal=0;
long int reversemax;
long int max=0;

int main(){
#ifdef DEBUG
	printf("DEBUGGING ON\n");
#endif
	scanf("%ld %ld",&N,&C);
	for (int i=0;i<N;++i){
		scanf("%ld %ld",&XV[0][i],&XV[1][i]);
		gaintotal+=XV[1][i];
	}
	gaintotal-=C;
	gain[0][0]=XV[1][0]-XV[0][0];
	gain[1][0]=gaintotal-gain[0][0]+XV[1][0];
	gainmax[0][0]=0;
	if(gainmax[0][0]<gain[0][0])gainmax[0][0]=gain[0][0];
	for (long int i=1;i<N;++i){
		gain[0][i]=gain[0][i-1]+XV[1][i]-XV[0][i]+XV[0][i-1];
		gain[1][i]=gaintotal-gain[0][i]+XV[1][i];
		gainmax[0][i]=gainmax[0][i-1];
		if(gainmax[0][i]<gain[0][i]) gainmax[0][i]=gain[0][i];
	}
	gainmax[1][N-1]=0;
	if(gainmax[1][N-1]<gain[1][N-1])gainmax[1][N-1]=gain[1][N-1];
	for (long int i=N-2;i>=0;--i){
		gainmax[1][i]=gainmax[1][i+1];
		if(gainmax[1][i]<gain[1][i]) gainmax[1][i]=gain[1][i];
	}
	max=gainmax[0][N-1];
	if(max<gainmax[1][0])max=gainmax[1][N-1];
	for(int i=0;i<N-1;++i){
		if(max<gain[0][i]-XV[0][i]+gainmax[1][i+1])max=gain[0][i]-XV[0][i]+gainmax[1][i+1];
		if(max<gain[1][i+1]-C+XV[0][i+1]+gainmax[0][i])max=gain[1][i+1]-C+XV[0][i+1]+gainmax[0][i];
	}
	printf("%ld\n",max);

	return (0);
}

Score : 500 points

Problem Statement

"Teishi-zushi", a Japanese restaurant, is a plain restaurant with only one round counter. The outer circumference of the counter is C meters. Customers cannot go inside the counter.

Nakahashi entered Teishi-zushi, and he was guided to the counter. Now, there are N pieces of sushi (vinegared rice with seafood and so on) on the counter. The distance measured clockwise from the point where Nakahashi is standing to the point where the i-th sushi is placed, is x_i meters. Also, the i-th sushi has a nutritive value of v_i kilocalories.

Nakahashi can freely walk around the circumference of the counter. When he reach a point where a sushi is placed, he can eat that sushi and take in its nutrition (naturally, the sushi disappears). However, while walking, he consumes 1 kilocalories per meter.

Whenever he is satisfied, he can leave the restaurant from any place (he does not have to return to the initial place). On balance, at most how much nutrition can he take in before he leaves? That is, what is the maximum possible value of the total nutrition taken in minus the total energy consumed? Assume that there are no other customers, and no new sushi will be added to the counter. Also, since Nakahashi has plenty of nutrition in his body, assume that no matter how much he walks and consumes energy, he never dies from hunger.

Constraints

1 ≤ N ≤ 10^5

2 ≤ C ≤ 10^{14}

1 ≤ x_1 < x_2 < ... < x_N < C

1 ≤ v_i ≤ 10^9

All values in input are integers.

Subscores

300 points will be awarded for passing the test set satisfying N ≤ 100.

Input

Input is given from Standard Input in the following format:

N C
x_1 v_1
x_2 v_2
:
x_N v_N

Output

If Nakahashi can take in at most c kilocalories on balance before he leaves the restaurant, print c.

Sample Input 1

3 20
2 80
9 120
16 1

Sample Output 1

191

There are three sushi on the counter with a circumference of 20 meters. If he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks seven more meters clockwise, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 9 kilocalories, thus he can take in 191 kilocalories on balance, which is the largest possible value.

Sample Input 2

3 20
2 80
9 1
16 120

Sample Output 2

192

The second and third sushi have been swapped. Again, if he walks two meters clockwise from the initial place, he can eat a sushi of 80 kilocalories. If he walks six more meters counterclockwise this time, he can eat a sushi of 120 kilocalories. If he leaves now, the total nutrition taken in is 200 kilocalories, and the total energy consumed is 8 kilocalories, thus he can take in 192 kilocalories on balance, which is the largest possible value.

Sample Input 3

1 100000000000000
50000000000000 1

Sample Output 3

0

Even though the only sushi is so far that it does not fit into a 32-bit integer, its nutritive value is low, thus he should immediately leave without doing anything.

Sample Input 4

15 10000000000
400000000 1000000000
800000000 1000000000
1900000000 1000000000
2400000000 1000000000
2900000000 1000000000
3300000000 1000000000
3700000000 1000000000
3800000000 1000000000
4000000000 1000000000
4100000000 1000000000
5200000000 1000000000
6600000000 1000000000
8000000000 1000000000
9300000000 1000000000
9700000000 1000000000

Sample Output 4

6500000000

All these sample inputs above are included in the test set for the partial score.


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

Score : 100 points

Problem Statement

There are a total of A + B cats and dogs.
Among them, A are known to be cats, but the remaining B are not known to be either cats or dogs.

Determine if it is possible that there are exactly X cats among these A + B animals.

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

1 \leq X \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B X

Output

If it is possible that there are exactly X cats, print YES; if it is impossible, print NO.

Sample Input 1

3 5 4

Sample Output 1

YES

If there are one cat and four dogs among the B = 5 animals, there are X = 4 cats in total.

Sample Input 2

2 2 6

Sample Output 2

NO

Even if all of the B = 2 animals are cats, there are less than X = 6 cats in total.

Sample Input 3

5 3 2

Sample Output 3

NO

Even if all of the B = 3 animals are dogs, there are more than X = 2 cats in total.


#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a,b,x;
    1<=a<=100;
    1<=b<=100;
    1<=x<=200;
    scanf("%d %d %d",&a,&b,&x);
    if(x<(a+b)&&x>a)
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }
    return 0;
}


#include<stdio.h>
int main(void)
{
  int a,b,x;
  scanf("%d%d%d",&a,&b,&x);
  if(a+b>=x){
    if(a<=x){
      puts("Yes");
      return 0;
    }
  }
  puts("No");
  return 0;
}


#include <stdio.h>

int main(void)
{
	int a,b,c;
	scanf("%d %d %d",&a,&b,&c);
	if(c-a<=b){
		printf("YES");
	}else{
		printf("NO");
	}
	return 0;
}


#include <stdio.h>

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

    if (a < x && a + b >= x) printf ("YES");
    else printf ("NO");

    return (0);
}


#include<stdio.h>
int main(void){
	int n,m,x,i,s=0,t=0;
	scanf("%d %d %d",&n,&m,&x);
	for(i=0;i<m;i++){
		int b;
		scanf("%d",&b);
		if(b<x){s++;}
		else{t++;}
	}
	printf("%d",s<t?s:t);
	return 0;
}

Score : 200 points

Problem Statement

There are N + 1 squares arranged in a row, numbered 0, 1, ..., N from left to right.

Initially, you are in Square X.
You can freely travel between adjacent squares. Your goal is to reach Square 0 or Square N.
However, for each i = 1, 2, ..., M, there is a toll gate in Square A_i, and traveling to Square A_i incurs a cost of 1.
It is guaranteed that there is no toll gate in Square 0, Square X and Square N.

Find the minimum cost incurred before reaching the goal.

Constraints

1 \leq N \leq 100

1 \leq M \leq 100

1 \leq X \leq N - 1

1 \leq A_1 < A_2 < ... < A_M \leq N

A_i \neq X

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M X
A_1 A_2 ... A_M

Output

Print the minimum cost incurred before reaching the goal.

Sample Input 1

5 3 3
1 2 4

Sample Output 1

1

The optimal solution is as follows:

First, travel from Square 3 to Square 4. Here, there is a toll gate in Square 4, so the cost of 1 is incurred.

Then, travel from Square 4 to Square 5. This time, no cost is incurred.

Now, we are in Square 5 and we have reached the goal.

In this case, the total cost incurred is 1.

Sample Input 2

7 3 2
4 5 6

Sample Output 2

0

We may be able to reach the goal at no cost.

Sample Input 3

10 7 5
1 2 3 4 6 8 9

Sample Output 3

3


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

int main(void)
{
	int N, M, X;
	int *A;
	int i;
	int cost1 = 0, cost2 = 0;

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

	A = (int *)malloc(sizeof(int) * M);

	for(i = 0; i < M; i++)
		scanf("%d", &A[i]);

	if(X >= A[M - 1])
	{
		printf("01\n");
		return 0;
	}

	for(i = 0; A[i] < X && i < M; i++)
		cost1++;
	cost2 = M - cost1;

	if(cost1 < cost2)
		printf("%d\n", cost1);
	else
		printf("%d\n", cost2);

	return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define Rep(i,a,n)for(int i=(int)(a);i<(int)(n);i++)
#define rep(i,n)Rep(i,0,n)
int up(const void* a, const void* b) {
	return *(int*)a - *(int*)b;
}

int main() {
	int n;
	scanf("%d\n",&n);
	int x[n],y[n],MED,med;
	rep(i, n) { scanf("%d", &x[i]); y[i] = x[i]; }
	qsort(y, n, sizeof(int), up);
	med = y[n / 2-1];
	MED = y[n / 2];
	rep(i, n) {
		if (x[i] <= med)printf("%d\n", MED);
		else printf("%d\n", med);
	}
}

Score : 300 points

Problem Statement

When l is an odd number, the median of l numbers a_1, a_2, ..., a_l is the (\frac{l+1}{2})-th largest value among a_1, a_2, ..., a_l.

You are given N numbers X_1, X_2, ..., X_N, where N is an even number.
For each i = 1, 2, ..., N, let the median of X_1, X_2, ..., X_N excluding X_i, that is, the median of X_1, X_2, ..., X_{i-1}, X_{i+1}, ..., X_N be B_i.

Find B_i for each i = 1, 2, ..., N.

Constraints

2 \leq N \leq 200000

N is even.

1 \leq X_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
X_1 X_2 ... X_N

Output

Print N lines.
The i-th line should contain B_i.

Sample Input 1

4
2 4 4 3

Sample Output 1

4
3
3
4

Since the median of X_2, X_3, X_4 is 4, B_1 = 4.

Since the median of X_1, X_3, X_4 is 3, B_2 = 3.

Since the median of X_1, X_2, X_4 is 3, B_3 = 3.

Since the median of X_1, X_2, X_3 is 4, B_4 = 4.

Sample Input 2

2
1 2

Sample Output 2

2
1

Sample Input 3

6
5 5 4 4 3 3

Sample Output 3

4
4
4
4
4
4


#include<stdio.h>
#include<stdlib.h>
int compare(const void* a, const void* b){
    return *(int*)a - *(int*)b;
}

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

    qsort(x, n, sizeof(int)*2, compare);

    int ans[n];
    for(int i=0; i<(n/2); i++){
        ans[x[i][1]] = x[n/2][0];
    }
    for(int i=n/2; i<n; i++){
        ans[x[i][1]] = x[(n/2)-1][0];
    }

    for(int i=0; i<n; i++){
        printf("%d\n", ans[i]);
    }

    return 0;

}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int compare(const void *a,const void *b){
    return *(int*)a-*(int*)b;
}
/*
int comb(int n, int r){
  if(r==0 || r==n)return (1);
  else if(r==1)return(n);
  return(comb(n-1,r-1)+comb(n-1,r));
}
*/
int main(){
  int n;
  int a[100001];
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d",&a[i]);
  }
  qsort(a,n,sizeof(a[0]),compare);
  float nmax=a[n-1];
  float med = nmax/2.000;
  int rmax=1111111111;
  double tempbest=1111111111;
  for(int i=0;i<n-1;i++){
    int temp=abs(med-a[i]);
    if(temp<tempbest){
      rmax=a[i];
      tempbest=temp;
    }
  }
  printf("%d %d\n",(int)nmax,rmax);
  return 0;
}

Score : 400 points

Problem Statement

Let {\rm comb}(n,r) be the number of ways to choose r objects from among n objects, disregarding order.
From n non-negative integers a_1, a_2, ..., a_n, select two numbers a_i > a_j so that {\rm comb}(a_i,a_j) is maximized.
If there are multiple pairs that maximize the value, any of them is accepted.

Constraints

2 \leq n \leq 10^5

0 \leq a_i \leq 10^9

a_1,a_2,...,a_n are pairwise distinct.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print a_i and a_j that you selected, with a space in between.

Sample Input 1

5
6 9 4 2 11

Sample Output 1

11 6

\rm{comb}(a_i,a_j) for each possible selection is as follows:

\rm{comb}(4,2)=6

\rm{comb}(6,2)=15

\rm{comb}(6,4)=15

\rm{comb}(9,2)=36

\rm{comb}(9,4)=126

\rm{comb}(9,6)=84

\rm{comb}(11,2)=55

\rm{comb}(11,4)=330

\rm{comb}(11,6)=462

\rm{comb}(11,9)=55

Thus, we should print 11 and 6.

Sample Input 2

2
100 0

Sample Output 2

100 0


Score : 100 points

Problem Statement

You are given a string S of length 3 consisting of a, b and c. Determine if S can be obtained by permuting abc.

Constraints

|S|=3

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

If S can be obtained by permuting abc, print Yes; otherwise, print No.

Sample Input 1

bac

Sample Output 1

Yes

Swapping the first and second characters in bac results in abc.

Sample Input 2

bab

Sample Output 2

No

Sample Input 3

abc

Sample Output 3

Yes

Sample Input 4

aaa

Sample Output 4

No


#include<stdio.h>

int main(){
  int A,B,K;
  int i,j,k;
  scanf("%d %d %d",&A,&B,&K);
  if(B-A-K <=1){
    for(i=A;i<=B;i++)
      printf("%d\n",i);
    return 0;
  }

  for(i=A;i<A+K;i++){
    printf("%d\n",i);
  }

  for(i=B-K+1;i<=B;i++)
    printf("%d\n",i);

  return 0;
}

Score : 200 points

Problem Statement

Print all the integers that satisfies the following in ascending order:

Among the integers between A and B (inclusive), it is either within the K smallest integers or within the K largest integers.

Constraints

1 \leq A \leq B \leq 10^9

1 \leq K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

Print all the integers that satisfies the condition above in ascending order.

Sample Input 1

3 8 2

Sample Output 1

3
4
7
8

3 is the first smallest integer among the integers between 3 and 8.

4 is the second smallest integer among the integers between 3 and 8.

7 is the second largest integer among the integers between 3 and 8.

8 is the first largest integer among the integers between 3 and 8.

Sample Input 2

4 8 3

Sample Output 2

4
5
6
7
8

Sample Input 3

2 9 100

Sample Output 3

2
3
4
5
6
7
8
9


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

int main(){
    int A,B,K,i;
    scanf("%d %d %d",&A,&B,&K);
    if (B-A > 2*K){
        int * s = malloc(sizeof(int)*2*K);
        for(i=0;i<K;i++){
            s[i]=A+i;
            s[2*K-1-i]=B-i;
        }
        for(i=0;i<2*K;i++){
            printf("%d\n", s[i]);
        }
    } else{
        for(i=A;i<=B;i++){
        printf("%d\n", i);
        }
    }
    return 0;
}


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

int main (void){
        char S[27];
        int lng;
        int al[26];
        char* s1=S;
        char* s2="zyxwvutsrqponmlkjihgfedcba";
        int flag;
        int z=0;
        for (int i = 0; i < 26; i++) {
                al[i]=0;
        }
        scanf("%s",S);
        lng=strlen(S);
        for (int i = 0; i < lng; i++) {
                al[S[i]-97]=1+i;
                // printf("%d",S[i]-97);
        }
        //printf("\n");
        if (lng<26) {
                printf("%s",S );
                for (int i = 0; i < 26; i++) {
                        if (al[i]==0) {
                                printf("%c",i+97 );
                                break;
                        }
                }
        }else if (strcmp(s1,s2)==0) {
                printf("-1");
        }else{
                for (int i = 0; i < 25; i++) {
                        if (S[i]<S[i+1]) {
                                flag=i;
                                if (S[i+1]=='z') {
                                        z=1;
                                }else{
                                        z=0;
                                }
                        }
                }
                //printf("%d\n",flag);
                //printf("%c\n",S[flag] );
                //printf("%d\n",z );
                if (z==0) {
                        for (int i = 0; i < flag; i++) {
                                printf("%c",S[i] );
                        }
                        for (int i = 0; i < 26; i++) {
                                if (al[i]>flag+1) {
                                        printf("%c",i+97 );
                                        break;
                                }
                        }
                }else{
                        for (int i = 0; i < flag+1; i++) {
                                printf("%c",S[i] );
                        }
                        for (int i = 0; i < 26; i++) {
                                if (al[i]>flag+1) {
                                        printf("%c",i+97 );
                                        break;
                                }
                        }
                }
        }
        return 0;
}

Score : 300 points

Problem Statement

Gotou just received a dictionary. However, he doesn't recognize the language used in the dictionary. He did some analysis on the dictionary and realizes that the dictionary contains all possible diverse words in lexicographical order.

A word is called diverse if and only if it is a nonempty string of English lowercase letters and all letters in the word are distinct. For example, atcoder, zscoder and agc are diverse words while gotou and connect aren't diverse words.

Given a diverse word S, determine the next word that appears after S in the dictionary, i.e. the lexicographically smallest diverse word that is lexicographically larger than S, or determine that it doesn't exist.

Let X = x_{1}x_{2}...x_{n} and Y = y_{1}y_{2}...y_{m} be two distinct strings. X is lexicographically larger than Y if and only if Y is a prefix of X or x_{j} > y_{j} where j is the smallest integer such that x_{j} \neq y_{j}.

Constraints

1 \leq |S| \leq 26

S is a diverse word.

Input

Input is given from Standard Input in the following format:

S

Output

Print the next word that appears after S in the dictionary, or -1 if it doesn't exist.

Sample Input 1

atcoder

Sample Output 1

atcoderb

atcoderb is the lexicographically smallest diverse word that is lexicographically larger than atcoder. Note that atcoderb is lexicographically smaller than b.

Sample Input 2

abc

Sample Output 2

abcd

Sample Input 3

zyxwvutsrqponmlkjihgfedcba

Sample Output 3

-1

This is the lexicographically largest diverse word, so the answer is -1.

Sample Input 4

abcdefghijklmnopqrstuvwzyx

Sample Output 4

abcdefghijklmnopqrstuvx


#include<stdio.h>
int main()

{

    int A,B,C,D,t,z;
    scanf("%d",&A);
    scanf("%d",&B);
    scanf("%d",&C);
    scanf("%d",&D);

    if(A<B){
        t=A;
    }
    else{
        t=B;
    }
    if(C<D){
        z=t+C;

    }
    else{
        z=t+D;
    }
    printf("%d\n",z);
    return 0;
}

Score : 100 points

Problem Statement

You planned a trip using trains and buses.
The train fare will be A yen (the currency of Japan) if you buy ordinary tickets along the way, and B yen if you buy an unlimited ticket.
Similarly, the bus fare will be C yen if you buy ordinary tickets along the way, and D yen if you buy an unlimited ticket.

Find the minimum total fare when the optimal choices are made for trains and buses.

Constraints

1 \leq A \leq 1 000

1 \leq B \leq 1 000

1 \leq C \leq 1 000

1 \leq D \leq 1 000

All input values are integers.

Input

Input is given from Standard Input in the following format:

A
B
C
D

Output

Print the minimum total fare.

Sample Input 1

600
300
220
420

Sample Output 1

520

The train fare will be 600 yen if you buy ordinary tickets, and 300 yen if you buy an unlimited ticket.
Thus, the optimal choice for trains is to buy an unlimited ticket for 300 yen.
On the other hand, the optimal choice for buses is to buy ordinary tickets for 220 yen.

Therefore, the minimum total fare is 300 + 220 = 520 yen.

Sample Input 2

555
555
400
200

Sample Output 2

755

Sample Input 3

549
817
715
603

Sample Output 3

1152


#include <stdio.h>

int main()
{
   int A,B,C,D;
   scanf("%d%d%d%d",&A,&B,&C,&D);
   if(A < B)
    {
      if (C < D)
       printf("%d", A+C);
       else
      printf("%d", A+D);
    }
    else
    {
     if (C < D)
       printf("%d", B+C);
       else
      printf("%d", B+D);
    }
}


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

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

int main()
{
  long long A,B,C,D;
  int i,j;
  int number;
  char s[10];
  long long  cnt_t=0;
  long long  cnt_b=0;

//    int k=0;
  scanf("%lld ", &A);
    scanf("%lld",  &B);
    scanf("%lld",  &C);
    scanf("%lld",  &D);
 //printf("%lld %lld %lld %lld", A, B, C, D);
//  scanf("%lld", &M);
  if(A<B) cnt_t=A;
  else    cnt_t=B;
  if(C<D) cnt_b=C;
  else    cnt_b=D;

  printf("%lld\n",(cnt_t + cnt_b));
//     printf("%c%c%c\n",s[0][0],s[1][1],s[2][2]);

return 0;
}


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

#define LINE_BUF_SIZE 16
//#define N 100

int main(){

    int i,j,k;
    int max = 0;
    int count = 0;
    int A,B,N = 100;
    char line[LINE_BUF_SIZE];

    fgets(line,sizeof(line),stdin);
    sscanf(line,"%d %d", &A,&B);

    char **C = (char **)malloc( sizeof(char *) * N );   /*行数分{(char *)*N byte}だけのメモリを動的確保した、それへのポインタ*/

    /*ちょうど半分で色分け*/

    for(i=0;i<N;i++){
        C[i] = (char *)malloc( sizeof(char) * ( N + 1 ));   /*一行の文字列を格納するために動的確保したメモリへのポインタ*/
        for(j=0;j<N;j++){
            if(i < N / 2){
                C[i][j] = '.';
            }
            else{
                C[i][j] = '#';
            }
        }
        C[i][N] = '\0';
    }

    //printf("色分け完了\n");

    /*必要数だけブロックを配置*/

    int Line = 0,Colum = 1;

    for(i=1;i<=A;i++){
        if(N-(2*Colum-1) > 1){
            C[N-2*Line-1][N-(2*Colum-1)] = '.';
            Colum += 1;
        }
        else{           /*一列が全て白黒のブロックで埋まった場合*/
            Colum = 1;
            Line += 1;
        }
    }

    Line = 0;
    Colum = 1;

    for(i=1;i<=B;i++){
        if((2*Colum-1) < N-1){
            C[2*Line][2*Colum] = '#';
            Colum += 1;
        }
        else{           /*一列が全て白黒のブロックで埋まった場合*/
            Colum = 1;
            Line += 1;
        }
    }

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

    for(i=0;i<N;i++){
        printf("%s\n",C[i]);
    }

    return 0;

}

Score : 500 points

Problem Statement

You are given two integers A and B.

Print a grid where each square is painted white or black that satisfies the following conditions, in the format specified in Output section:

Let the size of the grid be h \times w (h vertical, w horizontal). Both h and w are at most 100.

The set of the squares painted white is divided into exactly A connected components.

The set of the squares painted black is divided into exactly B connected components.

It can be proved that there always exist one or more solutions under the conditions specified in Constraints section.
If there are multiple solutions, any of them may be printed.

Notes

Two squares painted white, c_1 and c_2, are called connected when the square c_2 can be reached from the square c_1 passing only white squares by repeatedly moving up, down, left or right to an adjacent square.

A set of squares painted white, S, forms a connected component when the following conditions are met:

Any two squares in S are connected.

No pair of a square painted white that is not included in S and a square included in S is connected.

A connected component of squares painted black is defined similarly.

Constraints

1 \leq A \leq 500

1 \leq B \leq 500

Input

Input is given from Standard Input in the following format:

A B

Output

Output should be in the following format:

In the first line, print integers h and w representing the size of the grid you constructed, with a space in between.

Then, print h more lines. The i-th (1 \leq i \leq h) of these lines should contain a string s_i as follows:

If the square at the i-th row and j-th column (1 \leq j \leq w) in the grid is painted white, the j-th character in s_i should be ..

If the square at the i-th row and j-th column (1 \leq j \leq w) in the grid is painted black, the j-th character in s_i should be #.

Sample Input 1

2 3

Sample Output 1

3 3
##.
..#
#.#

This output corresponds to the grid below:

Sample Input 2

7 8

Sample Output 2

3 5
#.#.#
.#.#.
#.#.#

Sample Input 3

1 1

Sample Output 3

4 2
..
#.
##
##

Sample Input 4

3 14

Sample Output 4

8 18
..................
..................
....##.......####.
....#.#.....#.....
...#...#....#.....
..#.###.#...#.....
.#.......#..#.....
#.........#..####.


#include <stdio.h>
int main(void)
{   int i,j,k;
    scanf("%d %d %d",&i,&j,&k);

    if(scanf("%d %d %d",&i,&j,&k) == 1){
    if((i + j)>= k)
    {
        printf("yes");
    }
    else{

        printf("No");}}
}

Score : 100 points

Problem Statement

An elementary school student Takahashi has come to a variety store.

He has two coins, A-yen and B-yen coins (yen is the currency of Japan), and wants to buy a toy that costs C yen. Can he buy it?

Note that he lives in Takahashi Kingdom, and may have coins that do not exist in Japan.

Constraints

All input values are integers.

1 \leq A, B \leq 500

1 \leq C \leq 1000

Input

Input is given from Standard Input in the following format:

A B C

Output

If Takahashi can buy the toy, print Yes; if he cannot, print No.

Sample Input 1

50 100 120

Sample Output 1

Yes

He has 50 + 100 = 150 yen, so he can buy the 120-yen toy.

Sample Input 2

500 100 1000

Sample Output 2

No

He has 500 + 100 = 600 yen, but he cannot buy the 1000-yen toy.

Sample Input 3

19 123 143

Sample Output 3

No

There are 19-yen and 123-yen coins in Takahashi Kingdom, which are rather hard to use.

Sample Input 4

19 123 142

Sample Output 4

Yes


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

int main (void){
  int n,m;
  int Is_exist_s[100];
  int Is_exist_t[100];
  char s[100][11],t[100][11];
  int max;
  int i,j;
  int point;
  max=0;
  point=0;

  scanf("%d",&n);
  for(i=0;i<n;i++){
    Is_exist_s[i]=1;
    scanf("%s",s[i]);
  }

  scanf("%d",&m);
  for(i=0;i<m;i++){
    Is_exist_t[i]=1;
    scanf("%s",t[i]);
  }

  for(i=0;i<n;i++)for(j=0;j<m;j++){
    if(Is_exist_s[i]!=0 && Is_exist_t[j]!=0 && strcmp(s[i],t[j])==0){
      Is_exist_s[i]=0;
      Is_exist_t[j]=0;
    }
   }

   for(i=0;i<n;i++,point=0)for(j=0;j<n;j++){
     if(Is_exist_s[i]!=0 && Is_exist_s[j]!=0 && strcmp(s[i],s[j])==0) point++;
     if(max<point)max=point;
   }

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

Score : 200 points

Problem Statement

Takahashi has N blue cards and M red cards.
A string is written on each card. The string written on the i-th blue card is s_i, and the string written on the i-th red card is t_i.

Takahashi will now announce a string, and then check every card. Each time he finds a blue card with the string announced by him, he will earn 1 yen (the currency of Japan); each time he finds a red card with that string, he will lose 1 yen.

Here, we only consider the case where the string announced by Takahashi and the string on the card are exactly the same. For example, if he announces atcoder, he will not earn money even if there are blue cards with atcoderr, atcode, btcoder, and so on. (On the other hand, he will not lose money even if there are red cards with such strings, either.)

At most how much can he earn on balance?

Note that the same string may be written on multiple cards.

Constraints

N and M are integers.

1 \leq N, M \leq 100

s_1, s_2, ..., s_N, t_1, t_2, ..., t_M are all strings of lengths between 1 and 10 (inclusive) consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N
M
t_1
t_2
:
t_M

Output

If Takahashi can earn at most X yen on balance, print X.

Sample Input 1

3
apple
orange
apple
1
grape

Sample Output 1

2

He can earn 2 yen by announcing apple.

Sample Input 2

3
apple
orange
apple
5
apple
apple
apple
apple
apple

Sample Output 2

1

If he announces apple, he will lose 3 yen. If he announces orange, he can earn 1 yen.

Sample Input 3

1
voldemort
10
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort
voldemort

Sample Output 3

0

If he announces voldemort, he will lose 9 yen. If he announces orange, for example, he can avoid losing a yen.

Sample Input 4

6
red
red
blue
yellow
yellow
red
5
red
red
yellow
green
blue

Sample Output 4

1


#include <stdio.h>

int main()
{
  int i, j;
  char c, s_out[64];

  for (i = 1; i < 10; i++) {
    c = getchar();
    if (i % 4 == 1) {
      j = i / 4;
      s_out[j] = putchar(c);
    }
  }

  return 0;
}

Score : 100 points

Problem Statement

We have a 3×3 square grid, where each square contains a lowercase English letters.
The letter in the square at the i-th row from the top and j-th column from the left is c_{ij}.

Print the string of length 3 that can be obtained by concatenating the letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

Constraints

Input consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

c_{11}c_{12}c_{13}
c_{21}c_{22}c_{23}
c_{31}c_{32}c_{33}

Output

Print the string of length 3 that can be obtained by concatenating the letters on the diagonal connecting the top-left and bottom-right corner of the grid, from the top-left to bottom-right.

Sample Input 1

ant
obe
rec

Sample Output 1

abc

The letters in the squares on the diagonal connecting the top-left and bottom-right corner of the grid are a, b and c from top-right to bottom-left. Concatenate these letters and print abc.

Sample Input 2

edu
cat
ion

Sample Output 2

ean


Score : 200 points

Problem Statement

Find the number of palindromic numbers among the integers between A and B (inclusive).
Here, a palindromic number is a positive integer whose string representation in base 10 (without leading zeros) reads the same forward and backward.

Constraints

10000 \leq A \leq B \leq 99999

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the number of palindromic numbers among the integers between A and B (inclusive).

Sample Input 1

11009 11332

Sample Output 1

4

There are four integers that satisfy the conditions: 11011, 11111, 11211 and 11311.

Sample Input 2

31415 92653

Sample Output 2

612


#include <stdio.h>

int main(void){
  int N;
  scanf("%d", &N);
  printf("%d\n", N/3);
  return 0;
}

Score : 100 points

Problem Statement

There are N students in a school.

We will divide these students into some groups, and in each group they will discuss some themes.

You think that groups consisting of two or less students cannot have an effective discussion, so you want to have as many groups consisting of three or more students as possible.

Divide the students so that the number of groups consisting of three or more students is maximized.

Constraints

1 \leq N \leq 1000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N

Output

If you can form at most x groups consisting of three or more students, print x.

Sample Input 1

8

Sample Output 1

2

For example, you can form a group of three students and another of five students.

Sample Input 2

2

Sample Output 2

0

Sometimes you cannot form any group consisting of three or more students, regardless of how you divide the students.

Sample Input 3

9

Sample Output 3

3


#include<stdio.h>

int main() {
	int a;
	int b;
	int n=0;
	scanf("%d", &a);
	for (b = 1; b < 350; b++) {
		a = a - 3;
		n++;
		if (a < 3) {
			break;
		}
	}
	printf("%d", n);
	return 0;
}


#include <stdio.h>

int main(void)
{
    int n,s[100],i,a={0};

    scanf("%d",&n);

    for(i=0;i<n;i++) scanf("%d",&s[i]);

    for(i=0;i<n;i++)
        if(s[i]=='Y') a==1;

    if(a==1) printf("Four\n");
    else     printf("Three\n");

  return 0;
}

Score : 200 points

Problem Statement

In Japan, people make offerings called hina arare, colorful crackers, on March 3.

We have a bag that contains N hina arare. (From here, we call them arare.)

It is known that the bag either contains arare in three colors: pink, white and green, or contains arare in four colors: pink, white, green and yellow.

We have taken out the arare in the bag one by one, and the color of the i-th arare was S_i, where colors are represented as follows - pink: P, white: W, green: G, yellow: Y.

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

Constraints

1 \leq N \leq 100

S_i is P, W, G or Y.

There always exist i, j and k such that S_i=P, S_j=W and S_k=G.

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_N

Output

If the number of colors of the arare in the bag was three, print Three; if the number of colors was four, print Four.

Sample Input 1

6
G W Y P Y W

Sample Output 1

Four

The bag contained arare in four colors, so you should print Four.

Sample Input 2

9
G W W G P W P G G

Sample Output 2

Three

The bag contained arare in three colors, so you should print Three.

Sample Input 3

8
P Y W G Y W Y Y

Sample Output 3

Four


#include <stdio.h>

int main(void)
{
    int n,s[100],i,a={0};

    scanf("%d",&n);

    for(i=0;i<n;i++) scanf("%d",&s[i]);

    for(i=0;i<n;i++){
        if(s[i]=='Y') a=a+1;
    }

    if(a>=1) printf("Four\n");
    else     printf("Three\n");

  return 0;
}


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

int main(){
    int x, i;
    char c;
    int flag = 0;
    scanf("%d", &x);

    for(i = 0; i < x; i++){
        scanf(" %c", &c);
        if (c == 'Y') flag = 1;
    }

    flag ? printf("Four\n") : printf("Three\n");

    return 0;
}


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

#define NEW(p,n){p=malloc((n)*sizeof(p[0]));if(p==NULL){printf("not enough memory\n");exit(1);};}
//pの型の変数n個の要素分のメモリを確保し、そのアドレスをpに代入するマクロ

#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define SWAP(type, x, y) do { type tmp = x; x = y; y = tmp; } while (0)

#define MOD 1000000007

typedef struct{
    int x;
    int y;
    int value;
}grid;

//昇順
int asc(const void* a, const void* b){
    grid p = *(grid*)a;
    grid q = *(grid*)b;
    return p.value-q.value;
}

int main(void){
    int H,W,D;
    scanf("%d%d%d",&H,&W,&D);

    //A[0]~A[N-1]に格納する
    grid* G;
    NEW(G,H*W);
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            G[i*W+j].x=i;
            G[i*W+j].y=j;
            scanf("%d",&G[i*W+j].value);
        }
    }
    qsort(G,H*W,sizeof(grid),asc);

    int Q;
    scanf("%d",&Q);
    for(int i=0;i<Q;i++){
        int L,R;
        long ans=0;
        while(L!=R){
            ans+=abs(G[L+D].x-G[L].x)+abs(G[L+D].y-G[L].y);
            L+=D;
        }
        printf("%ld\n",ans);
    }

    return 0;
}

Score : 400 points

Problem Statement

We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j).

The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is A_{i,j}.

You, a magical girl, can teleport a piece placed on Square (i,j) to Square (x,y) by consuming |x-i|+|y-j| magic points.

You now have to take Q practical tests of your ability as a magical girl.

The i-th test will be conducted as follows:

Initially, a piece is placed on the square where the integer L_i is written.

Let x be the integer written in the square occupied by the piece. Repeatedly move the piece to the square where the integer x+D is written, as long as x is not R_i. The test ends when x=R_i.

Here, it is guaranteed that R_i-L_i is a multiple of D.

For each test, find the sum of magic points consumed during that test.

Constraints

1 \leq H,W \leq 300

1 \leq D \leq H×W

1 \leq A_{i,j} \leq H×W

A_{i,j} \neq A_{x,y} ((i,j) \neq (x,y))

1 \leq Q \leq 10^5

1 \leq L_i \leq R_i \leq H×W

(R_i-L_i) is a multiple of D.

Input

Input is given from Standard Input in the following format:

H W D
A_{1,1} A_{1,2} ... A_{1,W}
:
A_{H,1} A_{H,2} ... A_{H,W}
Q
L_1 R_1
:
L_Q R_Q

Output

For each test, print the sum of magic points consumed during that test.

Output should be in the order the tests are conducted.

Sample Input 1

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

Sample Output 1

5

4 is written in Square (1,2).

6 is written in Square (3,3).

8 is written in Square (3,1).

Thus, the sum of magic points consumed during the first test is (|3-1|+|3-2|)+(|3-3|+|1-3|)=5.

Sample Input 2

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

Sample Output 2

0
0

Note that there may be a test where the piece is not moved at all, and there may be multiple identical tests.

Sample Input 3

5 5 4
13 25 7 15 17
16 22 20 2 9
14 11 12 1 19
10 6 23 8 18
3 21 5 24 4
3
13 13
2 10
13 13

Sample Output 3

0
5
0


#include<stdio.h>
#include<string.h>
int main()
{
    int len,maxsum,flag,i;
    char str[50];
    while(gets(str)!=NULL)
    {
        flag=1;
        len = strlen(str);
        for(i=1;i<len;i++)
        {
            if(str[i]<'9')
            {
                flag=0;
                break;
            }
        }
        if(flag==0)
            maxsum=9*(len-1)-1+str[0]-'0';
            else
                maxsum=9*(len-1)+str[0]-'0';
        printf("%d\n",maxsum);
    }
    return 0;
}

Score : 300 points

Problem Statement

Find the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Constraints

1\leq N \leq 10^{16}

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the maximum possible sum of the digits (in base 10) of a positive integer not greater than N.

Sample Input 1

100

Sample Output 1

18

For example, the sum of the digits in 99 is 18, which turns out to be the maximum value.

Sample Input 2

9995

Sample Output 2

35

For example, the sum of the digits in 9989 is 35, which turns out to be the maximum value.

Sample Input 3

3141592653589793

Sample Output 3

137


#include<stdio.h>
int main()
{
    int N,A;
    scanf("%d",&N);
    scanf("%d",&A);
    if(A==0 && 500*2!=N && N>500)
    {
        printf("NO");
    }
    else
    {

           int d = N/500;
           int e = d*500;
           int r = N - e;
           if(r<=A)
           {
             printf("YES");
           }
           else
           {
               printf("NO");
           }
    }

}

Score: 100 points

Problem Statement

E869120 has A 1-yen coins and infinitely many 500-yen coins.

Determine if he can pay exactly N yen using only these coins.

Constraints

N is an integer between 1 and 10000 (inclusive).

A is an integer between 0 and 1000 (inclusive).

Input

Input is given from Standard Input in the following format:

N
A

Output

If E869120 can pay exactly N yen using only his 1-yen and 500-yen coins, print Yes; otherwise, print No.

Sample Input 1

2018
218

Sample Output 1

Yes

We can pay 2018 yen with four 500-yen coins and 18 1-yen coins, so the answer is Yes.

Sample Input 2

2763
0

Sample Output 2

No

When we have no 1-yen coins, we can only pay a multiple of 500 yen using only 500-yen coins. Since 2763 is not a multiple of 500, we cannot pay this amount.

Sample Input 3

37
514

Sample Output 3

Yes


#include <stdio.h>
int main(void){
  int a,b;
  // a en wo b en bun hikeru.
  scanf("%d",&a);
    scanf("%d",&b);
  (a-(a/500))<=b ? printf("Yes") : printf("No");
return 0;
}


#include <stdio.h>

int main(){

    int a,n,o,p,q;

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

	q = n % 500;

	if(a>=q){
		printf("Yes");
	}
	else{
		printf("No");
	}

    return 0;
}


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

int main(){

    int a = 500;//infinite coin
    int b = 1;//limited

    int N,A;

    scanf("%d",&N);// harga
    scanf("%d",&A);//jumlah 1 yen

    int totalYen = b * A; //total

    int a2 = N % a;
    int a3 = totalYen -  a2;

    if(a3 >=0){

        printf("Yes");

    }

    else{
        printf("No");
    }

    return 0;
}


#include <stdio.h>

int main(void)
{
	int N, A;

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

	if (N % 500 <= A){
		printf("Yes\n");
	}
	else {
		printf("No\n");
	}

	return (0);
}


#include <stdio.h>

int main()
  {
  int A, N;
  scanf( "%d", &N );
  scanf( "%d", &A );
  if( N%500 <= A )
     {
       printf( "Yes\n" );
    }
  else
    {
    printf( "No\n" );
    }
  return 0;
  }


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

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

int main(void)
{
  int s, n, a[100];
  scanf("%d", &n);
  for (int i = 0; i<n; ++i) {
    scanf("%d", &a[i]);
  }
  qsort(a, n, sizeof(int), comp_inv);
  for (int i=s=0; i < n; ++i) {
    s = i%2?s-a[i]:s+a[i];
  }
  printf("%d\n", s);
  return 0;
}

Score: 200 points

Problem Statement

We have N cards. A number a_i is written on the i-th card.

Alice and Bob will play a game using these cards. In this game, Alice and Bob alternately take one card. Alice goes first.

The game ends when all the cards are taken by the two players, and the score of each player is the sum of the numbers written on the cards he/she has taken. When both players take the optimal strategy to maximize their scores, find Alice's score minus Bob's score.

Constraints

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

a_i \ (1 \leq i \leq N) is an integer between 1 and 100 (inclusive).

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

Print Alice's score minus Bob's score when both players take the optimal strategy to maximize their scores.

Sample Input 1

2
3 1

Sample Output 1

2

First, Alice will take the card with 3. Then, Bob will take the card with 1.
The difference of their scores will be 3 - 1 = 2.

Sample Input 2

3
2 7 4

Sample Output 2

5

First, Alice will take the card with 7. Then, Bob will take the card with 4. Lastly, Alice will take the card with 2. The difference of their scores will be 7 - 4 + 2 = 5. The difference of their scores will be 3 - 1 = 2.

Sample Input 3

4
20 18 2 18

Sample Output 3

18


Score: 400 points

Problem statement

We have an H \times W grid whose squares are painted black or white. The square at the i-th row from the top and the j-th column from the left is denoted as (i, j).

Snuke would like to play the following game on this grid. At the beginning of the game, there is a character called Kenus at square (1, 1). The player repeatedly moves Kenus up, down, left or right by one square. The game is completed when Kenus reaches square (H, W) passing only white squares.

Before Snuke starts the game, he can change the color of some of the white squares to black. However, he cannot change the color of square (1, 1) and (H, W). Also, changes of color must all be carried out before the beginning of the game.

When the game is completed, Snuke's score will be the number of times he changed the color of a square before the beginning of the game. Find the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed, that is, Kenus can never reach square (H, W) regardless of how Snuke changes the color of the squares.

The color of the squares are given to you as characters s_{i, j}. If square (i, j) is initially painted by white, s_{i, j} is .; if square (i, j) is initially painted by black, s_{i, j} is #.

Constraints

H is an integer between 2 and 50 (inclusive).

W is an integer between 2 and 50 (inclusive).

s_{i, j} is . or # (1 \leq i \leq H, 1 \leq j \leq W).

s_{1, 1} and s_{H, W} are ..

Input

Input is given from Standard Input in the following format:

H W
s_{1, 1}s_{1, 2}s_{1, 3} ... s_{1, W}
s_{2, 1}s_{2, 2}s_{2, 3} ... s_{2, W}
: :
s_{H, 1}s_{H, 2}s_{H, 3} ... s_{H, W}

Output

Print the maximum possible score that Snuke can achieve, or print -1 if the game cannot be completed.

Sample Input 1

3 3
..#
#..
...

Sample Output 1

2

The score 2 can be achieved by changing the color of squares as follows:

Sample Input 2

10 37
.....................................
...#...####...####..###...###...###..
..#.#..#...#.##....#...#.#...#.#...#.
..#.#..#...#.#.....#...#.#...#.#...#.
.#...#.#..##.#.....#...#.#.###.#.###.
.#####.####..#.....#...#..##....##...
.#...#.#...#.#.....#...#.#...#.#...#.
.#...#.#...#.##....#...#.#...#.#...#.
.#...#.####...####..###...###...###..
.....................................

Sample Output 2

209


#include <stdio.h>
int main(){
 int a,b,c,x,i,j,k,count=0;
  scanf("%d¥n%d¥n%d¥n%d¥n",&a,&b,&c,&x);
  for(i=0;i<=a;++i){
    for(j=0;j<=b;++j){
      for(k=0;k<=c;++k){
        if(500*i+100*j+k*50==x)
        count++;
      }
    }
  }
 printf("%d",count);
    return 0;
}

Score : 100 points

Problem Statement

You went shopping to buy cakes and donuts with X yen (the currency of Japan).

First, you bought one cake for A yen at a cake shop.
Then, you bought as many donuts as possible for B yen each, at a donut shop.

How much do you have left after shopping?

Constraints

1 \leq A, B \leq 1 000

A + B \leq X \leq 10 000

X, A and B are integers.

Input

Input is given from Standard Input in the following format:

X
A
B

Output

Print the amount you have left after shopping.

Sample Input 1

1234
150
100

Sample Output 1

84

You have 1234 - 150 = 1084 yen left after buying a cake.
With this amount, you can buy 10 donuts, after which you have 84 yen left.

Sample Input 2

1000
108
108

Sample Output 2

28

Sample Input 3

579
123
456

Sample Output 3

0

Sample Input 4

7477
549
593

Sample Output 4

405


/*
	submission # - User: herp_sy
	https://atcoder.jp/contests/
*/

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

#define _CRT_SECURE_NO_WARNINGS
#define TLong long long
#define TBMod 1000000007

int main(int argc, char const *argv[])
{
	int n;
	char a[7],b[7];

	scanf("%s %s",a,b);
	strcat(a,b);
	n = atoi(a);
	if((int)sqrt(n) * (int)sqrt(n) == n)	puts("Yes");
	else puts("No");
	return 0;
}

Score : 200 points

Problem Statement

AtCoDeer the deer has found two positive integers, a and b.
Determine whether the concatenation of a and b in this order is a square number.

Constraints

1 ≤ a,b ≤ 100

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

If the concatenation of a and b in this order is a square number, print Yes; otherwise, print No.

Sample Input 1

1 21

Sample Output 1

Yes

As 121 = 11 × 11, it is a square number.

Sample Input 2

100 100

Sample Output 2

No

100100 is not a square number.

Sample Input 3

12 10

Sample Output 3

No


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
int main(){
    double num;
    char a[201], b[101];
    scanf("%s %s", a, b);
    strcat(a, b);
    num = atoi(a);
    puts(num==pow((int)pow(num, 0.5), 2) ? "Yes" : "No");
    return 0;
}


#include<stdio.h>
int main(void){
    int a,b,c,i=1;
    scanf("%d %d",&a,&b);
    while(i<=100){
        if(b/i==0){
            c=a*i+b;
            break;
        }
        i*=10;
    }
    for(i=0;i<=100;i++){
        if(i*i==c){
            printf("Yes\n");
            return 0;
        }
    }
    printf("No\n");
	return 0;
}


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

int main(int argc, char **argv)
{
    int n;
    scanf("%d", &n);
    int pt = 0;
    int px = 0;
    int py = 0;
    for (int i = 0; i < n; i++) {
        int t, x, y;
        scanf("%d %d %d", &t, &x, &y);
        int dt = t - pt;
        int dx = abs(x - px);
        int dy = abs(y - py);
        int dd = dt - dx - dy;
        if (dd < 0 || dd % 2 != 0) {
            printf("No\n");
            return EXIT_SUCCESS;
        }
        pt = t;
        px = x;
        py = y;
    }
    printf("Yes\n");
    return EXIT_SUCCESS;
}

Score : 300 points

Problem Statement

AtCoDeer the deer is going on a trip in a two-dimensional plane.
In his plan, he will depart from point (0, 0) at time 0, then for each i between 1 and N (inclusive), he will visit point (x_i,y_i) at time t_i.

If AtCoDeer is at point (x, y) at time t, he can be at one of the following points at time t+1: (x+1,y), (x-1,y), (x,y+1) and (x,y-1).
Note that he cannot stay at his place.
Determine whether he can carry out his plan.

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

t_i < t_{i+1} (1 ≤ i ≤ N-1)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

If AtCoDeer can carry out his plan, print Yes; if he cannot, print No.

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

For example, he can travel as follows: (0,0), (0,1), (1,1), (1,2), (1,1), (1,0), then (1,1).

Sample Input 2

1
2 100 100

Sample Output 2

No

It is impossible to be at (100,100) two seconds after being at (0,0).

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


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

#define rep(n, i) for(int i = 0; i < n; i++)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))

void solve(void){
    int n, k;
    scanf("%d%d\n",&n, &k);
    int xs[n], ys[n];
    char cs[n];
    rep (n, i) scanf("%d %d %s\n", &xs[i], &ys[i], &cs[i]);

    int ans = 0;
    rep (k, x){
        if (ans == n) break;
        rep(k, y){
            int temp_ans = 0;
            rep(n, i){
                char temp_c;
                if (((xs[i] + x) / k + (ys[i] + y) / k) % 2 == 0) temp_c = 'B';
                else temp_c = 'W';
                if (temp_c == cs[i]) temp_ans++;
            }
            ans = MAX(ans, MAX(temp_ans, n-temp_ans));
            if (ans == n) break;
        }
    }
  printf("%d\n", ans);
}

int main(void){
  solve();
  return 0;
}

Score : 500 points

Problem Statement

AtCoDeer is thinking of painting an infinite two-dimensional grid in a checked pattern of side K.
Here, a checked pattern of side K is a pattern where each square is painted black or white so that each connected component of each color is a K × K square.
Below is an example of a checked pattern of side 3:

AtCoDeer has N desires.
The i-th desire is represented by x_i, y_i and c_i.
If c_i is B, it means that he wants to paint the square (x_i,y_i) black; if c_i is W, he wants to paint the square (x_i,y_i) white.
At most how many desires can he satisfy at the same time?

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 1000

0 ≤ x_i ≤ 10^9

0 ≤ y_i ≤ 10^9

If i ≠ j, then (x_i,y_i) ≠ (x_j,y_j).

c_i is B or W.

N, K, x_i and y_i are integers.

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1 c_1
x_2 y_2 c_2
:
x_N y_N c_N

Output

Print the maximum number of desires that can be satisfied at the same time.

Sample Input 1

4 3
0 1 W
1 2 W
5 3 B
5 4 B

Sample Output 1

4

He can satisfy all his desires by painting as shown in the example above.

Sample Input 2

2 1000
0 0 B
0 1 W

Sample Output 2

2

Sample Input 3

6 2
1 2 B
2 1 W
2 2 B
1 0 B
0 6 W
4 5 W

Sample Output 3

4


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

long main(){
	long long N;
	scanf("%lld",&N);

	long long A[N];

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

	bool dp[N+1][sum+1];

	memset(dp,0,sizeof(dp));

	dp[0][0]=1;

	for(long long i=0;i<N;i++){
		for(long long j=0;j<=sum;j++){
			dp[i+1][j] |= dp[i][j];
			if(j>=A[i])dp[i+1][j] |= dp[i][j-A[i]];
		}
	}

	for(long long i=(sum+1)/2;;i++){
		if(dp[N][i]==1){
			printf("%lld\n",i);
			break;
		}
	}

	return 0;
}

Score : 700 points

Problem Statement

You are given N integers A_1, A_2, ..., A_N.

Consider the sums of all non-empty subsequences of A. There are 2^N - 1 such sums, an odd number.

Let the list of these sums in non-decreasing order be S_1, S_2, ..., S_{2^N - 1}.

Find the median of this list, S_{2^{N-1}}.

Constraints

1 \leq N \leq 2000

1 \leq A_i \leq 2000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the median of the sorted list of the sums of all non-empty subsequences of A.

Sample Input 1

3
1 2 1

Sample Output 1

2

In this case, S = (1, 1, 2, 2, 3, 3, 4). Its median is S_4 = 2.

Sample Input 2

1
58

Sample Output 2

58

In this case, S = (58).


#include<stdio.h>
int main()
{
  char M[11];

  // 入力
  scanf("%s", &M);

  M[3] = '8';

  // 出力
  printf("%s\n", M);
  return 0;
}

Score : 100 points

Problem Statement

On some day in January 2018, Takaki is writing a document. The document has a column where the current date is written in yyyy/mm/dd format. For example, January 23, 2018 should be written as 2018/01/23.

After finishing the document, she noticed that she had mistakenly wrote 2017 at the beginning of the date column. Write a program that, when the string that Takaki wrote in the date column, S, is given as input, modifies the first four characters in S to 2018 and prints it.

Constraints

S is a string of length 10.

The first eight characters in S are 2017/01/.

The last two characters in S are digits and represent an integer between 1 and 31 (inclusive).

Input

Input is given from Standard Input in the following format:

S

Output

Replace the first four characters in S with 2018 and print it.

Sample Input 1

2017/01/07

Sample Output 1

2018/01/07

Sample Input 2

2017/01/31

Sample Output 2

2018/01/31


#include<stdio.h>
    int main(){
        int M;
            scanf("%d", &M);
                printf("%d", 48 - M);
    return 0;
    }

Score : 100 points

Problem Statement

How many hours do we have until New Year at M o'clock (24-hour notation) on 30th, December?

Constraints

1≤M≤23

M is an integer.

Input

Input is given from Standard Input in the following format:

M

Output

If we have x hours until New Year at M o'clock on 30th, December, print x.

Sample Input 1

21

Sample Output 1

27

We have 27 hours until New Year at 21 o'clock on 30th, December.

Sample Input 2

12

Sample Output 2

36


#include <stdio.h>
int main()
{
	int a,b,c,m;

	a=24;
	scanf("%d",&m);
	c=24-m;
	b=a+c;
	printf("%d",&b);

	return 0;
}


#include <stdio.h>
int main(){
  int a,b,c,d,e,f;
  char ch;
  c =scanf("%d%d",&a,&b);
  d = scanf("%d%c%d",&e,&ch,&f);
  if(c ==0||d ==0||d !=(a+b)) {
      printf("No");
    }
else{

  int count1,count2;
  while(e>0){
    e = e/10;
    count1 +=1;
  }
  while(f>0){
    f =f/10;
    count2 += 1;
  }

  if(a ==count1 &&b ==count2){
    printf("Yes");
  }else{
    printf("No");
  }
}
  return 0;
}

Score : 200 points

Problem Statement

The postal code in Atcoder Kingdom is A+B+1 characters long, its (A+1)-th character is a hyphen -, and the other characters are digits from 0 through 9.

You are given a string S. Determine whether it follows the postal code format in Atcoder Kingdom.

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

Print Yes if S follows the postal code format in AtCoder Kingdom; print No otherwise.

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

The (A+1)-th character of S is -, and the other characters are digits from 0 through 9, so it follows the format.

Sample Input 2

1 1
---

Sample Output 2

No

S contains unnecessary -s other than the (A+1)-th character, so it does not follow the format.

Sample Input 3

1 2
7444

Sample Output 3

No


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int acs(const void *a, const void *b) {
  return *(int*)a - *(int*)b;
} /* 1,2,3,4.. */
int des(const void *a, const void *b) {
  return *(int*)b - *(int*)a;
} /* 8,7,6,5.. */
int cmp_char(const void *a, const void *b) {
  return *(char*)a - *(char*)b;
} /* a,b,c,d.. */
int cmp_str(const void *a, const void *b) {
  return strcmp(*(const char **)a, *(const char **)b);
} /* aaa,aab.. */
#define min(a,b) (a < b ? a : b)
#define max(a,b) (a > b ? a : b)
#define rep(i, l, r) for (int i = l; i < r; i++)
#define MAX 100001
#define MOD 1000000007

typedef long long int lli;

int main(void) {
  int N;
  int C[505], S[505], F[505];
  scanf("%d", &N);
  rep(i, 0, N-1) scanf("%d %d %d", &C[i], &S[i], &F[i]);

  rep(x, 0, N) {
    int t = 0;
    rep(s, x, N-1) {
      if ( t < S[s] ) t = S[s];
      else t = ((t - 1) / F[s] + 1) * F[s];
      t += C[s];
    }
    printf("%d\n", t);
  }
  return 0;
}

Score : 300 points

Problem Statement

A railroad running from west to east in Atcoder Kingdom is now complete.

There are N stations on the railroad, numbered 1 through N from west to east.

Tomorrow, the opening ceremony of the railroad will take place.

On this railroad, for each integer i such that 1≤i≤N-1, there will be trains that run from Station i to Station i+1 in C_i seconds. No other trains will be operated.

The first train from Station i to Station i+1 will depart Station i S_i seconds after the ceremony begins. Thereafter, there will be a train that departs Station i every F_i seconds.

Here, it is guaranteed that F_i divides S_i.

That is, for each Time t satisfying S_i≤t and t％F_i=0, there will be a train that departs Station i t seconds after the ceremony begins and arrives at Station i+1 t+C_i seconds after the ceremony begins, where A％B denotes A modulo B, and there will be no other trains.

For each i, find the earliest possible time we can reach Station N if we are at Station i when the ceremony begins, ignoring the time needed to change trains.

Constraints

1≤N≤500

1≤C_i≤100

1≤S_i≤10^5

1≤F_i≤10

S_i％F_i=0

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
C_1 S_1 F_1
:
C_{N-1} S_{N-1} F_{N-1}

Output

Print N lines. Assuming that we are at Station i (1≤i≤N) when the ceremony begins, if the earliest possible time we can reach Station N is x seconds after the ceremony begins, the i-th line should contain x.

Sample Input 1

3
6 5 1
1 10 1

Sample Output 1

12
11
0

We will travel from Station 1 as follows:

5 seconds after the beginning: take the train to Station 2.

11 seconds: arrive at Station 2.

11 seconds: take the train to Station 3.

12 seconds: arrive at Station 3.

We will travel from Station 2 as follows:

10 seconds: take the train to Station 3.

11 seconds: arrive at Station 3.

Note that we should print 0 for Station 3.

Sample Input 2

4
12 24 6
52 16 4
99 2 2

Sample Output 2

187
167
101
0

Sample Input 3

4
12 13 1
44 17 17
66 4096 64

Sample Output 3

4162
4162
4162
0


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

#define rep(i, n) for(int i=0; i<(n); ++i)
#define max  200001

//qsort(str, n, sizeof(int), up_comp);
int up_comp(const void *a, const void *b){return *(int*)a - *(int*)b;}
int down_comp(const void *a, const void *b){return *(int*)b - *(int*)a;}
int int_sort( const void * a , const void * b ) {return strcmp(( char * )a , ( char * )b );}
int count_digit(int a){int i=0;while(a!=0){a=a/10;i++;}return i;}
int euclid( int a, int b ){int temp;if(a<b){temp=a;a=b;b=temp;}if(b<1)return -1;if(a%b==0) return b;return euclid(b,a%b);}

int main(void){

    int a, b, c, d;

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

    if (a+b<c+d) {
        printf("Right");
    } else if (a+b>c+d) {
        printf("Left");
    } else {
        printf("Balanced");
    }

    return 0;
}

Score : 100 points

Problem Statement

A balance scale tips to the left if L>R, where L is the total weight of the masses on the left pan and R is the total weight of the masses on the right pan. Similarly, it balances if L=R, and tips to the right if L<R.

Takahashi placed a mass of weight A and a mass of weight B on the left pan of a balance scale, and placed a mass of weight C and a mass of weight D on the right pan.

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Constraints

1\leq A,B,C,D \leq 10

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print Left if the balance scale tips to the left; print Balanced if it balances; print Right if it tips to the right.

Sample Input 1

3 8 7 1

Sample Output 1

Left

The total weight of the masses on the left pan is 11, and the total weight of the masses on the right pan is 8. Since 11>8, we should print Left.

Sample Input 2

3 4 5 2

Sample Output 2

Balanced

The total weight of the masses on the left pan is 7, and the total weight of the masses on the right pan is 7. Since 7=7, we should print Balanced.

Sample Input 3

1 7 6 4

Sample Output 3

Right

The total weight of the masses on the left pan is 8, and the total weight of the masses on the right pan is 10. Since 8<10, we should print Right.


#include <stdio.h>

int main(){
  int a[5];
  int sumA,sumB,i=0;
  while(i<4){
  	scanf("%d",&a[i]);
    i++;
  }
  sumA = a[0]+a[1];
  sumB = a[2]+a[3];

  if(sumA > sumB) printf("Left\n");
  else if(sumA < sumB) printf("Right\n");
  else printf("Balanced");
  return 0;
}


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


#include<stdio.h>
int main()
{
    int a,b,ave,acu_ave;
    scanf("%d %d",&a,&b);
    if((a+b)%2==0)
       {
           ave=(a+b)/2;
           printf("%d\n",ave);

    }
    else
    {
        ave=(a+b)/2;
        acu_ave=ave+1;
        printf("%d\n",acu_ave);
    }

    return 0;
}

Score : 100 points

Problem Statement

You are given two positive integers a and b.
Let x be the average of a and b.
Print x rounded up to the nearest integer.

Constraints

a and b are integers.

1 \leq a, b \leq 100

Input

Input is given from Standard Input in the following format:

a b

Output

Print x rounded up to the nearest integer.

Sample Input 1

1 3

Sample Output 1

2

The average of 1 and 3 is 2.0, and it will be rounded up to the nearest integer, 2.

Sample Input 2

7 4

Sample Output 2

6

The average of 7 and 4 is 5.5, and it will be rounded up to the nearest integer, 6.

Sample Input 3

5 5

Sample Output 3

5


#include<stdio.h>
int main()
{
	int a,b;
	double s;
	scanf("%d %d",&a,&b);
	s=(a+b)/2.0;
	printf("%.0lf\n",s);
	return 0;
}


#include<stdio.h>
#include<string.h>
void sort(char x[])
{
	int i,j,n;
	char t;

	n=strlen(x);
	for(i=0;i<n;i++)
		for(j=i+1;j<n;j++)
			if(x[i]<x[j]){
				t=x[i];
				x[i]=x[j];
				x[j]=t;
			}
}

void tros(char x[])
{
	int i,j,n;
	char t;

	n=strlen(x);
	for(i=0;i<n;i++)
		for(j=i+1;j<n;j++)
			if(x[i]>x[j]){
				t=x[i];
				x[i]=x[j];
				x[j]=t;
			}
}

int main()
{
	char s[111],t[111];

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

	sort(s);
	tros(t);
	printf("%s",(s<t)?"Yes":"No");
	return 0;
}

Score : 200 points

Problem Statement

You are given strings s and t, consisting of lowercase English letters.
You will create a string s' by freely rearranging the characters in s.
You will also create a string t' by freely rearranging the characters in t.
Determine whether it is possible to satisfy s' < t' for the lexicographic order.

Notes

For a string a = a_1 a_2 ... a_N of length N and a string b = b_1 b_2 ... b_M of length M, we say a < b for the lexicographic order if either one of the following two conditions holds true:

N < M and a_1 = b_1, a_2 = b_2, ..., a_N = b_N.

There exists i (1 \leq i \leq N, M) such that a_1 = b_1, a_2 = b_2, ..., a_{i - 1} = b_{i - 1} and a_i < b_i. Here, letters are compared using alphabetical order.

For example, xy < xya and atcoder < atlas.

Constraints

The lengths of s and t are between 1 and 100 (inclusive).

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

If it is possible to satisfy s' < t', print Yes; if it is not, print No.

Sample Input 1

yx
axy

Sample Output 1

Yes

We can, for example, rearrange yx into xy and axy into yxa. Then, xy < yxa.

Sample Input 2

ratcode
atlas

Sample Output 2

Yes

We can, for example, rearrange ratcode into acdeort and atlas into tslaa. Then, acdeort < tslaa.

Sample Input 3

cd
abc

Sample Output 3

No

No matter how we rearrange cd and abc, we cannot achieve our objective.

Sample Input 4

w
ww

Sample Output 4

Yes

Sample Input 5

zzz
zzz

Sample Output 5

No


#include<stdio.h>
int main()
{
	int i,j, jud; // jud =1 yes, =0 no
	char s[101], t[101], letter;
	jud = 0;
	// I scan and rearrange s:
	scanf("%s",s);
	for (j=0;j<=100;j++){
	for (i=0;s[i+1]!=0;i++){
		if (s[i]>s[i+1]){ letter=s[i]; s[i]=s[i+1]; s[i+1]=letter;}
		else ;
	} }
	// next, I do the same for t:
		scanf("%s",t);
	for (j=0;j<=100;j++){
	for (i=0;t[i+1]!=0;i++){
		if (t[i]<t[i+1]){ letter=t[i]; t[i]=t[i+1]; t[i+1]=letter;}
		else ;
	} }
	// printf("%s\n",s); printf("%s\n",t);
	// Lastly, I compare s and t:
	j=0;
	for (i=0;j==0&&s[i]!=0;i++){
		if (s[i]>t[i]) j=1;
			else{ 	if(s[i]<t[i]){ jud = 1; j=1;}
					else ; }
	}
	if (jud==0) printf("No"); else printf("Yes");
	return 0;
}


#include<stdio.h>

int main(void)
{
	char s[110] = {0};
	char t[110] = {0};
	int s_counter = 0;
	int t_counter = 0;
	int s_frag = 0;
	int t_frag = 0;

	int max_s = 0;
	int max_t = 0;
	fflush(stdout);
	scanf("%s", s);
	scanf("%s", t);

	for (int i = 0; i < 110; i++)
	{
		if (s[i] != 0)
		{
			s_counter++;
		}
		if (max_s < s[i])
		{
			max_s = s[i];
			s_frag++;
		}
	}
	for (int i = 0; i < 110; i++)
	{
		if (t[i] != 0)
		{
			t_counter++;
		}
		if (max_t < t[i])
		{
			max_t = t[i];
			t_frag++;
		}
	}

	while (1)
	{

		if ((s_counter == t_counter) && (s_frag == 1) && (t_frag == 1))
		{
			printf("No");
			break;
		}
			if (max_s <= max_t)
			{
				printf("Yes");
				break;
			}
			else
			{
				printf("No");
				break;
			}

	}
	return 0;
}


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

int main(void)
{
    int smin=123, smax=0, tmax=0, i, slen, tlen;
    char s[200], t[200];

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

    slen = strlen(s);
    tlen = strlen(t);

    for (i = 0; i < slen; i++){
        if(s[i] < smin){
            smin = s[i];
        }
        if(s[i] > smax){
            smax = s[i];
        }
    }
    for (i = 0; i < tlen; i++){
        if(t[i] > tmax){
            tmax = t[i];
        }
    }

    if(smin < tmax){
        printf("Yes\n");
    } else if(tmax < smin){
        printf("No\n");
    } else {
        if(smax > tmax){
            printf("No\n");
        } else if(smax < tmax){
            printf("Yes\n");
        } else {
            if(slen < tlen){
                printf("Yes\n");
            } else {
                printf("No\n");
            }
        }
    }

    return 0;
}


#include <stdio.h>

int main()
{
    int n;
    scanf("%d", &n);
    int temp;
    int count[100001];
    int i;
    int count_del = 0;
    for(i=0;i<=100000;i++) count[i] = 0;
    for(i=1;i<=n;i++){
        scanf("%d", &temp);
        if(temp<=100000) count[temp] += 1;
        else count_del += 1;
    }
    for(i=1;i<=n;i++) if(count[i]<i) count_del += count[i];
    printf("%d", count_del);

    return 0;
}

Score : 300 points

Problem Statement

You are given a sequence of positive integers of length N, a = (a_1, a_2, ..., a_N).
Your objective is to remove some of the elements in a so that a will be a good sequence.

Here, an sequence b is a good sequence when the following condition holds true:

For each element x in b, the value x occurs exactly x times in b.

For example, (3, 3, 3), (4, 2, 4, 1, 4, 2, 4) and () (an empty sequence) are good sequences, while (3, 3, 3, 3) and (2, 4, 1, 4, 2) are not.

Find the minimum number of elements that needs to be removed so that a will be a good sequence.

Constraints

1 \leq N \leq 10^5

a_i is an integer.

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the minimum number of elements that needs to be removed so that a will be a good sequence.

Sample Input 1

4
3 3 3 3

Sample Output 1

1

We can, for example, remove one occurrence of 3. Then, (3, 3, 3) is a good sequence.

Sample Input 2

5
2 4 1 4 2

Sample Output 2

2

We can, for example, remove two occurrences of 4. Then, (2, 1, 2) is a good sequence.

Sample Input 3

6
1 2 2 3 3 3

Sample Output 3

0

Sample Input 4

1
1000000000

Sample Output 4

1

Remove one occurrence of 10^9. Then, () is a good sequence.

Sample Input 5

8
2 7 1 8 2 8 1 8

Sample Output 5

5


#include <stdio.h>

void swap(long *a, long *b)
{
	long tmp = *a;
	*a = *b;
	*b = tmp;
}

void sort(long a[], long n)
{
	long i, j;
	for (i = 0; i<n; i++){
		for (j = n - 1; j >= 0; j--){
			if (a[j - 1]>a[j]) swap(&a[j - 1], &a[j]);
		}
	}
}

long search(long x, long B[], long n)
{
	//printf("search for %d from %d - %d \n",x, 1, n-1);
	long l, h, m;
	l = 1; h = n;
	while (l <= h)
	{
		m = (l + h) / 2;
		//printf("m=%d: ", m);
		if (x == B[m]){
			//printf("found>>%d\n", m);
			return m;
		}
		if (x > B[m]) l = m + 1;
		else h = m - 1;
	}
	//printf("Cannot Find\n");
	return -1;
}

long count(long A[], long B[], long C[], long n)
{
	sort(A, n);
	long i; long idx = 1; long id;
	for (i = 1; i <= n; i++) {
		if ((id = search(A[i], B, idx)) == -1) { B[idx++] = A[i]; C[idx - 1] += 1;// printf("C[%d]=%d\n", idx - 1, C[idx - 1]);
		}
		else { C[id] += 1;  //printf("C[%d]=%d\n", id, C[id]);
		}
	}
	//printf("Finished!\n");
	return idx;
}

/*void print(long a[], long n)
{
for (long i = 1; i <= n; i++){
printf("[%d] ", a[i]);
}
}*/
int main(void)
{
	long N, i, cnt;
	scanf("%ld", &N);
	long a[1001], b[1001] = { 0 }, c[1001] = { 0 };
	for (i = 1; i <= N; i++){ scanf("%ld", &a[i]); }

	//printf("Start Count\n");
	long idx = count(a, b, c, N);
	//printf("Counted\n");
	//print(a, N);
	//puts("");
	//print(b, idx);
	//puts("");
	//print(c, idx);
	cnt = 0; long x;
	for (i = 1; i <idx; i++){
		if (c[i] > 0){
			if (c[i] > b[i]) { x = c[i] - b[i]; }
			if (c[i] < b[i]) { x = c[i]; }
			if (c[i] == b[i]) { x = 0; }
			cnt += x;
		}
		else continue;
	}
	printf("%d\n", cnt);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) > (b) ? (b) : (a))
#define abs(x) ((x) > 0 ? (x) : -(x))
#define MOD (ll)1000000007 //10^9 + 7
#define endl printf("\n")
typedef long long ll;
#define MAX_N (1 << 17)

bool judge(int *a, int n, int x, bool dp[][8000]) {
  dp[0][0] = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= x; j++) {
      dp[i + 1][j] = dp[i][j];
      if (j - a[i] >= 0) dp[i + 1][j] |= dp[i][j - a[i]];
    }
  }
  return dp[n][x];
}

bool dp1[4010][8010], dp2[4010][8010];

int
main(int argc, char *argv[])
{
  int a[8010], x, y;
  char s[8010];
  scanf("%s", s);
  scanf("%d %d", &x, &y);

  int k = 0, l = 0;
  int n = strlen(s);
  while (k < n) {
    int j = k;
    while (s[j] == 'F') j++;
    a[l++] = j - k;
    k = j + 1;
  }

  int b[8000], c[8000], e = 0, o = 0;
  int sum1 = 0, sum2 = 0;
  for (int i = 0; i < l; i++) {
    if (i % 2 == 0) sum1 += (b[e++] = a[i]);
    else sum2 += (c[o++] = a[i]);
  }

  int p = sum1 - x, q = sum2 - y;
  if (p % 2 == 0 && q % 2 == 0 && p >= 0 && q >= 0) {
    if (judge(b, e, p / 2, dp1) && judge(c, o, q / 2, dp2)) {
      printf("Yes\n");
      return 0;
    }
  }

  printf("No\n");

  return 0;
}

Score : 500 points

Problem Statement

A robot is put at the origin in a two-dimensional plane.
Initially, the robot is facing in the positive x-axis direction.

This robot will be given an instruction sequence s.
s consists of the following two kinds of letters, and will be executed in order from front to back.

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

The objective of the robot is to be at coordinates (x, y) after all the instructions are executed.
Determine whether this objective is achievable.

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

|x|, |y| \leq |s|

Input

Input is given from Standard Input in the following format:

s
x y

Output

If the objective is achievable, print Yes; if it is not, print No.

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning clockwise in the second T.

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

The objective can be achieved by, for example, turning clockwise in the first T and turning clockwise in the second T.

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

The objective can be achieved by, for example, turning counterclockwise in the first T and turning counterclockwise in the second T.

Sample Input 6

TTTT
1 0

Sample Output 6

No


#include <stdio.h>
int main(void)
{
    int n;
    scanf("%d",&n);
    long int a[n];
    for(int p=0;p<n;p++)scanf("%ld",&a[p]);
    int co=0;
    while(a[co]<=0&&co<n)co++;
    if(co==n)
    {
        printf("%d\n",2*(n-1));
        for(int q=n-1;q>0;q--)
        {
            printf("%d %d\n",q,q-1);
            printf("%d %d\n",q,q-1);
        }
    }
    else
    {
        printf("%d\n",2*(n-2));
        for(int r=co-1;r>0;r--)
        {
            printf("%d%d\n",r,r-1);
            printf("%d%d\n",r,r-1);
        }
        for(int s=co;s<n-1;s++)
        {
            printf("%d %d\n",s,s+1);
            printf("%d %d\n",s,s+1);
        }
    }
}

Score : 600 points

Problem Statement

Snuke has an integer sequence, a, of length N. The i-th element of a (1-indexed) is a_{i}.

He can perform the following operation any number of times:

Operation: Choose integers x and y between 1 and N (inclusive), and add a_x to a_y.

He would like to perform this operation between 0 and 2N times (inclusive) so that a satisfies the condition below. Show one such sequence of operations.
It can be proved that such a sequence of operations always exists under the constraints in this problem.

Condition: a_1 \leq a_2 \leq ... \leq a_{N}

Constraints

2 \leq N \leq 50

-10^{6} \leq a_i \leq 10^{6}

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{N}

Output

Let m be the number of operations in your solution. In the first line, print m.
In the i-th of the subsequent m lines, print the numbers x and y chosen in the i-th operation, with a space in between.
The output will be considered correct if m is between 0 and 2N (inclusive) and a satisfies the condition after the m operations.

Sample Input 1

3
-2 5 -1

Sample Output 1

2
2 3
3 3

After the first operation, a = (-2,5,4).

After the second operation, a = (-2,5,8), and the condition is now satisfied.

Sample Input 2

2
-1 -3

Sample Output 2

1
2 1

After the first operation, a = (-4,-3) and the condition is now satisfied.

Sample Input 3

5
0 0 0 0 0

Sample Output 3

0

The condition is satisfied already in the beginning.


#include <stdio.h>

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

  if(s[0]==s[1] && s[1]==s[2] || s[1]==s[2] && s[2]==s[3])
    printf("Yes\n");
  else
    printf("No\n");

  return 0;
}

Score : 100 points

Problem Statement

We call a 4-digit integer with three or more consecutive same digits, such as 1118, good.

You are given a 4-digit integer N. Answer the question: Is N good?

Constraints

1000 ≤ N ≤ 9999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is good, print Yes; otherwise, print No.

Sample Input 1

1118

Sample Output 1

Yes

N is good, since it contains three consecutive 1.

Sample Input 2

7777

Sample Output 2

Yes

An integer is also good when all the digits are the same.

Sample Input 3

1234

Sample Output 3

No


#include<stdio.h>
#include<string.h>
int main(){
	int n;
	int a[4];
	scanf("%d",&n);
	a[0]=n/1000;
	a[1]=n/100%10;
	a[2]=n/10%10;
	a[3]=n%10;

	if(a[0]==a[1]&&a[0]==a[2]&&a[0]==a[3]){
			printf("Yes\nAn integer is also good when all the digits are the same.\n");
		return 0;
	}
	if((a[0]==a[1]&&a[0]==a[2])){
		printf("Yes\nN is good, since it contains three consecutive %d.\n",a[0]);
		return 0;
	}
	if((a[1]==a[2]&&a[1]==a[3])){
		printf("Yes\nN is good, since it contains three consecutive %d.\n",a[1]);
		return 0;
	}
	if((a[0]==a[3]&&a[0]==a[2])){
		printf("Yes\nN is good, since it contains three consecutive %d.\n",a[0]);
		return 0;
	}
	if((a[3]==a[1]&&a[0]==a[3])){
		printf("Yes\nN is good, since it contains three consecutive %d.\n",a[0]);
		return 0;
	}
	printf("No\n");
	return 0;
}


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

#define rep(i, n) for(int i=0; i<(n); ++i)
#define max  200001

//qsort(str, n, sizeof(int), up_comp);
int up_comp(const void *a, const void *b){return *(int*)a - *(int*)b;}
int down_comp(const void *a, const void *b){return *(int*)b - *(int*)a;}
int int_sort( const void * a , const void * b ) {return strcmp(( char * )a , ( char * )b );}
int count_digit(int a){int i=0;while(a!=0){a=a/10;i++;}return i;}
int euclid( int a, int b ){int temp;if(a<b){temp=a;a=b;b=temp;}if(b<1)return -1;if(a%b==0) return b;return euclid(b,a%b);}

int main(void){

    int x;
    long int ans, n=2, m=1;

    scanf("%d", &x);

    for (int i=1; i<x; i++) {
        ans=n+m;
        n=m;
        m=ans;
    }

    if (x==1) {
        ans=1;
    }

    printf("%ld", ans);

    return 0;
}

Score : 200 points

Problem Statement

It is November 18 now in Japan. By the way, 11 and 18 are adjacent Lucas numbers.

You are given an integer N. Find the N-th Lucas number.

Here, the i-th Lucas number L_i is defined as follows:

L_0=2

L_1=1

L_i=L_{i-1}+L_{i-2} (i≥2)

Constraints

1≤N≤86

It is guaranteed that the answer is less than 10^{18}.

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


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

int l (int x) {
    if (x == 0) return 2;
    else if (x == 1) return 1;
    else if (x >= 2) return l(x-2) + l(x-1);
    return 0;
}

int main()
{
    int n;
    scanf("%d", &n);
    long long int ans = l(n);
    printf("%lld\n", ans);
    return 0;
}


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

Score : 100 points

Problem Statement

In programming, hexadecimal notation is often used.

In hexadecimal notation, besides the ten digits 0, 1, ..., 9, the six letters A, B, C, D, E and F are used to represent the values 10, 11, 12, 13, 14 and 15, respectively.

In this problem, you are given two letters X and Y. Each X and Y is A, B, C, D, E or F.

When X and Y are seen as hexadecimal numbers, which is larger?

Constraints

Each X and Y is A, B, C, D, E or F.

Input

Input is given from Standard Input in the following format:

X Y

Output

If X is smaller, print <; if Y is smaller, print >; if they are equal, print =.

Sample Input 1

A B

Sample Output 1

<

10 < 11.

Sample Input 2

E C

Sample Output 2

>

14 > 12.

Sample Input 3

F F

Sample Output 3

=

15 = 15.


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

Score : 200 points

Problem Statement

We have a long seat of width X centimeters.
There are many people who wants to sit here. A person sitting on the seat will always occupy an interval of length Y centimeters.

We would like to seat as many people as possible, but they are all very shy, and there must be a gap of length at least Z centimeters between two people, and between the end of the seat and a person.

At most how many people can sit on the seat?

Constraints

All input values are integers.

1 \leq X, Y, Z \leq 10^5

Y+2Z \leq X

Input

Input is given from Standard Input in the following format:

X Y Z

Output

Print the answer.

Sample Input 1

13 3 1

Sample Output 1

3

There is just enough room for three, as shown below:

Figure

Sample Input 2

12 3 1

Sample Output 2

2

Sample Input 3

100000 1 1

Sample Output 3

49999

Sample Input 4

64146 123 456

Sample Output 4

110

Sample Input 5

64145 123 456

Sample Output 5

109


#include <stdio.h>

int main(void)
{
	int x,y,z;
	scanf("%d%d%d",&x,&y,&z);
	printf("%d\n",(x-z)/(y+z));
return 0;
}


#include<stdio.h>
int main()
{
	int x,y,z;
	scanf("%d",&x);
	scanf("%d",&y);
	scanf("%d",&z);
	fflush(stdout);
	int count=(x-z)/(y+z);
	printf("%d",count);
	return 0;
}


Score : 500 points

Problem Statement

We have a deck consisting of N cards. Each card has an integer written on it. The integer on the i-th card from the top is a_i.

Two people X and Y will play a game using this deck. Initially, X has a card with Z written on it in his hand, and Y has a card with W written on it in his hand. Then, starting from X, they will alternately perform the following action:

Draw some number of cards from the top of the deck. Then, discard the card in his hand and keep the last drawn card instead. Here, at least one card must be drawn.

The game ends when there is no more card in the deck. The score of the game is the absolute difference of the integers written on the cards in the two players' hand.

X will play the game so that the score will be maximized, and Y will play the game so that the score will be minimized. What will be the score of the game?

Constraints

All input values are integers.

1 \leq N \leq 2000

1 \leq Z, W, a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N Z W
a_1 a_2 ... a_N

Output

Print the score.

Sample Input 1

3 100 100
10 1000 100

Sample Output 1

900

If X draws two cards first, Y will draw the last card, and the score will be |1000 - 100| = 900.

Sample Input 2

3 100 1000
10 100 100

Sample Output 2

900

If X draws all the cards first, the score will be |1000 - 100| = 900.

Sample Input 3

5 1 1
1 1 1 1 1

Sample Output 3

0

Sample Input 4

1 1 1
1000000000

Sample Output 4

999999999


Score : 100 points

Problem Statement

You are given a grid with 2 rows and 3 columns of squares.
The color of the square at the i-th row and j-th column is represented by the character C_{ij}.

Write a program that prints YES if this grid remains the same when rotated 180 degrees, and prints NO otherwise.

Constraints

C_{i,j}(1 \leq i \leq 2, 1 \leq j \leq 3) is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

C_{11}C_{12}C_{13}
C_{21}C_{22}C_{23}

Output

Print YES if this grid remains the same when rotated 180 degrees; print NO otherwise.

Sample Input 1

pot
top

Sample Output 1

YES

This grid remains the same when rotated 180 degrees.

Sample Input 2

tab
bet

Sample Output 2

NO

This grid does not remain the same when rotated 180 degrees.

Sample Input 3

eye
eel

Sample Output 3

NO


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

typedef long long ll;

int max(int a,int b){
    if(a>b) return a;
    else return b;
}

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

/*qsort(array,num_of_index,sizeof(int),compare_int); */
 int compare_int(const void *a,const void *b){
     return *(int*) a - *(int *) b;
 }

int b_search(int* array,int left,int right,int key){
    int mid = left / 2 + right / 2;
     if( right - left == 1){
         if(array[mid]==key)
             return mid;
          else
               return -1;
     }
     if(array[mid]==key)
         return mid;
     else if(array[mid]>key)
         b_search(array,left,mid,key);
     else if(array[mid]<key)
         b_search(array,mid,right,key);
}

/*lower_bound(key以上のイテレータを返す)*/
 /*例えば,2,2,5,5,9でkey=2なら(2),2,5,5,9を返す*/
 int lower_bound(int  *array,int left,int right,int key){
     if(right-left<=1)
         return left;
     int mid = left + (right - left) / 2;
     if(array[mid]<key)
         return lower_bound(array,mid,right,key);
     else
         return lower_bound(array,left,mid,key);
 }
 int upper_bound(int  *array,int left,int right,int key){
     if(right-left<=1)
         return left;
     int mid = left + (right - left) / 2;
     if(array[mid]<=key)
         return upper_bound(array,mid,right,key);
     else
         return upper_bound(array,left,mid,key);
 }

int main(){
    int N;
    int A[MAX],B[MAX],C[MAX];
    scanf("%d",&N);
    for(int i=1;i<=N;i++)
        scanf("%d",A+i);
    for(int i=1;i<=N;i++)
        scanf("%d",B+i);
    for(int i=1;i<=N;i++)
        scanf("%d",C+i);
    A[0] = B[0] = C[0] = 0;
    qsort(A,N+1,sizeof(int),compare_int);
    qsort(B,N+1,sizeof(int),compare_int);
    qsort(C,N+1,sizeof(int),compare_int);
    int ans = 0;
    for(int i=1;i<=N;i++)
        ans += lower_bound(A,0,N+1,B[i]) * (N - upper_bound(C,0,N+1,B[i]));
    printf("%d",ans);
}

Score : 300 points

Problem Statement

The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.

He has N parts for each of the three categories. The size of the i-th upper part is A_i, the size of the i-th middle part is B_i, and the size of the i-th lower part is C_i.

To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.

How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9(1\leq i\leq N)

1 \leq B_i \leq 10^9(1\leq i\leq N)

1 \leq C_i \leq 10^9(1\leq i\leq N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N
B_1 ... B_N
C_1 ... C_N

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

Upper: 1-st part, Middle: 1-st part, Lower: 1-st part

Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part

Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

87


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include <time.h>
typedef long long ll;
typedef long double ld;
#define INF (1LL<<60)
/*swap 交換*/
void swap(ll *a, ll *b){ll c;c=*b;*b=*a;*a=c;}
/*2つのうち大きい数を返す*/
ll max2(ll a,ll b){return a>=b?a:b;}
/*2つのうち小さい数を返す*/
ll min2(ll a,ll b){return a>=b?b:a;}
/*絶対値*/
ll ABS(ll a){return a>=0?a:(-a);}
typedef struct{
    ll aa;
    ll bb;
}frequent;
// 小大
int compare(const void *a, const void *b){return *(ll *)a - *(ll *)b;}
ll bisearch(ll x, ll l, ll r, ll n, ll a[n]){
    // ll ans=0;
    if(a[r] <= x) return 0;
    if(a[l] > x){
        if(l==0) return n;
        if(a[l-1] <= x) return n-l;
        return bisearch(x,l,(l+r)/2,n,a);
    }else{
        if(a[l+1] > x) return n-l-1;
        return bisearch(x,(l+r)/2,r,n,a);
    }
}

int main(void){
    ll n,i,j,ans=0;
    scanf("%lld",&n);
    ll a[n],b[n],c[n],ab[n];
    for(i=0;i<n;i++){
        scanf("%lld",&a[i]);
    }
    for(i=0;i<n;i++){
        scanf("%lld",&b[i]);
    }
    for(i=0;i<n;i++){
        scanf("%lld",&c[i]);
    }
    qsort(a,n,sizeof(ll),compare);
    qsort(b,n,sizeof(ll),compare);
    qsort(c,n,sizeof(ll),compare);
    // ll marka=0,markb=0;
    for(i=0;i<n;i++){
        for(j=n;j>0;j--){
            if(a[j-1]<b[i]) break;
        }
        ab[i] = j;
        if(i!=0) ab[i]+=ab[i-1];
    }
    for(i=n;i>0;i--){
        for(j=n;j>0;j--){
            if(b[j-1]<c[i-1]){
                // printf("%lld ",j-1);
                ans+=ab[j-1];
                break;
            }
            // break;
        }
    }

    // for(i=0;i<n;i++){
    //     printf("%lld\n",ab[i]);
    // }

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