//ABC135C
#include <stdio.h>
#include <string.h>
#include <math.h>
int main(){
	int n;
	scanf("%d",&n);
	int a[n];
	int i, j, k, l, m;
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	int max;
	for(j=0;j<n;j++){
		max = a[0];
		if(j==0){
			max = a[1];
			for(k=2;k<n;k++){
				if(max<a[k]){
					max = a[k];
				}else{
					max = max;
				}
			}
			printf("%d\n",max);
		}else{
			for(l=0;l<j;l++){
				if(max<a[l]){
					max = a[l];
					}else{
						max = max;
					}
			}
			for(m=j+1;m<n;m++){
				if(max<a[m]){
					max = a[m];
				}else{
					max = max;
				}
			}
		printf("%d\n",max);
		}
	}
	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){
    long n, max1=0, max2=0, maxnum=0;/*max1最大値, その次 */
    scanf("%ld", &n);
    long a[n];
    for(long i=0; i<n; i++){
        scanf("%ld", &a[i]);
        if(a[i] > max1){
            max2 = max1;
            max1 = a[i];
            maxnum = i;//最大値を持つiを保存しておく
        }
        else if(a[i] <= max1 && a[i] > max2) max2 = a[i];
    }
    puts("");
    for(long i=0; i<n; i++){
        if(i == maxnum){
            printf("%ld\n", max2);
        }else{
            printf("%ld\n", max1);
        }
    }

    return 0;
}

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

Submitted source-code file size, in bytes.

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

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


#include <stdio.h>

int main()
{
  int n, a[200000], i, max, max2, mnum = 0;

  scanf("%d", &n);

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

  for(i = 0; i < n; i++){
    if(mnum == i)
      printf("%d\n", max2);
    else
      printf("%d\n", max);
  }

  return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int compare_int(const void *a, const void *b)
{
    return *(int*)a - *(int*)b;
//    return *(long long*)a - *(long long*)b;
}
//  qsort(A, b, sizeof(int), compare_int);

long long  factorial(long long n) {
    if (n > 0) {
        return n * factorial(n - 1);
    } else {
        return 1;
    }
}

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

int distance(int a, int b) {
  return abs(a-b)*abs(a-b);
}

//int ()

int main()
{
  int a ,b, c, d, e, X, Y;
//  long long  a, b, c, d, e, X, Y;
  //  double  c;
  int  A[200003]={};
//  long long  A[100003]={};
//  long long  B[100003]={};
  int  C[100003][11]={};

  int  flg=0;

//  long long  a, b, c, d, X, Y;
  char s[101][101]={};
  char t[101]={};
//  int N, M;
//  int A[1000000001]={};
//  long long  C[101]={};
  int i,j,k;
  long long p,q;
//  int flag=0;
//  int tmp=0;
  char tmp[101]={};
//  long long min=10000000000;
//  long long min=100000001;
  int len=0;
  int l=0;
  int r=1000001;
  int cnt=0;
  int num=0;
  int num1=0;
  int num2=0;
  long long ans=1;
  int max=0;
  int sum=0;
  int addr=0;
  int ptn=0;
//  bool on_off[100]={};

//  char alp[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

  //  a=0;
//  scanf("%lld %lld", &a, &b);
  scanf("%d", &a);
//    printf("%d\n", num);
  for(i=0;i<a;i++){
    scanf("%d", &A[i]);
  }
  for(i=0;i<a;i++){
    if(num2<A[i]){
      num2=A[i];
    }
    if(num1<num2){
      ptn=num1;
      num1=num2;
      num2=ptn;
      num=i;
    }
  }
  for(i=0;i<a;i++){
    if(i==num)    printf("%d\n", num2);
    else          printf("%d\n", num1);
  }

//    printf("%s %d %d\n", s[i],C[i][0],C[i][1]);
   // printf("%d\n", C[i][1]);
/*  for(i=0;i<addr;i++){
//  scanf("%lld%lld", &B[0][i],&B[1][i]);
//    ans=ans+A[i];
    printf("%lld ", A[i]);
  }
*/
//    printf("%d ", cnt);

return 0;
}


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

#define N_MAX (100)
#define P_MAX (100)
#define DP_ARRAY_SIZE (N_MAX * P_MAX / 32 + 1)

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(a) ((a) < 0 ? -(a) : (a))
#define ABSS(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))

int compare_sz_asc(const void* a, const void* b) {
    return *((size_t*)a) < *((size_t*)b) ? -1 : 1;
}

int compare_sz_desc(const void* a, const void* b) {
    return *((size_t*)a) > * ((size_t*)b) ? -1 : 1;
}

int compare_i64_asc(const void* a, const void* b) {
    return *((int64_t*)a) < *((int64_t*)b) ? -1 : 1;
}

int compare_i64_desc(const void* a, const void* b) {
    return *((int64_t*)a) > * ((int64_t*)b) ? -1 : 1;
}

int compare_c_asc(const void* a, const void* b) {
    return *((char*)a) < *((char*)b) ? -1 : 1;
}

int compare_c_desc(const void* a, const void* b) {
    return *((char*)a) > * ((char*)b) ? -1 : 1;
}

static size_t powSz(const size_t base, const size_t exp) {
    if (exp == 0) {
        return 1;
    }

    if (exp == 1) {
        return base;
    }

    if (exp % 2 == 0) {
        return powSz(base * base, exp / 2);
    }
    else {
        return base * powSz(base, exp - 1);
    }
}

static size_t comb(const size_t n, const size_t r) {
    size_t result = 1;

    for (size_t i = 0; i < r; i++) {
        result *= n - i;
        result /= i + 1;
    }

    return result;
}

static uint64_t combU64(const uint64_t n, const uint64_t r) {
    uint64_t result = 1;

    for (uint64_t i = 0; i < r; i++) {
        result *= n - i;
        result /= i + 1;
    }

    return result;
}

static size_t gcdZu(size_t m, size_t n) {
    size_t temp;

    while (m % n != 0) {
        temp = n;
        n = m % n;
        m = temp;
    }

    return n;
}

static uint64_t gcdU64(uint64_t m, uint64_t n)
{
    uint64_t temp;

    while (m % n != 0) {
        temp = n;
        n = m % n;
        m = temp;
    }

    return n;
}

typedef struct {
    uint64_t A;
    uint64_t idx;
    uint64_t order;
} _info;

static _info info[200000];

static int compare_num(const void* a, const void* b) {
    const _info* aP = (_info*)a;
    const _info* bP = (_info*)b;

    return aP->A > bP->A ? -1 : 1;
}

static int compare_idx(const void* a, const void* b) {
    const _info* aP = (_info*)a;
    const _info* bP = (_info*)b;

    return aP->idx < bP->idx ? -1 : 1;
}

int main(void) {
    uint64_t N;

    scanf("%"PRIu64, &N);

    for (uint64_t i = 0; i < N; i++) {
        scanf("%"PRIu64, &(info[i].A));
        info[i].idx = i;
    }

    qsort(info, (size_t)N, sizeof(_info), compare_num);

    for (uint64_t i = 0; i < N; i++) {
        info[i].order = i;
    }

    uint64_t orderIdx[2];

    orderIdx[0] = info[0].idx;
    orderIdx[1] = info[1].idx;

    qsort(info, (size_t)N, sizeof(_info), compare_idx);

    for (uint64_t i = 0; i < N; i++) {
        printf("%"PRIu64"\n", info[i].order == 0 ? info[orderIdx[1]].A : info[orderIdx[0]].A);
    }

    return 0;
}


#include <stdio.h>
#include<math.h>
int main()
{
    int arr[50],flag=0;
    int t;
    scanf("%d",&t);
    for(int j=0;j<t;j++)
    {
        scanf("%d",&arr[j]);
    }
    int diff[t-1];
    for (int i = 0; i < (t-1); i++) {
        diff[i] = abs(arr[i] - arr[i + 1]);
    }
    for(int i=0;i<(t-1);i++)
    {
        int n=diff[i];
        if(n==diff[i+1])
            flag++;
        else
            break;
    }

    if(flag==(t-2))
        printf("Yes\n");
    else
        printf("No\n");
}

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>
int main ()
{
  int N,A,B,X;
  scanf("%d %d %d",&N,&A,&B);
  X=N*A;
  if(X<B)
  {
       printf("%d",X);
  }
  else if (X>B)
  {
      printf("%d",B);
  }
  else if (X=B)
  {
       printf("%d",B);
  }
}

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 n,a,b;
scanf("%d%d%d",&n,&a,&b);
if(a>b)
printf("%d",n*b);
else
printf("%d",n*a);
return 0;
}


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

  int A,B,N;
  int sum1,sum2,min;
  1 <= N <=20;
  1 <= A <=50;
  1 <= B <=50;
  sum1 = A*N;
  sum2 = B*N;

  scanf("%d,%d,%d\n", N,A,B);
  if (sum1 >= sum2) {
    min = sum2;
    printf("%d\n", min);
  }
  else
   min = sum1;
   printf("%d\n", min);

   return 0;
}


