#include<stdio.h>

int main(){

	int number,i,min=9,sum=0,x=0,temp;

	for(i=0;i<5;i++){
		scanf("%d",&number);

		if(min>((number-1)%10)){

			min=number%10;
			temp=number;
			number=x;
			x=temp;

		}

		number=(number/10+(number%10!=0?1:0))*10;

		sum+=number;

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

	}

	sum+=x;

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

	return 0;
}

Score: 200 points

Problem Statement

The restaurant AtCoder serves the following five dishes:

ABC Don (rice bowl): takes A minutes to serve.

ARC Curry: takes B minutes to serve.

AGC Pasta: takes C minutes to serve.

APC Ramen: takes D minutes to serve.

ATC Hanbagu (hamburger patty): takes E minutes to serve.

Here, the time to serve a dish is the time between when an order is placed and when the dish is delivered.

This restaurant has the following rules on orders:

An order can only be placed at a time that is a multiple of 10 (time 0, 10, 20, ...).

Only one dish can be ordered at a time.

No new order can be placed when an order is already placed and the dish is still not delivered, but a new order can be placed at the exact time when the dish is delivered.

E869120 arrives at this restaurant at time 0. He will order all five dishes. Find the earliest possible time for the last dish to be delivered.

Here, he can order the dishes in any order he likes, and he can place an order already at time 0.

Constraints

A, B, C, D and E are integers between 1 and 123 (inclusive).

Input

Input is given from Standard Input in the following format:

A
B
C
D
E

Output

Print the earliest possible time for the last dish to be delivered, as an integer.

Sample Input 1

29
20
7
35
120

Sample Output 1

215

If we decide to order the dishes in the order ABC Don, ARC Curry, AGC Pasta, ATC Hanbagu, APC Ramen, the earliest possible time for each order is as follows:

Order ABC Don at time 0, which will be delivered at time 29.

Order ARC Curry at time 30, which will be delivered at time 50.

Order AGC Pasta at time 50, which will be delivered at time 57.

Order ATC Hanbagu at time 60, which will be delivered at time 180.

Order APC Ramen at time 180, which will be delivered at time 215.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 2

101
86
119
108
57

Sample Output 2

481

If we decide to order the dishes in the order AGC Pasta, ARC Curry, ATC Hanbagu, APC Ramen, ABC Don, the earliest possible time for each order is as follows:

Order AGC Pasta at time 0, which will be delivered at time 119.

Order ARC Curry at time 120, which will be delivered at time 206.

Order ATC Hanbagu at time 210, which will be delivered at time 267.

Order APC Ramen at time 270, which will be delivered at time 378.

Order ABC Don at time 380, which will be delivered at time 481.

There is no way to order the dishes in which the last dish will be delivered earlier than this.

Sample Input 3

123
123
123
123
123

Sample Output 3

643

This is the largest valid case.


#include<stdio.h>

int main(){
  int i,n,min;
  min=1000000000000000;
  int arroy[6];
  arroy[0]=0;
  scanf("%d",&n);
  for(int j=1;j<6;j++){
    scanf("%d",&arroy[j]);
    if(min>arroy[j]){
      min=arroy[j];
      arroy[0]=j;}
  }
  if(n<=min)
    printf("5\n");
  else if(n%min==0) {
    int time;
    time = 4+n/min;
    printf("%d\n",time);
  }
  else {
    int time2;
    time2=5+n/min;
    printf("%d\n",time2);
  }
  return 0;
}

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

Score: 300 points

Problem Statement

In 2028 and after a continuous growth, AtCoder Inc. finally built an empire with six cities (City 1, 2, 3, 4, 5, 6)!

There are five means of transport in this empire:

Train: travels from City 1 to 2 in one minute. A train can occupy at most A people.

Bus: travels from City 2 to 3 in one minute. A bus can occupy at most B people.

Taxi: travels from City 3 to 4 in one minute. A taxi can occupy at most C people.

Airplane: travels from City 4 to 5 in one minute. An airplane can occupy at most D people.

Ship: travels from City 5 to 6 in one minute. A ship can occupy at most E people.

For each of them, one vehicle leaves the city at each integer time (time 0, 1, 2, ...).

There is a group of N people at City 1, and they all want to go to City 6.

At least how long does it take for all of them to reach there?
You can ignore the time needed to transfer.

Constraints

1 \leq N, A, B, C, D, E \leq 10^{15}

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A
B
C
D
E

Output

Print the minimum time required for all of the people to reach City 6, in minutes.

Sample Input 1

5
3
2
4
3
5

Sample Output 1

7

One possible way to travel is as follows.
First, there are N = 5 people at City 1, as shown in the following image:

In the first minute, three people travels from City 1 to City 2 by train. Note that a train can only occupy at most three people.

In the second minute, the remaining two people travels from City 1 to City 2 by train, and two of the three people who were already at City 2 travels to City 3 by bus. Note that a bus can only occupy at most two people.

In the third minute, two people travels from City 2 to City 3 by train, and another two people travels from City 3 to City 4 by taxi.

From then on, if they continue traveling without stopping until they reach City 6, all of them can reach there in seven minutes.

There is no way for them to reach City 6 in 6 minutes or less.

Sample Input 2

10
123
123
123
123
123

Sample Output 2

5

All kinds of vehicles can occupy N = 10 people at a time.
Thus, if they continue traveling without stopping until they reach City 6, all of them can reach there in five minutes.

Sample Input 3

10000000007
2
3
5
7
11

Sample Output 3

5000000008

Note that the input or output may not fit into a 32-bit integer type.

Submitted source-code file size, in bytes.

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

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


#include<stdio.h>

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

Score : 100 points

Problem Statement

You are given three integers A, B and C.

Determine if there exists an equilateral triangle whose sides have lengths A, B and C.

Constraints

All values in input are integers.

1 \leq A,B,C \leq 100

Input

Input is given from Standard Input in the following format:

A B C

Output

If there exists an equilateral triangle whose sides have lengths A, B and C, print Yes; otherwise, print No.

Sample Input 1

2 2 2

Sample Output 1

Yes

There exists an equilateral triangle whose sides have lengths 2, 2 and 2.

Sample Input 2

3 4 5

Sample Output 2

No

There is no equilateral triangle whose sides have lengths 3, 4 and 5.


#include<stdio.h>

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

	if(a == b && b == c && c == a){
		printf("Yes\n");
	}

	else{
		printf("No\n");
	}

	return 0;
}


#include <stdio.h>

int GetGolemCount(int position, int turn, const int n, const char s[], const char t[], const char d[]);

int main()
{
	int i, j, n, q, cnt;

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

	char s[n], t[q], d[q];
	int g[n];

	for(i = 0; i < n; i++)
		g[i] = 1;

	scanf("%s", s);

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

	int tmp;
	for(i = 0; i < q; i++){
		if(d[i] == 'L' && t[i] == s[0]){
			tmp = GetGolemCount(0, i, n, s, t, d);
			cnt -= tmp;
		}
		if(d[i] == 'R' && t[i] == s[n - 1]){
			tmp = GetGolemCount(n - 1, i, n, s, t, d);
			cnt -= tmp;
		}
	}

	printf("%d", cnt);

	return 0;
}

int GetGolemCount(int position, int turn, const int n, const char s[], const char t[], const char d[]){

	if(turn <= 0){
		return 1;
	}else{
		// 1ターン前の自マス及び隣接マスのゴーレム数を参照して現在のマスのゴーレム数を求める

		int cnt = 0;

		// 自マス(移動なしの場合1ターン前のそのマスのゴーレム数、ありの場合0)
		if(s[position] != t[turn - 1])
			cnt += GetGolemCount(position, turn - 1, n, s, t, d);

		// 左マス(左端マス以外 : R移動の場合1ターン前のそのマスのゴーレム数、それ以外の場合0)
		if(position > 0){
			if(s[position - 1] == t[turn - 1] && d[turn - 1] == 'R')
				cnt += GetGolemCount(position - 1, turn - 1, n, s, t, d);
		}

		// 右マス(右端マス以外 : L移動の場合1ターン前のそのマスのゴーレム数、それ以外の場合0)
		if(position < n - 1){
			if(s[position + 1] == t[turn - 1] && d[turn - 1] == 'L')
				cnt += GetGolemCount(position + 1, turn - 1, n, s, t, d);
		}

		return cnt;
	}
}

Score : 500 points

Problem Statement

There are N squares numbered 1 to N from left to right.
Each square has a character written on it, and Square i has a letter s_i. Besides, there is initially one golem on each square.

Snuke cast Q spells to move the golems.

The i-th spell consisted of two characters t_i and d_i, where d_i is L or R.
When Snuke cast this spell, for each square with the character t_i, all golems on that square moved to the square adjacent to the left if d_i is L, and moved to the square adjacent to the right if d_i is R.

However, when a golem tried to move left from Square 1 or move right from Square N, it disappeared.

Find the number of golems remaining after Snuke cast the Q spells.

Constraints

1 \leq N,Q \leq 2 \times 10^{5}

|s| = N

s_i and t_i are uppercase English letters.

d_i is L or R.

Input

Input is given from Standard Input in the following format:

N Q
s
t_1 d_1
\vdots
t_{Q} d_Q

Output

Print the answer.

Sample Input 1

3 4
ABC
A L
B L
B R
A R

Sample Output 1

2

Initially, there is one golem on each square.

In the first spell, the golem on Square 1 tries to move left and disappears.

In the second spell, the golem on Square 2 moves left.

In the third spell, no golem moves.

In the fourth spell, the golem on Square 1 moves right.

After the four spells are cast, there is one golem on Square 2 and one golem on Square 3, for a total of two golems remaining.

Sample Input 2

8 3
AABCBDBA
A L
B R
A R

Sample Output 2

5

After the three spells are cast, there is one golem on Square 2, two golems on Square 4 and two golems on Square 6, for a total of five golems remaining.

Note that a single spell may move multiple golems.

Sample Input 3

10 15
SNCZWRCEWB
B R
R R
E R
W R
Z L
S R
Q L
W L
B R
C L
A L
N L
E R
Z L
S L

Sample Output 3

3


#include<stdio.h>

int main(void) {
	char b,s[4];
	scanf("%c", &b);
	s['A'] = 'T';
	s['T'] = 'A';
	s['C'] = 'G';
	s['G'] = 'C';
	printf("%c", s[b]);

	return 0;
}

Score : 100 points

Problem Statement

On the Planet AtCoder, there are four types of bases: A, C, G and T. A bonds with T, and C bonds with G.

You are given a letter b as input, which is A, C, G or T. Write a program that prints the letter representing the base that bonds with the base b.

Constraints

b is one of the letters A, C, G and T.

Input

Input is given from Standard Input in the following format:

b

Output

Print the letter representing the base that bonds with the base b.

Sample Input 1

A

Sample Output 1

T

Sample Input 2

G

Sample Output 2

C


#include <stdio.h>

void main()
{
  int in_ch;
  int out_ch;

  in_ch = getchar();

  switch( in_ch ){
    case 'A':
      out_ch = 'T';
      break;
    case 'T':
      out_ch = 'A';
      break;
    case 'C':
      out_ch = 'G';
      break;
    case 'G':
      out_ch = 'C';
      break;
  }
  printf("%c\n", out_ch );
  exit(0);
}


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

int main()
{
	char b;
	scanf("%c", &b);
	switch(b)
	{
	case 'A':printf("T\n");break;
	case 'T':printf("A\n");break;
	case 'C':printf("G\n");break;
	case'G':printf("C\n");break;
	}

	return 0;
}


/* ex 5_4
   potter0723sw */

#include <stdio.h>
#include <string.h>
#define SIZE 11

