Score : 200 points

Problem Statement

We have a sequence p = {p_1,\ p_2,\ ...,\ p_N} which is a permutation of {1,\ 2,\ ...,\ N}.

You can perform the following operation at most once: choose integers i and j (1 \leq i < j \leq N), and swap p_i and p_j. Note that you can also choose not to perform it.

Print YES if you can sort p in ascending order in this way, and NO otherwise.

Constraints

All values in input are integers.

2 \leq N \leq 50

p is a permutation of {1,\ 2,\ ...,\ N}.

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 ... p_N

Output

Print YES if you can sort p in ascending order in the way stated in the problem statement, and NO otherwise.

Sample Input 1

5
5 2 3 4 1

Sample Output 1

YES

You can sort p in ascending order by swapping p_1 and p_5.

Sample Input 2

5
2 4 3 5 1

Sample Output 2

NO

In this case, swapping any two elements does not sort p in ascending order.

Sample Input 3

7
1 2 3 4 5 6 7

Sample Output 3

YES

p is already sorted in ascending order, so no operation is needed.


#include <stdio.h>

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

   int p[N];
   int count = 0;

   for (int i = 1; i <= N; i++) {
      scanf("%d", &p[i]);

      if (i != p[i])
         count++;
   }

   if (count < 3)
      printf("YES\n");
   else
      printf("NO\n");

   return 0;
}

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

Submitted source-code file size, in bytes.

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

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


Score : 300 points

Problem Statement

There are N+1 towns. The i-th town is being attacked by A_i monsters.

We have N heroes. The i-th hero can defeat monsters attacking the i-th or (i+1)-th town, for a total of at most B_i monsters.

What is the maximum total number of monsters the heroes can cooperate to defeat?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_{N+1}
B_1 B_2 ... B_N

Output

Print the maximum total number of monsters the heroes can defeat.

Sample Input 1

2
3 5 2
4 5

Sample Output 1

9

If the heroes choose the monsters to defeat as follows, they can defeat nine monsters in total, which is the maximum result.

The first hero defeats two monsters attacking the first town and two monsters attacking the second town.

The second hero defeats three monsters attacking the second town and two monsters attacking the third town.

Sample Input 2

3
5 6 3 8
5 100 8

Sample Output 2

22

Sample Input 3

2
100 1 1
1 100

Sample Output 3

3


#include <stdio.h>

int main(){
  int r;
  scanf("%d",&r);
  printf("%d\n",3*r*r);
  return 0;
}

Score : 100 points

Problem Statement

It is known that the area of a regular dodecagon inscribed in a circle of radius a is 3a^2.

Given an integer r, find the area of a regular dodecagon inscribed in a circle of radius r.

Constraints

1 \leq r \leq 100

r is an integer.

Input

Input is given from Standard Input in the following format:

r

Output

Print an integer representing the area of the regular dodecagon.

Sample Input 1

4

Sample Output 1

48

The area of the regular dodecagon is 3 \times 4^2 = 48.

Sample Input 2

15

Sample Output 2

675

Sample Input 3

80

Sample Output 3

19200


#include<stdio.h>

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


#include<stdio.h>
int main(){

    int N , D ;
    scanf("%d%d",&N,&D);
    int i = D+1 ; int count = 0 ;
    while(i <= N){
        count++;
        if(N >= 2*i){
            i = 2*i ;
        }
        else{
            i = i + (N-(D+1));
            }
    }
    printf("%d",count) ;
}

Score : 200 points

Problem Statement

There are N apple trees in a row. People say that one of them will bear golden apples.

We want to deploy some number of inspectors so that each of these trees will be inspected.

Each inspector will be deployed under one of the trees. For convenience, we will assign numbers from 1 through N to the trees. An inspector deployed under the i-th tree (1 \leq i \leq N) will inspect the trees with numbers between i-D and i+D (inclusive).

Find the minimum number of inspectors that we need to deploy to achieve the objective.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq D \leq 20

Input

Input is given from Standard Input in the following format:

N D

Output

Print the minimum number of inspectors that we need to deploy to achieve the objective.

Sample Input 1

6 2

Sample Output 1

2

We can achieve the objective by, for example, placing an inspector under Tree 3 and Tree 4.

Sample Input 2

14 3

Sample Output 2

2

Sample Input 3

20 4

Sample Output 3

3


#include<stdio.h>
int main(){
  int n,a[200001],max1=0,max2=0;
  scanf("%d",&n);
  for(int i=1;i<=n;i++){
  	scanf("%d",&a[i]);
    if(max1<a[i]){
      max2=max1;
      max1=a[i];
    }else if(max2<a[i]){
      max2=a[i];
    }
  }
  for(int j=1;j<=n;j++){
    if(a[j]==max1)	printf("%d\n",max2);
    else	printf("%d\n",max1);
  }
  return 0;
}

Score : 300 points

Problem Statement

You are given a sequence of length N: A_1, A_2, ..., A_N.
For each integer i between 1 and N (inclusive), answer the following question:

Find the maximum value among the N-1 elements other than A_i in the sequence.

Constraints

2 \leq N \leq 200000

1 \leq A_i \leq 200000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print N lines. The i-th line (1 \leq i \leq N) should contain the maximum value among the N-1 elements other than A_i in the sequence.

Sample Input 1

3
1
4
3

Sample Output 1

4
3
4

The maximum value among the two elements other than A_1, that is, A_2 = 4 and A_3 = 3, is 4.

The maximum value among the two elements other than A_2, that is, A_1 = 1 and A_3 = 3, is 3.

The maximum value among the two elements other than A_3, that is, A_1 = 1 and A_2 = 4, is 4.

Sample Input 2

2
5
5

Sample Output 2

5
5


//ヘッダファイル
#include<stdio.h>

//エントリポイント
int main(void)
{
	int n;
	int a[200000]={};

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

	int first,fi;
	int second;

	if(a[0]>=a[1]){
		first=a[0];fi=0;
		second=a[1];
	}else{
		first=a[1];fi=1;
		second=a[0];
	}

	for(int i=2;i<n;i++){
		if(a[i]>=first){
			second=first;
			first=a[i];fi=i;
		}else if(a[i]>=second){
			second=a[i];
		}
	}

	for(int i=0;i<n-1;i++){
		printf("%d\n",i!=fi?first:second);
	}

	return 0;
}


    #include <stdio.h>

    int main(){
    	int n;
        int a[200000];
        int i;
        int k;
        int max,second_max;

      	scanf("%d",&n);
      	for(i=0; i<n; i++){
      		scanf("%d",&a[i]);
      	}
       	max = a[0];
        second_max = a[0];
      	for(i=0; i<n; i++){
            if (max < a[i]) {
                second_max = max;
                max = a[i];
            }else if(second_max = a[i]){
                second_max = a[i];
            }
        }

        for(i=0; i<n; i++){
            if(max == a[i]){
               printf("%d\n",second_max);
            }else{
               printf("%d\n",max);
            }
        }

       	return 0;
    }


#include <stdio.h>

typedef enum {false, true} bool;

int main(void)
{
    int N;
    bool flag, ans;
    int a[3], ai;
    int a_num[3];
    char c;
    int i, j;
    int ctr;

    ans = true;

    a_num[0] = a_num[1] = a_num[2] = 0;
    ctr = 0;

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

    for(i=0; i<N; i++)
    {
        scanf("%d%c", &ai, &c);

        flag = false;

        for(j=0; j<ctr; j++)
        {
            if(ai == a[j])
            {
                flag = true;

                a_num[j]++;

                break;
            }
        }

        if(!flag)
        {
            if(ctr < 3)
            {
                a[ctr] = ai;
                a_num[ctr++]++;
            }
            else
            {
                ans = false;

                break;
            }
        }
    }

    if(ans)
    {
        switch(ctr)
        {
            case 1:
                if(a[0] != 0)
                    ans = false;

                break;

            case 2:
                if(a[0] == 0 && a_num[0]*2 == a_num[1])
                {
                    a[2] = a[1];
                }
                else if(a[1] == 0 && a_num[1]*2 == a_num[0])
                {
                    a[2] = a[0];
                }
                else
                {
                    ans = false;

                    break;
                }
            break;

            case 3:
                if(a[0]^a[2] == a[1])
                {
                    if((a_num[0]+a_num[2]) != 2*a_num[1] && (a_num[1]+a_num[2]) != 2*a_num[0])
                        ans = false;
                }
                else
                    ans = false;
        }
    }

    if(ans)
        printf("Yes\n");
    else
        printf("No\n");

    //system("pause");

    return 0;
}

Score : 300 points

Problem Statement

Snuke has N hats. The i-th hat has an integer a_i written on it.

There are N camels standing in a circle.
Snuke will put one of his hats on each of these camels.

If there exists a way to distribute the hats to the camels such that the following condition is satisfied for every camel, print Yes; otherwise, print No.

The bitwise XOR of the numbers written on the hats on both adjacent camels is equal to the number on the hat on itself.

What is XOR?

The bitwise XOR x_1 \oplus x_2 \oplus \ldots \oplus x_n of n non-negative integers x_1, x_2, \ldots, x_n is defined as follows:

- When x_1 \oplus x_2 \oplus \ldots \oplus x_n is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if the number of integers among x_1, x_2, \ldots, x_n whose binary representations have 1 in the 2^k's place is odd, and 0 if that count is even.

For example, 3 \oplus 5 = 6.

Constraints

All values in input are integers.

3 \leq N \leq 10^{5}

0 \leq a_i \leq 10^{9}

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_{N}

Output

Print the answer.

Sample Input 1

3
1 2 3

Sample Output 1

Yes

If we put the hats with 1, 2, and 3 in this order, clockwise, the condition will be satisfied for every camel, so the answer is Yes.

Sample Input 2

4
1 2 4 8

Sample Output 2

No

There is no such way to distribute the hats; the answer is No.


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

void swap (unsigned long *x, unsigned long *y) {
    int temp;    // 値を一時保存する変数

    temp = *x;
    *x = *y;
    *y = temp;
}

/***
 * pivotを決め、
 * 全データをpivotを境目に振り分け、
 * pivotの添え字を返す
 ***/
int partition (unsigned long array[], int left, int right) {
    int i, j, pivot;
    i = left;
    j = right + 1;
    pivot = left;   // 先頭要素をpivotとする

    do {
        do { i++; } while (array[i] < array[pivot]);
        do { j--; } while (array[pivot] < array[j]);
        // pivotより小さいものを左へ、大きいものを右へ
        if (i < j) { swap(&array[i], &array[j]); }
    } while (i < j);

    swap(&array[pivot], &array[j]);   //pivotを更新

    return j;
}

/* クイックソート */
void quick_sort (unsigned long array[], int left, int right) {
    int pivot;

    if (left < right) {
        pivot = partition(array, left, right);
        quick_sort(array, left, pivot-1);   // pivotを境に再帰的にクイックソート
        quick_sort(array, pivot+1, right);
    }
}