#include<stdio.h>
int main()
{
    int n,p,q,j;
    scanf("%d %d %d",&n,&p,&q);
    if(p>q){j=q*n;}
    else j=p*n;
    printf("%d\n",j);
    return 0;
}


#include<stdio.h>

int main(void) {
	int N, A, B;
	scanf("%d%d%d", &N, &A, &B);
	if (N * A >= B) {
		printf("%d\n",B);
	}else {
		printf("%d\n", N * A);
	}
	return 0;
}


#include <stdio.h>

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


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

int main(void)
{
  //nとdを入力
  int n,d;
  scanf("%d%d",&n,&d);

  //n個の座標をforで入力
  int i,j,k;
  int x[n][d];
  for(i=0;i<n;i++)
    for(k=0;k<d;k++)
      scanf("%d",&x[i][k]);

  //計算開始
  float dist;
  int cnt=0;
  //i,jを決める
  for(i=0;i<n;i++)
    for(j=i+1;j<n-(i+1);j++){

      //点iと点jの距離を求める
      dist = 0;
      for(k=0;k<d;k++)
        dist += pow((x[i][k]-x[j][k]),2);
      dist = sqrt(dist);

      //ceil(切り上げ関数)とfloor(切り捨て関数)を使ってdistが整数か判定
      //distが整数ならcntを1増やす
      if(ceil(dist)==floor(dist))
        cnt++;
    }

  //cntを出力して終了
  printf("%d\n",cnt);
  return 0;
}

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


Score : 400 points

Problem Statement

There are N mountains in a circle, called Mountain 1, Mountain 2, ..., Mountain N in clockwise order. N is an odd number.

Between these mountains, there are N dams, called Dam 1, Dam 2, ..., Dam N. Dam i (1 \leq i \leq N) is located between Mountain i and i+1 (Mountain N+1 is Mountain 1).

When Mountain i (1 \leq i \leq N) receives 2x liters of rain, Dam i-1 and Dam i each accumulates x liters of water (Dam 0 is Dam N).

One day, each of the mountains received a non-negative even number of liters of rain.

As a result, Dam i (1 \leq i \leq N) accumulated a total of A_i liters of water.

Find the amount of rain each of the mountains received. We can prove that the solution is unique under the constraints of this problem.

Constraints

All values in input are integers.

3 \leq N \leq 10^5-1

N is an odd number.

0 \leq A_i \leq 10^9

The situation represented by input can occur when each of the mountains receives a non-negative even number of liters of rain.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print N integers representing the number of liters of rain Mountain 1, Mountain 2, ..., Mountain N received, in this order.

Sample Input 1

3
2 2 4

Sample Output 1

4 0 4

If we assume Mountain 1, 2, and 3 received 4, 0, and 4 liters of rain, respectively, it is consistent with this input, as follows:

Dam 1 should have accumulated \frac{4}{2} + \frac{0}{2} = 2 liters of water.

Dam 2 should have accumulated \frac{0}{2} + \frac{4}{2} = 2 liters of water.

Dam 3 should have accumulated \frac{4}{2} + \frac{4}{2} = 4 liters of water.

Sample Input 2

5
3 8 7 5 5

Sample Output 2

2 4 12 2 8

Sample Input 3

3
1000000000 1000000000 0

Sample Output 3

0 2000000000 0


#include <stdio.h>