int main(void){
    int i,pre,counter = 0,ans = 0;
    char str1[SIZE],sub[2] = "A", *ret;
    scanf("%s", str1);
    while(counter != 4){//4回ループでそれぞれA,T,C,Gから始まる部分文字列に対して長さを調べる
        pre=0;
        while((ret = strstr(str1+pre,sub)) != NULL){
            i = 0;
            while(*(ret+i) == 'A' || *(ret+i) == 'T' || *(ret+i) == 'C' || *(ret+i) == 'G'){
                i++;//ATCGが続く度長さを追加
            }
            if(i + 1 > ans){//部分文字列が今までで最長なら更新
                ans = i + 1;
            }
            pre+=strlen(str1)-strlen(ret)+strlen(sub);
        }
        switch(counter){//次の頭文字に変換
            case 0:
                sub[0] = 'T';
                break;
            case 1:
                sub[0] = 'C';
                break;
            case 2:
                sub[0] = 'G';
                break;
            case 3:
                break;//4回終わったあとなので特にする必要なし
        }
        counter++;
    }
    printf("%d\n", ans);
    return 0;
}

Score : 200 points

Problem Statement

You are given a string S consisting of uppercase English letters. Find the length of the longest ACGT string that is a substring (see Notes) of S.

Here, a ACGT string is a string that contains no characters other than A, C, G and T.

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

S is a string of length between 1 and 10 (inclusive).

Each character in S is an uppercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

Print the length of the longest ACGT string that is a substring of S.

Sample Input 1

ATCODER

Sample Output 1

3

Among the ACGT strings that are substrings of ATCODER, the longest one is ATC.

Sample Input 2

HATAGAYA

Sample Output 2

5

Among the ACGT strings that are substrings of HATAGAYA, the longest one is ATAGA.

Sample Input 3

SHINJUKU

Sample Output 3

0

Among the ACGT strings that are substrings of SHINJUKU, the longest one is (the empty string).


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

int main(void){
	int i,j,k;
	char S[11];
	int len;

	scanf("%s",S);
	len=strlen(S);
	int ans=0;
	int flag=0;
	for(i=1;i<len;i++){//長さ
		for(j=0;j<=len-i;j++){//最初の位置
			flag=1;
			for(k=j;k<j+i;k++){//検索位置
				if(S[k]!='A' && S[k]!='C' && S[k]!='G' && S[k]!='T')flag=0;
			}
			if(flag==1)ans=i;
		}
	}
	printf("%d\n",ans);
	return 0;
}


#include<stdio.h>

int main()
{
   char s[11];
   scanf("%s",&s);

   int i,cnt=0,max=0;
   for(i=0;s[i];++i)
    {
        if(s[i]=='A' || s[i]=='C' || s[i]=='T' || s[i]=='G')
           cnt++;

        else
         {
             if(max<cnt)
                max=cnt;

            cnt=0;

         }

    }

    printf("%d",max);
}


#include<stdio.h>

int main(void)
{
  char S[11];//'\0'が入る
  scanf("%s",S );// Sが文字列の先頭のアドレスを渡す
  int i = 0;
  int count = 0;
  int max = 0;
  for(i = 0; S[i] != '\0'; i++)
  {
    if(S[i]=='A'||S[i]=='T'||S[i]=='G'||S[i]=='C')
    {
      count++;
      if(count > max) max = count;
    }
    else
    {
      count = 0;
    }

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


#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.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, q;
  scanf("%d %d", &n, &q);

  char s[MAX];
  scanf("%s", s);

  int dp[MAX]; //　dp[i]: 先頭からi文字目までのAC数
  dp[0] = 0;
  rep(i, 1, n) {
    if ( s[i - 1] == 'A' && s[i] == 'C' ) {
      dp[i] += dp[i - 1] + 1;
    } else {
      dp[i] = dp[i - 1];
    }
  }

  int l, r;
  rep(i, 0, q) {
    scanf("%d %d", &l, &r);
    int ans = dp[r - 1] - dp[l - 1];
    printf("%d\n", ans);
  }
  return 0;
}

Score : 300 points

Problem Statement

You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries:

Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring?

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and (the empty string), but not AC.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

Each character in S is A, C, G or T.

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.

Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.

Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.


#include <stdio.h>

int main(){
    int A,B,K ;
    int i,cnt,small;
    int a[101]={};
    int ans=0;
    cnt=0;

    scanf("%d %d %d",&A,&B,&K);
    if(A>B) small=B;
    else small=A;
    for(i=1;i<=small;i++){
        if(A%i==0 && B%i==0){
            cnt++;
            a[cnt]=i;
            // if(cnt==K) ans = i;
        }
    }
    //printf("%d\n",cnt);
    //for(i=0;i<=small;i++){
    //    printf("%d\n",a[i]);
    //}
    printf("%d\n",a[cnt-K+1]);

}

Score : 200 points

Problem Statement

You are given positive integers A and B.

Find the K-th largest positive integer that divides both A and B.

The input guarantees that there exists such a number.

Constraints

All values in input are integers.

1 \leq A, B \leq 100

The K-th largest positive integer that divides both A and B exists.

K \geq 1

Input

Input is given from Standard Input in the following format:

A B K

Output

Print the K-th largest positive integer that divides both A and B.

Sample Input 1

8 12 2

Sample Output 1

2

Three positive integers divides both 8 and 12: 1, 2 and 4.
Among them, the second largest is 2.

Sample Input 2

100 50 4

Sample Output 2

5

Sample Input 3

1 1 1

Sample Output 3

1


#include <stdio.h>

int main(void)
{
  int a,b,k,d;
  int c[100];

  c[0] = 1;

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

  int i;
  int count = 1;
  if(a >= b)
    d = a;
  else
    d = b;

  for(i = 2; i <= d; i++){
    if(a%i==0&&b%i==0){
      c[count] = i;
      count += 1;
    }
  }

  printf("%d",c[count - (k+1)]);

  return 0;

}


/*
 * ex 9-2
 * kamecha
 */
#include <stdio.h>
#include <stdlib.h>

#define SIZE (100000+10)

//スタックを単方向リストで実装
typedef struct node{
    char elem;
    struct node *next;
}node;

void push(node **first, char elem){
    node *new = calloc(1, sizeof(node));
    if(new == NULL) exit(1);
    new->elem = elem;
    new->next = *first;
    *first = new;
}

char pop(node **first){
    char elem;
    node *tmp = *first;
    if(tmp == NULL) exit(1);
    elem = tmp->elem;
    *first = tmp->next;
    free(tmp);
    return elem;
}

//stackの一番上の値を取得
char top(node **first){
    if(*first == NULL)   exit(1);
    return (*first)->elem;
}

//debug用print関数
void debug_print(node **first){
    node *ptr = *first;
    while(ptr != NULL){
        printf("|%c| => ", ptr->elem);
        ptr = ptr->next;
    }
    puts("NULL");
}

int main(void){
    node *first = NULL;
    char str[SIZE];
    int ans = 0;
    scanf("%s", str);
    //下から順に取り除いていく
    for(int i = 0; str[i] != '\0'; i++){
        if(first == NULL){
            push(&first, str[i]);
        }else if(top(&first) == str[i]){
            push(&first, str[i]);
        }else{
            pop(&first);
            ans += 2;
        }
    }
    printf("%d\n", ans);
    return 0;
}

Score : 300 points

Problem Statement

There are N cubes stacked vertically on a desk.

You are given a string S of length N. The color of the i-th cube from the bottom is red if the i-th character in S is 0, and blue if that character is 1.

You can perform the following operation any number of times: choose a red cube and a blue cube that are adjacent, and remove them. Here, the cubes that were stacked on the removed cubes will fall down onto the object below them.

At most how many cubes can be removed?

Constraints

1 \leq N \leq 10^5

|S| = N

Each character in S is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of cubes that can be removed.

Sample Input 1

0011

Sample Output 1

4

All four cubes can be removed, by performing the operation as follows:

Remove the second and third cubes from the bottom. Then, the fourth cube drops onto the first cube.

Remove the first and second cubes from the bottom.

Sample Input 2

11011010001011

Sample Output 2

12

Sample Input 3

0

Sample Output 3

0


#include <stdio.h>

int main()
{
 int N=0;
 char S[100005];
 int i;
 int zeros=0;
 int ones=0;
 int out;

 scanf("%s",&S);
 for(i=0;i<100005;i++)
 {
  if(S[i]=='0')
  {
   zeros++;
   N++;
  }else if(S[i]=='1')
  {
   ones++;
   N++;
  }else{
   break;
  }
 }
 out=ones;
 if(ones>zeros) out=zeros;
 out=out*2;
 printf("%d\n",out);
 return 0;
}


#include<stdio.h>
int main(void){
    char S[11];
    scanf("%s",S);
    if(S[6]=='4'){
        printf("Heisei\n");
    }
    else if(S[6]=='3'){
    printf("Heisei\n");
    }
    else if(S[6]=='2'){
        printf("Heisei\n");
    }
    else if(S[6]=='1'){
        printf("Heisei\n");
    }
    else{
        printf("TBD\n");
    }
    return 0;
}

Score : 100 points

Problem Statement

You are given a string S as input. This represents a valid date in the year 2019 in the yyyy/mm/dd format. (For example, April 30, 2019 is represented as 2019/04/30.)

Write a program that prints Heisei if the date represented by S is not later than April 30, 2019, and prints TBD otherwise.

Constraints

S is a string that represents a valid date in the year 2019 in the yyyy/mm/dd format.

Input

Input is given from Standard Input in the following format:

S

Output

Print Heisei if the date represented by S is not later than April 30, 2019, and print TBD otherwise.

Sample Input 1

2019/04/30

Sample Output 1

Heisei

Sample Input 2

2019/11/01

Sample Output 2

TBD


#include <stdio.h>

int main(void){

  char date[10];

  scanf("%s",date);

  if(date[5] == '1')
    printf("TBD");

  if(date[5] == '0'){
    if(date[6] == '1')
      printf("Heisei");
    else if(date[6] == '2')
      printf("Heisei");
    else if(date[6] == '3')
      printf("Heisei");
    else if(date[6] == '4')
      printf("Heisei");

    else
    printf("TBD");
  }

return 0;
}


#include<stdio.h>
#include<string.h>
int main(){
  int n;
  int i;
  char temp[4];

  double money = 0;
  double sum = 0;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%lf %s",&money,temp);
    if(strcmp(temp,"JPY") ==0){
      sum+=money;
    }else{
      sum+=money*380000;
    }
  }
  printf("%lf",sum);
}

Score : 200 points

Problem Statement