int main(void){
    int i,j,k;
    int N;
    unsigned long a[100000],temp;

    scanf("%d",&N);
    for(i = 0;i < N-1;i++){
        scanf("%ld\n",&a[i]);
    }
    scanf("%ld",&a[i]);

    /*
    for(i = 1;i < N-1;i++){//真ん中
        if(i == 1){
            for(j = i;j < N;j++){//真ん中
                for(k = i;k < N;k++){//右隣
                    if(j != k){
                        if(a[j] == (a[i-1] ^ a[k])){//入れ替え処理
                            temp = a[i];
                            a[i] = a[j];
                            a[j] = temp;
                            temp = a[k];
                            a[k] = a[i+1];
                            a[i+1] = temp;
                        }else{
                            if(k == N-1){
                                printf("No\n");
                                return 0;
                            }
                        }
                    }
                }
            }
        }else{
            for(j = i;j == i;j++){//真ん中
                for(k = i+1;k < N;k++){//右隣
                    if(a[j] == (a[i-1] ^ a[k])){//入れ替え処理
                        temp = a[k];
                        a[k] = a[i+1];
                        a[i+1] = temp;
                    }else{
                        if(k == N-1){
                            printf("No\n");
                            return 0;
                        }
                    }
                }
            }
        }
    }
     */
    quick_sort(a,0,N-1);

    //判定処理
    for(i = 1;i < N-1;i++){//真ん中
        if(a[i] != (a[i-1] ^ a[i+1])){
            printf("No\n");
            return 0;
        }
    }
    if(a[N-1] != (a[0] ^ a[N-2])){
        printf("No\n");
        return 0;
    }
    if(a[0] != (a[1] ^ a[N-1])){
        printf("No\n");
        return 0;
    }

    printf("No\n");
    return 0;
}


#include <stdio.h>

int main()
{
int L,R;
scanf("%d %d",&L,&R);
int i,s;
int j=0;
int m=2018;
for(i=L;i<R;i++){
  s=i%2019;
  if(s<m){
    m=s;
    j=i;
  }}
if((R-j)<2019)printf("%d\n",m*(m+1));
else printf("%d\n",m*m);
return 0;
}

Score : 100 points

Problem Statement

N of us are going on a trip, by train or taxi.

The train will cost each of us A yen (the currency of Japan).

The taxi will cost us a total of B yen.

How much is our minimum total travel expense?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq A \leq 50

1 \leq B \leq 50

Input

Input is given from Standard Input in the following format:

N A B

Output

Print an integer representing the minimum total travel expense.

Sample Input 1

4 2 9

Sample Output 1

8

The train will cost us 4 \times 2 = 8 yen, and the taxi will cost us 9 yen, so the minimum total travel expense is 8 yen.

Sample Input 2

4 2 7

Sample Output 2

7

Sample Input 3

4 2 8

Sample Output 3

8


#include<stdio.h>
int main(){

    int a,b,n;
    scanf("%d", &n);
    scanf("%d", &a);
    scanf("%d", &b);
    if(n*a > b){
        printf("%d\n", b);
    }else{
        printf("%d\n", n*a);
    }
    return 0;
}


#include<stdio.h>

int main()
{
  int N, A, B;
  int x, y;

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

  x = N*A;
  y = B;

  if(x<=y){
    printf("%d", x);
  } else {
    printf("%d", y);
  }

  return 0;

}


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

                        int main(void){
                          int n,d;
                          scanf("%d %d",&n,&d);
                          int ar[10][10];
                          for(int r=0; r<n;r++){
                            for(int c=0; c<d;c++){
                              scanf("%d",&ar[r][c]);
                            }
                          }
                          double distance;
                          int correctCount;
                          for(int i=0; i<n; i++){
                            for(int j=i+1; j<n; j++){
                              distance = 0;
                          		for(int k=0; k<d;k++){
                            		distance = distance + pow((double)abs(ar[i][k] - ar[j][k]),2.0);
                                }
                              distance = pow(distance,0.5);
                              distance = sqrt(distance);
                              if(ceil(distance)!= floor(distance)){
                                correctCount++;
                              }

                            }
                          }
                          printf("%d",correctCount);
                        }

Score : 200 points

Problem Statement

There are N points in a D-dimensional space.

The coordinates of the i-th point are (X_{i1}, X_{i2}, ..., X_{iD}).

The distance between two points with coordinates (y_1, y_2, ..., y_D) and (z_1, z_2, ..., z_D) is \sqrt{(y_1 - z_1)^2 + (y_2 - z_2)^2 + ... + (y_D - z_D)^2}.

How many pairs (i, j) (i < j) are there such that the distance between the i-th point and the j-th point is an integer?

Constraints

All values in input are integers.

2 \leq N \leq 10

1 \leq D \leq 10

-20 \leq X_{ij} \leq 20

No two given points have the same coordinates. That is, if i \neq j, there exists k such that X_{ik} \neq X_{jk}.

Input

Input is given from Standard Input in the following format:

N D
X_{11} X_{12} ... X_{1D}
X_{21} X_{22} ... X_{2D}
\vdots
X_{N1} X_{N2} ... X_{ND}

Output

Print the number of pairs (i, j) (i < j) such that the distance between the i-th point and the j-th point is an integer.

Sample Input 1

3 2
1 2
5 5
-2 8

Sample Output 1

1

The number of pairs with an integer distance is one, as follows:

The distance between the first point and the second point is \sqrt{|1-5|^2 + |2-5|^2} = 5, which is an integer.

The distance between the second point and the third point is \sqrt{|5-(-2)|^2 + |5-8|^2} = \sqrt{58}, which is not an integer.

The distance between the third point and the first point is \sqrt{|-2-1|^2+|8-2|^2} = 3\sqrt{5}, which is not an integer.

Sample Input 2

3 4
-3 7 8 2
-12 1 10 2
-2 8 9 3

Sample Output 2

2

Sample Input 3

5 1
1
2
3
4
5

Sample Output 3

10


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

typedef long long ll;
typedef char keytype;

void quicksort_des(keytype a[], int first, int last)
{
    int i, j;
    keytype x, t;

    x = a[(first + last) / 2];
    i = first;  j = last;
    for ( ; ; ) {
        while (a[i] > x) i++;
        while (x > a[j]) j--;
        if (i >= j) break;
        t = a[i];  a[i] = a[j];  a[j] = t;
        i++;  j--;
    }
    if (first  < i - 1) quicksort_des(a, first , i - 1);
    if (j + 1 < last) quicksort_des(a, j + 1, last);
}

int main(){
    char s[5];

    scanf("%s", s);

    quicksort_des(s, 0, 3);

    if(s[0] == s[1] && s[2] == s[3] && s[0] != s[2]) puts("Yes");
    else puts("No");

    return 0;
}

Score : 100 points

Problem Statement

You are given a 4-character string S consisting of uppercase English letters.
Determine if S consists of exactly two kinds of characters which both appear twice in S.

Constraints

The length of S is 4.

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If S consists of exactly two kinds of characters which both appear twice in S, print Yes; otherwise, print No.

Sample Input 1

ASSA

Sample Output 1

Yes

S consists of A and S which both appear twice in S.

Sample Input 2

STOP

Sample Output 2

No

Sample Input 3

FFEE

Sample Output 3

Yes

Sample Input 4

FREE

Sample Output 4

No


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#define BUFSIZE 1000000

int main(void){
char s[BUFSIZE];
scanf("%s",s);
int cnt =0 ;
char rem;

for(int i = 0 ;i < 4;i++){
  for(int j = 0;j < 4;j++){
    if(i == j)  break;
    if(s[i] == s[j]){
    if(s[i] == rem){
      rem = '0';
      break;
    }
      cnt++;
      rem = s[i];
      break;
    }

  }

}

if(cnt == 2) printf("Yes");
else printf("No");

return 0;

}


#include <stdio.h>

int main (void)
{
	char str[6];
	scanf("%s",str);
	if (str[0] == str[1] && str[1] == str[2] && str[2] == str[3])
		printf("No");
	else if(str[0] == str[1] && str[2] == str[3])
		printf("Yes");
	else if(str[0] == str[2] && str[1] == str[3])
		printf("Yes");
	else if(str[0] == str[3] && str[1] == str[2])
		printf("Yes");
	else
		printf("No");

	return(0);
}


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

Score : 200 points

Problem Statement

We have a permutation p = {p_1,\ p_2,\ ...,\ p_n} of {1,\ 2,\ ...,\ n}.

Print the number of elements p_i (1 < i < n) that satisfy the following condition:

p_i is the second smallest number among the three numbers p_{i - 1}, p_i, and p_{i + 1}.

Constraints

All values in input are integers.

3 \leq n \leq 20

p is a permutation of {1,\ 2,\ ...,\ n}.

Input

Input is given from Standard Input in the following format:

n
p_1 p_2 ... p_n

Output

Print the number of elements p_i (1 < i < n) that satisfy the condition.

Sample Input 1

5
1 3 5 4 2

Sample Output 1

2

p_2 = 3 is the second smallest number among p_1 = 1, p_2 = 3, and p_3 = 5. Also, p_4 = 4 is the second smallest number among p_3 = 5, p_4 = 4, and p_5 = 2. These two elements satisfy the condition.

Sample Input 2

9
9 6 3 2 5 8 7 4 1

Sample Output 2

5


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

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

int main(void)
{
    int N, i;
    int d[200000];

    scanf("%d",&N);
    for(i-0; i<N; i++) scanf("%d",&d[i]);

    qsort(d,N,sizeof(int),compare_int);

    N = N/2;

    printf("%d\n",d[N]-d[N-1]);

    return 0;
}

Score : 300 points

Problem Statement

Takahashi made N problems for competitive programming.
The problems are numbered 1 to N, and the difficulty of Problem i is represented as an integer d_i (the higher, the harder).

He is dividing the problems into two categories by choosing an integer K, as follows:

A problem with difficulty K or higher will be for ARCs.

A problem with difficulty lower than K will be for ABCs.

How many choices of the integer K make the number of problems for ARCs and the number of problems for ABCs the same?

Problem Statement

2 \leq N \leq 10^5

N is an even number.

1 \leq d_i \leq 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
d_1 d_2 ... d_N

Output

Print the number of choices of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 1

6
9 1 4 4 6 7

Sample Output 1

2

If we choose K=5 or 6, Problem 1, 5, and 6 will be for ARCs, Problem 2, 3, and 4 will be for ABCs, and the objective is achieved.
Thus, the answer is 2.

Sample Input 2

8
9 1 14 5 5 4 4 14

Sample Output 2

0

There may be no choice of the integer K that make the number of problems for ARCs and the number of problems for ABCs the same.

Sample Input 3

14
99592 10342 29105 78532 83018 11639 92015 77204 30914 21912 34519 80835 100000 1

Sample Output 3

42685