int main()
{
    char s[100];
    gets(s);

    int count = 0;

    for(int i=0; i<2; i++){
      for(int j=1; j<3; j++){
        if(s[i]==s[j])
          count++;
      }
    }

    if(count==2){
      printf("YES");
    }
  else{
    printf("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>

int main(void){
  int n;
  scanf("%d", &n);
  int array[n];
  int i , count = 0;
  int l, c, r; //left, center, right

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

  for(i=1 ; i<n-1 ; i++){
    l = array[i-1]; c = array[i]; r = array[i+1];
    if( (l<= c && c<=r)     //a<b<c
       || ( r<=c && c<=l ) ) //c<b<a
      count++;
  }

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

  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>

int main(){
  int n,tmp,miman =0,ans =0;
  scanf("%d",&n);
  int d[n];
  for(int i=0;i<n;i++) scanf("%d",&d[i]);
  for(int i=0;i<n;i++){
    for(int j=i+1;j<n;j++){
      if(d[i] > d[j]){
        tmp = d[i];
        d[i] = d[j];
        d[j] = tmp;
      }
    }
  }
  int size = sizeof d / sizeof d[0];
  if(n %2 == 0){
    printf("%d",d[(size-1)/2+1]-d[(size-1)/2]);
  }else {
    printf("0");
  }
  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 <stdlib.h>

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

int main() {
    int n;
    scanf("%d", &n);
    int nums[n];

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

    qsort(nums, n, sizeof(int), compare);

    if (n % 2 == 0) {
        if (nums[n / 2] == nums[n / 2 - 1])
            printf("0\n");
        else
            printf("%d\n", nums[n / 2] - nums[n / 2 - 1]);
    } else {
        printf("0\n");
    }
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<stdbool.h>
#include<assert.h>
typedef long long ll;
typedef long double ld;
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define rrep(i,l,r)for(ll i=(l);i>=(r);i--)
#define INF (1LL<<60)
#define MOD1 1000000007
#define MOD2 998244353
#define MAX_N (1 << 17)
#define YES printf("Yes\n")
#define NO printf("No\n")
#define PN printf("\n")
#define charsize 100005 //10^5+5
#define PI 3.141592653589793238

void swap(ll *a, ll *b){ll c;c=*b;*b=*a;*a= c;}
void cin(ll *n){ scanf("%lld",&(*n));}
void cin2(ll *a, ll *b){ scanf("%lld%lld",&(*a),&(*b)); }
void cin3(ll *a, ll *b, ll *c){ scanf("%lld%lld%lld",&(*a),&(*b),&(*c)); }
ll max2(ll a,ll b){return a>=b?a:b;}
ll min2(ll a,ll b){return a>=b?b:a;}
ll min3(ll a, ll b, ll c){return (a<=b && a<=c) ? a : b<=c ? b : c;}
ll max3(ll a, ll b, ll c){return (a>=b && a>=c) ? a : b>=c ? b : c;}
ll minn(ll n, ll a[n]){ll b=INF;rep(i,0,n) b=min2(b,a[i]);return b;}
ll maxn(ll n, ll a[n]){ll b=-INF;rep(i,0,n) b=max2(b,a[i]);return b;}
ll ABS(ll a){return a>=0?a:(-a);}
ll POW(ll a, ll b){ll c=1;rep(i,0,b) c*=a;return c;}
double POW_d(double a, double b){double c=1;rep(i,0,b) c*=a;return c;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll mod_MOD1(ll n){n+= n<0?((-n)/MOD1+1)*MOD1:0; return n%=MOD1;}
ll mod_p(ll n ,ll p){n+= n<0?((-n)/p+1)*p:0; return n%=p;}
ll change_into_num(char s[] , ll len, ll p){ return !p ? 0 : POW(10,p-1)*(s[len-p]-'0') + change_into_num(s,len,p-1); }

void lr_lower( int *l, int *r, ll am, ll val , int type ){ (type<3) ? ( am < val ?  ( *l = (*l+*r)/2 ) : ( *r= (*l+*r)/2 ) ) : ( am <= val ? ( *l = (*l+*r)/2 ) : ( *r= (*l+*r)/2 ) ); }
void lr_upper( int *l, int *r, ll am, ll val , int type ){ (type<3) ? ( am <= val ?  ( *l = (*l+*r)/2 ) : ( *r= (*l+*r)/2 ) ) : ( am < val ? ( *l = (*l+*r)/2 ) : ( *r= (*l+*r)/2 ) ); }
int cmp_lower( ll a, ll b, int type ){ return (type==1) ? ( a==b ? 1 : 0 ) : (type==2) ? ( a>=b ? 1 : 0 ) : ( a>b ? 1 : 0 ) ; }
int cmp_upper( ll a, ll b, int type ){ return (type==1) ? ( a==b ? 1 : 0 ) : (type==2) ? ( a<=b ? 1 : 0 ) : ( a<b ? 1 : 0 ) ; }
// return smallest p  which meets  a[p]==val :1  >=:2   >:3
ll lower_bound( ll a[], int l, int r, ll val , int type ){  while(r-l>1) lr_lower(&l,&r,a[ (l+r)/2 ],val,type);  return cmp_lower(a[l],val,type) ? l : cmp_lower(a[r],val,type) ? r : -1;  }
// return biggest p  which meets   a[p]==val :1  <=:2   <:3
ll upper_bound( ll a[], int l, int r, ll val , int type ){  while(r-l>1) lr_upper(&l,&r,a[ (l+r)/2 ],val,type); return cmp_upper(a[r],val,type) ? r : cmp_upper(a[l],val,type) ? l : -1; }
// count i  which meets ai==x
ll count(ll a[], int l, int r, ll x){  int p = lower_bound(a,l,r,x,1);  return p==-1 ? 0 : upper_bound(a,p,r,x,1)-p+1; }

int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
int cmp_string( const void * a , const void * b ) {  return strcmp( (char *)a , (char *)b ); }  // qsort((void*)s,n,sizeof(s[0]),int_sort );
int cmp_char(const void * a, const void * b) { return *(char *)a - *(char *)b;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
void sort_string(int n,int size,char s[][size]){ qsort( (void*)s , n , sizeof(s[0]) , cmp_string ); }
void sort_char(char *s){ qsort( (void *)s , strlen(s) , sizeof(char) , cmp_char ); }
ll unique_string(ll n ,ll size, char s[][size]){ ll ans=1; rep(i,1,n) if( strcmp(s[i],s[i-1]) ) ans++; return ans; }
ll unique_num(ll n , ll a[]){ ll ans=1; rep(i,1,n) if( a[i]!=a[i-1] ) ans++; return ans; }

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

/*------------------------------Dijkstra start-------------------------------------------------------------*/
//http://www.deqnotes.net/acmicpc/dijkstra/
int edge_cnt[114123];
ll* graph[114123];
ll ans[114123];
bool is_conf[114123];
// freeeeeeeeeeeeeeeeeeeeeeeeeeee!!!!!!!!!!!!

void pushGraph(ll v1, ll v2, ll weight , ll is_dir){
    graph[v1] = realloc( graph[v1] , sizeof(ll)*(edge_cnt[v1]*2+2) );
    graph[v1][edge_cnt[v1]*2] = v2;
    graph[v1][edge_cnt[v1]*2+1] = weight;
    // ednum[v1] = realloc( ednum[v1] , sizeof(ll)*(edge_cnt[v1]+1) );
    // ednum[v1][edge_cnt[v1]] = num;
    edge_cnt[v1]++;

    if(!is_dir){
        graph[v2] = realloc( graph[v2] , sizeof(ll)*(edge_cnt[v2]*2+2) );
        graph[v2][edge_cnt[v2]*2] = v1;
        graph[v2][edge_cnt[v2]*2+1] = weight;
        // ednum[v2] = realloc( ednum[v2] , sizeof(ll)*(edge_cnt[v2]+1) );
        // ednum[v2][edge_cnt[v2]] = num;
        edge_cnt[v2]++;
    }
}
/*-----------priorityqueue(strurct)---------------*/
// kind = max?1:0
ll HeapValue(ll a){return a?-INF:INF;}
ll Heapcmp(ll a, ll b, ll kind){return kind ? (a<b?1:0) : (a>b?1:0);}

typedef struct{
    int from;
    int to;
    ll cost;
}graph_heap;

graph_heap heap[2*MAX_N];
void str_swap(graph_heap *a,graph_heap *b){ graph_heap c;c=*b;*b=*a;*a=c; }

void graph_pushHeap(ll val, ll len , ll kind){
    ll i=len; heap[i].cost=val;
    while(i!=0){
        if( Heapcmp( heap[i].cost, heap[(i-1)/2].cost , ABS(kind-1) )  ) str_swap(&heap[i],&heap[(i-1)/2]);
        i--;i/=2;
    }
}
void graph_popHeap(ll len, ll kind){
    ll i=0; heap[0].cost=HeapValue(kind);
    str_swap(&heap[0],&heap[len-1]);
    while(len>i*2+1){
        if(i*2+2<len && Heapcmp(heap[i].cost,heap[i*2+2].cost,kind) && Heapcmp(heap[i*2+1].cost,heap[i*2+2].cost,kind) ){
            str_swap(&heap[i],&heap[i*2+2]);
            i*=2;i+=2;
        }else{
            if( Heapcmp(heap[i].cost,heap[i*2+1].cost,kind) ){
                str_swap(&heap[i],&heap[i*2+1]);
                i*=2;i++;
            }else break;
        }
    }
}
/*---------------------------------------------*/

void dijkstra(ll start){
    rep(i,0,114123){
        ans[i] = INF;
        is_conf[i] = false;
    }
    ans[start] = 0;
    is_conf[start]  = true;
    int size=0;
    rep(i,0,edge_cnt[start]){
        heap[size].from = start;
        heap[size].to = graph[start][i*2];
        ans[ heap[size].to ] = graph[start][i*2+1];
        graph_pushHeap( graph[start][i*2+1] , size , 0 );
        size++;
    }
    // rep(i,0,4) printf("%lld ",ans[i]);
    // PN;
    ll piv;
    while(size){
        if( is_conf[ heap[0].to ] ){
            graph_popHeap(size,0);
            size--;
            continue;
        }
        // printf("AA: %d %d\n",heap[0].from, heap[0].to );
        is_conf[ heap[0].to ] = true;
        ans[ heap[0].to ] = heap[0].cost;
        piv = heap[0].to;
        // rep(i,0,4) printf("%lld ",ans[i]);
        // PN;
        rep(i,0,edge_cnt[piv]){
            if( ans[ graph[ heap[0].to ][i*2] ] > ans[ heap[0].to ] + graph[ heap[0].to ][i*2+1] ){
                heap[size].from = heap[0].to;
                heap[size].to = graph[ heap[0].to ][i*2];
                graph_pushHeap( ans[ heap[0].to ] + graph[ heap[0].to ][i*2+1] , size , 0 );
                size++;
            }
        }
        graph_popHeap(size,0);
        size--;
    }
}
/*---------------------------Dijkstra end--------------------------------------------------------------------*/

int main(void){
    // fgets(s,sizeof(s),stdin);
    // char ;
    ll n,m,s,t;
    // ll ans=0;
    cin(&n);
    cin(&m);
    // cin2(&,&);
    // cin3(&,&,&);
    // scanf("%s",);
    ll u,v;
    rep(i,0,m){
        cin(&u);
        cin(&v);
        // pushGraph(u-1,v-1,1,1);
        pushGraph(u-1,v-1+n,1,1);
        pushGraph(u-1+n,v-1+2*n,1,1);
        pushGraph(u-1+2*n,v-1,1,1);
    }
    // rep(i,0,n){
    //     printf("%d ",edge_cnt[i]);
    // }
    // PN;
    cin(&s);
    cin(&t);
    dijkstra(s-1);
    // rep(i,0,n){
    //     printf("%lld ",ans[i]);
    // }
    // PN;

    if(ans[t-1]==INF) printf("-1\n");
    else printf("%lld\n",ans[t-1]/3);
    return 0;
}

Score : 500 points

Problem Statement

Ken loves ken-ken-pa (Japanese version of hopscotch). Today, he will play it on a directed graph G.
G consists of N vertices numbered 1 to N, and M edges. The i-th edge points from Vertex u_i to Vertex v_i.

First, Ken stands on Vertex S. He wants to reach Vertex T by repeating ken-ken-pa. In one ken-ken-pa, he does the following exactly three times: follow an edge pointing from the vertex on which he is standing.

Determine if he can reach Vertex T by repeating ken-ken-pa. If the answer is yes, find the minimum number of ken-ken-pa needed to reach Vertex T. Note that visiting Vertex T in the middle of a ken-ken-pa does not count as reaching Vertex T by repeating ken-ken-pa.

Constraints

2 \leq N \leq 10^5

0 \leq M \leq \min(10^5, N (N-1))

1 \leq u_i, v_i \leq N(1 \leq i \leq M)

u_i \neq v_i (1 \leq i \leq M)

If i \neq j, (u_i, v_i) \neq (u_j, v_j).

1 \leq S, T \leq N

S \neq T

Input

Input is given from Standard Input in the following format:

N M
u_1 v_1
:
u_M v_M
S T

Output

If Ken cannot reach Vertex T from Vertex S by repeating ken-ken-pa, print -1.
If he can, print the minimum number of ken-ken-pa needed to reach vertex T.

Sample Input 1

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

Sample Output 1

2

Ken can reach Vertex 3 from Vertex 1 in two ken-ken-pa, as follows: 1 \rightarrow 2 \rightarrow 3 \rightarrow 4 in the first ken-ken-pa, then 4 \rightarrow 1 \rightarrow 2 \rightarrow 3 in the second ken-ken-pa. This is the minimum number of ken-ken-pa needed.

Sample Input 2

3 3
1 2
2 3
3 1
1 2

Sample Output 2

-1

Any number of ken-ken-pa will bring Ken back to Vertex 1, so he cannot reach Vertex 2, though he can pass through it in the middle of a ken-ken-pa.

Sample Input 3

2 0
1 2

Sample Output 3

-1

Vertex S and Vertex T may be disconnected.

Sample Input 4

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

Sample Output 4

2


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char s[5];
scanf("%s",s);
if(s[0]==s[1]||s[0]==s[2]||s[0]==s[3]||s[1]==s[2]||s[1]==s[3]||s[2]==s[3]){printf("Bad");}
else{printf("Good");}
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>

int main(void){
  char S[4];
  scanf("%s",S);
  for(int i=0;i<3;i++){
    if(S[i]==S[i+1]){
      printf("Bad");
    }else if(i==2){
      printf("Good");
    }
  }
  return 0;
}


#include "stdio.h"
#include "math.h"
int main()
{
  int i,S,g2,h[3],d;
  scanf("%d",&S);
  g2=S;
  for (i = 0; i < 4; i++)
  {
    S=S/(int)pow(10,3-i);
    h[i]=S;
    S=g2%(int)pow(10,3-i);
  }
  for (i = 0; i < 5; i++)
  {
    if (h[i]==h[i+1])
    {
      d=1;
      break;
    }else{
      d=0;
    }

  }
  if (d==1) {
    puts("Bad");
  }else{
    puts("Good");
  }
  return 0;
}


#include <stdio.h>

int main(int argc, char *argv[]){
  int i;
  char a,b,c,d;
  scanf("%c%c%c%c", &a,&b,&c,&d);
  if(a == b || b == c || a == c || b == d || c == d || a == d){
    printf("Bad");
  }else{
    printf("Good");
  }
  return 0;
}


// AtCoder ABC131: B - Bite Eating
// 2019.9.3 bal4u

#include <stdio.h>

#define ABS(x)  ((x)>=0?(x):-(x))

int main()
{
	int i, L, N, ans, mi, x;

	scanf("%d%d", &N, &L);
	mi = 1000;
	for (i = 0; i < N; i++) {
		if (ABS(L+i) < mi) x = L+i, mi = ABS(x);
	}
	printf("%d\n", N*L+(N-1)*N/2 - x);
	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;
	scanf("%d%d", &N, &L);

	int i;
	int v = 0;

	for (i = 0;i < N;i++) {
		v += L + i;
	}

	if (N + L - 1 < 0) {
		v -= N + L - 1;
	}else if(L > 0) {
		v -= L;
	}else{}

	printf("%d", v);

	return 0;
}


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

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

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

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

int main(int argc, char const *argv[])
{
	int n,l;
	int a[203];
	int sum = 0;
	scanf("%d%d",&n,&l);
	for (int i = 1; i <= n; ++i){
		a[i] = l + i - 1;
		sum += a[i];
	}
	for (int i = 1; i <= n; ++i)
	{
		if(a[i] == 0){
			printf("%d\n", sum);
			return 0;
		}
	}
	if(sum < 0){
		printf("%d\n", sum - a[n]);
	}
	else{
		printf("%d\n", sum - a[1]);
	}
	return 0;
}


#include <stdio.h>

int main(){
	long long int a, b, c, d;
	scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
	long long int i, ans=0, x, y, z;

    x = (b-a+1+a%c)/c;
    y = (b-a+1+a%d)/d;
    z = (b-a+1+a%(c*d))/(c*d);

	printf("%lld", (b-a+1)-x-y+z);

	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(void){
  unsigned long long a,b,c,d,sum=0;
  scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
  sum += (b/c) - (a-1)/c;
  sum += (b/d) - (a-1)/d;
  printf("%lld\n",sum);
  return 0;
}


#include <stdio.h>
int main(void){
    // Your code here!
    int a,b,c,d;
    unsigned long long int cnt = 0;
    scanf("%d %d %d %d",&a,&b,&c,&d);
    for(int i=a; i<= b;i++){
        if(i%c != 0 && i%d != 0){
            cnt++;
        }
    }
    printf("%d\n",cnt);
}


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

typedef struct {
  int a_val;
  int b_val;
} st;

int cmp_a(const void *x, const void *y) {
  st *X = (st *)x;
  st *Y = (st *)y;
  return Y->a_val - X->a_val;
}

int cmp_b(const void *x, const void *y) {
  st *X = (st *)x;
  st *Y = (st *)y;
  return X->b_val - Y->b_val;
}
/*
int cmp(const void *a, const void *b) {
  int(*func[])(st* pa_, st* pb_) = {&cmp_a, &cmp_b};
  st *pa = (st*)a;
  st *pb = (st*)b;
  for (int i = 0; i < sizeof(func) / sizeof(func[0]); i++) {
    int ret = func[i](pa, pb);
    if (ret != 0) {
      return ret;
		}
	}
}
*/

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

  int *a, *b;
  a = (int *)malloc(sizeof(int) * n);
  b = (int *)malloc(sizeof(int) * n);

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

  st *c;
  c = (st *)malloc(sizeof(st) * n);
  for(i = 0; i < n; i++) {
    c[i].a_val = a[i];
    c[i].b_val = b[i];
  }

  qsort(c, n, sizeof(st), cmp_b);

  int alltime = 0;
  for(i = 0; i < n; i++) {
    alltime += c[i].a_val;
    if(alltime > c[i].b_val) {
      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>
#define N 100

int main(){
int n,x,l[N];
  int i,j,cnt;

  scanf("%d%d",&n,&x);
  for(i=0;i<3;i++){
    scanf("%d",l[i]);
  }

  for(j=0;j<n;j++){
    if(l[j]<=x) cnt++;
  }

  printf("%d",cnt);

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.


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(void){
  double W,H,x,y;
  scanf("%d %d %d %d",&W,&H,&x,&y);
  if(W/2==x && H/2==y){
    printf("%f 1\n",W*H/2);
  }else{
    printf("%f 0\n",W*H/2);
  }
  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(void) {
    float w,h,x,y;
    scanf("%f %f %f %f",&w, &h, &x, &y);
    float size = w*h/2.0;
    int flag;
    if(x==w/2.0 && y==h/2.0)
        flag = 1;
    else
        flag = 0;
    printf("%f %d\n", size,flag);

}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define bit(n,m)(((n)>>(m))&1)
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
//#define MOD 998244353
#define MOD 1000000007
#define invp(a,p)pom(a,p-2,p)

ll n;
ll x[99],y[99];
ll idx[99];
ll idx2[99];

int c(const void*a,const void*b){
	ll p=*(ll*)a,q=*(ll*)b;
	ll ppp=x[p]*2000000001+y[p];
	ll qqq=x[q]*2000000001+y[q];
	if(ppp<qqq)return -1;
	return 1;
}
int c2(const void*a,const void*b){
	ll p=*(ll*)a,q=*(ll*)b;
	ll ppp=x[p]*2000000001-y[p];
	ll qqq=x[q]*2000000001-y[q];
	if(ppp<qqq)return -1;
	return 1;
}

int main(){
	scanf("%lld",&n);
	rep(i,0,n)scanf("%lld %lld",x+i,y+i);
	rep(i,0,n)idx[i]=idx2[i]=i;
	qsort(idx,n,sizeof(ll),c);
	qsort(idx2,n,sizeof(ll),c2);
	ll ans=n;

	rep(aaa,0,n)rep(bbb,0,n)if(aaa!=bbb){
		//aaa,bbb間の移動を(p,q)に指定する
		ll p=x[aaa]-x[bbb];
		ll q=y[aaa]-y[bbb];
		ll tempx[99]={};
		ll tempy[99]={};

		if(p*q>=0){
			if(p>0||q>0){
				//左下から右下へ
				rep(i,0,n){
					tempx[i]=x[idx[i]];
					tempy[i]=y[idx[i]];
				}
			}else{
				//右上から左下へ
				for(ll i=n-1;i>=0;i--){
					tempx[i]=x[idx[i]];
					tempy[i]=y[idx[i]];
				}
			}
		}else{
			if(p<0){
				//左上から右下へ
				rep(i,0,n){
					tempx[i]=x[idx2[i]];
					tempy[i]=y[idx2[i]];
				}
			}else{
				//右下から左上へ
				for(ll i=n-1;i>=0;i--){
					tempx[i]=x[idx2[i]];
					tempy[i]=y[idx2[i]];
				}
			}
		}

		ll cnt=0;
		rep(i,0,n){
			if(tempx[i]!=INF){
				cnt++;
				int flag=1;
				ll crrx=tempx[i];
				ll crry=tempy[i];
				tempx[i]=INF;
				tempy[i]=INF;

				while(flag){
					flag=0;
					crrx+=p;
					crry+=q;
					rep(j,0,n){
						if(crrx==tempx[j]&&crry==tempy[j]){
							flag=1;
							tempx[j]=INF;
							tempy[j]=INF;
							break;
						}
					}
				}
			}
		}
		ans=min(ans,cnt);
	}
	printf("%lld",ans);
}

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


Score : 600 points

Problem Statement

The squirrel Chokudai has N acorns.
One day, he decides to do some trades in multiple precious metal exchanges to make more acorns.

His plan is as follows:

Get out of the nest with N acorns in his hands.

Go to Exchange A and do some trades.

Go to Exchange B and do some trades.

Go to Exchange A and do some trades.

Go back to the nest.

In Exchange X (X = A, B), he can perform the following operations any integer number of times (possibly zero) in any order:

Lose g_{X} acorns and gain 1 gram of gold.

Gain g_{X} acorns and lose 1 gram of gold.

Lose s_{X} acorns and gain 1 gram of silver.

Gain s_{X} acorns and lose 1 gram of silver.

Lose b_{X} acorns and gain 1 gram of bronze.

Gain b_{X} acorns and lose 1 gram of bronze.

Naturally, he cannot perform an operation that would leave him with a negative amount of acorns, gold, silver, or bronze.

What is the maximum number of acorns that he can bring to the nest?
Note that gold, silver, or bronze brought to the nest would be worthless because he is just a squirrel.

Constraints

1 \leq N \leq 5000

1 \leq g_{X} \leq 5000

1 \leq s_{X} \leq 5000

1 \leq b_{X} \leq 5000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
g_A s_A b_A
g_B s_B b_B

Output

Print the maximum number of acorns that Chokudai can bring to the nest.

Sample Input 1

23
1 1 1
2 1 1

Sample Output 1

46

He can bring 46 acorns to the nest, as follows:

In Exchange A, trade 23 acorns for 23 grams of gold. {acorns, gold, silver, bronze}={ 0,23,0,0 }

In Exchange B, trade 23 grams of gold for 46 acorns. {acorns, gold, silver, bronze}={ 46,0,0,0 }

In Exchange A, trade nothing. {acorns, gold, silver, bronze}={ 46,0,0,0 }

He cannot have 47 or more acorns, so the answer is 46.


#include  <stdio.h>
int main(){
  int a,b,c,d,e,f;
  scanf("%d %d %d",&a,&b,&c);
  d=a+b;
  e=b+c;
  f=c+a;
  if(d>e && d>f){
    printf("%d",d);
  }
  else if(e>d && e>f){
    printf("%d",e);
  }
  else{
    printf("%d",f);
  }
  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>
#define max(a,b) a>b?a:b
int main()
{
    int p,q,r;
    int s1,s2,s3;
    scanf("%d %d %d",&p,&q,&r);
    s1=p+q;
    s2=p+r;
    s2=q+r;
    int d=max(s1,max(s2,s3));
    printf("%d",d);
}


#include<stdio.h>

int main(void){
        int P, Q, R;
        int SUM[6] = {0};
        int i;
        int minimam = 0;

        scanf("%d %d %d", &P, &Q, &R);
        SUM[0] = P + Q;
        SUM[1] = R + Q;
        SUM[2] = P + R;
        SUM[3] = Q + R;
        SUM[4] = R + P;
        SUM[5] = Q + P;

        minimam = SUM[0];
        for(i=1; i< 6; i++){
                if(minimam > SUM[i]){
                        minimam = SUM[i];
                }
        }
        printf("%d\n", minimam);
        return 0;
}


#include<stdio.h>
int main()
{
  int p,q,r,t1,t2,t3,min;
  scanf("%d%d%d", &p, &q, &r);
  t1 = p+q; min = t1;
  t2 = q+r; if (min>t2) min = t2;
  t3 = r+p; if (min>t3) min = t3;
  printf("%d", min);
  return 0;
}


#include<stdio.h>
int main(void){
    int p,q,r,min;
    scanf("%d",&p);
    scanf("%d",&q);
    scanf("%d",&r);
    min=p+q;
    if(p+r<min){
        min=p+r;
    }
    else if(q+p<min){
        min=q+p;
    }
    else if(q+r<min){
        min=q+r;
    }
    else if(r+p<min){
        min=r+p;
    }
    else if(r+q<min){
        min=r+q;
    }
        printf("%d",min);
    return 0;
}


#include<stdio.h>
int main()
{
	int P, Q, R;
	scanf("%d %d %d", &P,&Q, &R);

	int a, b, c;

	if(P <= Q && Q <= R){
		a=P;
		b=Q;
		c=R;
	}

	else if (P > Q && P <= R){
		a = Q;
		b = P;
		c = R;
	}

	else if (P <= R && Q > R){
		a = P;
		b = R;
		c =Q;
	}

	else if (P <= Q && R < P){
		a = R;
		b = P;
		c = Q;
	}

	else if (P > Q && P > R){
		a = Q;
		b = R;
		c = P;
	}

	else{
		a = R;
		b = Q;
		c = P;
	}

	printf("%d", a+b);

	return 0;

}


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

int main(void){
  int n,i,j,s1,s2,tmp,ans=1e9;
  scanf("%d",&n);
  int a[n];
  for(i=0;i<n;i++) scanf("%d",&a[i]);
  //for(i=0;i<n;i++) printf("%d\n",a[i]);
  for(i=0;i<n-1;i++){
    s1=0;
    s2=0;
    for(j=0;j<=i;j++) s1+=a[j];
    for(j=i+1;j<n;j++) s2+=a[j];
    tmp = abs(s1-s2);
    ans = min(ans,tmp);
  }
  printf("%d\n",ans);
  return 0;
}

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


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


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

int max(int a, int b)
{
    return a >= b ? a : b;
}

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

    char map[H][W];
    for (int i = 0; i < H; ++i)
    {
        scanf("%s", map[i]);
    }

    int ans[H][W];

    for (int i = 0; i < H; ++i)
    {
        int ct = 0;
        int tmp = 0;
        for (int j = 0; j < W; ++j)
        {
            if (map[i][j] == '.')
            {
                if (j != W-1) ct++;
                else
                {
                    ct++;
                    for (int k = tmp+1; k < W; ++k)
                    {
                        ans[i][k] = ct;
                    }
                }
            }
            else
            {
                ans[i][j] = 0;
                int buf = tmp + 1;
                if (j == tmp + 1) buf = j;
                if (tmp == 0) buf = 0;

                for (int k = buf; k < j; ++k)
                {
                    ans[i][k] = ct;
                }
                ct = 0;
                tmp = j;
            }
        }
    }

    int re = 0;

    for (int i = 0; i < W; ++i)
    {
        int ct = 0;
        int tmp = 0;
        for (int j = 0; j < H; ++j)
        {
            if (map[j][i] == '.')
            {
                if (j != H-1) ct++;
                else
                {
                    ct++;
                    for (int k = tmp+1; k < H; ++k)
                    {
                        re = max(re, ans[k][i] + ct);
                    }
                }
            }
            else
            {
                ans[j][i] = 0;
                int buf = tmp + 1;
                if (j == tmp + 1) buf = j;
                if (tmp == 0) buf = 0;

                for (int k = buf; k < j; ++k)
                {
                    re = max(re, ans[k][i] + ct);
                }
                ct = 0;
                tmp = j;
            }
        }
    }

    printf("%d", re-1);

    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>

int main(){
  int ret,win = 0,lose = 0,num = 0;
  char c;
  while(1){
    ret = scanf("%c", &c);
    if(ret == EOF || c == '\n') break;
    else if(c == 'o') win++;
    else if(c == 'x') lose++;
    num++;
  }
  int flag = 0;
  if(lose >= 8) flag = 1;
  if(flag == 0) printf("YES\n");
  else printf("NO\n");
  return 0;
}

Score : 200 points

Problem Statement

Takahashi is competing in a sumo tournament.
The tournament lasts for 15 days, during which he performs in one match per day.
If he wins 8 or more matches, he can also participate in the next tournament.

The matches for the first k days have finished.
You are given the results of Takahashi's matches as a string S consisting of o and x.
If the i-th character in S is o, it means that Takahashi won the match on the i-th day; if that character is x, it means that Takahashi lost the match on the i-th day.

Print YES if there is a possibility that Takahashi can participate in the next tournament, and print NO if there is no such possibility.

Constraints

1 \leq k \leq 15

S is a string of length k consisting of o and x.

Input

Input is given from Standard Input in the following format:

S

Output

Print YES if there is a possibility that Takahashi can participate in the next tournament, and print NO otherwise.

Sample Input 1

oxoxoxoxoxoxox

Sample Output 1

YES

Takahashi has 7 wins and 7 losses before the last match. If he wins that match, he will have 8 wins.

Sample Input 2

xxxxxxxx

Sample Output 2

NO


#include<stdio.h>
int main(){
    int a,p;
    scanf("%d %d",&a,&p);
    printf("%d\n",((a*3)+p)/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\n",(3*a+p)/2);
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int compare_int(const void *a, const void *b)
{
//    return *(int*)a - *(int*)b;
    return *(long long*)a - *(long long*)b;
}
//  qsort(A, b, sizeof(int), compare_int);

int main()
{
  int a ,b, c, d, e, X, Y;
//  long long  b, c, d, e, X, Y;
  //  double  c;
  long long  A[200003]={};
  long long  B[100003]={};
  int  C[100003]={};

  int  flag[103]={};

//  long long  a, b, c, d, X, Y;
  char s[101]={};
//  int N, M;
//  int A[1000000001]={};
//  long long  C[101]={};
  int i,j,k;
//  int flag=0;
  int tmp=0;
//  int min=1000000;
  long long min=100000001;
  int len=0;
  int l=0;
  int r=1000001;
  int cnt=0;
  int num=0;
  int ans=0;
  int max=0;
  int addr=0;
//  char alp[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

  //  a=0;
//  scanf("%lld %lld", &a, &b);
//  scanf("%d%d", &a,&c);
//  cnt = strlen(t);
// printf("%d\n",a);
    scanf("%d%d", &a,&b);
/*  for(i=0;i<a;i++){
    scanf("%lld", &A[i]);
  }
//  puts("");
  addr=a;
  for(i=0;i<b;i++){
    scanf("%lld%lld", &B[0],&B[1]);
    for(j=0;j<B[0];j++,addr++){
      A[addr]=B[1];
      cnt++;
    }
  }
/*  for(i=0;i<addr;i++){
//  scanf("%lld%lld", &B[0][i],&B[1][i]);
//    ans=ans+A[i];
    printf("%lld ", A[i]);
  }
*/
//    printf("%d ", cnt);

  ans=a*3;
  ans=(ans+b)/2;
  printf("%d\n", ans);

//  printf("%lf\n",ans);
  //  //  if(flag==0)   printf("TLE\n");
//    if(max < cnt - max)  printf("Yes\n");
//    else  printf("No\n");

return 0;
}


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

typedef struct {
	char toshi[256];
	int hyouka;
	int x;
}house;

void scan(house* data);
int cmp_str(const void* p, const void* q) {
	return strcmp(((house*)p)->toshi,((house*)q)->toshi);
}
int cmp_num(const void* p, const void* q) {
	return((house*)q)->hyouka - ((house*)p)->hyouka;
}
int print(house* data);
/*int testprint(house* data);*/

int main(void) {
	int i;
	int N;

	scanf("%d", &N);

	house data[N];

	for (i = 0;i < N;i++) {

		scan(&data[i]);
		data[i].x = i + 1;

	}

	qsort(data, N, sizeof(house), cmp_num);
	qsort(data, N, sizeof(house), cmp_str);

	for (i = 0;i < N;i++) {
		print(&data[i]);
	}

	/*for (i = 0;i < N;i++) {
		testprint(&data[i]);
	}*/

	return 0;

}

void scan(house * data) {

		scanf("%s%d", data->toshi, &data->hyouka);

		return;
}

int print(house * data) {
		printf("%d\n", data->x);

		return 0;
}

/*int testprint(house* data) {
	printf("%s %d\n", data->toshi, data->hyouka);

	return 0;
}*/

Score : 200 points

Problem Statement

You have decided to write a book introducing good restaurants.
There are N restaurants that you want to introduce: Restaurant 1, Restaurant 2, ..., Restaurant N. Restaurant i is in city S_i, and your assessment score of that restaurant on a 100-point scale is P_i.
No two restaurants have the same score.

You want to introduce the restaurants in the following order:

The restaurants are arranged in lexicographical order of the names of their cities.

If there are multiple restaurants in the same city, they are arranged in descending order of score.

Print the identification numbers of the restaurants in the order they are introduced in the book.

Constraints

1 ≤ N ≤ 100

S is a string of length between 1 and 10 (inclusive) consisting of lowercase English letters.

0 ≤ P_i ≤ 100

P_i is an integer.

P_i ≠ P_j (1 ≤ i < j ≤ N)

Input

Input is given from Standard Input in the following format:

N
S_1 P_1
:
S_N P_N

Output

Print N lines. The i-th line (1 ≤ i ≤ N) should contain the identification number of the restaurant that is introduced i-th in the book.

Sample Input 1

6
khabarovsk 20
moscow 10
kazan 50
kazan 35
moscow 60
khabarovsk 40

Sample Output 1

3
4
6
1
5
2

The lexicographical order of the names of the three cities is kazan < khabarovsk < moscow. For each of these cities, the restaurants in it are introduced in descending order of score. Thus, the restaurants are introduced in the order 3,4,6,1,5,2.

Sample Input 2

10
yakutsk 10
yakutsk 20
yakutsk 30
yakutsk 40
yakutsk 50
yakutsk 60
yakutsk 70
yakutsk 80
yakutsk 90
yakutsk 100

Sample Output 2

10
9
8
7
6
5
4
3
2
1


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

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>
int main(){
  int a,b;
  scanf("%d",&a);
  scanf("%d",&b);
  if(a>=13)printf("%d",b);
  else if(12>=a>=6)printf("%d",b/2);
  else printf("%d",0);
}


#include <stdio.h>

int main(void){
  int a,b;
  scanf("%d%d",&a,&b);
  if(a>=13){
    printf("%d",b);
  }else if(a>=6 && a<=12){
    printf("%d",b/2);
  }else{
    printf("0");
  }
}


#include <stdio.h>

void main()
{
  int age;
  int money;
  scanf("%d",&age);
  scanf("%d",&money);
  if(age>12){
  printf("%d",&money);
  }else if(age<13){
  printf("%d",money/2);
  }else{
  printf("0");
  }
  return 0;

}


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


#include <stdio.h>

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

    if(a >= 13){
        printf("%d\n", b);
    }else if(a >= 6){
        printf("%d\n", b/2);
    }else{
        puts("0");
    }

    return 0;
}


#include <stdio.h>

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

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

  for(int i = 0; i < 10; i++){
    x = r*x - d;
    printf("%d", x);
  }

}

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 N,M,i,ans;
  scanf("%d %d",&N,&M);
  int L[M],R[M];
  int L_max = 1,R_min = N;
  for(i = 0;i < M;i++) {
    scanf("%d %d",&L[i],&R[i]);
  }
  for(i = 0;i < M;i++) {
    if(L_max < L[i]) L_max = L[i];
    if(R_min > R[i]) R_min = R[i];
  }
  ans = R_min - L_max + 1;
  if(ans >= 0) printf("%d\n",ans);
  else printf("0\n");
  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>
#include <stdlib.h>
#include <string.h>
#include <math.h>

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

int main(void) {
	int N, M, L, R, max_L = 0, min_R = 100001;

	scanf("%d %d", &N, &M);
	for (int i = 0; i < M; i++) {
		scanf("%d %d", &L, &R);
		max_L = max(max_L, L);
		min_R = min(min_R, R);
	}

	if (min_R - max_L >= 0)
		printf("%d\n", min_R - max_L + 1);
	else
		printf("0\n");

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

int merge(long int a1[], long int a2[], int a1_size, int a2_size, long int a[]);
int merge2(long int b1[], long int b2[], long int c1[], long int c2[], int b1_size, int b2_size, long int b[], long int c[]);
int mergeSort(long int a[], int number);
int mergeSort2(long int a[], long int b[], int number);

int main(void)
{
	int n, m;
	long int sum = 0;
	int i, j, index = 0;;
	long int work;
	long int a[100000], b[100000], c[100000];

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

	for(i = 0; i < n; i++) {
		scanf("%ld", &a[i]);
		sum += a[i];
	}
	for(i = 0; i < m; i++) {
		scanf("%ld%ld", &b[i], &c[i]);
	}

	mergeSort(a, n);
	mergeSort2(b, c, m);

	for(i = 0; i < m; i++) {
		if(a[index] > c[i]) {
			break;
		}
		for(j = 0; j < b[i]; j++) {
			if(index + j >= n) {
				break;
			}
			if(a[index + j] < c[i]) {
				sum += c[i] - a[index + j];
			}
		}
		index += b[i];
		if(index >= n) {
			break;
		}
	}
	/*
	for(i = 0; i < n; i++) {
		printf("%ld\n", a[i]);
	}

	for(i = 0; i < m; i++) {
		printf("%ld %ld\n", b[i], c[i]);
	}
	*/
	printf("%ld\n", sum);

	return 0;
}

int merge(long int a1[], long int a2[], int a1_size, int a2_size, long int a[])
{
	int i = 0, j = 0;

	while(i < a1_size ) {
		if( (j >= a2_size ) || ( (i < a1_size) && (a1[i] < a2[j]) ) ) {
			a[i + j] = a1[i];
			i++;
		} else {
			a[i + j] = a2[j];
			j++;
		}
	}

	return 0;
}

int merge2(long int b1[], long int b2[], long int c1[], long int c2[], int b1_size, int b2_size, long int b[], long int c[])
{
	int i = 0, j = 0;

	while(i < b1_size ) {
		if( (j >= b2_size ) || ( (i < b1_size) && (c1[i] > c2[j]) ) ) {
			b[i + j] = b1[i];
			c[i + j] = c1[i];
			i++;
		} else {
			b[i + j] = b2[j];
			c[i + j] = c2[j];
			j++;
		}
	}

	return 0;
}

int mergeSort(long int a[], int number)
{
	int m = number / 2, n = number - m;
	long int a1[m];
	long int a2[n];
	int i, j;

	if(number > 1) {
		for(i = 0; i < m; i++) {
			a1[i] = a[i];
		}
		for(i = 0; i < n; i++) {
			a2[i] = a[m + i];
		}
		mergeSort(a1, m);
		mergeSort(a2, n);
		merge(a1, a2, m, n, a);
	}

	return 0;
}

int mergeSort2(long int b[], long int c[], int number)
{
	int m = number / 2, n = number - m;
	long int b1[m];
	long int b2[n];
	long int c1[m];
	long int c2[n];
	int i, j;

	if(number > 1) {
		for(i = 0; i < m; i++) {
			b1[i] = b[i];
		}
		for(i = 0; i < n; i++) {
			b2[i] = b[m + i];
		}

		for(i = 0; i < m; i++) {
			c1[i] = c[i];
		}
		for(i = 0; i < n; i++) {
			c2[i] = c[m + i];
		}

		mergeSort2(b1, c1, n);
		mergeSort2(b2, c2, m);
		merge2(b1, b2, c1, c2, m, n, b, c);
	}

	return 0;
}


#include <stdio.h>
#define MAX 200010
#define MOD 1000000007

long long int fac[MAX]={0};
long long int inv[MAX]={0};
long long int finv[MAX]={0};
int i;

void comb_init()
{
	fac[0]=1;
	fac[1]=1;
	finv[0]=1;
	finv[1]=1;
	inv[1]=1;
	for(i=2;i<MAX;i++)
	{
		fac[i]=fac[i-1]*i%MOD;
		inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;
		finv[i]=finv[i-1]*inv[i]%MOD;
	}
}

long long int comb(int n,int k)
{
	if(n<k)return 0;
	if(n<0||k<0)return 0;
	return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;
}

long long int tetra(int n)
{
	long long int lln=(long long int)n;
	return (lln*(lln+1)*(lln+2)/6)%MOD;
}

int main()
{
	int n,m,k;

	long long int ans,a,b;
	scanf("%d %d %d",&n,&m,&k);
	comb_init();
	a=((tetra(m-1)*n)%MOD*n)%MOD;
	//printf("%d\n",((tetra(m-1)*n)%MOD*n));
	b=((tetra(n-1)*m)%MOD*m)%MOD;
	ans=(((a+b)%MOD)*(comb((n*m-2),(k-2)))%MOD);
	//printf("%d %d %d",a+b,comb((n*m-2),(k-2)),ans);
	printf("%d",ans);
	return 0;
}

Score : 500 points

Problem Statement

We have a grid of squares with N rows and M columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left. We will choose K of the squares and put a piece on each of them.

If we place the K pieces on squares (x_1, y_1), (x_2, y_2), ..., and (x_K, y_K), the cost of this arrangement is computed as:

\sum_{i=1}^{K-1} \sum_{j=i+1}^K (|x_i - x_j| + |y_i - y_j|)

Find the sum of the costs of all possible arrangements of the pieces. Since this value can be tremendous, print it modulo 10^9+7.

We consider two arrangements of the pieces different if and only if there is a square that contains a piece in one of the arrangements but not in the other.

Constraints

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

2 \leq K \leq N \times M

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N M K

Output

Print the sum of the costs of all possible arrangements of the pieces, modulo 10^9+7.

Sample Input 1

2 2 2

Sample Output 1

8

There are six possible arrangements of the pieces, as follows:

((1,1),(1,2)), with the cost |1-1|+|1-2| = 1

((1,1),(2,1)), with the cost |1-2|+|1-1| = 1

((1,1),(2,2)), with the cost |1-2|+|1-2| = 2

((1,2),(2,1)), with the cost |1-2|+|2-1| = 2

((1,2),(2,2)), with the cost |1-2|+|2-2| = 1

((2,1),(2,2)), with the cost |2-2|+|1-2| = 1

The sum of these costs is 8.

Sample Input 2

4 5 4

Sample Output 2

87210

Sample Input 3

100 100 5000

Sample Output 3

817260251

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


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

int main(void){
    int num, att, i;
    char *message;

    scanf("%d %d", &num, &att);
    message=(char*)malloc(sizeof(char)*(num+1));
    (void)getchar();
    for(i=0;i<num;i++){
        scanf("%c", &message[i]);
    }

    message[att-1]+=32;
    printf("%s\n", message);
    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


#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
int main() {
	int num, num1;
	char str[50];
	scanf("%d %d", &num, &num1);

		scanf("%s", str);

	for (int i = 0; i < num; i++) {

		if (num1 -1== i) {
			if ('A'==str[i]) {
				str[i ] = 'a';
			}
			else if ('B'==str[i ]) {
				str[i] = 'b';
			}
			else if ('C'==str[i]) {
				str[i] = 'c';
			}

		}
	}

			printf("%s", str);

	}


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

int main(int argc, char const *argv[])
{
	int n;
	int l,r;

	scanf("%d",&n);
	l = n / 100;
	r = n % 100;

	if( 1 <= l && l <= 12 )
	{
		if( 1 <= r && r <= 12 )
			puts("AMBIGUOUS");
		else
			puts("MMYY");
	}
	else{
		if( 1 <= r && r <= 12 )
			puts("YYMM");
		else
			puts("NA");
	}

	return 0;
}


#include<stdio.h>

int main(void)
{
  int S,S100,S1;
  scanf("%d",&S);
  S100 = S/100;
  S1 = S - (S100*100);
  if(S100 >= 13){
    if(S1 >= 13){
      printf("NA");
    }
    else if(S1 == 0){
      printf("NA");
    }
    else{
      printf("YYMM");
    }
  }
  else if(S100 == 0){
    printf("NA");
  }
  else{
    if(S1 >= 1 && S1 <=12){
      printf("AMBIGUOUS");
    }
    else if(S1 >= 13 && S1 <= 31){
      printf("MMDD");
    }
    else if(S1 == 0){
      printf("NA");
    }
    else{
      printf("NA");
    }
  }
  return 0;

}


#include<stdio.h>
int main(){
    //printf("ok1");
    int n;
    int k;
    scanf("%d %d",&n,&k);
    int score=1;
    float prob=0;
   // printf("ok");
    for(int i=1;i<=n;i++){
        float p=1;
        while(score<k){
            p=p/(2*n);
            score=i;
            score=score*2;
        }
        prob=prob+p;
    }
    printf("%f",prob);
    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 <math.h>

double N;
double K;

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

	double winPro=0;
	double invN=1/N;

	double m;

	for(double i=1; i<=N; i++){

		for(m=0; ;m++){
			if(K<=pow(2,m)*i){

				break;
			}
		}

		winPro += invN * pow(0.5,m);
	}

	printf("%.13lf\n",winPro);

}


#include <stdio.h>

int main() {
    int N, K, a;
    scanf("%d, %d", &N, &K);
    if(N>50 || N <= 1) return -1;
    else if(K > N) return -1;

    a = N - K + 1;
    printf("%d", a);

    return 0;
}

Score : 100 points

Problem Statement

Snuke has N integers: 1,2,\ldots,N.
He will choose K of them and give those to Takahashi.

How many ways are there to choose K consecutive integers?

Constraints

All values in input are integers.

1 \leq K \leq N \leq 50

Input

Input is given from Standard Input in the following format:

N K

Output

Print the answer.

Sample Input 1

3 2

Sample Output 1

2

There are two ways to choose two consecutive integers: (1,2) and (2,3).

Sample Input 2

13 3

Sample Output 2

11


#include <stdio.h>
int main(void)
{
	int r;
	int g;
	int b;
	int n;
	int x;
	int y;
	int temp;
	int res;

	scanf("%d%d%d%d", &r,&g,&b,&n);
	res = 0;
	x = 0;
	while (x <= n)
	{
		y = 0;
		while (y <= n)
		{
			temp = n - r*x - g*y;
			if (temp >= 0 && temp % b == 0)
			{
				res++;
			}
			y++;
		}
		x++;
	}
	printf("%d\n", res);
	return (0);
}

Score : 200 points

Problem Statement

Snuke has come to a store that sells boxes containing balls. The store sells the following three kinds of boxes:

Red boxes, each containing R red balls

Green boxes, each containing G green balls

Blue boxes, each containing B blue balls

Snuke wants to get a total of exactly N balls by buying r red boxes, g green boxes and b blue boxes.
How many triples of non-negative integers (r,g,b) achieve this?

Constraints

All values in input are integers.

1 \leq R,G,B,N \leq 3000

Input

Input is given from Standard Input in the following format:

R G B N

Output

Print the answer.

Sample Input 1

1 2 3 4

Sample Output 1

4

Four triples achieve the objective, as follows:

(4,0,0)

(2,1,0)

(1,0,1)

(0,2,0)

Sample Input 2

13 1 4 3000

Sample Output 2

87058


#include <stdio.h>
int min(int a,int b){
    if(a<b) return a;
    return b;
}
int max(int a,int b){
    if(a>b) return a;
    return b;
}
int main(void){
    int n;
    char s[10000][15]={0};
    int i,j;
    int cnt=0,first=0,last=0,b=0,sum=0;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s",s[i]);
    }
    for(i=0;i<n;i++){
        j=0;
        while(s[i][j]){
            if(s[i][j]=='A'&&(s[i][j-1]=='B')) cnt++;
            j++;
        }
        if((s[i][j-1]=='A')&&s[i][0]=='B'){
            b++;
        }else if(s[i][j-1]=='A'){
            last++;
        }else if(s[i][0]=='B'){
            first++;
        }
    }
    sum=max(b-1,0);
    if(b!=0){
        sum+=min(first+1,last+1);
    }else{
        sum+=min(first,last);
    }
    printf("%d\n",cnt+sum);//cnt+sum);
    return 0;
}

Score : 400 points

Problem Statement

Snuke has N strings. The i-th string is s_i.

Let us concatenate these strings into one string after arranging them in some order.
Find the maximum possible number of occurrences of AB in the resulting string.

Constraints

1 \leq N \leq 10^{4}

2 \leq |s_i| \leq 10

s_i consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

N
s_1
\vdots
s_N

Output

Print the answer.

Sample Input 1

3
ABCA
XBAZ
BAD

Sample Output 1

2

For example, if we concatenate ABCA, BAD and XBAZ in this order, the resulting string ABCABADXBAZ has two occurrences of AB.

Sample Input 2

9
BEWPVCRWH
ZZNQYIJX
BAVREA
PA
HJMYITEOX
BCJHMRMNK
BP
QVFABZ
PRGKSPUNA

Sample Output 2

4

Sample Input 3

7
RABYBBE
JOZ
BMHQUVA
BPA
ISU
MCMABAOBHZ
SZMEHMA

Sample Output 3

4


#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>

int main (int argc, char const *argv[])
{
    int64_t N;
    char s[(int)1e4][11];
    int64_t initial_b = 0, final_a = 0, both = 0, ans = 0, flag = 0;

    scanf("%"PRId64, &N);
    for (int i = 0; i < N; i++) {
        int p = 0;
        scanf("%s", s[i]);
        while (s[i][p]) {
            if ((s[i][p] == 'A') && (s[i][p + 1] == 'B')) ans++;
            p++;
        }
        if ((s[i][p - 1] == 'A') && (s[i][0] == 'B')) both++;
        else if (s[i][0] == 'B') initial_b++;
        else if (s[i][p - 1] == 'A') final_a++;
    }

    ans += initial_b > final_a ? final_a : initial_b;
    if ((initial_b != 0) || (final_a != 0)) {
        ans += both;
    } else {
        ans += both - 1;
    }

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


#include<stdio.h>
#include<math.h>
int main()
{
	long N;
	long sum=0;
	long i;
	scanf("%ld",&N);
	for(i=1;i<=sqrt(N);i++){
		if(N%i==0){
			sum+=N/i-1;
		}
	}
	printf("%ld",sum);
}

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(){
  int a,b,t;
  scanf("%d%d%d",&a,&b,&t);
  printf("%d\n",b*(t/a));
  return 0;
}

Score : 100 points

Problem Statement

A biscuit making machine produces B biscuits at the following moments: A seconds, 2A seconds, 3A seconds and each subsequent multiple of A seconds after activation.

Find the total number of biscuits produced within T + 0.5 seconds after activation.

Constraints

All values in input are integers.

1 \leq A, B, T \leq 20

Input

Input is given from Standard Input in the following format:

A B T

Output

Print the total number of biscuits produced within T + 0.5 seconds after activation.

Sample Input 1

3 5 7

Sample Output 1

10

Five biscuits will be produced three seconds after activation.

Another five biscuits will be produced six seconds after activation.

Thus, a total of ten biscuits will be produced within 7.5 seconds after activation.

Sample Input 2

3 2 9

Sample Output 2

6

Sample Input 3

20 20 19

Sample Output 3

0


#include<stdio.h>

int main(){
	int a, b, t;
	scanf("%d%d%d", &a, &b,&t);
	printf("%d", (t / a)*b);

	return 0;

}


#include <stdio.h>
int main(void){
	int a,b,t;
	scanf("%d%d%d",&a,&b,&t);
	int ans=t/a*b;

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


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

int main(int argc, char const *argv[]) {
    int N;
    scanf("%d", &N);
    int *t1 = (int *)malloc(sizeof(int) * N);
    int *t2 = (int *)malloc(sizeof(int) * N);
    int i;
    int som = 0;
    for (i = 0; i < N; i++)
        scanf("%d", t1 + i);

    for (i = 0; i < N; i++)
        scanf("%d", t2 + i);

    for (i = 0; i < N; i++) {
        if (t1[i] - t2[i] > 0)
            som += t1[i] - t2[i];
    }

    printf("%d", som);

    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<stdint.h>
#include<inttypes.h>

typedef int32_t i32;

#define MIN(a,b) ((a) < (b) ? (a) : (b))

void run (void) {
  i32 n;
  scanf ("%" SCNi32, &n);
  char *s = (char *) calloc (n + 1, sizeof (char));
  scanf ("%s", s);
  uint8_t *d = (uint8_t *) calloc (n, sizeof (uint8_t));
  i32 cnt[2];
  for (i32 i = 0; i < n; ++i) {
    d[i] = (s[i] == '.' ? 0 : 1);
    cnt[d[i]]++;
  }
  i32 min = MIN(cnt[1], cnt[0]);
  i32 lcnt[2] = {0, 0};
  for (i32 i = 0; i < n; ++i) {
    cnt[d[i]]--;
    lcnt[d[i]]++;
    min = MIN(min, cnt[0] + lcnt[1]);
  }
  printf ("%" PRIi32, min);
}

int main (void) {
  run();
  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){
    int N,K,n;
    char S[20];

    scanf("%d",&N);
    scanf("%s",S);
    scanf("%d",&K);

    for(n=0 ; n < N ; n++){
        if(S[n] != S[K-1]){
            S[n] = '*';
        }
    }
    printf("%s\n",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**


 int main() {
   int ablack=0,black=0;
  int  white=0,rwhite=0,rblack=0,sum=0,n,i;

   scanf("%d",&n);
   char s[n];
   scanf("%s",s);

   for(i=0;i<n;i++){
  if(s[i]=='.')rwhite++;
     if(s[i]=='#'){rwhite=0;ablack;}
   }
   n=n-rwhite;
       for(i=0;i<n;i++){
  if(s[i]=='.')rblack=0;
     if(s[i]=='#')rblack++;
   }

   n=n-rblack;

    for(i=0;i<n;i++){
  if(s[i]=='.')white++;
     if(s[i]=='#')black++;
   }

   if(rblack>rwhite)
    sum=rwhite;
   else
     sum=rblack;

   if(black<white)
     sum=sum+black;
   else
     sum=sum+white;
   printf("%d",sum);
   return 0;
 }


#include <stdio.h>
int main(){
    long N;
    scanf("%ld", &N);
    char S[N+1];
    scanf("%s", S);
    S[N] = '\0';
    long cnt = -1;
    for(long i=0;i<N;i++){
        if(S[i]=='#'){
            cnt = i;
            break;
        }
    }
    long ans = 0;
    for(long i=cnt;i<N;i++){
        if(S[i] == '.'){
            ans ++;
        }
    }
    if(cnt == -1){
        ans = 0;
    }
    if(ans > N/2){
        ans = N - ans;
    }
    printf("%ld\n", ans);
    return 0;
}


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

int main(void) {
	int n;
	char s[200001];
	int flag = 0, count = 0;

	scanf("%d %s", &n, &s[0]);

	for (int i = 0; i < n; i++) {
		if (s[i] == '#') {
			flag = 1;
		} else {
			if (flag)
				count++;
		}
	}

	printf("%d", count);

	return 0;
}


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

  if(a>b) num=a+a-1;
  if(b>a) num=b+b-1;
  if(a==b) num=a+b;

  printf("%d",num);

  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 sum=0,N,H,i,max=0;
  scanf("%d",&N);
  for(i=0;i<N;i++){
    scanf("%d",&H);
    if(H>=max){
      sum++;
      max=H;
    }

  }
  printf("%d\n",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>
#include<string.h>
#define S 100
int main()
{
  char a[S];
  int m,k,n,i;
  m=0;
  k=0;
  scanf("%s",a);
  n=strlen(a);
  char b[S],c[S];
  for(i=0;i<n;i++)
  {
      if(i%2==0)
           {
               b[i]='1';
               c[i]='0';

           }
      else
         {
              b[i]='0';
              c[i]='1';
         }
  }
   for(i=0;i<n;i++)
     {
         if(a[i]!=b[i])
               m++;
     }
   for(i=0;i<n;i++)
     {
         if(a[i]!=c[i])
               k++;
     }
   if(m<k)
        printf("%d",m);
   else
        printf("%d",k);
   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 <string.h>

int main(){
  int i,flag=0,acount=0,bcount=0;
  char s[100001];
  scanf("%s",s);
    for(i=0;i<strlen(s);i++){
      if(flag == 0){
        if(s[i]=='0') acount++;
        else bcount++;
        flag=1;
      }
      else{
        if(s[i]=='1') acount++;
        else bcount++;
        flag=0;
      }
    }

    if(acount>bcount) printf("%d",strlen(s)-acount);
    else printf("%d",strlen(s)-bcount);
  return 0;
}