Takahashi received otoshidama (New Year's money gifts) from N of his relatives.

You are given N values x_1, x_2, ..., x_N and N strings u_1, u_2, ..., u_N as input. Each string u_i is either JPY or BTC, and x_i and u_i represent the content of the otoshidama from the i-th relative.

For example, if x_1 = 10000 and u_1 = JPY, the otoshidama from the first relative is 10000 Japanese yen; if x_2 = 0.10000000 and u_2 = BTC, the otoshidama from the second relative is 0.1 bitcoins.

If we convert the bitcoins into yen at the rate of 380000.0 JPY per 1.0 BTC, how much are the gifts worth in total?

Constraints

2 \leq N \leq 10

u_i = JPY or BTC.

If u_i = JPY, x_i is an integer such that 1 \leq x_i \leq 10^8.

If u_i = BTC, x_i is a decimal with 8 decimal digits, such that 0.00000001 \leq x_i \leq 100.00000000.

Input

Input is given from Standard Input in the following format:

N
x_1 u_1
x_2 u_2
:
x_N u_N

Output

If the gifts are worth Y yen in total, print the value Y (not necessarily an integer).

Output will be judged correct when the absolute or relative error from the judge's output is at most 10^{-5}.

Sample Input 1

2
10000 JPY
0.10000000 BTC

Sample Output 1

48000.0

The otoshidama from the first relative is 10000 yen. The otoshidama from the second relative is 0.1 bitcoins, which is worth 38000.0 yen if converted at the rate of 380000.0 JPY per 1.0 BTC. The sum of these is 48000.0 yen.

Outputs such as 48000 and 48000.1 will also be judged correct.

Sample Input 2

3
100000000 JPY
100.00000000 BTC
0.00000001 BTC

Sample Output 2

138000000.0038

In this case, outputs such as 138001000 and 1.38e8 will also be judged correct.


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

int main(void){

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

  double sum = 0;
  double x;
  char u[4];
  for(int i = 0 ; i < N ; i++){
    scanf("%lf %s",&x , u);

    if(strcmp(u , "JPY") == 0){
      sum += x;
    }else if(strcmp(u , "BTC") == 0){
      sum += x * 380000.0;
    }

  }

  printf("%lf", sum);

  return 0;
}


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

int main()
{
  int a,i;
  float sum=0;
    char s[15];
    float y;
    // 入力
    scanf("%d",&a);
    for(i=0;i<a;i++){
    scanf("%f %s",&y,s);
	if(strcmp(s,"JPY")==0){
    sum+=y;
    }else{
    sum+=y*380000;
    }
    }
    // 出力
    printf("%f",sum);

  return 0;
}


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

int main(void) {
  int i, N;
  double x, T=0;
  char s[10];
  scanf("%d", &N);
  for (i=0;i<N;i++){
    scanf("%lf %s", &x, s);
    if (strcmp(s, "BTC") == 0){
      T += 380000 * x;
    } else {
      T += x;
    }
  }
  printf("%lf\n", T);
  return 0;
}


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

int main(void) {
  const double RATE = 380000.0;
  const char *UNIT_JPY = "JPY";

  int N;
  double price, sum;
  char unit[4];
  scanf("%d", &N);
  for(int i=0;i<N;i++) {
    double x;
    scanf("%lf %s", &x, unit);
    if(strcmp(unit, UNIT_JPY) == 0) {
      sum += x;
    } else {
      sum += RATE * x;
    }
  }

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

  return 0;
}


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

Score : 100 points

Problem Statement

You are given positive integers A and B.

If A is a divisor of B, print A + B; otherwise, print B - A.

Constraints

All values in input are integers.

1 \leq A \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

If A is a divisor of B, print A + B; otherwise, print B - A.

Sample Input 1

4 12

Sample Output 1

16

As 4 is a divisor of 12, 4 + 12 = 16 should be printed.

Sample Input 2

8 20

Sample Output 2

12

Sample Input 3

1 1

Sample Output 3

2

1 is a divisor of 1.


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

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

    if(b%a==0)
        printf("%d",a+b);
    else
        printf("%d",b-a);
}


#include<stdio.h>
int main(void){
  int A=0,B=0;

 if(scanf("%d %d",&A,&B)==1);

  //if(A==0){return 0;}
  if(B%A==0){printf("%d\n",A+B);}
  else if(B%A!=0){printf("%d\n",A-B);}

  return 0;
}


#include<stdio.h>
int main(){
  int n, m;
  scanf("%d %d",&n,&m);

  int cnt[30];
  int k, a;
  for(int i = 0; i < n; i++){
    scanf("%d",&k);
    for(int j = 0; j < k; j++){
      scanf("%d",&a);
      cnt[a-1]++;
    }
  }

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

Score : 200 points

Problem Statement

Katsusando loves omelette rice.

Besides, he loves crème brûlée, tenderloin steak and so on, and believes that these foods are all loved by everyone.

To prove that hypothesis, he conducted a survey on M kinds of foods and asked N people whether they like these foods or not.

The i-th person answered that he/she only likes the A_{i1}-th, A_{i2}-th, ..., A_{iK_i}-th food.

Find the number of the foods liked by all the N people.

Constraints

All values in input are integers.

1 \leq N, M \leq 30

1 \leq K_i \leq M

1 \leq A_{ij} \leq M

For each i (1 \leq i \leq N), A_{i1}, A_{i2}, ..., A_{iK_i} are distinct.

Constraints

Input is given from Standard Input in the following format:

N M
K_1 A_{11} A_{12} ... A_{1K_1}
K_2 A_{21} A_{22} ... A_{2K_2}
:
K_N A_{N1} A_{N2} ... A_{NK_N}

Output

Print the number of the foods liked by all the N people.

Sample Input 1

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

Sample Output 1

1

As only the third food is liked by all the three people, 1 should be printed.

Sample Input 2

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

Sample Output 2

0

Katsusando's hypothesis turned out to be wrong.

Sample Input 3

1 30
3 5 10 30

Sample Output 3

3


#include <stdio.h>
int main(void){
  int n,i,j,g,tmp,r,b,c;
  int a[100000];
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&a[i]);
  }
  b=a[0];c=a[1];
  if(b<c){
    tmp=b;b=c;c=tmp;
  }
  r=b%c;
  while(r!=0){
    b=c;c=r;r=b%c;
  }
  g=c;
  for(i=2;i<n;i++){
    b=a[i];c=g;
    if(b<c){
      tmp=b;
      b=c;
      c=tmp;
    }
    r=b%c;
    while(r!=0){
      b=c;
      c=r;
      r=b%c;
    }
    g=c;
  }
  printf("%d",g);
  return 0;
}

Score : 300 points

Problem Statement

There are N monsters, numbered 1, 2, ..., N.

Initially, the health of Monster i is A_i.

Below, a monster with at least 1 health is called alive.

Until there is only one alive monster, the following is repeated:

A random alive monster attacks another random alive monster.

As a result, the health of the monster attacked is reduced by the amount equal to the current health of the monster attacking.

Find the minimum possible final health of the last monster alive.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

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 possible final health of the last monster alive.

Sample Input 1

4
2 10 8 40

Sample Output 1

2

When only the first monster keeps on attacking, the final health of the last monster will be 2, which is minimum.

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000


#include<stdio.h>
#include<stdlib.h>
int compar(const int *val1, const int *val2) {
    if ( *val1 < *val2 ) {
        return -1;
    } else if ( *val1 == * val2 ) {
        return 0;
    } else {
        return 1;
    }
}

int main(){
  int N;
  scanf("%d",&N);
  int i,j,k=0;
  int A[N];
  int flg=0;
  for(i=0;i<N;i++){
    scanf("%d ",&A[i]);
    /*for(j=0;j<i;j++){
      if(A[j] == A[i]){
        N--;
        i--;
        break;
      }
  }*/
  }
  int tmp,ans=0;

  while(1){
    ans=0;
    qsort(A, N, sizeof(int),
        (int (*)(const void *, const void *))compar);

    for(i=0;i<N;i++){
      if(A[i] == 1){
        puts("1");
        return 0;
      }
      if(A[i] > 0){
        flg=A[i];
        break;
      }
    }
    for(i=i+1;i<N;i++){
      A[i] -= flg;
    }

    for(i=0;i<N;i++){
      if(A[i] > 0)
        ans++;
    }

    if(ans == 1)
      break;

    k++;
  }
  for(i=0;i<N;i++){
    if(A[i] >0){
    printf("%d\n",A[i]);
  return 0;}
  }
}


#include <stdio.h>
int main(void)
{
  int a=0;int b=0;int c=0;int d=0;int e=0;int f=0;int g=0;
  scanf("%d,%d,%d,%d,%d,%d",&a,&b,&c,&d,&e,&f);
  g=a+b+c+d+e+f;
  if(g==15){
    printf("YES\n");
  }else{
    printf("NO\n");
  }
  return 0;
}

Score : 200 points

Problem Statement

There are four towns, numbered 1,2,3 and 4.
Also, there are three roads. The i-th road connects different towns a_i and b_i bidirectionally.
No two roads connect the same pair of towns. Other than these roads, there is no way to travel between these towns, but any town can be reached from any other town using these roads.

Determine if we can visit all the towns by traversing each of the roads exactly once.

Constraints

1 \leq a_i,b_i \leq 4(1\leq i\leq 3)

a_i and b_i are different. (1\leq i\leq 3)

No two roads connect the same pair of towns.

Any town can be reached from any other town using the roads.

Input

Input is given from Standard Input in the following format:

a_1 b_1
a_2 b_2
a_3 b_3

Output

If we can visit all the towns by traversing each of the roads exactly once, print YES; otherwise, print NO.

Sample Input 1

4 2
1 3
2 3

Sample Output 1

YES

We can visit all the towns in the order 1,3,2,4.

Sample Input 2

3 2
2 4
1 2

Sample Output 2

NO

Sample Input 3

2 1
3 2
4 3

Sample Output 3

YES


Score : 900 points

Problem Statement

There are N Snukes lining up in a row.
You are given a string S of length N. The i-th Snuke from the front has two red balls if the i-th character in S is 0; one red ball and one blue ball if the i-th character in S is 1; two blue balls if the i-th character in S is 2.

Takahashi has a sequence that is initially empty. Find the number of the possible sequences he may have after repeating the following procedure 2N times, modulo 998244353:

Each Snuke who has one or more balls simultaneously chooses one of his balls and hand it to the Snuke in front of him, or hand it to Takahashi if he is the first Snuke in the row.

Takahashi receives the ball and put it to the end of his sequence.

Constraints

1 \leq |S| \leq 2000

S consists of 0,1 and 2.

Note that the integer N is not directly given in input; it is given indirectly as the length of the string S.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of the possible sequences Takahashi may have after repeating the procedure 2N times, modulo 998244353.

Sample Input 1

02

Sample Output 1

3

There are three sequences that Takahashi may have: rrbb, rbrb and rbbr, where r and b stand for red and blue balls, respectively.

Sample Input 2

1210

Sample Output 2

55

Sample Input 3

12001021211100201020

Sample Output 3

543589959


#include<stdio.h>
#define MAX 100

int main(){
  int T,X;
  double sum;
scanf("%d%d",&T,&X);
sum = T/X;
printf("%ld",sum);

return 0;
}

Score : 100 points

Problem Statement

In order to pass the entrance examination tomorrow, Taro has to study for T more hours.

Fortunately, he can leap to World B where time passes X times as fast as it does in our world (World A).

While (X \times t) hours pass in World B, t hours pass in World A.

How many hours will pass in World A while Taro studies for T hours in World B?

Constraints

All values in input are integers.

1 \leq T \leq 100

1 \leq X \leq 100

Input

Input is given from Standard Input in the following format:

T X

Output

Print the number of hours that will pass in World A.

The output will be regarded as correct when its absolute or relative error from the judge's output is at most 10^{-3}.

Sample Input 1

8 3

Sample Output 1

2.6666666667

While Taro studies for eight hours in World B where time passes three times as fast, 2.6666... hours will pass in World A.

Note that an absolute or relative error of at most 10^{-3} is allowed.

Sample Input 2

99 1

Sample Output 2

99.0000000000

Sample Input 3

1 100

Sample Output 3

0.0100000000


#include<stdio.h>

int main()
{
    int R,i;
    scanf("%d",&R);
    int Q;
    int maxi;
    int total = 0;
    for(int i=0;i<R;i++)
    {
        scanf("%d",&Q);
        total+=R;
        if(Q>maxi)
        {
        maxi=Q;
        }
    }
    if(total-maxi>maxi)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}

Score : 200 points

Problem Statement

Determine if an N-sided polygon (not necessarily convex) with sides of length L_1, L_2, ..., L_N can be drawn in a two-dimensional plane.

You can use the following theorem:

Theorem: an N-sided polygon satisfying the condition can be drawn if and only if the longest side is strictly shorter than the sum of the lengths of the other N-1 sides.

Constraints

All values in input are integers.

3 \leq N \leq 10

1 \leq L_i \leq 100

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Output

If an N-sided polygon satisfying the condition can be drawn, print Yes; otherwise, print No.

Sample Input 1

4
3 8 5 1

Sample Output 1

Yes

Since 8 < 9 = 3 + 5 + 1, it follows from the theorem that such a polygon can be drawn on a plane.

Sample Input 2

4
3 8 4 1

Sample Output 2

No