#include <stdio.h>
#include <string.h>
int main(){
    int N;
    scanf("%d",&N);
    int d[N];
    for(int i=0; i<N; i++){
        scanf("%d",&d[i]);
    }
    //int sort[N];
    int tmp;
    for(int i=0;i<N;i++){
        for(int j=N-1;j>i;j--){
            if(d[j-1]>d[j]){
                tmp = d[j];
                d[j] = d[j-1];
                d[j-1] = tmp;
            }
        }
    }
    printf("%d\n",d[N/2]-d[N/2-1]);
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#define LL long long
#define INF 1<<31-1
#define MIN(x,y) ((x)<(y)?(x):(y))
#define MAX(x,y) ((x)<(y)?(y):(x))
#define FOR(i,a,n) for(i=a;i<n;i++)
#define MOD 1000000007
#define NUM 100005
void swap(int* a,int* b){int tmp=*a;*a=*b;*b=tmp;}
int GCD(int a,int b){int c=1,tmp=MAX(a,b);b=MIN(a,b);a=tmp;while(c!=0){c=a%b;a=b;b=c;}return a;}
int LCM(int a,int b){return a*b/GCD(a,b);}
int POW(int a,int n){int i;LL x=a;if(n==0)return 1;else if(n==1)return a;else FOR(i,0,n-1){x=x*a;if(x>MOD)x%=MOD;}return (int)x;}
int cmp(const void* a,const void* b){return *(int*)a-*(int*)b;}

int main() {
  int N,i,ans=0;
  scanf("%d",&N);
  int d[N];
  FOR(i,0,N)scanf("%d",&d[i]);
  qsort(d,N,4,cmp);
  ans=d[N/2]-d[N/2-1];

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


#include <stdio.h>
#include <stdlib.h>
#define MAX_N 100000

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

int main(void) {
  int N;
  scanf("%d", &N);
  int d[MAX_N];
  for(int i = 0; i < N; i++) {
    scanf("%d", &d[i]);
  }
  qsort(d, N, sizeof(int), int_sort);

  int lp = (N-1) / 2;
  int rp = lp + 1;
  int num = d[rp] - d[lp];
  printf("%d\n", num);
  return 0;
}


#include <stdio.h>
#include <assert.h>

#define MOD 1000000007

int dp[2][101][100001];

int isqrt(long long int x) {
	long long int a = x, b = (x+1)/2;
	while (a > b) {
		a = b;
		b = (a + x/a)/2;
	}
	return a;
}

int main(void) {
	long long int n;
	int k;
	scanf("%lld%d", &n, &k);

	int sqrt = isqrt(n)+1;

	dp[0][0][1] = 1;
	for (int i = 0; i < k; i++) {
		for (int j = 1; j < sqrt; j++) {
			dp[0][i][j] += dp[0][i][j-1];
			dp[0][i][j] %= MOD;
		}
		for (int j = 1; j < sqrt-1; j++) {
			dp[1][i][sqrt-j-1] += dp[1][i][sqrt-j];
			dp[1][i][sqrt-j-1] %= MOD;
		}

		for (int j = 1; j < sqrt; j++) {
			dp[0][i+1][j] = dp[0][i][sqrt-1] + dp[1][i][j];
			dp[0][i+1][j] %= MOD;

			if (n/j != j) {
				dp[1][i+1][j] = ((long long int) dp[0][i][j] * (n/j - n/(j+1))) % MOD;
			}
		}

	}

	int ans = 0;
	for (int i = 0; i < sqrt; i++) {
		ans = (ans + dp[0][k][i]) % MOD;
		ans = (ans + dp[1][k][i]) % MOD;
	}

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

Score : 600 points

Problem Statement

Find the number of sequences of length K consisting of positive integers such that the product of any two adjacent elements is at most N, modulo 10^9+7.

Constraints

1\leq N\leq 10^9

1 2\leq K\leq 100 (fixed at 21:33 JST)

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of sequences, modulo 10^9+7.

Sample Input 1

3 2

Sample Output 1

5

(1,1), (1,2), (1,3), (2,1), and (3,1) satisfy the condition.

Sample Input 2

10 3

Sample Output 2

147

Sample Input 3

314159265 35

Sample Output 3

457397712


#include<stdio.h>
int main(){
	char s[4];
	int cont=0;
	scanf("%s", s);
	for(int i=0; i<3; ++i){
		if(s[i]==s[i+1])
			cont=1;
	}

	if(cont==1)
		printf("Bad\n");
	else
		printf("Good\n");

    return 0;
}

Score : 100 points

Problem Statement

The door of Snuke's laboratory is locked with a security code.

The security code is a 4-digit number. We say the security code is hard to enter when it contains two consecutive digits that are the same.

You are given the current security code S. If S is hard to enter, print Bad; otherwise, print Good.

Constraints

S is a 4-character string consisting of digits.

Input

Input is given from Standard Input in the following format:

S

Output

If S is hard to enter, print Bad; otherwise, print Good.

Sample Input 1

3776

Sample Output 1

Bad

The second and third digits are the same, so 3776 is hard to enter.

Sample Input 2

8080

Sample Output 2

Good

There are no two consecutive digits that are the same, so 8080 is not hard to enter.

Sample Input 3

1333

Sample Output 3

Bad

Sample Input 4

0024

Sample Output 4

Bad


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

int main(){
	int taste, apple,pi;
	int tmp,min,all=0;
	int aji=0;
	int init;
	scanf("%d %d",&apple,&taste);
	init = taste;
	for(int i = 0; i < apple;i++){
		aji += init;
		init++;
	}
	tmp = taste;
	min = taste;
	for(int i = 0;i < apple-1;i++){
		tmp = abs(taste);
		if(tmp > abs(taste+1)){
			min = taste+1;
		}
		taste++;
	}
	pi = aji - min;
	printf("%d\n",pi);
	return 0;
}

Score : 200 points

Problem Statement

You have N apples, called Apple 1, Apple 2, Apple 3, ..., Apple N. The flavor of Apple i is L+i-1, which can be negative.

You can make an apple pie using one or more of the apples. The flavor of the apple pie will be the sum of the flavors of the apples used.

You planned to make an apple pie using all of the apples, but being hungry tempts you to eat one of them, which can no longer be used to make the apple pie.

You want to make an apple pie that is as similar as possible to the one that you planned to make. Thus, you will choose the apple to eat so that the flavor of the apple pie made of the remaining N-1 apples will have the smallest possible absolute difference from the flavor of the apple pie made of all the N apples.

Find the flavor of the apple pie made of the remaining N-1 apples when you choose the apple to eat as above.

We can prove that this value is uniquely determined.

Constraints

2 \leq N \leq 200

-100 \leq L \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N L

Output

Find the flavor of the apple pie made of the remaining N-1 apples when you optimally choose the apple to eat.

Sample Input 1

5 2

Sample Output 1

18

The flavors of Apple 1, 2, 3, 4, and 5 are 2, 3, 4, 5, and 6, respectively. The optimal choice is to eat Apple 1, so the answer is 3+4+5+6=18.

Sample Input 2

3 -1

Sample Output 2

0

The flavors of Apple 1, 2, and 3 are -1, 0, and 1, respectively. The optimal choice is to eat Apple 2, so the answer is (-1)+1=0.

Sample Input 3

30 -50

Sample Output 3

-1044


#include <stdio.h>

int main(void)
{
    int N,L,j=0,sum=0,min=100;
    scanf("%d%d",&N,&L);
    int taste[N],abso[N];
    for(int i=0;i<N;i++){
        sum+=L+i;
    }
    for(int i=0;i<N;i++){
        taste[i]=L+i;
        if(taste[i]<0)
            abso[i]=-taste[i];
        else
            abso[i]=taste[i];
        if(min>abso[i]){
            min=abso[i];
            j=i;
        }
    }
    printf("%d\n",sum-taste[j]);
    return 0;
}


/* Coached by Dukkha */
#include <stdio.h>

long long solve(long long a, long long b, long long c) {
	return b / c - (a - 1) / c;
}

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

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

int main() {
	long long a, b;
	int c, d;

	scanf("%lld%lld%d%d", &a, &b, &c, &d);
	printf("%lld\n", b - a + 1 - solve(a, b, c) - solve(a, b, d) + solve(a, b, lcm(c, d)));
	return 0;
}

Score : 300 points

Problem Statement

You are given four integers A, B, C, and D. Find the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Constraints

1\leq A\leq B\leq 10^{18}

1\leq C,D\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the number of integers between A and B (inclusive) that can be evenly divided by neither C nor D.

Sample Input 1

4 9 2 3

Sample Output 1

2

5 and 7 satisfy the condition.

Sample Input 2

10 40 6 8

Sample Output 2

23

Sample Input 3

314159265358979323 846264338327950288 419716939 937510582

Sample Output 3

532105071133627368


#include<stdio.h>

int main(){

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

    unsigned long count=0;
    count += b-a+1;
    int x=c*d;
    count -= (b/c)-((a-1)/c)+(b/d)-((a-1)/d);

    int r = d % c;
    while(r!=0){
        d=c;
        c=r;
        r=d%c;
    }

    count += (b/(x/c))-((a-1)/(x/c));

    printf("%ld\n",count);

    return 0;
}


#include <stdio.h>

long long gcd(long long n, long long m);

int main()
{
    long long a, b, c, d;
    long long lc;

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

    lc = c * d / gcd(c, d);

    printf("%lld\n",
           (b - a + 1)
           - (b / c - (a - 1) / c)//cで割り切れる
           - (b / d - (a - 1) / d)//dで割り切れる
           + (b / lc - (a - 1) / lc));

    return 0;
}

long long gcd(long long n, long long m)
{
    if (m < n) {
        long long tmp = n;
        n = m;
        m = tmp;
    }

    while (n) {
        long long r = m % n;

        m = n;
        n = r;
    }

    return m;
}


#include<stdio.h>
void sort(int a[],int b[],int n){
  int i,k,min,hog;
  for(i=0;i<n-1;i++){
    min=i;
    for(k=i;k<n;k++){
      if(b[min]>b[k]){
        min=k;
      }
    }
    hog=b[min];
    b[min]=b[i];
    b[i]=hog;
    hog=a[min];
    a[min]=a[i];
    a[i]=hog;

  }
}
int main(void){
  int a[200000],b[200000];
  int n,i,as=0;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%d%d",&a[i],&b[i]);
  }
  sort(a,b,n);
  for(i=0;i<n;i++){
    as+=a[i];
    if(as>b[i]){
      printf("No\n");
      return(0);
    }
  }
  printf("Yes\n");
  return(0);
}

Score: 400 points

Problem Statement

Kizahashi, who was appointed as the administrator of ABC at National Problem Workshop in the Kingdom of AtCoder, got too excited and took on too many jobs.

Let the current time be time 0. Kizahashi has N jobs numbered 1 to N.

It takes A_i units of time for Kizahashi to complete Job i. The deadline for Job i is time B_i, and he must complete the job before or at this time.

Kizahashi cannot work on two or more jobs simultaneously, but when he completes a job, he can start working on another immediately.

Can Kizahashi complete all the jobs in time? If he can, print Yes; if he cannot, print No.

Constraints

All values in input are integers.

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

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

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
.
.
.
A_N B_N

Output

If Kizahashi can complete all the jobs in time, print Yes; if he cannot, print No.

Sample Input 1

5
2 4
1 9
1 8
4 9
3 12

Sample Output 1

Yes

He can complete all the jobs in time by, for example, doing them in the following order:

Do Job 2 from time 0 to 1.

Do Job 1 from time 1 to 3.

Do Job 4 from time 3 to 7.

Do Job 3 from time 7 to 8.

Do Job 5 from time 8 to 11.

Note that it is fine to complete Job 3 exactly at the deadline, time 8.

Sample Input 2

3
334 1000
334 1000
334 1000

Sample Output 2

No

He cannot complete all the jobs in time, no matter what order he does them in.

Sample Input 3

30
384 8895
1725 9791
170 1024
4 11105
2 6
578 1815
702 3352
143 5141
1420 6980
24 1602
849 999
76 7586
85 5570
444 4991
719 11090
470 10708
1137 4547
455 9003
110 9901
15 8578
368 3692
104 1286
3 4
366 12143
7 6649
610 2374
152 7324
4 7042
292 11386
334 5720

Sample Output 3

Yes


#include <stdio.h>

int main(void) {
  int x,a;

  scanf("%d %d",x,a);

  if(x<a){
    printf("0");
  }else{
    printf("10");
  }

  return 0;

}

Score : 100 points

Problem Statement

X and A are integers between 0 and 9 (inclusive).

If X is less than A, print 0; if X is not less than A, print 10.

Constraints

0 \leq X, A \leq 9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X A

Output

If X is less than A, print 0; if X is not less than A, print 10.

Sample Input 1

3 5

Sample Output 1

0

3 is less than 5, so we should print 0.

Sample Input 2

7 5

Sample Output 2

10

7 is not less than 5, so we should print 10.

Sample Input 3

6 6

Sample Output 3

10

6 is not less than 6, so we should print 10.


#include <stdio.h>
int main(void)
{
    int X,A;
    scanf("%d %d", &X, &A);
    if (X == X < 9 )
    {
        printf("0\n");
    }
    else if ( X == X > A)
    {
        printf("10\n");
    }
    return 0;
}


#include <stdio.h>
int main(void)
{
  int x,a;
  scanf("%d",&x);
  scanf("%d",&a);
  if(x<a)
	{
	  printf("%d",0);
	}
  else{
	  printf("%d",10);
	   }

  return 0;
}


#include <stdio.h>

int main(){
	int X,A;
	scanf("%d%d",&X,&A);
	if(X<A)printf("0\n");
	else printf("10\n");
	return 0;
}


#include<stdio.h>
int main(void){
  int n,l,i,sum=0;
  scanf("%d %d",&n,&l);
  int j[n];
  for(i=0;i<n;i++){
    scanf("%d",&j[i]);
    sum+=j[i];
    if(sum>l){
      break;
    }
  }
  printf("%d",i+1);
  return 0;
}

Score : 200 points

Problem Statement

A ball will bounce along a number line, making N + 1 bounces. It will make the first bounce at coordinate D_1 = 0, and the i-th bounce (2 \leq i \leq N+1) at coordinate D_i = D_{i-1} + L_{i-1}.

How many times will the ball make a bounce where the coordinate is at most X?

Constraints

1 \leq N \leq 100

1 \leq L_i \leq 100

1 \leq X \leq 10000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
L_1 L_2 ... L_{N-1} L_N

Output

Print the number of times the ball will make a bounce where the coordinate is at most X.

Sample Input 1

3 6
3 4 5

Sample Output 1

2

The ball will make a bounce at the coordinates 0, 3, 7 and 12, among which two are less than or equal to 6.

Sample Input 2

4 9
3 3 3 3

Sample Output 2

4

The ball will make a bounce at the coordinates 0, 3, 6, 9 and 12, among which four are less than or equal to 9.


#include <stdio.h>

int main(){
  int N, X;
  scanf("%d %d", &N, &X);
  int i;
  int L, D=0;
  for(i=0; i<N; i++){
    if(i>0){
      D+=L;
      if(D>X) break;
    }
    scanf("%d", &L);
  }
  printf("%d", i);
}


#include<stdio.h>
int main()

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

    }
    int count=0;
    for(int i=0;i<n+1;i++){
        if(ara2[i]<=x){
            count++;
        }
    }
    printf("%d\n",count);
    return 0;
}