Since 8 \geq 8 = 3 + 4 + 1, it follows from the theorem that such a polygon cannot be drawn on a plane.

Sample Input 3

10
1 8 10 5 8 12 34 100 11 3

Sample Output 3

No


#include<stdio.h>
int main(){
  int a,b=0,c=0;
  scanf("%d",&a);
  for(int i=0; i<a; i++){
    int d;
    scanf("%d",&d);
    if(b<d){
      c+=b;b=d;
    }
  }
  printf(c>b?"Yes":"No");
  return 0;}


#include<stdio.h>
int main(void){
  int N,i,j,tmp,sum=0;
  scanf("%d",&N);
  int L[N];
  for(i=0;i<N;i++){
    scanf("%d",&L[i]);
  }
  for(i=N-1;i>0;i--){
    for(j=0;j<i;j++){
      if(L[j]>L[j+1]){
      tmp=L[j+1];
      L[j+1]=L[j];
      L[j]=tmp;
      }
    }
  }
  for(i=0;i<N-1;i++){
    sum+=L[i];
  }
  if(sum>L[N-1]){
    printf("Yes");
  }
  else
    printf("No");
  return 0;
}


#include <stdlib.h>
#include<string.h>
#include <stdio.h>
int compare(const void *a,const void *b){
    if(*(int*)a>*(int *)b)return 1;
    return -1;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    int i,sum=0,max=0,x[m],gap[m-1];
    for(i=0;i<m;i++){
        scanf("%d",&x[i]);
    }
    if(n>=m){
    printf("0");
    return 0;
    }
    qsort(x,m,sizeof(int),compare);
    for(i=0;i<m;i++){
        if(i==0){
            continue;
        }
        gap[i-1]=x[i]-x[i-1];
    }
    qsort(gap,m-1,sizeof(int),compare);
    long long ans=0;
    for(i=0;i<m-n;i++){
        ans+=gap[i];
    }
    printf("%lld",ans);

    return 0;
}

Score : 300 points

Problem Statement

We will play a one-player game using a number line and N pieces.

First, we place each of these pieces at some integer coordinate.

Here, multiple pieces can be placed at the same coordinate.

Our objective is to visit all of the M coordinates X_1, X_2, ..., X_M with these pieces, by repeating the following move:

Move: Choose a piece and let x be its coordinate. Put that piece at coordinate x+1 or x-1.

Note that the coordinates where we initially place the pieces are already regarded as visited.

Find the minimum number of moves required to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

-10^5 \leq X_i \leq 10^5

X_1, X_2, ..., X_M are all different.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_M

Output

Find the minimum number of moves required to achieve the objective.

Sample Input 1

2 5
10 12 1 2 14

Sample Output 1

5

The objective can be achieved in five moves as follows, and this is the minimum number of moves required.

Initially, put the two pieces at coordinates 1 and 10.

Move the piece at coordinate 1 to 2.

Move the piece at coordinate 10 to 11.

Move the piece at coordinate 11 to 12.

Move the piece at coordinate 12 to 13.

Move the piece at coordinate 13 to 14.

Sample Input 2

3 7
-10 -3 0 9 -100 2 17

Sample Output 2

19

Sample Input 3

100 1
-100000

Sample Output 3

0


#include <stdio.h>
#include <stdlib.h>
#define int long long

typedef struct {
	int N;
	int *u;
	int *u_rank;
}union_find;

union_find *make_union_find(int N){
	int i;
	union_find *uf = (union_find *)malloc(sizeof(union_find));
	uf->N = N;
	uf->u = (int *)malloc(sizeof(int) * N);
	uf->u_rank = (int *)malloc(sizeof(int) * N);
	for(i = 0; i < N; i++){
		uf->u[i] = i;
		uf->u_rank[i] = 1;
	}
	return uf;
}

int root_uf(int x, union_find *uf){
	int *u = uf->u;
	if(u[x] == x){
		return x;
	}
	else{
		u[x] = root_uf(u[x], uf);
		return u[x];
	}
}

void combine_uf(int x, int y, union_find *uf){
	int x_root = root_uf(x, uf);
	int y_root = root_uf(y, uf);
	int *u = uf->u;
	int *u_rank = uf->u_rank;
	if(x_root == y_root){
		return;
	}
	else if(u_rank[x_root] < u_rank[y_root]){
		u[x_root] = y_root;
		u_rank[y_root] += u_rank[x_root];
		u_rank[x_root] = 0;
	}
	else{
		u[y_root] = x_root;
		u_rank[x_root] += u_rank[y_root];
		u_rank[y_root] = 0;
	}
}

//xとyが同じ集合に属していれば1を,そうでなければ0を返す
int is_same_union_uf(int x, int y, union_find *uf){
	if(root_uf(x, uf) == root_uf(y, uf)){
		return 1;
	}
	else{
		return 0;
	}
}

//xが属する集合の要素数を返す
int rank_uf(int x, union_find *uf){
	return uf->u_rank[root_uf(x, uf)];
}

typedef struct {
	int A;
	int B;
	int Y;
}tuple;

signed compare_tuple(const void *a, const void *b){
	int sub = ((tuple *)a)->Y - ((tuple *)b)->Y;
	if(sub < 0){
		return -1;
	}
	else if(sub == 0){
		return 0;
	}
	else{
		return 1;
	}
}

signed main(){
	int N, M, A, B, Y, i;
	scanf("%lld%lld", &N, &M);
	int *X = (int *)malloc(sizeof(int) * N);
	for(i = 0; i < N; i++){
		scanf("%lld", &X[i]);
	}
	tuple *tps = (tuple *)malloc(sizeof(tuple) * M);
	for(i = 0; i < M; i++){
		scanf("%lld%lld%lld", &tps[i].A, &tps[i].B, &tps[i].Y);
		tps[i].A--;
		tps[i].B--;
	}
	qsort(tps, M, sizeof(tuple), compare_tuple);
	union_find *uf = make_union_find(N);
	int *edge_num = (int *)malloc(sizeof(int) * N);
	int *good_edge_num = (int *)malloc(sizeof(int) * N);
	for(i = 0; i < N; i++){
		edge_num[i] = 0;
		good_edge_num[i] = 0;
	}
	int ans = 0;
	int r, rA, rB;
	for(i = 0; i < M; i++){
		A = tps[i].A;
		B = tps[i].B;
		Y = tps[i].Y;
//		printf("(A, B, Y)[%lld] = (%lld, %lld, %lld)\n", i, A, B, Y);
		if(is_same_union_uf(A, B, uf) == 1){
//			printf("test3\n");
			r = root_uf(A, uf);
			edge_num[r]++;
			if(X[r] >= Y){
//				printf("test4\n");
				good_edge_num[r]++;
				ans++;
			}
//			printf("(edge_num, good_edge_num)[%lld] = (%lld, %lld)\n", r, edge_num[r], good_edge_num[r]);
		}
		else{
//			printf("test5\n");
			rA = root_uf(A, uf);
			rB = root_uf(B, uf);
//			printf("(rA, rB) = (%lld, %lld)\n", rA, rB);
			combine_uf(A, B, uf);
			r = root_uf(A, uf);
			X[r] = X[rA] + X[rB];
//			printf("X[%lld] = %lld\n", r, X[r]);
			if(X[r] >= Y){
//				printf("test6\n");
				ans += (edge_num[rA] - good_edge_num[rA]) + (edge_num[rB] - good_edge_num[rB]) + 1;
				good_edge_num[r] = edge_num[rA] + edge_num[rB] + 1;
				edge_num[r] = edge_num[rA] + edge_num[rB] + 1;
			}
			else{
//				printf("test7\n");
				good_edge_num[r] = good_edge_num[rA] + good_edge_num[rB];
				edge_num[r] = edge_num[rA] + edge_num[rB] + 1;
			}
//			printf("(edge_num, good_edge_num)[%lld] = (%lld, %lld)\n", r, edge_num[r], good_edge_num[r]);
		}
//		printf("ans = %lld\n", ans);
	}
	printf("%lld\n", M - ans);
	return 0;
}

Score : 800 points

Problem Statement

There is a connected undirected graph with N vertices and M edges.
The vertices are numbered 1 to N, and the edges are numbered 1 to M.
Also, each of these vertices and edges has a specified weight.
Vertex i has a weight of X_i; Edge i has a weight of Y_i and connects Vertex A_i and B_i.

We would like to remove zero or more edges so that the following condition is satisfied:

For each edge that is not removed, the sum of the weights of the vertices in the connected component containing that edge, is greater than or equal to the weight of that edge.

Find the minimum number of edges that need to be removed.

Constraints

1 \leq N \leq 10^5

N-1 \leq M \leq 10^5

1 \leq X_i \leq 10^9

1 \leq A_i < B_i \leq N

1 \leq Y_i \leq 10^9

(A_i,B_i) \neq (A_j,B_j) (i \neq j)

The given graph is connected.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
X_1 X_2 ... X_N
A_1 B_1 Y_1
A_2 B_2 Y_2
:
A_M B_M Y_M

Output

Find the minimum number of edges that need to be removed.

Sample Input 1

4 4
2 3 5 7
1 2 7
1 3 9
2 3 12
3 4 18

Sample Output 1

2

Assume that we removed Edge 3 and 4.
In this case, the connected component containing Edge 1 contains Vertex 1, 2 and 3, and the sum of the weights of these vertices is 2+3+5=10.
The weight of Edge 1 is 7, so the condition is satisfied for Edge 1.
Similarly, it can be seen that the condition is also satisfied for Edge 2.
Thus, a graph satisfying the condition can be obtained by removing two edges.

The condition cannot be satisfied by removing one or less edges, so the answer is 2.

Sample Input 2

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

Sample Output 2

4

Sample Input 3

10 9
81 16 73 7 2 61 86 38 90 28
6 8 725
3 10 12
1 4 558
4 9 615
5 6 942
8 9 918
2 7 720
4 7 292
7 10 414

Sample Output 3

8


#include<stdio.h>

int main() {
  int ab, bc, ca;
  scanf("%d %d %d", &ab, &bc, &ca);
  int are = ab * bc / 2;
  printf("%d", are);
}

Score : 100 points

Problem Statement

There is a right triangle ABC with ∠ABC=90°.

Given the lengths of the three sides, |AB|,|BC| and |CA|, find the area of the right triangle ABC.

It is guaranteed that the area of the triangle ABC is an integer.

Constraints

1 \leq |AB|,|BC|,|CA| \leq 100

All values in input are integers.

The area of the triangle ABC is an integer.

Input

Input is given from Standard Input in the following format:

|AB| |BC| |CA|

Output

Print the area of the triangle ABC.

Sample Input 1

3 4 5

Sample Output 1

6

This triangle has an area of 6.

Sample Input 2

5 12 13

Sample Output 2

30

This triangle has an area of 30.

Sample Input 3

45 28 53

Sample Output 3

630

This triangle has an area of 630.


#include<stdio.h>

int main(void){

  int s,n;
  int list[10000000];
  scanf("%s",&s);

  int a = 1;
  list[a] += 1;
  for(n=1;n <= 100;n++){
    if(n % 2 == 0){
      a = a / 2;
    }else{
      a = 3*a + 1;
    }
    list[a] += 1;
    if(list[a] == 2){
      printf(n);
      break;
    }
  }
  return 0;
}

Score : 200 points

Problem Statement

A sequence a=\{a_1,a_2,a_3,......\} is determined as follows:

The first term s is given as input.

Let f(n) be the following function: f(n) = n/2 if n is even, and f(n) = 3n+1 if n is odd.

a_i = s when i = 1, and a_i = f(a_{i-1}) when i > 1.

Find the minimum integer m that satisfies the following condition:

There exists an integer n such that a_m = a_n (m > n).

Constraints

1 \leq s \leq 100

All values in input are integers.

It is guaranteed that all elements in a and the minimum m that satisfies the condition are at most 1000000.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum integer m that satisfies the condition.

Sample Input 1

8

Sample Output 1

5

a=\{8,4,2,1,4,2,1,4,2,1,......\}. As a_5=a_2, the answer is 5.

Sample Input 2

7

Sample Output 2

18

a=\{7,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1,4,2,1,......\}.

Sample Input 3

54

Sample Output 3

114


#include <stdio.h>

int main(void) {
  int count = 1;
  int n;
  scanf("%d",&n);
  if (n == 1 || n==2||n==4){
      count = 4;
      printf("%d\n",count);
    }
  else {
     while(n != 4) {
      if(n % 2 == 0) {
      n /= 2;
        }   else {
      n = (3 * n) + 1;
        }
    count++;
     }
     printf("%d\n",count+3);
   }

  return 0;
}


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

int main() {
    int s;
    scanf("%d", &s);
    int a[10000000];
    int pref = s, now;
    for (int i = 2; i <= 10000000; i++) {
        if ((pref % 2) == 0) {
            now = pref / 2;
        } else {
            now = pref * 3 + 1;
        }
        if (a[now] == 1) {
            printf("%d\n", i);
            break;
        } else {
            a[now] = 1;
//            printf("%d %d\n", pref, now);
            pref = now;
        }
    }
    return 0;
}


#include <stdio.h>

int main(){
	int n;
	scanf("%d",&n);
	int h[n];
	for (int i = 0; i < n; ++i)
	{
		scanf("%d",&h[i]);
	}
	int ans = 0;
	for (int i = 0; i < 100000; ++i)
	{
		int count = 0;
		for (int j = 0; j < n; ++j)
		{
			if(count>0&&h[j]==0){
				break;
			}
			if(h[j]>0){
				count++;
				h[j]=h[j]-1;
			}
		}
		if(count==0){
			break;
		}
		ans++;
	}
	printf("%d",ans);
	return 0;
}

Score : 300 points

Problem Statement

In a flower bed, there are N flowers, numbered 1,2,......,N. Initially, the heights of all flowers are 0.
You are given a sequence h=\{h_1,h_2,h_3,......\} as input. You would like to change the height of Flower k to h_k for all k (1 \leq k \leq N), by repeating the following "watering" operation:

Specify integers l and r. Increase the height of Flower x by 1 for all x such that l \leq x \leq r.

Find the minimum number of watering operations required to satisfy the condition.

Constraints

1 \leq N \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

Print the minimum number of watering operations required to satisfy the condition.

Sample Input 1

4
1 2 2 1

Sample Output 1

2

The minimum number of watering operations required is 2.
One way to achieve it is:

Perform the operation with (l,r)=(1,3).

Perform the operation with (l,r)=(2,4).

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221


#include<stdio.h>
int main(){
  int n,h[100],i,j,max=0,min,sum=0;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d",&h[i]);
  }
  for(i=1;i<n;i++){
    if(h[i]>=h[i-1]){
      max=i;
    }
    else {
      sum+=h[max];
      for(j=i;j<n;j++){
        if(h[j]<h[j-1]){
          min=j;
        }
        else {
          sum-=h[min];
          i=j;
          break;
        }
      }
    }
  }
  if(h[n-1]>h[n-2]){
    sum+=h[max];
  }
  printf("%d",sum);
}


#include <stdio.h>
#include <stdlib.h>
#define int long long
#define p (int)(1e9 + 7)

int MOD(int a){
	a %= p;
	return a >= 0 ? a : a + p;
}

signed main(){
	int H, W, K, i;
	scanf("%lld%lld%lld", &H, &W, &K);
	int *fact = (int *)malloc(sizeof(int) * (H + W + 1));
	fact[0] = 1;
	for(i = 1; i <= H + W; i++){
		fact[i] = MOD(fact[i - 1] * i);
	}
	int *inverse = (int *)malloc(sizeof(int) * (H + W + 1));
	inverse[0] = -1;
	inverse[1] = 1;
	for(i = 2; i <= H + W; i++){
		inverse[i] = MOD(-(p / i) * inverse[p % i]);
	}
	int *fact_inv = (int *)malloc(sizeof(int) * (H + W + 1));
	fact_inv[0] = 1;
	for(i = 1; i <= H + W; i++){
		fact_inv[i] = MOD(fact_inv[i - 1] * inverse[i]);
	}
	int ans = K, C = MOD(inverse[H + W] * MOD(inverse[H + W - 1] * MOD(H * W)));
	for(i = 1; i <= K; i++){
		ans = MOD(ans + 2 * MOD(C * MOD((i - 1) * (K - i + 1))) + (K - i + 1));
	}
	printf("%lld\n", MOD(ans * MOD(fact[H + W] * fact_inv[H + W - K])));
	return 0;
}

Score : 900 points

Problem Statement

There is a rectangular sheet of paper with height H+1 and width W+1. We introduce an xy-coordinate system so that the four corners of the sheet are (0, 0), (W + 1, 0), (0, H + 1) and (W + 1, H + 1).

This sheet can be cut along the lines x = 1,2,...,W and the lines y = 1,2,...,H. Consider a sequence of operations of length K where we choose K of these H + W lines and cut the sheet along those lines one by one in some order.

Let the score of a cut be the number of pieces of paper that exist just after the cut. The score of a sequence of operations is the sum of the scores of all of the K cuts.

Find the sum of the scores of all possible sequences of operations of length K. Since this value can be extremely large, print the number modulo 10^9 + 7.

Constraints

1 \leq H,W \leq 10^7

1 \leq K \leq H + W

H, W and K are integers.

Input

Input is given from Standard Input in the following format:

H W K

Output

Print the sum of the scores, modulo 10^9 + 7.

Sample Input 1

2 1 2

Sample Output 1

34

Let x_1, y_1 and y_2 denote the cuts along the lines x = 1, y = 1 and y = 2, respectively. The six possible sequences of operations and the score of each of them are as follows:

y_1, y_2: 2 + 3 = 5

y_2, y_1: 2 + 3 = 5

y_1, x_1: 2 + 4 = 6

y_2, x_1: 2 + 4 = 6

x_1, y_1: 2 + 4 = 6

x_1, y_2: 2 + 4 = 6

The sum of these is 34.

Sample Input 2

30 40 50

Sample Output 2

616365902

Be sure to print the sum modulo 10^9 + 7.


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

#define DEBUG 0

int iniPos(int n,unsigned long long a[],unsigned long long int x)
{
	int i;
	for (i=0;i<n;i++) {
		if (a[i] >= x) break;
	}
	return i-1;
}

char judgeContact(int rP,int tP)
{
	if (rP > tP) return 1;
	return 0;
}

char nearCard(unsigned long long int x[],int n,int i,int l,int r)
{
	unsigned long long int now,left,right;
	now = x[i];
	if (l < 0) left = -999999999;
	else left = x[l];
	if (r >= n) right = 1000000001;
	else right = x[r];

	if ((now - left) <= (right - now)) return 0;
	else return 1;
}

int main(void)
{
	int n, q, i, j, num;
	unsigned long long int *a;
	unsigned long long int *x;

	int aokiPos,leftPos,rightPos,takaPos;
	char des,turn;
	unsigned long long int *res;

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

	a = (unsigned long long int *)malloc(sizeof(unsigned long long int) * n);
	x = (unsigned long long int *)malloc(sizeof(unsigned long long int) * q);
	res = (unsigned long long int *)malloc(sizeof(unsigned long long int) * q);

	for (i=0;i<n;i++) {
		scanf("%lld",&a[i]);
		if (DEBUG) printf("a[%d] = %lld\n",i,a[i]);
	}
	for (i=0;i<q;i++) {
		scanf("%lld",&x[i]);
		if (DEBUG) printf("x[%d] = %lld\n",i,x[i]);
	}

	for (i=0;i<q;i++) {
		if (DEBUG) printf("i = %d\n",i);
		res[i] = 0;
		aokiPos = iniPos(n,a,x[i]);
		leftPos = aokiPos;
		rightPos = aokiPos+1;
		takaPos = n-1;
		num = n;

		// before contact
		while (1) {
			if (DEBUG) printf("=== %d %d %d %d\n",aokiPos,leftPos,rightPos,takaPos);

			if (judgeContact(rightPos,takaPos) == 1) {
				turn = 0;
				break;
			}
		// turn of Takahashi
			if (DEBUG) printf("res + %lld\n",a[takaPos]);
			res[i] += a[takaPos];
			takaPos -= 1;
			num -= 1;

			if (judgeContact(rightPos,takaPos) == 1) {
				turn = 1;
				break;
			}

			if (DEBUG) printf("=== %d %d %d %d\n",aokiPos,leftPos,rightPos,takaPos);
			// turn of Aoki
			des = nearCard(x,n,i,leftPos,rightPos);
			if (des == 0) {
				leftPos -= 1;
			} else {
				rightPos += 1;
			}
			num -= 1;
		}

		// after contact
		if (DEBUG) printf("after\n");
			// turn of Aoki
		if (turn == 1) {
			leftPos -= 1;
			num -= 1;
		}
		while (1) {
			if (num <= 0) break;
			if (DEBUG) printf("res + %lld\n",a[leftPos]);
			res[i] += a[leftPos];
			leftPos -= 2;
			num -= 2;
		}

		printf("%lld\n",res[i]);
	}

	return 0;
}

Score : 500 points

Problem Statement

There are N cards. The i-th card has an integer A_i written on it.
For any two cards, the integers on those cards are different.

Using these cards, Takahashi and Aoki will play the following game:

Aoki chooses an integer x.

Starting from Takahashi, the two players alternately take a card. The card should be chosen in the following manner:

Takahashi should take the card with the largest integer among the remaining card.

Aoki should take the card with the integer closest to x among the remaining card. If there are multiple such cards, he should take the card with the smallest integer among those cards.

The game ends when there is no card remaining.

You are given Q candidates for the value of x: X_1, X_2, ..., X_Q.
For each i (1 \leq i \leq Q), find the sum of the integers written on the cards that Takahashi will take if Aoki chooses x = X_i.

Constraints

2 \leq N \leq 100 000

1 \leq Q \leq 100 000

1 \leq A_1 < A_2 < ... < A_N \leq 10^9

1 \leq X_i \leq 10^9 (1 \leq i \leq Q)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 ... A_N
X_1
X_2
:
X_Q

Output

Print Q lines. The i-th line (1 \leq i \leq Q) should contain the answer for x = X_i.

Sample Input 1

5 5
3 5 7 11 13
1
4
9
10
13

Sample Output 1

31
31
27
23
23

For example, when x = X_3(= 9), the game proceeds as follows:

Takahashi takes the card with 13.

Aoki takes the card with 7.

Takahashi takes the card with 11.

Aoki takes the card with 5.

Takahashi takes the card with 3.

Thus, 13 + 11 + 3 = 27 should be printed on the third line.

Sample Input 2

4 3
10 20 30 40
2
34
34

Sample Output 2

70
60
60


//set many funcs template
//Ver.20180717
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
#include<time.h>
#define inf 1072114514
#define llinf 4154118101919364364
#define mod 1000000007
#define pi 3.1415926535897932384

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

void shuffledget(int x[],int n){
    srand(time(0));
    int i,b[524288],p,c;
    for(i=0;i<n;i++){
        b[i]=i;
    }
    for(i=n;i>=1;i--){
        p=rand()%i;
        c=b[i-1];b[i-1]=b[p];b[p]=c;
    }
    for(i=0;i<n;i++){
        scanf("%d",&x[b[i]]);
    }
}

int dx4[4]={1,-1,0,0};
int dy4[4]={0,0,1,-1};
int dx8[8]={-1,-1,-1,0,0,1,1,1};
int dy8[8]={-1,0,1,-1,1,-1,0,1};

int search(int x,int a[],int n){
    int st=0,fi=n-1,te;
    while(st<=fi){
        te=(st+fi)/2;
        if(a[te]<x){st=te+1;}else{fi=te-1;}
    }
    return st;
}