#include <stdio.h>
int main(void)
{
  int w,h,x,y;
  double ans=0;
  scanf("%d",&w);
  scanf("%d",&h);
  scanf("%d",&x);
  scanf("%d",&y);
  ans = (double)(w * h) / 2;
  printf("%f",ans);

  if((double)w/2 == x && (double)h/2 == y)
	printf("%d",1);
  else
	printf("%d",0);

  return 0;
}

Score : 300 points

Problem Statement

There is a rectangle in a coordinate plane. The coordinates of the four vertices are (0,0), (W,0), (W,H), and (0,H).
You are given a point (x,y) which is within the rectangle or on its border. We will draw a straight line passing through (x,y) to cut the rectangle into two parts. Find the maximum possible area of the part whose area is not larger than that of the other. Additionally, determine if there are multiple ways to cut the rectangle and achieve that maximum.

Constraints

1 \leq W,H \leq 10^9

0\leq x\leq W

0\leq y\leq H

All values in input are integers.

Input

Input is given from Standard Input in the following format:

W H x y

Output

Print the maximum possible area of the part whose area is not larger than that of the other, followed by 1 if there are multiple ways to cut the rectangle and achieve that maximum, and 0 otherwise.

The area printed will be judged correct when its absolute or relative error is at most 10^{-9}.

Sample Input 1

2 3 1 2

Sample Output 1

3.000000 0

The line x=1 gives the optimal cut, and no other line does.

Sample Input 2

2 2 1 1

Sample Output 2

2.000000 1


#include<stdio.h>
int main()
{
	long long int w, h, x, y;
	scanf("%lld %lld %lld %lld", &w, &h, &x, &y);
	if (2 * x == w && 2 * y == h)
		printf("%.20lf 1\n", (double)(h * w) / 2);
	else
		printf("%.20lf 0\n", (double)(h * w) / 2);
	return 0;
}


#include <stdio.h>

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

    if (k != 1) {
        printf("%d\n", n - k);
    } else {
        printf("0\n");
    }

    return 0;
}

Score : 100 points

Problem Statement

Takahashi is distributing N balls to K persons.

If each person has to receive at least one ball, what is the maximum possible difference in the number of balls received between the person with the most balls and the person with the fewest balls?

Constraints

1 \leq K \leq N \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the maximum possible difference in the number of balls received.

Sample Input 1

3 2

Sample Output 1

1

The only way to distribute three balls to two persons so that each of them receives at least one ball is to give one ball to one person and give two balls to the other person.

Thus, the maximum possible difference in the number of balls received is 1.

Sample Input 2

3 1

Sample Output 2

0

We have no choice but to give three balls to the only person, in which case the difference in the number of balls received is 0.

Sample Input 3

8 5

Sample Output 3

3

For example, if we give 1, 4, 1, 1, 1 balls to the five persons, the number of balls received between the person with the most balls and the person with the fewest balls would be 3, which is the maximum result.


#include <stdio.h>
#include <string.h>
int main(void){
    int x,y,a,count,count2;
    count = count2 = 0;
    scanf("%d %d\n",&x, &y);
    count = x - y;
    if ( y == 1){
        count = 0;
    }
    printf("%d",count);
    return 0;
}


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

void print_array(int arr[][2], int size_array){
  int i;

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

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

int main(void){
	int i, j, k;

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

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

	qsort(pos, n, sizeof(int)*2, asc);
	//print_array(pos, n);

	int dif[2500][2] = {};
	for(i = 0; i < n-1; i++){
		for(j = i+1; j < n; j++){
			for(k = 0; ; k++){
				if(dif[k][0] == 0 && dif[k][1] == 0){
					dif[k][0] = pos[j][0]-pos[i][0];
					dif[k][1] = pos[j][1]-pos[i][1];
					break;
				}
			}
		}
	}

	int ans = 0, cnt = 0;
	for(i = 0; ; i++){
		if(dif[i][0] == 0 && dif[i][1] == 0) break;
		cnt = 0;
		for(j = i+1; ;j++){
			if(dif[j][0] == 0 && dif[j][1] == 0) break;
			if(dif[j][0] == dif[i][0] && dif[j][1] == dif[i][1]) cnt++;
		}
		if(cnt > ans) ans = cnt;
	}

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

	return 0;
}

Score : 300 points

Problem Statement

There are N balls in a two-dimensional plane. The i-th ball is at coordinates (x_i, y_i).

We will collect all of these balls, by choosing two integers p and q such that p \neq 0 or q \neq 0 and then repeating the following operation:

Choose a ball remaining in the plane and collect it. Let (a, b) be the coordinates of this ball. If we collected a ball at coordinates (a - p, b - q) in the previous operation, the cost of this operation is 0. Otherwise, including when this is the first time to do this operation, the cost of this operation is 1.

Find the minimum total cost required to collect all the balls when we optimally choose p and q.

Constraints

1 \leq N \leq 50

|x_i|, |y_i| \leq 10^9

If i \neq j, x_i \neq x_j or y_i \neq y_j.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the minimum total cost required to collect all the balls.

Sample Input 1

2
1 1
2 2

Sample Output 1

1

If we choose p = 1, q = 1, we can collect all the balls at a cost of 1 by collecting them in the order (1, 1), (2, 2).

Sample Input 2

3
1 4
4 6
7 8

Sample Output 2

1

If we choose p = -3, q = -2, we can collect all the balls at a cost of 1 by collecting them in the order (7, 8), (4, 6), (1, 4).

Sample Input 3

4
1 1
1 2
2 1
2 2

Sample Output 3

2


#include<stdio.h>
int main(){
  int i,j,ii,jj,n;
  long p,q;
  int count, mincount;
  long xdiff,ydiff;
  long x[50],y[50],used[50];
  scanf("%d",&n);
  for(i=0;i<n;i++){
    scanf("%ld%ld",&x[i],&y[i]);
  }
  mincount=99;
  for(ii=0;ii<n;ii++) for(jj=ii+1;jj<n;jj++){
    p=x[jj]-x[ii];
    q=y[jj]-y[ii];
    count=0;
    for(i=0;i<n;i++) used[i]=0;
    for(i=0;i<n;i++){
      if(!used[i]){
        used[i]=1;
        count++;
        for(j=0;j<n;j++){
          if(!used[j]){
            xdiff=x[j]-x[i];
            ydiff=y[j]-y[i];
            if(p!=0 && q!=0){
              if(xdiff%p==0 && ydiff%q==0 && xdiff/p==ydiff/q){
                used[j]=1;
              }
            }else if(p==0){
              if(xdiff==0 && ydiff%q==0){
                used[j]=1;
              }
            }else if(q==0){
              if(xdiff%p==0 && ydiff==0){
                used[j]=1;
              }
            }
          }
        }
      }
    }
    if(count<mincount) mincount=count;
  }
  printf("%d\n",mincount);
}


#include <stdio.h>

int main() {
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);
	int sumP = a+b, sumQ = a+c, sumR = b+c;
	if(sumP <= sumQ && sumP <= sumR)
		printf("%d\n", sumP);
	else
		if(sumQ <= sumR)
			printf("%d\n", sumQ);
		else
			printf("%d\n", sumR);

	return 0;
}

Score : 100 points

Problem Statement

There are three airports A, B and C, and flights between each pair of airports in both directions.

A one-way flight between airports A and B takes P hours, a one-way flight between airports B and C takes Q hours, and a one-way flight between airports C and A takes R hours.

Consider a route where we start at one of the airports, fly to another airport and then fly to the other airport.

What is the minimum possible sum of the flight times?

Constraints

1 \leq P,Q,R \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

P Q R

Output

Print the minimum possible sum of the flight times.

Sample Input 1

1 3 4

Sample Output 1

4

The sum of the flight times in the route A \rightarrow B \rightarrow C: 1 + 3 = 4 hours

The sum of the flight times in the route A \rightarrow C \rightarrow C: 4 + 3 = 7 hours

The sum of the flight times in the route B \rightarrow A \rightarrow C: 1 + 4 = 5 hours

The sum of the flight times in the route B \rightarrow C \rightarrow A: 3 + 4 = 7 hours

The sum of the flight times in the route C \rightarrow A \rightarrow B: 4 + 1 = 5 hours

The sum of the flight times in the route C \rightarrow B \rightarrow A: 3 + 1 = 4 hours

The minimum of these is 4 hours.

Sample Input 2

3 2 3

Sample Output 2

5


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

int main (int argc, char const *argv[])
{
    intmax_t P, Q, R;
    intmax_t ans = 0;

    scanf("%jd%jd%jd", &P, &Q, &R);

    ans += P + Q + R;
    ans -= P < Q ? ((Q < R) ? R : Q) : P;

    printf("%jd\n", ans);
}


Score : 200 points

Problem Statement

We have N weights indexed 1 to N. The mass of the weight indexed i is W_i.

We will divide these weights into two groups: the weights with indices not greater than T, and those with indices greater than T, for some integer 1 \leq T < N. Let S_1 be the sum of the masses of the weights in the former group, and S_2 be the sum of the masses of the weights in the latter group.

Consider all possible such divisions and find the minimum possible absolute difference of S_1 and S_2.

Constraints

2 \leq N \leq 100

1 \leq W_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
W_1 W_2 ... W_{N-1} W_N

Output

Print the minimum possible absolute difference of S_1 and S_2.

Sample Input 1

3
1 2 3

Sample Output 1

0

If T = 2, S_1 = 1 + 2 = 3 and S_2 = 3, with the absolute difference of 0.

Sample Input 2

4
1 3 1 1

Sample Output 2

2

If T = 2, S_1 = 1 + 3 = 4 and S_2 = 1 + 1 = 2, with the absolute difference of 2. We cannot have a smaller absolute difference.

Sample Input 3

8
27 23 76 2 3 5 62 52

Sample Output 3

2


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