typedef struct{
int val;
int node;
}sd;

int sdsortfnc(const void *a,const void *b){
if(((sd*)a)->val < ((sd*)b)->val){return -1;}
if(((sd*)a)->val > ((sd*)b)->val){return 1;}
return 0;
}

int main(void){
    int i,j,n,m,k,a[524288],b,c,h,w,r=0,l,t;
    double d;
    char s[524288];
    scanf("%s",s);
    for(i=0;i<4;i++){if(s[i]=='2'){r++;}}
    printf("%d\n",r);
    return 0;
}

Score : 100 points

Problem Statement

You are given an integer N that has exactly four digits in base ten.
How many times does 2 occur in the base-ten representation of N?

Constraints

1000 \leq N \leq 9999

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

1222

Sample Output 1

3

2 occurs three times in 1222. By the way, this contest is held on December 22 (JST).

Sample Input 2

3456

Sample Output 2

0

Sample Input 3

9592

Sample Output 3

1


#include <stdio.h>
typedef unsigned int uint;

#pragma GCC optimize ("O3")
inline static uint gl(void){
    uint x = 0;
    for(;;){
        const uint c = getchar_unlocked() - 48;
        if(c < 10){
            x = x*10 + c;
        }else{
            break;
        }
    }
    return x;
}
#pragma GCC optimize ("Ofast")
#define H 200000
uint v[H] __attribute__((aligned(64)));
int main(void){
    const uint h = gl();
    gl();
    const uint n = gl();
    for(uint i=0; i<H; i++){ v[i] = 0xffffff; }
    for(uint i=0; i<n; i++){
        const uint x = gl()-1;
        const uint y = gl()-1;
        v[x] = y<v[x] ? y : v[x];
    }
    uint right = 0;
    for(uint x=1; x<h; x++){
        const auto b = v[x];
        const auto r = right+1;
        if(b<r){
            printf("%d\n", x);
            return 0;
        }else if(b>r){
            right=r;
        }
    }
    printf("%d\n", h);
    return 0;
}

Score : 800 points

Problem Statement

Takahashi and Aoki will play a game using a grid with H rows and W columns of square cells.
There are N obstacles on this grid; the i-th obstacle is at (X_i,Y_i).
Here, we represent the cell at the i-th row and j-th column (1 \leq i \leq H, 1 \leq j \leq W) by (i,j).
There is no obstacle at (1,1), and there is a piece placed there at (1,1).

Starting from Takahashi, he and Aoki alternately perform one of the following actions:

Move the piece to an adjacent cell.
Here, let the position of the piece be (x,y). Then Takahashi can only move the piece to (x+1,y), and Aoki can only move the piece to (x,y+1).
If the destination cell does not exist or it is occupied by an obstacle, this action cannot be taken.

Do not move the piece, and end his turn without affecting the grid.

The game ends when the piece does not move twice in a row.

Takahashi would like to perform as many actions (including not moving the piece) as possible before the game ends, while Aoki would like to perform as few actions as possible before the game ends.
How many actions will Takahashi end up performing?

Constraints

1 \leq H,W \leq 2\times 10^5

0 \leq N \leq 2\times 10^5

1 \leq X_i \leq H

1 \leq Y_i \leq W

If i \neq j, (X_i,Y_i) \neq (X_j,Y_j)

(X_i,Y_i) \neq (1,1)

X_i and Y_i are integers.

Input

Input is given from Standard Input in the following format:

H W N
X_1 Y_1
:
X_N Y_N

Output

Print the number of actions Takahashi will end up performing.

Sample Input 1

3 3 1
3 2

Sample Output 1

2

For example, the game proceeds as follows:

Takahashi moves the piece to (2,1).

Aoki does not move the piece.

Takahashi moves the piece to (3,1).

Aoki does not move the piece.

Takahashi does not move the piece.

Takahashi performs three actions in this case, but if both players play optimally, Takahashi will perform only two actions before the game ends.

Sample Input 2

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

Sample Output 2

6

Sample Input 3

100000 100000 0

Sample Output 3

100000


#include <stdio.h>
int main(void){
    int n,p,max=0,sum;
    int i;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d",&p);
        if(p>max){
            sum+=p/2;
            sum+=max/2;
            max=p;
        }else{
            sum+=p;
        }
    }
    printf("%d\n",sum);
    return 0;
}

Score : 200 points

Problem Statement

In some other world, today is the day before Christmas Eve.

Mr. Takaha is buying N items at a department store. The regular price of the i-th item (1 \leq i \leq N) is p_i yen (the currency of Japan).

He has a discount coupon, and can buy one item with the highest price for half the regular price. The remaining N-1 items cost their regular prices. What is the total amount he will pay?

Constraints

2 \leq N \leq 10

100 \leq p_i \leq 10000

p_i is an even number.

Input

Input is given from Standard Input in the following format:

N
p_1
p_2
:
p_N

Output

Print the total amount Mr. Takaha will pay.

Sample Input 1

3
4980
7980
6980

Sample Output 1

15950

The 7980-yen item gets the discount and the total is 4980 + 7980 / 2 + 6980 = 15950 yen.

Note that outputs such as 15950.0 will be judged as Wrong Answer.

Sample Input 2

4
4320
4320
4320
4320

Sample Output 2

15120

Only one of the four items gets the discount and the total is 4320 / 2 + 4320 + 4320 + 4320 = 15120 yen.


#include<stdio.h>

int main(void){
  int N,b=0,s=0,a[N];
  scanf("%d",&N);

  for (int i=0;i<N;i++){
    scanf("%d",&a[i]);
    if (a[i]>b){
      b=a[i];
    }
    s+=a[i];
  }
  s=s-b/2;
  printf("%d",s);
  return 0;
}


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

void quick_sort(int a[], int left, int right)
{
    int pl,pr,pivot;

    if(left>=right) return;
    pivot = a[(left + right)/2];
    pl=left;
    pr=right;
    while(pl<=pr){
        while(a[pl] < pivot) pl++;
        while(a[pr] > pivot) pr--;
        if(pl<=pr){
			int s=a[pl];
			a[pl]=a[pr];
			a[pr]=s;
            pl++;
            pr--;
        }
    }
    quick_sort(a,left,pr);
    quick_sort(a,pl,right);
}

int main(void)
{
	int n,k;
	scanf("%d%d",&n,&k);
	int *h;
	h=(int*)malloc(sizeof(int)*n);
	int i;
	for(i=0;i<n;i++) scanf("%d",&h[i]);
	int A=100000;
	quick_sort(h, 0, n);
	for(i=0;i<=n-k;i++){
		if(abs(h[i]-h[i+k-1])<=A) A=abs(h[i]-h[i+k-1]);
	}
	printf("%d\n",A);
	free(h);
	return 0;
}

Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

There are N trees planted in Mr. Takaha's garden. The height of the i-th tree (1 \leq i \leq N) is h_i meters.

He decides to choose K trees from these trees and decorate them with electric lights. To make the scenery more beautiful, the heights of the decorated trees should be as close to each other as possible.

More specifically, let the height of the tallest decorated tree be h_{max} meters, and the height of the shortest decorated tree be h_{min} meters. The smaller the value h_{max} - h_{min} is, the better. What is the minimum possible value of h_{max} - h_{min}?

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

Print the minimum possible value of h_{max} - h_{min}.

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

If we decorate the first, third and fifth trees, h_{max} = 12, h_{min} = 10 so h_{max} - h_{min} = 2. This is optimal.

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

If we decorate the second, fourth and fifth trees, h_{max} = 7, h_{min} = 7 so h_{max} - h_{min} = 0. This is optimal.

There are not too many trees in these sample inputs, but note that there can be at most one hundred thousand trees (we just can't put a sample with a hundred thousand lines here).


#include <stdio.h>
int main(){
  int x;
  scanf("%d",&x);
  puts(x==7||x==5||x==3?"YES":"NO");
  return 0;
}

Score : 100 points

Problem Statement

Shichi-Go-San (literally "Seven-Five-Three") is a traditional event in a certain country to celebrate the growth of seven-, five- and three-year-old children.

Takahashi is now X years old. Will his growth be celebrated in Shichi-Go-San this time?

Constraints

1 ≤ X ≤ 9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

If Takahashi's growth will be celebrated, print YES; if it will not, print NO.

Sample Input 1

5

Sample Output 1

YES

The growth of a five-year-old child will be celebrated.

Sample Input 2

6

Sample Output 2

NO

See you next year.


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

int main() {

    int n;
    scanf("%d", &n);
    if (n == 7 || n == 5 || n == 3) {
        puts("YES");
    } else {
        puts("NO");
    }

    return 0;
}


#include <stdio.h>

int main(void){
  int X;
  scanf("%d",&X);
  if (X = 3){
    printf("YES");
  }else if(X = 5){
    printf("YES");
  }else if(X = 7){
    printf("YES");
  }else{
    printf("NO");
  }
  return 0;
}


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

int main()
{
    int n;
    scanf("%d",&n);
    if(n==3||n==5||n==7) printf("YES\n");
    else printf("NO\n");
    return 0;
}


#include <stdio.h>
int main (void){
  int x;
scanf("%d",&x);
switch(x){
     case 3:
     case 5:
     case 7:
     printf("YES");
break;
     default:
    printf("NO");
break;}
return 0;}


#include<stdio.h>
#include<string.h>
int main()
{
    char str[10];
    int i,k,temp,num[10],dif[10];
    scanf("%s",&str);
    for(i=0;i<strlen(str)-2;i++)
    {
        num[i]=(str[i]-48)*100+(str[i+1]-48)*10+(str[i+2]-48);
        if(num[i]>753) dif[i]=num[i]-753;
        else if(num[i]<753) dif[i]=753-num[i];
        else{
            printf("0");
            return 0;
            }
    }
    for(i=0; i<strlen(str)-2; i++)
    {
        for(k=i+1; k<strlen(str)-2; k++)
        {
            if(dif[i] > dif[k])
            {
                temp = dif[i];
                dif[i] = dif[k];
                dif[k] = temp;
            }
        }
    }
printf("%d",dif[0]);
}

Score : 200 points

Problem Statement

There is a string S consisting of digits 1, 2, ..., 9.
Lunlun, the Dachshund, will take out three consecutive digits from S, treat them as a single integer X and bring it to her master. (She cannot rearrange the digits.)

The master's favorite number is 753. The closer to this number, the better.
What is the minimum possible (absolute) difference between X and 753?

Constraints

S is a string of length between 4 and 10 (inclusive).

Each character in S is 1, 2, ..., or 9.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum possible difference between X and 753.

Sample Input 1

1234567876

Sample Output 1

34

Taking out the seventh to ninth characters results in X = 787, and the difference between this and 753 is 787 - 753 = 34. The difference cannot be made smaller, no matter where X is taken from.

Note that the digits cannot be rearranged. For example, taking out 567 and rearranging it to 765 is not allowed.

We cannot take out three digits that are not consecutive from S, either. For example, taking out the seventh digit 7, the ninth digit 7 and the tenth digit 6 to obtain 776 is not allowed.

Sample Input 2

35753

Sample Output 2

0

If 753 itself can be taken out, the answer is 0.

Sample Input 3

1111111111

Sample Output 3

642

No matter where X is taken from, X = 111, with the difference 753 - 111 = 642.


#include <stdio.h>

char s[100];
int main(void){
	scanf("%s", s);
	int result = 1000;
	for (int i = 0; s[i + 2]; i++) {
		int item = ((s[i] - '0') * 10 + s[i + 1] - '0') * 10 + s[i + 2] - '0';
		int maybe = (item >= 753 ? item - 753 : 753 - item);
		if (maybe < result) result = maybe;
	}
	printf("%d\n", result);
}


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

//S[0]から始まる文字列の長さを求める関数
int string_len(char* str){
    int len=0;
    while(str[len]!=0){
        len++;
    }
    return len;
}

//753数なら1,そうでないなら0
int judge_753(int N){
    //int型Nを、文字列char配列に変換
    char s[10]={'\0'};
    snprintf(s,10,"%d",N);

    //Nの桁数
    int digit=string_len(s);

    int count=0;
    for(int i=0;i<digit;i++){
        if(s[i]=='3'){
            count++;
            break;
        }
    }
    for(int i=0;i<digit;i++){
        if(s[i]=='5'){
            count++;
            break;
        }
    }
    for(int i=0;i<digit;i++){
        if(s[i]=='7'){
            count++;
            break;
        }
    }

    if(count==3) return 1;
    else return 0;
}

void count_753(int s,int N, int ans,int a, int b, int c){
    if(s>N) return;

    if(a+b+c==3) ans++;

    count_753((s*10+3),N,ans,1,b,c);
    count_753((s*10+5),N,ans,a,1,c);
    count_753((s*10+7),N,ans,a,b,1);
}

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

    int ans=0;
    count_753(0,N,ans,0,0,0);

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

    return 0;
}