int main(){
    int n,w[100],a=0,b=0;
    scanf("%d",&n);
    int j=0,k=n-1;
    for(int i=0;i<n;i++){
        scanf("%d",&w[i]);
    }
    while(j<=k){
        if(a<=b){
            a+=w[j];
            j++;
        }
        if(a>b){
            b+=w[k];
            k--;
        }
    }
    printf("%d %d\n",a,b);
    printf("%d\n",abs(a-b));
    return 0;
}


#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#pragma warning( disable:4996 )
#define SWAP(a,b) (a += b,b = a - b,a -= b)
#define MIN(a, b) ((a) < (b) ? (a) : (b))

int a[100001];
int n, m;

int compare(const void *a, const void *b) {
	int an = *(int*)(a);
	int bn = *(int*)(b);
	if (an < bn) {
		return -1;
	}
	else if (an > bn) {
		return 1;

	}
	else {
		return 0;
	}
}

int inarray(int x) {
	void* ans = NULL;
	ans = bsearch(&x, a, m, sizeof(int), compare);
	if (ans != NULL) {
		return 1;
	}
	else {
		return 0;
	}
}

int main(void) {
	/*int p, q, r;
	scanf("%d %d %d", &p, &q, &r);
	int a, b, c;
	a = p + q;
	b = q + r;
	c = r + p;
	printf("%d\n", MIN(a, MIN(b, c)));*/
	/*
	int n;
	int w[101];
	scanf("%d", &n);
	scanf("%d", &w[0]);
	int first = w[0];
	int last = 0;
	for (int i = 1; i < n; i++) {
		scanf("%d", &w[i]);
		last += w[i];
	}

	int min = abs(first - last);
	for (int i = 1; i < n-1; i++) {
		first += w[i];
		last -= w[i];
		int temp = abs(first - last);
		if (min > temp) {
			min = temp;
		}
	}
	printf("%d\n", min);*/
	/*
	scanf("%d %d", &n, &m);
	long long int dp[100001];
	for (int i = 0; i < m; i++) {
		scanf("%d", &a[i]);
		a[i]--;
	}
	if (inarray(0)) {
		dp[0] = 0;
	}
	else {
		dp[0] = 1;
	}
	if (inarray(1)) {
		dp[1] = 0;
	}
	else {
		dp[1] = 1 + dp[0];
	}
	for (int i = 2; i < n; i++) {
		if (inarray(i)) {
			dp[i] = 0;
		}
		else {
			dp[i] = (dp[i - 1] + dp[i - 2])%1000000007;
		}
	}
	printf("%lld\n", dp[n - 1]);*/

	int H, W;
	scanf("%d %d", &H, &W);
	char s[2001][2001];
	for (int i = 0; i < H; i++) {
		scanf("%s", s[i]);
	}
	int vertical[2001] = { 0 };
	int horizontal[2001] = { 0 };

	//int v_count[2001] = { 0 };
	//int h_count[2001] = { 0 };

	int max = 0;
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (s[i][j] == '#') {
				//v_count[j] = 0;
				//h_count[i] = 0;
				vertical[j] = 0;
				horizontal[i] = 0;
			}
			else {
				if (vertical[j] == 0) {
					vertical[j]++;
					while (i+vertical[j]<H) {
						if (s[i + vertical[j]][j] == '#') {
							break;
						}
						else {
							vertical[j]++;
						}
					}
				}
				if (horizontal[i] == 0) {
					horizontal[i]++;
					while (j + horizontal[i] < W) {
						if (s[i][j + horizontal[i]] == '#') {
							break;
						}
						else {
							horizontal[i]++;
						}
					}
				}
				if (max < horizontal[i] + vertical[j] - 1) {
					max = horizontal[i] + vertical[j] - 1;
				}
			}
		}
	}
	printf("%d\n", max);

	return 0;
}

Score : 400 points

Problem Statement

There is a grid with H horizontal rows and W vertical columns, and there are obstacles on some of the squares.

Snuke is going to choose one of the squares not occupied by an obstacle and place a lamp on it.
The lamp placed on the square will emit straight beams of light in four cardinal directions: up, down, left, and right.
In each direction, the beam will continue traveling until it hits a square occupied by an obstacle or it hits the border of the grid. It will light all the squares on the way, including the square on which the lamp is placed, but not the square occupied by an obstacle.

Snuke wants to maximize the number of squares lighted by the lamp.

You are given H strings S_i (1 \leq i \leq H), each of length W. If the j-th character (1 \leq j \leq W) of S_i is #, there is an obstacle on the square at the i-th row from the top and the j-th column from the left; if that character is ., there is no obstacle on that square.

Find the maximum possible number of squares lighted by the lamp.

Constraints

1 \leq H \leq 2,000

1 \leq W \leq 2,000

S_i is a string of length W consisting of # and ..

. occurs at least once in one of the strings S_i (1 \leq i \leq H).

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the maximum possible number of squares lighted by the lamp.

Sample Input 1

4 6
#..#..
.....#
....#.
#.#...

Sample Output 1

8

If Snuke places the lamp on the square at the second row from the top and the second column from the left, it will light the following squares: the first through fifth squares from the left in the second row, and the first through fourth squares from the top in the second column, for a total of eight squares.

Sample Input 2

8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#

Sample Output 2

13


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

int main(){
	long int n, a, b, c, d;
	long int i, j;
	scanf("%ld %ld %ld %ld %ld",&n,&a,&b,&c,&d);
	char s[n];
	scanf("%s",s);
	for(i=0;i<n-1;i++){
		if(s[i]=='#' && s[i+1]=='#'){
			printf("No");
			return 0;
		}
	}
	if(c<d){
		printf("Yes");
		return 0;
	}else{
		for(j=b;j<d-1;j++){
			if(s[j-1]=='.' && s[j]=='.' && s[j+1]=='.'){
				printf("Yes");
				return 0;
			}
		}
	}
	printf("No");
	return 0;
}

Score : 400 points

Problem Statement

There are N squares arranged in a row, numbered 1, 2, ..., N from left to right.
You are given a string S of length N consisting of . and #. If the i-th character of S is #, Square i contains a rock; if the i-th character of S is ., Square i is empty.

In the beginning, Snuke stands on Square A, and Fnuke stands on Square B.

You can repeat the following operation any number of times:

Choose Snuke or Fnuke, and make him jump one or two squares to the right. The destination must be one of the squares, and it must not contain a rock or the other person.

You want to repeat this operation so that Snuke will stand on Square C and Fnuke will stand on Square D.

Determine whether this is possible.

Constraints

4 \leq N \leq 200\ 000

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

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

Square A, B, C and D do not contain a rock.

A, B, C and D are all different.

A < B

A < C

B < D

Input

Input is given from Standard Input in the following format:

N A B C D
S

Output

Print Yes if the objective is achievable, and No if it is not.

Sample Input 1

7 1 3 6 7
.#..#..

Sample Output 1

Yes

The objective is achievable by, for example, moving the two persons as follows. (A and B represent Snuke and Fnuke, respectively.)

A#B.#..

A#.B#..

.#AB#..

.#A.#B.

.#.A#B.

.#.A#.B

.#..#AB

Sample Input 2

7 1 3 7 6
.#..#..

Sample Output 2

No

Sample Input 3

15 1 3 15 13
...#.#...#.#...

Sample Output 3

Yes


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

int main()
{
	int N,A,B,C,D;
	char S[200005];
	int i,j;
	scanf("%d %d %d %d %d",&N,&A,&B,&C,&D);
	for(i=0;i<N;i++) scanf("%c",&S[i]);
	if(C<D)
	{
	// Move B first to D
	  for(i=B;i<D;i++)
	  {
		  if((S[i-1]=='#')&(S[i]=='#'))
		  {
			  printf("No\n");
			  return 0;
		  }
	  }
	// Move A to C
	  for(i=A;i<C;i++)
          {
                  if((S[i-1]=='#')&(S[i]=='#'))
                  {
                          printf("No\n");
                          return 0;
                  }
          }
	  printf("Yes\n");
	  return 0;
	}else{
		// Find a spot where A can pass B
	        for(i=1;i<N-1;i++)
		{
		   if((S[i-1]=='.')&(S[i]=='.')&(S[i+1])=='.')
		   {
			   // if the space is before A
			   if(i<A)
			   {
				   // check if can move A and B there
				   for(j=i;j<C;j++) if((S[j-1]=='#')&(S[j]=='#')) break;
				   printf("Yes\n");
				   return 0;
			   }
			   // if the space is between A and C
			   if((i>=A)&(i<=C))
			   {
				   // check if can move A and B there
                                   for(j=A;j<C;j++) if((S[j-1]=='#')&(S[j]=='#')) break;
				   printf("Yes\n");
				   return 0;
			   }
			   // if the space is after C
                           if(i>C)
                           {
                                   for(j=A;j<i+2;j++) if((S[j-1]=='#')&(S[j]=='#')) break;
                                   printf("Yes\n");
                                   return 0;
                           }
		   }
		}
		printf("No\n");
		return 0;
	   }
	return 0;
}


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

#define MAX 200002
#define max( a, b ) ( a > b? a:b )
#define min( a, b ) ( a < b? a:b )
void show( char *s, int Len ){
		for( int i = 0; i < Len; i++ )printf("%c%c", s[i], " \n"[i == Len-1]);
}

int main(){
		int N, A, B, C, D;scanf("%d%d%d%d%d", &N, &A, &B, &C, &D);
		char s[MAX];scanf("%s", s);
		//show( s, N );
		// A < B, if A can reacher, so do B
		// if A can go to D, B can to D too

		//station = ...
		int ptr = B;
		int end_ptr = D;
		int station = 0;
		while( ptr < end_ptr ){
				int counter = 0;
				while( ptr < end_ptr && s[ptr] == '.' ){
						counter++;
						ptr++;
				}
				ptr++;
				station = max( counter, station );
		}
		//if station == 3
		if( station >= 3 ){
				int start = min(A, B);
				int end = max(C, D)-1;
				for( int i = start; i < end; i++ )
						if( s[i] == '#' && s[i+1] == '#' ){
								puts("No");
								return 0;
						}
		}
		else{
				if( D > C ){
						for( int i = B; i < D; i++ ){
								if( s[i] == '#' && s[i+1] == '#' ){
										puts("No");
										return 0;
								}
						}
				}
				else{// C > D
						if( s[D-1-1] == '#' || s[D-1+1] == '#' ){
								puts("No");
								return 0;
						}
						for( int i = A; i < C; i++ ){
								if( s[i] == '#' && s[i+1] == '#' ){
										puts("No");
										return 0;
								}
						}
				}
		}
		puts("Yes");

}


#include<stdio.h>

int main()
{
	int q;
	scanf("%d",&q);
	for(int i = 0;i<q;i++){
		int x,d,n;
		scanf("%d %d %d",&x,&d,&n);
		long long int ans = x;
		n--;
		while(n){
			x+=d;
			ans*=x;
			ans = ans % 1000003;
			n--;
		}
		printf("%ld\n",ans);
	}
}

Score : 600 points

Problem Statement

Consider the following arithmetic progression with n terms:

x, x + d, x + 2d, \ldots, x + (n-1)d

What is the product of all terms in this sequence?
Compute the answer modulo 1\ 000\ 003.

You are given Q queries of this form.
In the i-th query, compute the answer in case x = x_i, d = d_i, n = n_i.

Constraints

1 \leq Q \leq 10^5

0 \leq x_i, d_i \leq 1\ 000\ 002

1 \leq n_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

Q
x_1 d_1 n_1
:
x_Q d_Q n_Q

Output

Print Q lines.

In the i-th line, print the answer for the i-th query.

Sample Input 1

2
7 2 4
12345 67890 2019

Sample Output 1

9009
916936

For the first query, the answer is 7 \times 9 \times 11 \times 13 = 9009.
Don't forget to compute the answer modulo 1\ 000\ 003.


#include<stdio.h>

int main () {
	int x, y;
	scanf("%d %d", &x, &y);
	printf("%d\n", ((x*3)+y)/2);
	return 0;
}

Score : 100 points

Problem Statement

We have A apples and P pieces of apple.

We can cut an apple into three pieces of apple, and make one apple pie by simmering two pieces of apple in a pan.

Find the maximum number of apple pies we can make with what we have now.

Constraints

All values in input are integers.

0 \leq A, P \leq 100

Input

Input is given from Standard Input in the following format:

A P

Output

Print the maximum number of apple pies we can make with what we have.

Sample Input 1

1 3

Sample Output 1

3

We can first make one apple pie by simmering two of the three pieces of apple. Then, we can make two more by simmering the remaining piece and three more pieces obtained by cutting the whole apple.

Sample Input 2

0 1

Sample Output 2

0

We cannot make an apple pie in this case, unfortunately.

Sample Input 3

32 21

Sample Output 3

58


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


Score : 300 points

Problem Statement

We have N switches with "on" and "off" state, and M bulbs. The switches are numbered 1 to N, and the bulbs are numbered 1 to M.

Bulb i is connected to k_i switches: Switch s_{i1}, s_{i2}, ..., and s_{ik_i}. It is lighted when the number of switches that are "on" among these switches is congruent to p_i modulo 2.

How many combinations of "on" and "off" states of the switches light all the bulbs?

Constraints

1 \leq N, M \leq 10

1 \leq k_i \leq N

1 \leq s_{ij} \leq N

s_{ia} \neq s_{ib} (a \neq b)

p_i is 0 or 1.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M
k_1 s_{11} s_{12} ... s_{1k_1}
:
k_M s_{M1} s_{M2} ... s_{Mk_M}
p_1 p_2 ... p_M

Output

Print the number of combinations of "on" and "off" states of the switches that light all the bulbs.

Sample Input 1

2 2
2 1 2
1 2
0 1

Sample Output 1

1

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

There are four possible combinations of states of (Switch 1, Switch 2): (on, on), (on, off), (off, on) and (off, off). Among them, only (on, on) lights all the bulbs, so we should print 1.

Sample Input 2

2 3
2 1 2
1 1
1 2
0 0 1

Sample Output 2

0

Bulb 1 is lighted when there is an even number of switches that are "on" among the following: Switch 1 and 2.

Bulb 2 is lighted when there is an even number of switches that are "on" among the following: Switch 1.

Bulb 3 is lighted when there is an odd number of switches that are "on" among the following: Switch 2.

Switch 1 has to be "off" to light Bulb 2 and Switch 2 has to be "on" to light Bulb 3, but then Bulb 1 will not be lighted. Thus, there are no combinations of states of the switches that light all the bulbs, so we should print 0.

Sample Input 3

5 2
3 1 2 5
2 2 3
1 0

Sample Output 3

8


#include<stdio.h>
#include<string.h>
int main()
{
    int a,b;
    scanf("%d %d",&a,&b);
    if(a<6)printf("0\n");
    else if(a>=6 && a<=12)printf("%d\n",b/2);
    else printf("%d\n",b);
}

Score : 100 points

Problem Statement

Takahashi, who is A years old, is riding a Ferris wheel.

It costs B yen (B is an even number) to ride the Ferris wheel if you are 13 years old or older, but children between 6 and 12 years old (inclusive) can ride it for half the cost, and children who are 5 years old or younger are free of charge. (Yen is the currency of Japan.)

Find the cost of the Ferris wheel for Takahashi.

Constraints

0 ≤ A ≤ 100

2 ≤ B ≤ 1000

B is an even number.

Input

Input is given from Standard Input in the following format:

A B

Output

Print the cost of the Ferris wheel for Takahashi.

Sample Input 1

30 100

Sample Output 1

100

Takahashi is 30 years old now, and the cost of the Ferris wheel is 100 yen.

Sample Input 2

12 100

Sample Output 2

50

Takahashi is 12 years old, and the cost of the Ferris wheel is the half of 100 yen, that is, 50 yen.

Sample Input 3

0 100

Sample Output 3

0

Takahashi is 0 years old, and he can ride the Ferris wheel for free.


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

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

    if((0>a)||(a>100)||(2>b)||(b>1000)||((b>=2)&&(1000>=b)&&(b%2==1))){
        printf("error!");
    }

    //return 0;

    if(a>=13){
        printf("entrance fee is %d dollar",b);
    }else if((12>=a)&&(a>=6)){
        printf("entrance fee is %d dollar",b/2);
    }else{
        printf("entrance fee is 0 dollar");
    }

}


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

int main(){
  int a,b;

  scanf("%d%d",&a,&b);
  if(a>12){
    printf("%d",b);
  }else if(13>a&&a>5){
    printf("%d",b/2);
  }else if(6>a){
    printf("%d",0);
  }

  return 0;
}


Score : 200 points

Problem Statement

The development of algae in a pond is as follows.

Let the total weight of the algae at the beginning of the year i be x_i gram. For i≥2000, the following formula holds:

x_{i+1} = rx_i - D

You are given r, D and x_{2000}. Calculate x_{2001}, ..., x_{2010} and print them in order.

Constraints

2 ≤ r ≤ 5

1 ≤ D ≤ 100

D < x_{2000} ≤ 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r D x_{2000}

Output

Print 10 lines. The i-th line (1 ≤ i ≤ 10) should contain x_{2000+i} as an integer.

Sample Input 1

2 10 20

Sample Output 1

30
50
90
170
330
650
1290
2570
5130
10250

For example, x_{2001} = rx_{2000} - D = 2 \times 20 - 10 = 30 and x_{2002} = rx_{2001} - D = 2 \times 30 - 10 = 50.

Sample Input 2

4 40 60

Sample Output 2

200
760
3000
11960
47800
191160
764600
3058360
12233400
48933560


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


#include <stdio.h>

int main(void)
{
	int r,d,x;

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

	int ans[10]={};

	ans[0] = x;

	int i;

	for(i = 1;i < 10; i++){
		ans[i] = r*ans[i-1] - d;
	}

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

	return 0;

}


#include<stdio.h>
int main()
{
int a,b;
long int c;
  scanf("%d %d %ld",&a,&b,&c);
  for(int i=0;i<10;i++)
  {
    c=a*c-b;
   printf("%ld",c);
     printf("\n");
  }
  return 0;
}


#include<stdio.h>
int main(void){
	int N,M;
	scanf("%d%d",&N,&M);
	int pass[N];
	int a;
	for(a=0;a<N;a++){
		pass[a]=0;
	}
	int l[N];
	int r[N];
	int b;
	int cnt=0;
	int min=N+1;
	int minplus,minl,minr;
	for(a=0;a<M;a++){
		scanf("%d%d",&l[a],&r[a]);
		if(r[a]-l[a]<=min){
			min=r[a]-l[a];
			minplus=r[a]+l[a];
		}
	}
	minr=(min+minplus)/2;
	minl=minplus-minr;
	for(a=0;a<M;a++){
		if((l[a]<=minr)||(r[a]>=minl)){
			for(b=l[a]-1;b<r[a];b++){
				pass[b]++;
				if(pass[b]==M){
					cnt++;
				}
			}
		}
	}
	printf("%d",cnt);
return 0;
}

Score : 300 points

Problem Statement

We have N ID cards, and there are M gates.

We can pass the i-th gate if we have one of the following ID cards: the L_i-th, (L_i+1)-th, ..., and R_i-th ID cards.

How many of the ID cards allow us to pass all the gates alone?

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq L_i \leq R_i \leq N

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1
L_2 R_2
\vdots
L_M R_M

Output

Print the number of ID cards that allow us to pass all the gates alone.

Sample Input 1

4 2
1 3
2 4

Sample Output 1

2

Two ID cards allow us to pass all the gates alone, as follows:

The first ID card does not allow us to pass the second gate.

The second ID card allows us to pass all the gates.

The third ID card allows us to pass all the gates.

The fourth ID card does not allow us to pass the first gate.

Sample Input 2

10 3
3 6
5 7
6 9

Sample Output 2

1

Sample Input 3

100000 1
1 100000

Sample Output 3

100000


#include<stdio.h>
int main(void){
	int n, m;
  int min, max;

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

  int r[m][2];

  for(int i = 0; i < m; i++){
  	for(int j = 0; j < 2; j++){
    	scanf("%d", &r[i][j]);
    }
  }

    for(int i = 0; i < m; i++){
  		if(i == 0){
        	min = r[i][1];
          	max = r[i][0];
        }
      	else{
        	if(min > r[i][1]){
            	min = r[i][1];
            }
          	if(max < r[i][0]){
            	max = r[i][0];
            }
        }
    }

  printf("%d\n", (min - max) + 1);
}


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