Score : 300 points

Problem Statement

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there?

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.

Constraints

1 \leq N < 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

Sample Input 1

575

Sample Output 1

4

There are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573.

Sample Input 2

3600

Sample Output 2

13

There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735, 753, 3357, 3375, 3537, 3557, 3573, 3575 and 3577.

Sample Input 3

999999999

Sample Output 3

26484


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

int is753(int a) {
	int k, flg[4] = { 0, 0, 0, 1};
	while (a > 0) {
		k = a % 10;
		a /= 10;
		if (k == 3) flg[0] = 1;
		else if (k == 5) flg[1] = 1;
		else if (k == 7) flg[2] = 1;
		else {
			flg[3] = 0;
			break;
		}
	}
	return flg[0] && flg[1] && flg[2] && flg[3];
}

int log_10(int a) {
	int cnt = 0;
	while (a > 0) {
		a /= 10;
		cnt++;
	}
	return cnt;
}

int main(void) {
	int n, log_n;
	scanf("%d", &n);
	log_n = log_10(n);

	int dfs(int cur, int a) {
		int ret0, ret3, ret5, ret7;
		ret0 = (is753(a) && a <= n) ? 1 : 0;
		if (cur == log_n) {
			return ret0;
		}
		ret3 = dfs(cur + 1, 10 * a + 3);
		ret5 = dfs(cur + 1, 10 * a + 5);
		ret7 = dfs(cur + 1, 10 * a + 7);
		return ret0 + ret3 + ret5 + ret7;
	}

	printf("%d", dfs(0, 0));
	return 0;
}


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

short p[100];
short pNum;

void makePrimeList(short n);