int main()
{
	int n, m;
	int l[100001];
	int r[100001];
	int left, right;

	scanf("%d%d", &n, &m);
	for (int i = 1;i <= m;i++)
	{
		scanf("%d%d", &l[i], &r[i]);
	}
	left = l[1];
	right = r[1];
	for (int i = 2;i <= m;i++)
	{
		if (l[i] > l[i - 1])left = l[i];
		if (r[i] < l[i - 1])right = r[i];

	}
	printf("%d\n",right - left + 1);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
int asc(const void* x, const void* y)
{
	return *(int*)x - *(int*)y;
}
int b[100005], c[100005];
int asc2(const void* x, const void* y)
{
	return c[*(int*)y] - c[*(int*)x];
}
int main()
{
	int n, m;
	scanf("%d %d", &n, &m);
	int i;
	int a[100005];
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for (i = 0; i < m; i++)
		scanf("%d %d", &b[i], &c[i]);
	qsort(a, n, sizeof(int), asc);
	int d[100005];
	for (i = 0; i < m; i++)
		d[i] = i;
	qsort(d, m, sizeof(int), asc2);
	int j = 0;
	for (i = 0; i < n; i++)
	{
		if (a[i] < c[d[j]])
		{
			a[i] = c[d[j]];
			b[d[j]]--;
			if (b[d[j]] == 0)
				j++;
			if (j == m)
				break;
		}
	}
	long long int ans = 0;
	for (i = 0; i < n; i++)
		ans += (long long int)a[i];
	printf("%lld\n", ans);
	return 0;
}

Score : 400 points

Problem Statement

You have N cards. On the i-th card, an integer A_i is written.

For each j = 1, 2, ..., M in this order, you will perform the following operation once:

Operation: Choose at most B_j cards (possibly zero). Replace the integer written on each chosen card with C_j.

Find the maximum possible sum of the integers written on the N cards after the M operations.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i, C_i \leq 10^9

1 \leq B_i \leq N

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N
B_1 C_1
B_2 C_2
\vdots
B_M C_M

Output

Print the maximum possible sum of the integers written on the N cards after the M operations.

Sample Input 1

3 2
5 1 4
2 3
1 5

Sample Output 1

14

By replacing the integer on the second card with 5, the sum of the integers written on the three cards becomes 5 + 5 + 4 = 14, which is the maximum result.

Sample Input 2

10 3
1 8 5 7 100 4 52 33 13 5
3 10
4 30
1 4

Sample Output 2

338

Sample Input 3

3 2
100 100 100
3 99
3 99

Sample Output 3

300

Sample Input 4

11 3
1 1 1 1 1 1 1 1 1 1 1
3 1000000000
4 1000000000
3 1000000000

Sample Output 4

10000000001

The output may not fit into a 32-bit integer type.


#include <stdio.h>
int main(void){
	int a,b;
	scanf("%d%d",&a,&b);
	char s[a+2];
	scanf("%s",s);
	s[b-1]+=32;
	printf("%s",s);
	return 0;
}

Score : 100 points

Problem Statement

You are given a string S of length N consisting of A, B and C, and an integer K which is between 1 and N (inclusive).
Print the string S after lowercasing the K-th character in it.

Constraints

1 ≤ N ≤ 50

1 ≤ K ≤ N

S is a string of length N consisting of A, B and C.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the string S after lowercasing the K-th character in it.

Sample Input 1

3 1
ABC

Sample Output 1

aBC

Sample Input 2

4 3
CABA

Sample Output 2

CAbA


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

int main(){
    int N,K;
    int a;
    double b=0;
    scanf("%d %d",&N,&K);
    if(K!=2){
        for(int n=1;n<=N;n++){
            a=0;
            for(int i=1;i>0;i++){
                if(K<=n){
                    break;
                }
                if(a+1<=log2(K/n)){
                    a=a+1;
                }else{
                    a=a+1;
                    break;
                }
            }
            double p=pow(2,a);
            b=b+1/p;
        }
    }else{
        b=N-0.5;
    }
    printf("%.9lf",b/N);
    return 0;
}


#include <stdio.h>

int main(void)
{
    int string;

    scanf("%d", &string);

    if (string / 100 > 12)
    {
        if (string % 100 <= 12 && string % 100 > 0)
        {
            printf("YYMM\n");
        }
        else
        {
            printf("NA\n");
        }
    }
    else if (string / 100 <= 12 && string / 100 > 0)
    {
        if (string % 100 <= 12 && string % 100 > 0)
        {
            printf("AMBIGUOUS\n");
        }
        else
        {
            printf("MMYY\n");
        }
    }
}

Score : 200 points

Problem Statement

You have a digit sequence S of length 4. You are wondering which of the following formats S is in:

YYMM format: the last two digits of the year and the two-digit representation of the month (example: 01 for January), concatenated in this order

MMYY format: the two-digit representation of the month and the last two digits of the year, concatenated in this order

If S is valid in only YYMM format, print YYMM; if S is valid in only MMYY format, print MMYY; if S is valid in both formats, print AMBIGUOUS; if S is valid in neither format, print NA.

Constraints

S is a digit sequence of length 4.

Input

Input is given from Standard Input in the following format:

S

Output

Print the specified string: YYMM, MMYY, AMBIGUOUS or NA.

Sample Input 1

1905

Sample Output 1

YYMM

May XX19 is a valid date, but 19 is not valid as a month. Thus, this string is only valid in YYMM format.

Sample Input 2

0112

Sample Output 2

AMBIGUOUS

Both December XX01 and January XX12 are valid dates. Thus, this string is valid in both formats.

Sample Input 3

1700

Sample Output 3

NA

Neither 0 nor 17 is valid as a month. Thus, this string is valid in neither format.


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

int main(){
	int N,K;
	scanf("%d%d",&N,&K);
	int ni[N],i=0,k,work =1;

	for(k=N;k>0;k--){

		while(k*work < K){
			i++;
			work = work * 2;
		}

		ni[k-1] = i;
	}

	double sum=1;
	for(k=1;k<N;k++){
		sum = sum + pow(2,ni[0]-ni[k]);
	}
	printf("%f",sum / (pow(2,ni[0])*N));
	return 0;
}

Score : 300 points

Problem Statement

Snuke has a fair N-sided die that shows the integers from 1 to N with equal probability and a fair coin. He will play the following game with them:

Throw the die. The current score is the result of the die.

As long as the score is between 1 and K-1 (inclusive), keep flipping the coin. The score is doubled each time the coin lands heads up, and the score becomes 0 if the coin lands tails up.

The game ends when the score becomes 0 or becomes K or above. Snuke wins if the score is K or above, and loses if the score is 0.

You are given N and K. Find the probability that Snuke wins the game.

Constraints

1 ≤ N ≤ 10^5

1 ≤ K ≤ 10^5

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the probability that Snuke wins the game. The output is considered correct when the absolute or relative error is at most 10^{-9}.

Sample Input 1

3 10

Sample Output 1

0.145833333333

If the die shows 1, Snuke needs to get four consecutive heads from four coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^4 = \frac{1}{48}.

If the die shows 2, Snuke needs to get three consecutive heads from three coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^3 = \frac{1}{24}.

If the die shows 3, Snuke needs to get two consecutive heads from two coin flips to obtain a score of 10 or above. The probability of this happening is \frac{1}{3} \times (\frac{1}{2})^2 = \frac{1}{12}.

Thus, the probability that Snuke wins is \frac{1}{48} + \frac{1}{24} + \frac{1}{12} = \frac{7}{48} \simeq 0.1458333333.

Sample Input 2

100000 5

Sample Output 2

0.999973749998


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

int main() {
	int n, m;
	int a, b, c;
	bool N[100000];
	scanf("%d %d", &n, &m);
	int i;
	int count = 0;
	for (i = 0; i < n; i++) N[i] = false;
	for (i = 0; i < m; i++) {
		scanf("%d %d %d", &a, &b, &c);
		if (!(N[a] && N[b])) {
			count++;
		}
		N[a] = true;
		N[b] = true;
	}
	printf("%d\n", n-count);

	return 0;
}

Score : 500 points

Problem Statement

There are N cards placed face down in a row. On each card, an integer 1 or 2 is written.

Let A_i be the integer written on the i-th card.

Your objective is to guess A_1, A_2, ..., A_N correctly.

You know the following facts:

For each i = 1, 2, ..., M, the value A_{X_i} + A_{Y_i} + Z_i is an even number.

You are a magician and can use the following magic any number of times:

Magic: Choose one card and know the integer A_i written on it. The cost of using this magic is 1.

What is the minimum cost required to determine all of A_1, A_2, ..., A_N?

It is guaranteed that there is no contradiction in given input.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq X_i < Y_i \leq N

1 \leq Z_i \leq 100

The pairs (X_i, Y_i) are distinct.

There is no contradiction in input. (That is, there exist integers A_1, A_2, ..., A_N that satisfy the conditions.)

Input

Input is given from Standard Input in the following format:

N M
X_1 Y_1 Z_1
X_2 Y_2 Z_2
\vdots
X_M Y_M Z_M

Output

Print the minimum total cost required to determine all of A_1, A_2, ..., A_N.

Sample Input 1

3 1
1 2 1

Sample Output 1

2

You can determine all of A_1, A_2, A_3 by using the magic for the first and third cards.

Sample Input 2

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

Sample Output 2

2

Sample Input 3

100000 1
1 100000 100

Sample Output 3

99999


#include<stdio.h>

int main(void)
{
  int N;
  int ans=0;
  //int j=0;

  scanf("%d",&N); //1<=N<=10e12

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

Score : 500 points

Problem Statement

Snuke received a positive integer N from Takahashi.
A positive integer m is called a favorite number when the following condition is satisfied:

The quotient and remainder of N divided by m are equal, that is, \lfloor \frac{N}{m} \rfloor = N \bmod m holds.

Find all favorite numbers and print the sum of those.

Constraints

All values in input are integers.

1 \leq N \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

8

Sample Output 1

10

There are two favorite numbers: 3 and 7. Print the sum of these, 10.

Sample Input 2

1000000000000

Sample Output 2

2499686339916

Watch out for overflow.


#include <stdio.h>

int main(void)
{
	long long int n,m;

	long long int sum = 0;

	scanf("%lld",&n);

	for(m = n - 1;m = 2;m--){
		if(n / m == n % m){
			sum = sum + m;
		}

	}

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

	return 0;

}


//set many funcs template
//Ver.20181228
#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);}
int chsortfncsj(const void *a,const void *b){if(*(char *)a>*(char *)b){return 1;}if(*(char *)a==*(char *)b){return 0;}return -1;}
int chsortfnckj(const void *a,const void *b){if(*(char *)a<*(char *)b){return 1;}if(*(char *)a==*(char *)b){return 0;}return -1;}

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

void prarr(int arr[],int n){
  int i;
  for(i=0;i<n;i++){
    if(i){printf(" ");}
    printf("%d",arr[i]);
  }
  printf("\n");
  return;
}

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){
  long long i,j,n,m,k,a,b,c,h,w,r=0,l,t;
  scanf("%lld",&n);
  for(i=1;i<=1234567;i++){
    if(n%i==0){
      h=i;
      w=n/i;
      if(h>=w){break;}
      r+=(w-1);
    }
  }
  printf("%lld\n",r);
  return 0;
}


#include <stdio.h>

int main()
{
	int i, N, V[21], C[21];
	scanf("%d", &N);
	for (i = 1; i <= N; i++) scanf("%d", &(V[i]));
	for (i = 1; i <= N; i++) scanf("%d", &(C[i]));

	int sum = 0;
	for (i = 1; i <= N; i++) if (V[i] > C[i]) sum += V[i] - C[i];
	printf("%d\n", sum);
	fflush(stdout);
	return 0;
}

Score : 200 points

Problem Statement

There are N gems. The value of the i-th gem is V_i.

You will choose some of these gems, possibly all or none, and get them.

However, you need to pay a cost of C_i to get the i-th gem.

Let X be the sum of the values of the gems obtained, and Y be the sum of the costs paid.

Find the maximum possible value of X-Y.

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq C_i, V_i \leq 50

Input

Input is given from Standard Input in the following format:

N
V_1 V_2 ... V_N
C_1 C_2 ... C_N

Output

Print the maximum possible value of X-Y.

Sample Input 1

3
10 2 5
6 3 4

Sample Output 1

5

If we choose the first and third gems, X = 10 + 5 = 15 and Y = 6 + 4 = 10.
We have X-Y = 5 here, which is the maximum possible value.

Sample Input 2

4
13 21 6 19
11 30 6 15

Sample Output 2

6

Sample Input 3

1
1
50

Sample Output 3

0


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

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 llgcd(long long a,long long b){long long c;while(b!=0){c=a%b;a=b;b=c;}return a;}
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;}

bool isprime(long long x){
  long long i;
  if(x==1){return false;}
  if(x==2){return true;}
  if(x%2==0){return false;}
  for(i=3;i*i<=x;i+=2){
    if(x%i==0){return false;}
  }
  return true;
}

long long calcmod(long long x,long long m){
  if(x>=0){return x%m;}
  return (m-((-x)%m))%m;
}

long long n,a[16384];

bool judge(long long p){
  if(!isprime(p)){return false;}
  long long i,b[16384];
  for(i=0;i<=n;i++){b[i]=calcmod(a[i],p);}
  for(i=n;i>=p-1;i--){
    b[i-(p-1)]+=p;
    b[i-(p-1)]-=b[i];
    b[i-(p-1)]%=p;
  }
  for(i=p-2;i>=0;i--){
    if((b[i]%p)!=0){return false;}
  }
  return true;
}

int main(){
  long long i,g;
  long long res[16384],rp=0,t;
  scanf("%lld",&n);
  for(i=n;i>=0;i--){scanf("%lld",&a[i]);}
  g=llzt(0,a[n]);
  for(i=n-1;i>=0;i--){
    g=llgcd(g,llzt(0,a[i]));
  }
  for(i=n;i>=0;i--){a[i]/=g;}
  for(i=2;i*i<=g;i++){
    if((g%i)!=0){continue;}
    if(isprime(i)){res[rp]=i;rp++;}
    while((g%i)==0){g/=i;}
  }
  if(isprime(g)){res[rp]=g;rp++;}
  t=a[0];
  for(i=2;i*i<=t;i++){
    if((a[0]%i)!=0){continue;}
    if(judge(i)){res[rp]=i;rp++;}
    while((t%i)==0){t/=i;}
  }
  if(judge(t)){res[rp]=t;rp++;}
  qsort(res,rp,sizeof(long long),llsortfncsj);
  res[rp]=-1;
  for(i=0;i<rp;i++){
    if(res[i]==res[i+1]){continue;}
    printf("%lld\n",res[i]);
  }
  return 0;
}

Score : 800 points

Problem Statement

You are given a polynomial of degree N with integer coefficients: f(x)=a_Nx^N+a_{N-1}x^{N-1}+...+a_0. Find all prime numbers p that divide f(x) for every integer x.

Constraints

0 \leq N \leq 10^4

|a_i| \leq 10^9(0\leq i\leq N)

a_N \neq 0

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_N
:
a_0

Output

Print all prime numbers p that divide f(x) for every integer x, in ascending order.

Sample Input 1

2
7
-7
14

Sample Output 1

2
7

2 and 7 divide, for example, f(1)=14 and f(2)=28.

Sample Input 2

3
1
4
1
5

Sample Output 2

There may be no integers that satisfy the condition.

Sample Input 3

0
998244353

Sample Output 3

998244353


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MOD 1000000007
#define MAX(a,b) (a>b?a:b)
#define MAX3(a,b,c) MAX(a,MAX(b,c))
#define MIN(a,b) (a<b?a:b)
#define MIN3(a,b,c) MIN(a, MIN(b,c))
typedef long long lli;
int cmp(const void *a, const void *b) {
    if((*(lli*)b - *(lli*)a) > 0) {
        return 1;
    } else if((*(lli*)b - *(lli*)a) < 0) {
        return -1;
    }
    return 0;
}

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

Score : 100 points

Problem Statement

There are three houses on a number line: House 1, 2 and 3, with coordinates A, B and C, respectively.
Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Constraints

0\leq A,B,C\leq 100

A, B and C are distinct integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print Yes if we pass the coordinate of House 3 on the straight way from House 1 to House 2 without making a detour, and print No otherwise.

Sample Input 1

3 8 5

Sample Output 1

Yes

We pass the coordinate 5 on the straight way from the house at coordinate 3 to the house at coordinate 8.

Sample Input 2

7 3 1

Sample Output 2

No

Sample Input 3

10 2 4

Sample Output 3

Yes

Sample Input 4

31 41 59

Sample Output 4

No


#include<stdio.h>

int main(){
    int a,b,c;
    scanf("%d %d %d",&a,&b,&c);
    if(a<b){
        if(a<c && c<b){
            printf("YES\n");
        }else{
            printf("NO\n");
        }
    }else if(a>b){
        if(a>c && c>b){
            printf("YES\n");
        }else{
            printf("NO\n");
        }
    }
}


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


#include <stdio.h>
#define MIN(a,b) ((a<b)?(a):(b))
#define MAX(a,b) ((a>b)?(a):(b))

int main() {
    int n, k, sk, i;
    char s[20];
    scanf("%d %s %d", &n, s, &k);

    sk = s[k-1];
    for (i = 0; s[i]; i++) {
        if (s[i] != sk) {
            s[i] = '*';
        }
    }

    printf("%s", s);

    return 0;
}

Score : 200 points

Problem Statement

You are given a string S of length N consisting of lowercase English letters, and an integer K.
Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Constraints

1 \leq K \leq N\leq 10

S is a string of length N consisting of lowercase English letters.

N and K are integers.

Input

Input is given from Standard Input in the following format:

N
S
K

Output

Print the string obtained by replacing every character in S that differs from the K-th character of S, with *.

Sample Input 1

5
error
2

Sample Output 1

*rr*r

The second character of S is r. When we replace every character in error that differs from r with *, we get the string *rr*r.

Sample Input 2

6
eleven
5

Sample Output 2

e*e*e*

Sample Input 3

9
education
7

Sample Output 3

******i**


#include <stdio.h>

int
main(int argc, char *argv[])
{
  int n; char s[400000]; int k = -1;
  scanf("%d%s", &n, s);
  for(int i = 0; i < n; i++){
    if(s[i] == '#'){
      k = i;
      break;
    }
    else if(i == n - 1){
      printf("%d\n", 0);
    }
  }

  int white = 0; int black = 0;

  if(k >= 0 && k < n - 1){
    for(int i = k + 1; i < n; i++){
      if(s[i] == '#'){
	black++;
      }
      else{
	white++;
      }
    }

    if(black + 1 < white){
      printf("%d\n", black + 1);
    }
    else{
      printf("%d\n", white);
    }
  }

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

  return 0;
}

Score : 300 points

Problem Statement

There are N stones arranged in a row. Every stone is painted white or black.
A string S represents the color of the stones. The i-th stone from the left is white if the i-th character of S is ., and the stone is black if the character is #.

Takahashi wants to change the colors of some stones to black or white so that there will be no white stone immediately to the right of a black stone.
Find the minimum number of stones that needs to be recolored.

Constraints

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

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

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the minimum number of stones that needs to be recolored.

Sample Input 1

3
#.#

Sample Output 1

1

It is enough to change the color of the first stone to white.

Sample Input 2

5
#.##.

Sample Output 2

2

Sample Input 3

9
.........

Sample Output 3

0


#include <stdio.h>
int main(void){
    // Your code here!
    int a,b;
    int tmp=0,i;

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

    for(i=0;i<2;i++){
    if(a>b)
    {tmp=tmp+a;
    a--;
    }
    else
    {tmp=tmp+b;
    b--;
    }

    }

    printf("%d",tmp);

    return 0;
}

Score : 100 points

Problem Statement

There are two buttons, one of size A and one of size B.

When you press a button of size X, you get X coins and the size of that button decreases by 1.

You will press a button twice. Here, you can press the same button twice, or press both buttons once.

At most how many coins can you get?

Constraints

All values in input are integers.

3 \leq A, B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

Print the maximum number of coins you can get.

Sample Input 1

5 3

Sample Output 1

9

You can get 5 + 4 = 9 coins by pressing the button of size 5 twice, and this is the maximum result.

Sample Input 2

3 4

Sample Output 2

7

Sample Input 3

6 6

Sample Output 3

12


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


#include<stdio.h>

int main(void) {

  int A = 0;
  int B = 0;
  int i = 2;
  int r = 0;

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

  while(i--!=0) r += (A>=B)? A-- : B--;
  printf("%d", r);
  return 0;

}


#include<stdio.h>

void pressbutten(int A,int B) {
	int sum=0;
	if (A >= B) {
		sum += A;
		A--;
		if (A > B) {
			sum += A;

		}
		else
			sum += B;
	}
	else {

		sum += B;
		B--;
		if (B > A) {
			sum += B;

		}
		else
			sum += A;
	}

	printf("%d",sum);
}

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

	return 0;
}


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


Score : 200 points

Problem Statement

There are N mountains ranging from east to west, and an ocean to the west.

At the top of each mountain, there is an inn. You have decided to choose where to stay from these inns.

The height of the i-th mountain from the west is H_i.

You can certainly see the ocean from the inn at the top of the westmost mountain.

For the inn at the top of the i-th mountain from the west (i = 2, 3, ..., N), you can see the ocean if and only if H_1 \leq H_i, H_2 \leq H_i, ..., and H_{i-1} \leq H_i.

From how many of these N inns can you see the ocean?

Constraints

All values in input are integers.

1 \leq N \leq 20

1 \leq H_i \leq 100

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

Print the number of inns from which you can see the ocean.

Sample Input 1

4
6 5 6 8

Sample Output 1

3

You can see the ocean from the first, third and fourth inns from the west.

Sample Input 2

5
4 5 3 5 4

Sample Output 2

3

Sample Input 3

5
9 5 6 8 4

Sample Output 3

1


#include<stdio.h>
int main(void){
  int N,i,max,sum=1;
  int H[20];
  scanf("%d",&N);
  for(i=0;i<N;i++){
    scanf("%d",&H[i]);
  }
  max=H[0];
  for(i=1;i<N;i++){
    if(max<=H[i]){
      sum++;
      max=H[i];
    }
  }
  printf("%d\n",sum);
  return 0;
}


#include <stdio.h>

int main()
{
    int h;
    int n[20];
    int i;
    int t=0;
    scanf("%d",&h);
    for(i=0;i<h;i++){
        scanf("%d",&n[i]);
    }
    for(i=0;i<h;i++){
        if(n[i]>t)
        t++;
    }
    printf("%d",t);
    return 0;
}


Score : 300 points

Problem Statement

N tiles are arranged in a row from left to right. The initial color of each tile is represented by a string S of length N.

The i-th tile from the left is painted black if the i-th character of S is 0, and painted white if that character is 1.

You want to repaint some of the tiles black or white, so that any two adjacent tiles have different colors.

At least how many tiles need to be repainted to satisfy the condition?

Constraints

1 \leq |S| \leq 10^5

S_i is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum number of tiles that need to be repainted to satisfy the condition.

Sample Input 1

000

Sample Output 1

1

The condition can be satisfied by repainting the middle tile white.

Sample Input 2

10010010

Sample Output 2

3

Sample Input 3

0

Sample Output 3

0


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

int main()
{
    int temp, flag, number;
    int arr[6];
    for (int i = 0; i < 6;i++)
    {
        scanf("%d", &number);
        arr[i] = number;
    }

    int k = arr[6];

    for (int i = 0; i < 6; i++)
    {
        if (0 < arr[i] && arr[i] <= 123)
        {
            temp = arr[i-1] - arr[i];
            for (int j = i; j < 6; j++)
                if (temp < k)
                {
                    flag = 1;
                } else {
                    flag = 0;
                    break;
                }
        } else {
            flag = 0;
            break;
        }
    }

    if(flag == 1)
        printf("Yay!");
    else
        printf(":(");

    return 0;
}

Score: 100 points

Problem Statement

In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively.

Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k.

Determine if there exists a pair of antennas that cannot communicate directly.

Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p.

Constraints

a, b, c, d, e and k are integers between 0 and 123 (inclusive).

a < b < c < d < e

Input

Input is given from Standard Input in the following format:

a
b
c
d
e
k

Output

Print :( if there exists a pair of antennas that cannot communicate directly, and print Yay! if there is no such pair.

Sample Input 1

1
2
4
8
9
15

Sample Output 1

Yay!

In this case, there is no pair of antennas that cannot communicate directly, because:

the distance between A and B is 2 - 1 = 1

the distance between A and C is 4 - 1 = 3

the distance between A and D is 8 - 1 = 7

the distance between A and E is 9 - 1 = 8

the distance between B and C is 4 - 2 = 2

the distance between B and D is 8 - 2 = 6

the distance between B and E is 9 - 2 = 7

the distance between C and D is 8 - 4 = 4

the distance between C and E is 9 - 4 = 5

the distance between D and E is 9 - 8 = 1

and none of them is greater than 15. Thus, the correct output is Yay!.

Sample Input 2

15
18
26
35
36
18

Sample Output 2

:(

In this case, the distance between antennas A and D is 35 - 15 = 20 and exceeds 18, so they cannot communicate directly.
Thus, the correct output is :(.


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

#define REP(i,n) for(i=0;i<n;i++)

int iin() {
    int i;
    scanf("%d", &i);
    return i;
}

long long llin() {
    long long i;
    scanf("%lld", &i);
    return i;
}

double din() {
    double i;
    scanf("%lf", &i);
    return i;
}

char cin() {
    char c;
    scanf("%c", &c);
    return c;
}

void stin(char *s) {
    scanf("%s", s);
}

void iout(int i) {
    printf("%d\n", i);
}

void llout(long long i) {
    printf("%lld\n", i);
}

void dout(double i) {
    printf("%lf\n", i);
}

void cout(char c) {
    printf("%c\n", c);
}

void stout(char *s) {
    printf("%s\n", s);
}

int main() {
    int tmp1 = iin(), tmp2, max = 0, k, i;
    REP(i, 4) {
      tmp2 = iin();
      if (max < (tmp2 - tmp1)) max = tmp2 - tmp1;
      tmp1 = tmp2;
    }
    k = iin();
    if (k < max) stout(":(");
    else stout("Yay!");
    return 0;
}