int main(void)
{
	char n,i,j,k,tmp;
	short b[100]={};
	int ans=0;
	scanf("%hhd",&n);

	makePrimeList(n);

	for (i=2;i<=n;i++) {
		for (j=0;j<pNum;j++) {
			if (i < p[j]) break;

			tmp = i;
			while (tmp > 1) {
				if (tmp % p[j] != 0) break;
				tmp = tmp / p[j];
				b[j]++;
			}
		}
	}

	// a^2 b^4 c^4
	for (i=0;i<pNum;i++) {
		if (b[i]>=4) {
			for (j=i+1;j<pNum;j++) {
				if (b[j]>=4) {
					for (k=0;k<pNum;k++) {
						if (b[k]>=2 && i!=k && j!=k) ans += 1;
					}
				}
			}
		}
	}

	// a^2 b^24
	for (i=0;i<pNum;i++) {
		if (b[i]>=24) {
			for (j=0;j<pNum;j++) {
				if (b[j]>=2 && i!=j) ans += 1;
			}
		}
	}

	// a^4 b^14
	for (i=0;i<pNum;i++) {
		if (b[i]>=14) {
			for (j=0;j<pNum;j++) {
				if (b[j]>=4 && i!=j) ans += 1;
			}
		}
	}

	// a^74
	for (i=0;i<pNum;i++) {
		if (b[i]>=74) ans += 1;
	}

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

void makePrimeList(short n)
{
	short i,j,*tmpP;
	tmpP = (short *)malloc(sizeof(short) * n);
	tmpP[0] = 1;
	for (i=1;i<n;i++) tmpP[i] = 0;

	pNum = 0;
	for (i=2;i<=sqrt(n);i++) {
		if (tmpP[i-1]==1) continue;

		p[pNum] = i;
		pNum++;
		for (j=i;j<=n;j+=i) {
			tmpP[j-1] = 1;
		}
	}

	for (i=(int)sqrt(n);i<=n;i++) {
		if (tmpP[i-1]==1) continue;

		p[pNum] = i;
		pNum++;
	}

	return;
}

Score : 400 points

Problem Statement

You are given an integer N. Among the divisors of N! (= 1 \times 2 \times ... \times N), how many Shichi-Go numbers (literally "Seven-Five numbers") are there?

Here, a Shichi-Go number is a positive integer that has exactly 75 divisors.

Note

When a positive integer A divides a positive integer B, A is said to a divisor of B.
For example, 6 has four divisors: 1, 2, 3 and 6.

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go numbers that are divisors of N!.

Sample Input 1

9

Sample Output 1

0

There are no Shichi-Go numbers among the divisors of 9! = 1 \times 2 \times ... \times 9 = 362880.

Sample Input 2

10

Sample Output 2

1

There is one Shichi-Go number among the divisors of 10! = 3628800: 32400.

Sample Input 3

100

Sample Output 3

543


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

Score: 100 points

Problem Statement

There is a train going from Station A to Station B that costs X yen (the currency of Japan).

Also, there is a bus going from Station B to Station C that costs Y yen.

Joisino got a special ticket. With this ticket, she can take the bus for half the fare if she travels from Station A to Station B by train and then travels from Station B to Station C by bus.

How much does it cost to travel from Station A to Station C if she uses this ticket?

Constraints

1 \leq X,Y \leq 100

Y is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

If it costs x yen to travel from Station A to Station C, print x.

Sample Input 1

81 58

Sample Output 1

110

The train fare is 81 yen.

The train fare is 58 ⁄ 2=29 yen with the 50% discount.

Thus, it costs 110 yen to travel from Station A to Station C.

Sample Input 2

4 54

Sample Output 2

31


#include<stdio.h>
int main()
{
    int t,a;
    scanf("%d %d",&t,&a);
    printf("%d\n",t+a/2);
}


#include<stdio.h>
int main()
{
    int X, Y, a;
    scanf("%d %d", &X, &Y);

    if(Y/2){
    a=X+(Y/2);
    printf("Total fare is %d", a);
    }

    else{printf("No Result");}
    return 0;
}


#include<stdio.h>
int main()
{
    int X,Y,c,cost;
    scanf("%d%d",&X,&Y);
    c=Y/2;
    cost=X+c;
    printf("%d\n",cost);

}


Score: 200 points

Problem Statement

A country decides to build a palace.

In this country, the average temperature of a point at an elevation of x meters is T-x \times 0.006 degrees Celsius.

There are N places proposed for the place. The elevation of Place i is H_i meters.

Among them, Princess Joisino orders you to select the place whose average temperature is the closest to A degrees Celsius, and build the palace there.

Print the index of the place where the palace should be built.

It is guaranteed that the solution is unique.

Constraints

1 \leq N \leq 1000

0 \leq T \leq 50

-60 \leq A \leq T

0 \leq H_i \leq 10^5

All values in input are integers.

The solution is unique.

Input

Input is given from Standard Input in the following format:

N
T A
H_1 H_2 ... H_N

Output

Print the index of the place where the palace should be built.

Sample Input 1

2
12 5
1000 2000

Sample Output 1

1

The average temperature of Place 1 is 12-1000 \times 0.006=6 degrees Celsius.

The average temperature of Place 2 is 12-2000 \times 0.006=0 degrees Celsius.

Thus, the palace should be built at Place 1.

Sample Input 2

3
21 -11
81234 94124 52141

Sample Output 2

3


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

void bubblesort(int a[],int N){
    for(int i=0; i<N-1; i++){
        for(int j=0; j<N-1; j++){
            if(a[j+1]<a[j]){
                int swap = a[j];
                a[j] = a[j+1];
                a[j+1] = swap;
            }
        }
    }

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

void specialsort(int a[],int N){
    int arr[100000],count=0;
    for(int i=0; i<N; i++){
        if(a[i]!=0){
            arr[count] = a[i];
            count++;
        }
    }
    bubblesort(arr,count);
    for(int j=0; j<count; j++){
        for(int i=0; i<N; i++){
            if(a[i]==arr[j]){
                a[i] = j+1;
            }
        }
    }
}

int main(void){
    int prefectureNum, statesNum;
    scanf("%d%d",&prefectureNum, &statesNum);
    int P[statesNum], Y[statesNum];
    for(int i=0; i<statesNum; i++){
        scanf("%d%d",&P[i], &Y[i]);
    }
    int len = prefectureNum+1;
    // 配列定義の際、長さを計算式で指定してはいけない
    int arr[len][len], count=0;
    for(int i=1; i<=prefectureNum; i++){
        for(int j=0; j<statesNum; j++){
            if(P[j] == i){
                arr[i][j] = Y[j];
                //printf("%d\n", arr[i][j]);
            }
        }
        specialsort(arr[i], prefectureNum+1);
    }

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

    return 0;
}

Score: 300 points

Problem Statement

In Republic of Atcoder, there are N prefectures, and a total of M cities that belong to those prefectures.

City i is established in year Y_i and belongs to Prefecture P_i.

You can assume that there are no multiple cities that are established in the same year.

It is decided to allocate a 12-digit ID number to each city.

If City i is the x-th established city among the cities that belong to Prefecture i, the first six digits of the ID number of City i is P_i, and the last six digits of the ID number is x.

Here, if P_i or x (or both) has less than six digits, zeros are added to the left until it has six digits.

Find the ID numbers for all the cities.

Note that there can be a prefecture with no cities.

Constraints

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq P_i \leq N

1 \leq Y_i \leq 10^9

Y_i are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
P_1 Y_1
:
P_M Y_M

Output

Print the ID numbers for all the cities, in ascending order of indices (City 1, City 2, ...).

Sample Input 1

2 3
1 32
2 63
1 12

Sample Output 1

000001000002
000002000001
000001000001

As City 1 is the second established city among the cities that belong to Prefecture 1, its ID number is 000001000002.

As City 2 is the first established city among the cities that belong to Prefecture 2, its ID number is 000002000001.

As City 3 is the first established city among the cities that belong to Prefecture 1, its ID number is 000001000001.

Sample Input 2

2 3
2 55
2 77
2 99

Sample Output 2

000002000001
000002000002
000002000003


#include <stdio.h>

int main(){
	int N;
	scanf("%d", &N);
		int l;
		for(l=1; l<N; l++){
			N -= l;
		}
		if(N != l){
			printf("No\n");
			return;
		}
		printf("Yes\n");
		printf("%d\n", l+1);
		int answer[l+1][l];
		int Index = 1;
		for(int i=0; i<=l; i++){
			printf("%d", l);
			int j=0;
			for(; j<i; j++){
				printf(" %d", answer[j][i-1]);
			}
			for(; j<l; j++){
				printf(" %d", answer[i][j]=Index++);
			}
			printf("\n");
		}
	return 0;
}

Score : 500 points

Problem Statement

You are given an integer N. Determine if there exists a tuple of subsets of \{1,2,...N\}, (S_1,S_2,...,S_k), that satisfies the following conditions:

Each of the integers 1,2,...,N is contained in exactly two of the sets S_1,S_2,...,S_k.

Any two of the sets S_1,S_2,...,S_k have exactly one element in common.

If such a tuple exists, construct one such tuple.

Constraints

1 \leq N \leq 10^5

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If a tuple of subsets of \{1,2,...N\} that satisfies the conditions does not exist, print No.
If such a tuple exists, print Yes first, then print such subsets in the following format:

k
|S_1| S_{1,1} S_{1,2} ... S_{1,|S_1|}
:
|S_k| S_{k,1} S_{k,2} ... S_{k,|S_k|}

where S_i={S_{i,1},S_{i,2},...,S_{i,|S_i|}}.

If there are multiple such tuples, any of them will be accepted.

Sample Input 1

3

Sample Output 1

Yes
3
2 1 2
2 3 1
2 2 3

It can be seen that (S_1,S_2,S_3)=(\{1,2\},\{3,1\},\{2,3\}) satisfies the conditions.

Sample Input 2

4

Sample Output 2

No


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

int main(void)
{
	/* code */
	int err, inp, a, b;
	err = scanf("%d", &inp);
	if (err!=1 || (inp!=1 && inp!=2)) exit(1);
	switch(inp)
	{
		case 1:
			printf("Hello, world\n");
			break;
		case 2:
			err=scanf("%d%d", &a, &b);
			if (err !=2 || (a<1 || b<9)) exit(1);
			printf("%d\n", a+b);
			break;
	}
	return 0;
}

Score: 100 points

Problem Statement

In 2020, AtCoder Inc. with an annual sales of more than one billion yen (the currency of Japan) has started a business in programming education.

One day, there was an exam where a one-year-old child must write a program that prints Hello World, and a two-year-old child must write a program that receives integers A, B and prints A+B.

Takahashi, who is taking this exam, suddenly forgets his age.

He decides to write a program that first receives his age N (1 or 2) as input, then prints Hello World if N=1, and additionally receives integers A, B and prints A+B if N=2.

Write this program for him.

Constraints

N is 1 or 2.

A is an integer between 1 and 9 (inclusive).

B is an integer between 1 and 9 (inclusive).

Input

Input is given from Standard Input in one of the following formats:

1

2
A
B

Output

If N=1, print Hello World; if N=2, print A+B.

Sample Input 1

1

Sample Output 1

Hello World

As N=1, Takahashi is one year old. Thus, we should print Hello World.

Sample Input 2

2
3
5

Sample Output 2

8

As N=2, Takahashi is two years old. Thus, we should print A+B, which is 8 since A=3 and B=5.


#include <stdio.h>

int main() {
    int n;
    int a;
    int b;
    scanf("%d",&n);
    if(n==1)
        printf("Hello, World\n");
    else if(n==2){
        scanf("%d",&a);
        scanf("%d",&b);
        printf("%d\n",a+b);
    }
    return 0;
}


#include <stdio.h>

int main(){
    int i,N,T,c,t,res = 1001;
    scanf("%d %d",&N,&T);
    for(i=0;i<N;i++){
        scanf("%d %d",&c,&t);
        if(t<T && c<res){
            c = res;
        }
    }
    if(res == 1001){
        printf("TLE");
    }else{
        printf("%d",res);
    }
    return 0;
}

Score : 200 points

Problem Statement

When Mr. X is away from home, he has decided to use his smartwatch to search the best route to go back home, to participate in ABC.

You, the smartwatch, has found N routes to his home.

If Mr. X uses the i-th of these routes, he will get home in time t_i at cost c_i.

Find the smallest cost of a route that takes not longer than time T.

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq T \leq 1000

1 \leq c_i \leq 1000

1 \leq t_i \leq 1000

The pairs (c_i, t_i) are distinct.

Input

Input is given from Standard Input in the following format:

N T
c_1 t_1
c_2 t_2
:
c_N t_N

Output

Print the smallest cost of a route that takes not longer than time T.

If there is no route that takes not longer than time T, print TLE instead.

Sample Input 1

3 70
7 60
1 80
4 50

Sample Output 1

4

The first route gets him home at cost 7.

The second route takes longer than time T = 70.

The third route gets him home at cost 4.

Thus, the cost 4 of the third route is the minimum.

Sample Input 2

4 3
1 1000
2 4
3 1000
4 500

Sample Output 2

TLE

There is no route that takes not longer than time T = 3.

Sample Input 3

5 9
25 8
5 9
4 10
1000 1000
6 1

Sample Output 3

5


#include <stdio.h>

int main(void){
	int a,b,c,d,i;
	scanf("%d %d",&a,&d);
	int e = 1001;
	for(i = 0;i < a;i++){
		scanf("%d %d",&b,&c);
		if(b < e && c < d){
			e = b;
		}
	}
	if(e != 1001){
		printf("%d\n",a);
	} else {
		printf("LTE\n");
	}
	return 0;
}


#include <stdio.h>

void main(){
    int i;
    char n[3];
    scanf("%s",n);
    for(i=0;i<3;i++){
        if(n[i] == '1'){
            printf("%d",9);
        }
        else{
            printf("%d",1);
        }
    }
}

Score : 100 points

Problem Statement

Cat Snuke is learning to write characters.
Today, he practiced writing digits 1 and 9, but he did it the other way around.

You are given a three-digit integer n written by Snuke.
Print the integer obtained by replacing each digit 1 with 9 and each digit 9 with 1 in n.

Constraints

111 \leq n \leq 999

n is an integer consisting of digits 1 and 9.

Input

Input is given from Standard Input in the following format:

n

Output

Print the integer obtained by replacing each occurrence of 1 with 9 and each occurrence of 9 with 1 in n.

Sample Input 1

119

Sample Output 1

991

Replace the 9 in the ones place with 1, the 1 in the tens place with 9 and the 1 in the hundreds place with 9. The answer is 991.

Sample Input 2

999

Sample Output 2

111


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

int main(void){
    int n;
    scanf("%d",&n);
    while(1){
        if(n/100 == n%10 && n%10 == (n/10)%10){
            break;
        }
        n++;
    }
    printf("%d\n",n);
    return 0;
}

Score : 200 points

Problem Statement

Kurohashi has never participated in AtCoder Beginner Contest (ABC).

The next ABC to be held is ABC N (the N-th ABC ever held).
Kurohashi wants to make his debut in some ABC x such that all the digits of x in base ten are the same.

What is the earliest ABC where Kurohashi can make his debut?

Constraints

100 \leq N \leq 999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If the earliest ABC where Kurohashi can make his debut is ABC n, print n.

Sample Input 1

111

Sample Output 1

111

The next ABC to be held is ABC 111, where Kurohashi can make his debut.

Sample Input 2

112

Sample Output 2

222

The next ABC to be held is ABC 112, which means Kurohashi can no longer participate in ABC 111.
Among the ABCs where Kurohashi can make his debut, the earliest one is ABC 222.

Sample Input 3

750

Sample Output 3

777


#include <stdio.h>

int main()
{
int n, i, even[200000], odd[200000], a[200000], max_even=0, max_odd=0, value_even, value_odd;
scanf("%d", &n);
for(i=0;i<n;i++){
even[i]=0;
odd[i]=0;
}
for(i=0;i<n;i++){
scanf("%d", &a[i]);
if(i%2==0)even[a[i]]++;
else odd[a[i]]++;
}
while(1){
for(i=0;i<200000;i++){
if(max_even<even[i]){
max_even=even[i];
value_even=i;
}
if(max_odd<odd[i]){
max_odd=odd[i];
value_odd=i;
}
}
if(value_even!=value_odd)break;
else{
if(max_even>max_odd)odd[value_odd]=0;
else even[value_even]=0;
}
}
int k=n-max_odd-max_even;
printf("%d\n", k);
return 0;
}

Score : 300 points

Problem Statement

A sequence a_1,a_2,... ,a_n is said to be /\/\/\/ when the following conditions are satisfied:

For each i = 1,2,..., n-2, a_i = a_{i+2}.

Exactly two different numbers appear in the sequence.

You are given a sequence v_1,v_2,...,v_n whose length is even.
We would like to make this sequence /\/\/\/ by replacing some of its elements.
Find the minimum number of elements that needs to be replaced.

Constraints

2 \leq n \leq 10^5

n is even.

1 \leq v_i \leq 10^5

v_i is an integer.

Input

Input is given from Standard Input in the following format:

n
v_1 v_2 ... v_n

Output

Print the minimum number of elements that needs to be replaced.

Sample Input 1

4
3 1 3 2

Sample Output 1

1

The sequence 3,1,3,2 is not /\/\/\/, but we can make it /\/\/\/ by replacing one of its elements: for example, replace the fourth element to make it 3,1,3,1.

Sample Input 2

6
105 119 105 119 105 119

Sample Output 2

0

The sequence 105,119,105,119,105,119 is /\/\/\/.

Sample Input 3

4
1 1 1 1

Sample Output 3

2

The elements of the sequence 1,1,1,1 are all the same, so it is not /\/\/\/.


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

int cmp( const void *p, const void *q ) {
    return (int*)p - (int*)q;
}

int main(void){

	int i, n, tmp, cnt, ans, odd1 = 0, odd2 = 0, even1 = 0, even2 = 0, cnt_odd1 = 0, cnt_odd2 = 0, cnt_even1 = 0, cnt_even2 = 0;

	scanf("%d", &n);

	int v_odd[n / 2], v_even[n / 2];

	for(i = 0; i < n; i++){
		if(i % 2 == 0){
			scanf("%d", &v_even[i / 2]);
		}else{
			scanf("%d", &v_odd[i / 2]);
		}
	}

	qsort(v_even, n / 2, sizeof(int), cmp);
	qsort(v_odd, n / 2, sizeof(int), cmp);

	tmp = v_odd[0];
	cnt = 1;
	i = 1;
	while(i < n / 2){
		if(tmp == v_odd[i]){
			cnt++;
		}else{
			if(cnt >= cnt_odd1){
				cnt_odd2 = cnt_odd1;
				odd2 = odd1;
				cnt_odd1 = cnt;
				odd1 = tmp;
			}else if(cnt >= cnt_odd2){
				cnt_odd2 = cnt;
				odd2 = tmp;
			}
			cnt = 1;
			tmp = v_odd[i];
		}
		i++;
	}
	if(cnt >= cnt_odd1){
		cnt_odd2 = cnt_odd1;
		odd2 = odd1;
		cnt_odd1 = cnt;
		odd1 = tmp;
	}else if(cnt >= cnt_odd2){
		cnt_odd2 = cnt;
		odd2 = tmp;
	}

	tmp = v_even[0];
	cnt = 1;
	i = 1;
	while(i < n / 2){
		if(tmp == v_even[i]){
			cnt++;
		}else{
			if(cnt >= cnt_even1){
				cnt_even2 = cnt_even1;
				even2 = even1;
				cnt_even1 = cnt;
				even1 = tmp;
			}else if(cnt >= cnt_even2){
				cnt_even2 = cnt;
				even2 = tmp;
			}
			cnt = 1;
			tmp = v_even[i];
		}
		i++;
	}
	if(cnt >= cnt_even1){
		cnt_even2 = cnt_even1;
		even2 = even1;
		cnt_even1 = cnt;
		even1 = tmp;
	}else if(cnt >= cnt_even2){
		cnt_even2 = cnt;
		even2 = tmp;
	}

	// printf("odd1 : %d (%d)\nodd2 : %d (%d)\neven1 : %d (%d)\neven2 : %d (%d)\n", odd1, cnt_odd1, odd2, cnt_odd2, even1, cnt_even1, even2, cnt_even2);

	if(odd1 == even1){
		int a[2];
		a[0] = (n / 2 - cnt_odd2) + (n / 2 - cnt_even1);
		a[1] = (n / 2 - cnt_odd1) + (n / 2 - cnt_even2);
		if(a[0] < a[1]){
			ans = a[0];
		}else{
			ans = a[1];
		}
	}else{
		ans = (n / 2 - cnt_odd1) + (n / 2 - cnt_even1);
	}

	printf("%d", ans);

	return 0;
}