/*
 * main.c
 *
 *  Created on: 2020/02/22
 *      Author: family
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MAX(a,b) (a > b ? a : b)
#define MIN(a,b) (a > b ? b : a)
typedef long long int ll;
typedef unsigned long long int ull;

int sort_inc(const void *a, const void *b) { return (*(int*)a - *(int*)b);}
int sort_dec(const void* a, const void* b) { return (*(int*)b - *(int*)a);}
int sort_dec_ll(const void *a, const void *b) {
    ll da = *(ll*)a, db = *(ll*)b;    int val = 0;
    if(da > db) { val = -1; }
    else if (da == db) { val = 0; }
    else { val = 1; }
    return val;
}
int sort_inc_ll(const void *a, const void *b) {
    ll da = *(ll*)a, db = *(ll*)b;    int val = 0;
    if(da > db) { val = 1; }
    else if (da == db) { val = 0; }
    else { val = -1; }
    return val;
}
int sort_dic(const void *a, const void *b) {
    char *pa = (char *)a;    char *pb = (char *)b;    int i = 0, val = 0, N = 10;
    for (i = 0; i < N; i++) {
    	char da = pa[i], db = pb[i];
    	if (da == db) continue;
  		if (da > db) val = 1; else val = -1;
        break;
    }
    return val;
}

int main()
{
	int a = 0, b = 0, c = 0;
	int ans = 0;
	scanf("%d %d %d", &a, &b, &c);
	if ((c - a - b) < 0) {
		ans = 1;
	} else {
		if ((4*a*b) < ((c-a-b) * (c-a-b))) {
			printf("Yes\n");
		} else {
			ans = 1;
		}
	}
	if (ans == 0) {
		printf("Yes\n");
	} else {
		printf("No\n");
	}
    return 0;
}


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

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

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

    d = c - a - b;
    if(d > 0 && d*d > 4*a*b)printf("Yes\n");
    else printf("No\n");

    return 0;
}

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

Score : 300 points

Problem Statement

Does \sqrt{a} + \sqrt{b} < \sqrt{c} hold?

Constraints

1 \leq a, b, c \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

a \ b \ c

Output

If \sqrt{a} + \sqrt{b} < \sqrt{c}, print Yes; otherwise, print No.

Sample Input 1

2 3 9

Sample Output 1

No

\sqrt{2} + \sqrt{3} < \sqrt{9} does not hold.

Sample Input 2

2 3 10

Sample Output 2

Yes

\sqrt{2} + \sqrt{3} < \sqrt{10} holds.

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

int main(void)
{
 long long a;
 long long b;
 long long c;

 scanf("%ld%ld%ld", &a, &b, &c);
 long double da = sqrt(a);
 long double db = sqrt(b);
 long double dc = sqrt(c);

 if ((da + db) < dc)
 {
  printf("Yes");
 }
 else
 {
  printf("No");
 }
 return 0;
}


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

int main(){
    double a,b,c;
    scanf("%lf %lf %lf",&a,&b,&c);
    if(4*a*b<(c-a-b)*(c-a-b))
        printf("Yes\n");
    else
        printf("No\n");

    return 0;
}


#include<stdio.h>
#include<math.h>
int main()
{
    long double a,b,c;
    scanf("%f%f%f",&a,&b,&c);
    long double sq_a=sqrt(a);
    long double sq_b=sqrt(b);
    long double sq_c=sqrt(c);
    if(sq_a+sq_b<sq_c){
        printf("Yes\n");
    }else{
        printf("No\n");
    }
    return 0;

}


#include<stdio.h>
#include<math.h>
int main(void)
{
	double a,b,c;
	scanf("%f", &a);
	scanf("%f", &b);
	scanf("%f", &c);

	a=sqrt(a);
	b=sqrt(b);
	c=sqrt(c);

	if(a+b < c){
		puts("Yes");
	}else{
		puts("No");
	}
	return 0;
}


#include <stdio.h>
#include <math.h>
int main()
{
  float a,b,c;
  scanf("%f%f%f",&a,&b,&c);
  if(sqrt(a)+sqrt(b)<sqrt(c))
    printf("Yes");
  else printf("No");
   return 0;
}


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

int main(void){

  double a,b,c;
  scanf("%lf",&a);
  scanf("%lf",&b);
  scanf("%lf",&c);

  // double pi = 100.123456789101112;
  // double ii = 83.435614514613452346;
  // printf("%.17lf",pi+ii);

  if(sqrtl(c)-sqrtl(a)-sqrtl(b)>0){
    printf("Yes");
  }
  else{
    printf("No");
  }

}


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

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

    double ab=a*b;
    double sq_ab=sqrt(ab);

    if(a+b+2*sq_ab<c){
        printf("Yes\n");
    }else{
        printf("No\n");
    }
    return 0;

}


#include<stdio.h>
#include<math.h>
int main(void){
    double a, b, c;
    double sa, sb, sc;
    scanf("%f%f%f",&a,&b,&c);
    sa = sqrt(a);
    sb = sqrt(b);
    sc = sqrt(c);
    if(sa+sb < sc){
        printf("Yes\n");
    }else{
        printf("No\n");
    }
    return 0;
}


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

int main(void){
  int a, b, c;
  double ra, rb, rc;
  double sa;

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

  ra = sqrt(a); rb = sqrt(b); rc = sqrt(c);
  sa = rc - (ra + rb);

  if(sa > 0)  printf("Yes\n");
  else  printf("No\n");

  return 0;
}


#include<stdio.h>
#include<math.h>
int main()
{
	double a,b,c;
	scanf("%lf %lf %lf",&a,&b,&c);
	if((sqrt(a) + sqrt(b)) < sqrt(c)) printf("Yes");
	else printf("No");
	return 0;
}


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

int	main(void)
{
  	long long int	a;
  	long long int	b;
  	long long int	c;
  	int	num;

  	num = scanf("%lld %lld %lld", &a, &b, &c);
  	if (num != 3)
      	return (0);

  	if (sqrt((double)a) + sqrt((double)b) < sqrt((double)c))
    {
      	printf("Yes");
    }
  	else
    {
      	printf("No");
    }
  	return (0);
}


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

int main(){
    long int a,b,c;
    long double aa,bb,cc;
    scanf("%ld %ld %ld",&a,&b,&c);
    aa = sqrtl(a);
    bb = sqrtl(b);
    cc = sqrtl(c);
    if(aa+bb<cc){
        printf("Yes\n");
    }else{
        printf("No\n");
    }
    return 0;
}


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

#define mod 1000000007

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

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

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

    if(((double)sqrt(a) + sqrt(b) < sqrt(c))) printf("Yes\n");
    else printf("No\n");

    return 0;
}


Score : 300 points

Problem Statement

If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1.

The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer. For other integers, leading zeros are not allowed.)

The s_i-th digit from the left is c_i. \left(i = 1, 2, \cdots, M\right)

Constraints

All values in input are integers.

1 \leq N \leq 3

0 \leq M \leq 5

1 \leq s_i \leq N

0 \leq c_i \leq 9

Input

Input is given from Standard Input in the following format:

N M
s_1 c_1
\vdots
s_M c_M

Output

Print the answer.

Sample Input 1

3 3
1 7
3 2
1 7

Sample Output 1

702

702 satisfies the conditions - its 1-st and 3-rd digits are 7 and 2, respectively - while no non-negative integer less than 702 satisfies them.

Sample Input 2

3 2
2 1
2 3

Sample Output 2

-1

Sample Input 3

3 1
1 0

Sample Output 3

-1


#include<stdio.h>
int main(void)
{
  int n,m;
  scanf("%d%d",&n,&m);
  int ans[n+1],tmp[n+1],flag=0;
  for(int i=0;i<=n;i++){
    ans[i]=0;
    tmp[i]=0;
  }
  for(int i=0;i<m;i++){
    int s,c;
    scanf("%d%d",&s,&c);
    //printf("%d %d\n",s,c);
    tmp[s]++;
    if(tmp[s]==1) ans[s]=c;
    else if(tmp[s]>1){
      if(ans[s]!=c){
        flag=1;
        break;
      }
    }
  }
  if(tmp[1]==0) ans[1]=1;
  if((tmp[1]>0) && (ans[1]==0)) flag=1;
  if(flag==1) printf("-1");
  else if(flag==0){
    for(int i=1;i<=n;i++) printf("%d",ans[i]);
  }
  return 0;
}


#include<stdio.h>
#include<math.h>
int main() {
  int i,n,m,s,c,x = 0;
  scanf("%d %d",&n,&m);
  int ansnum[n + 1],firstcheck[n + 1];

  for (i = 1;i <= n;i ++) {
    ansnum [i] = 0;
    firstcheck[i] = 1;
  }
  for (i = 1;i <= m;i ++) {
    scanf("%d %d",&s,&c);
    if (firstcheck[s] == 1) {
      ansnum[s] = c;
      firstcheck[s] = 0;
    }
    else if (ansnum[s] != c) {
      ansnum[1] = 0;
      firstcheck[1] = 0;
      break;
    }
  }
  if (m == 0 && n != 1) {
    ansnum[1] = 1;
  }
  if (n == 1 && ansnum[1] == 0) {
    x = 0;
    printf("%d",x);
  }
  else if (firstcheck[1] == 1) {
    ansnum[1] = 1;
  }
  else if (ansnum[1] != 0) {
      for (i = n - 1;i >= 0;i --) {
        x = x + ansnum[n - i] * pow(10,i);
      }
      printf("%d",(int)x);
    }
    else {
      x = -1;
      printf("%d",x);
    }
    //for (i = 0;i < n;i ++) {
      //printf("%d",firstcheck[i]);
    //}
}


#include<stdio.h>
int main(void){
    int N, M, d, e, check = 0, keta, jyousuu, i;
    scanf("%d", &N);
    scanf("%d", &M);
    int s[M], c[M], A[N];
    for(i = 0; i < M; i++){
        scanf("%d", &s[i]);
        scanf("%d", &c[i]);
    }
    //事前準備として、答えのもとになるA[N]を初期化
    for(i = 0; i < N; i++){
        A[i] = 0;
    }
    //まずs[M]を基準に降順にしたい
    for(i = 0; i < M; i++){
        for(int j = i + 1; j < M; j++){
            if(s[i] < s[j]){
                d = s[i];
                s[i] = s[j];
                s[j] = d;
                d = c[i];
                c[i] = c[j];
                c[j] = d;
            }
        }
    }
    //入力で同じ桁に異なる数値が定義されていないかチェック
    for(i = 0; i < M - 1; i++){
        e = i + 1;
        if(s[i] == s[e]){
            if(c[i] != c[e]){
                printf("-1\n");
                check += 1;
            }
        }
    }
    //入力される最大桁数がNより大きいと絶対にアウト。if文で囲まないとコンパイルエラーに
    if(N < s[0]){
        printf("-1\n");
        check += 1;
    }
    else{
    //(N-1)桁目から指定されてる最大桁数までの数を0に。また一桁目が0にならないかチェック
    if(s[0] > 1){
       A[0] = 1;
       for(i = 1; i < s[0]; i++ ){
           A[i] = 0;
       }
    }
    else {
      if(c[0] == 0){
        printf("-1\n");
        check += 1;
      }
    }
    //あとは各桁どんどん代入してく
    for(i = 0; i < M; i++){
        keta = s[i] - 1;
        A[keta] = c[i];
    }
    //最後にcheckを活用し、エラーがなければ答えを出力
    if(check == 0){
        int a = 0, x;
        for(i = 0; i < N; i++){
            jyousuu = 1; x = N - i - 1;
            for(int k = 0; k < i; k++) jyousuu *= 10;
            a += A[x] * jyousuu;
        }
        printf("%d", a);
    }
    }
    return 0;

}


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

int main ()
{

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

 if (M==0 && N==3)
 {
 printf("100");
 return 0;
 }
 else if (M==0 && N==1)
 {
 printf("0");
 return 0;
 }
 else if (M>1 &&  N==1)
 {
 printf("-1");
 return 0;
 }
 else if (M==0 && N==2)
 {
 printf("10");
 return 0;
 }
 int S[M];
 int C[M];
 int res[M];

memset(res,0,sizeof(res[0]) * M);

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

  for (int i = 0;i<M;i++)
  {
     res[ S[i]-1 ] = C[i];
  }

 for (int i = 0;i<M;i++)
  {
    if (res[ S[i] -1  ] > C[i] )res[ S[i] - 1]=C[i];
  }

 if (res[0]==0)
  {
   printf("-1");
   return 0 ;
  }

  for (int i = 0;i<N;i++)
  {
    printf("%d",res[i]);

  }

 return 0;
}


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

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

    for(int i = 0;i < 1000;i++){
        int a[3];
        int nx = i/10;
        int keta = 1;
        a[2] = i%10;
        int k = 2;
        while(nx > 0){
            keta++;
            k--;
            a[k] = nx%10;
            nx /= 10;
        }
        if(keta != n) continue;
        int flag = 0;
        for(int j = 0;j < m;j++){
            if(c[j] != a[s[j] - 1]) flag = 1;
        }
        if(flag == 0){
            printf("%d",i);
            return 0;
        }
    }
    printf("-1");
    return 0;
}


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

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

    for(int i = 0;i < 1000;i++){
        int a[3];
        int nx = i/10;
        int keta = 1;
        a[0] = i%10;
        int j = 0;
        while(nx > 0){
            keta++;
            j++;
            a[j] = nx%10;
            nx /= 10;
        }
        if(keta != n) continue;
        int flag = 0;
        for(int j = 0;j < m;j++){
            if(c[j] != a[3 - s[j]]) flag = 1;
        }
        if(flag == 0){
            printf("%d",i);
            return 0;
        }
    }
    printf("-1");
    return 0;
}


#include <stdio.h>
int main(void)
{
 int N,M;
 int g = 0;
 int s = 0;
 int b = 0;
 int i,j;
 int A[M];
 int B[M];
 int tmp;
 int true_false = 0;

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

 if(N==1)
  {
   for(i=0;i<M-1;i++)
    for(j=i+1;j<M;j++)
    {
      if(B[i] == B[j]) printf("-1\n");
      goto L_Break;
    }
   printf("%d\n",B[j]);
  }
 else if (N==2)
  {
   for(i=0;i<M-1;i++)
    for(j=i+1;j<M;j++)
    {
      if(B[i] != B[j] && A[i] == A[j]) printf("-1\n");
      goto L_Break;
    }
  for(i=0;i<M;i++)
   {
   if(A[i] == 1) s = B[i]*10;
   else if(A[i] == 2) g = B[i];
   if(s == 0)
	{printf("-1\n");
      goto L_Break;}
   }
  printf("%d\n",s+g);
  }
 else if (N==3)
  {
   for(i=0;i<M-1;i++)
    for(j=i+1;j<M;j++)
    {
      if(B[i] != B[j] && A[i] == A[j])
      {printf("-1\n");
      goto L_Break;}
    }
  for(i=0;i<M;i++)
   {
   if(A[i] == 1) b = B[i]*100;
   else if(A[i] == 2) s = B[i]*10;
   else if(A[i] == 3) g = B[i];

   if(b==0)
    {printf("-1\n");
      goto L_Break;}

   }
  printf("%d\n",b+s+g);
   }

 L_Break:

 return 0;
}


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

int main() {

	int n, m;

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

	int tmp[3] = {10, 10, 10};
	int s[5];
	int c[5];

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

	for(int i = 0; i < m; i++) {
		if( (tmp[s[i] -1]) == 10) {
			tmp[s[i] -1] = c[i];
		}
		else if( (tmp[s[i] -1]) == c[i]){
			continue;
		}
		else{
			printf("-1\n");
			return 0;
		}
	}

	for(int i = 0; i < n; i++) {
		if(tmp[i-1] == 10) {
			tmp[i-1] = 0;
		}
	}

	int ans = 0;
	if(n == 3) {
		if( tmp[0] == 0 ){
			printf("-1\n");
			return 0;
		}
		ans = tmp[0] * 100 + tmp[1] * 10 + tmp[2];
	} else if( n == 2) {
		if( tmp[0] == 0 ){
			printf("-1\n");
			return 0;
		}
		ans = tmp[0] * 10 + tmp[1];
	} else if( n == 1) {

		ans = tmp [0];
	}

	printf("%d", ans);

	return 0;

}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define TIME 3

int main(void){
int n,m,s[6],c[6];
int same[10][10];
scanf("%d %d",&n,&m);

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

//same S search

for(int i=0;i<m;i++){
    for(int j=0;j<m;j++){
        if((s[i]==s[j])&&(c[i]!=c[j])){printf("-1\n");return 0;}
    }
}

//ignore 05 005 etc

for(int j=0;j<m;j++){
    if(s[j]==1&&c[j]==0&&n!=1){printf("-1\n");return 0;}
}

int keta=1;
for(int i=0;i<m;i++){
    int min=99;
    for(int j=0;j<m;j++){
        if(s[j]==keta&&c[j]<=min){min=c[j];}
    }
    if(min==99){
        if(i==0){printf("1");}
        else{
        printf("0");
        }
    }
    else{printf("%d",min);}
    keta++;
}

}


#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;
//    }
//}
long long  factorial(long long n, long long depth) {
    if (n > 1) {
        depth++;
        return factorial(n / 2, depth);
    } else {
        return depth;
    }
}

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 triangle(int a, int b, int c){
  if(a<b+c){
    if(b<c+a){
      if(c<a+b){
        printf("1\n");
        return 1;
      }
    }
  }
  printf("1\n");
  return 0;
}
*/

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

  int  flg=0;

//  char s[101][101]={};
  char s[100001]={};
  char t[100001]={};
//  int N, M;
//  int A[1000000001]={};
//  long long  C[101]={};
  int i,j,k;
  long long p=0,q=0;
//  int flag=0;
//  int tmp=0;
//  char tmp[101]={};
//  long long min=10000000000;
//  long long min=100000001;
  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=0;
  int ans_ac=0;
  int ans_wa=0;
  int max=0;
  long long sum=0;
  int addr=0;
  int ptn=0;
  int  tmp=0;
  double per=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("%d%d", &a,&b);
  for(i=0;i<b;i++){
    scanf("%d%d", &A[i],&B[i]);
    if(A[i]==1 && B[i] ==0 && b!=1){
      printf("-1\n");
      return 0;
    }
  }
  for(i=0;i<b;i++){
    if(C[A[i]-1]==0)C[A[i]-1]=B[i];
    else if (C[A[i]-1]==B[i]) ;
    else {
      printf("-1\n");
      return 0;
    }
  }

  for(i=0;i<a;i++){
    if(i==0 && C[0]==0 && b!=1) C[0]=1;
    printf("%d",C[i]);
  }
  printf("\n");

return 0;
}


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

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

    int nums[digits];
    for (int i = 0; i < digits; i++) {
        nums[i] = -1;
    }
    for (int i = 0; i < count; i++) {
        int digit, num;
        scanf(" %d %d", &digit, &num);
        if (nums[digit - 1] < 0) {
            nums[digit - 1] = num;
        } else {
            if (nums[digit - 1] != num) {
                printf("-1\n");
                return 0;
            }
        }
    }

    int result = 0, times = 1, flag = 1;
    for (int j = 0; j < digits - 1; j++) {
        times *= 10;
    }
    for (int i = 0; i < count; i++) {
        if (flag && !nums[i]) {
            break;
        } else if (nums[i] > 0) {
            result += nums[i] * times;
            flag = 0;
        }
        times /= 10;
    }
    if (!flag) {
        printf("%d\n", result);
    } else if (digits == 1) {
        printf("0\n");
    } else {
        printf("-1\n");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define ll long long
const int MOD = 1000000007;

int main()
{
    int a[6] = { 0 }, b[3] = { 10,10,10 };
    int m = 0, n = 0,i = 0,num = 0;
    scanf("%d %d", &n, &m);
    for (i = 0; i < m; i++)
    {
        scanf("%d %d", &a[i],&num);
        if (num < b[a[i]-1])
        {
            b[a[i]-1] = num;
        }
    }
    if (b[0] == 0 || b[0] == 10)
    {
        printf("-1\n");
    }
    else
    {
        for (i = 0; i < n; i++)
        {
            if (b[i] != 10)
                printf("%d", b[i]);
            else
                printf("0");
        }
        printf("\n");
    }
    return 0;
}


#include<stdio.h>

int main(void){

    int N,M,s[4],c[4],i,j,ans[6] = {0},max,tmp1,tmp2;

    scanf("%d %d",&N,&M);
    for(i = 0;i < M;i++){
        scanf("%d %d",&s[i],&c[i]);
    }

    for(i = 0;i < M;i++){
        if(max < s[i]) max = s[i];
        ans[s[i]] = c[i];
    }

    if(ans[1] == 0){
        printf("-1\n");
    }else{
        for(i = 1;i <= max;i++) printf("%d\n",ans[i]);
        printf("\n");
    }
    return 0;
}


#include <stdio.h>

int main(void)
{
    int N, M, i, j,keta_tmp;
    int s[10],c[10];
    int suuzi[3] = {-1,-1,-1};

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

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

    for(i=0; i<M; i++){//左からs[i]桁目がc[i]である
        keta_tmp = s[i]-1;
        if((keta_tmp == 0) && (c[i] == 0)){//一番上の桁が0の場合は存在しないので-1
            printf("-1\n");
            return 0;
        }
        if((suuzi[keta_tmp] == -1)||(suuzi[keta_tmp] == c[i])){//未入力か一緒の場合
            suuzi[keta_tmp] = c[i];
        } else {//一つの桁に違う数字を指定された場合
            printf("-1\n");
            return 0;
        }
    }

    for(i=0; i<N; i++){
        if(suuzi[i] == -1){
            if(i==0){
                printf("1");
            } else {
                printf("0");
            }
        } else {
            printf("%d",suuzi[i]);
        }
        //-1の時は0を代入するっていうのを後で書く
        //一番上の桁が指定されてない時は1
    }
    printf("\n");

    return 0;
}


Score: 400 points

Problem Statement

We have N integers A_1, A_2, ..., A_N.

There are \frac{N(N-1)}{2} ways to choose two of them and form a pair. If we compute the product of each of those pairs and sort the results in ascending order, what will be the K-th number in that list?

Constraints

All values in input are integers.

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

1 \leq K \leq \frac{N(N-1)}{2}

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

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 \dots A_N

Output

Print the answer.

Sample Input 1

4 3
3 3 -4 -2

Sample Output 1

-6

There are six ways to form a pair. The products of those pairs are 9, -12, -6, -12, -6, 8.

Sorting those numbers in ascending order, we have -12, -12, -6, -6, 8, 9. The third number in this list is -6.

Sample Input 2

10 40
5 4 3 2 -1 0 0 0 0 0

Sample Output 2

6

Sample Input 3

30 413
-170202098 -268409015 537203564 983211703 21608710 -443999067 -937727165 -97596546 -372334013 398994917 -972141167 798607104 -949068442 -959948616 37909651 0 886627544 -20098238 0 -948955241 0 -214720580 277222296 -18897162 834475626 0 -425610555 110117526 663621752 0

Sample Output 3

448283280358331064


#include<stdio.h>

int main (void){
  int N,K,num[200000],cnt=0,buf;
  int id;
  scanf("%d %d",&N,&K);
  while(N>cnt){
      scanf("%d",&num[cnt]);
      cnt ++;
  }
    int i,j,temp;

  for(i=0; i<N; i++){
    for(j=N-1; j>i; j--){
      if(num[j] < num[j-1]){
        temp = num[j];
        num[j] = num[j-1];
        num[j-1] = temp;
      }
    }
  }

  printf("%d",num[K]*num[K-1]);
  return 0;

}


#include <stdio.h>
#include <math.h>
#define N_MAX 210000

int Sort( int *a, int len );

int main (){

    //標準入力から条件を入力
    int N, K;
    int A[N_MAX];
    scanf("%d%d",&N,&K);
    for ( int i=0; i<N; i++ ){
        scanf("%d",&A[i]);
    }

    //multiple[]に、for二重ループにより積の値を代入していく
    int multiple[N*N/2];
    int *p = &multiple[0];

    for ( int i=0; i<N-1; i++ ){
        for ( int j=i+1; j<N; j++ ){
            *p = ( A[i] * A[j] );
            p++;
        }
    }

    //バブルソートにより小さい順に並べ替える。
    Sort( multiple, N );

    printf("%d",multiple[K-1]);
    return 0;
}

//*バブルソート関数*//
int Sort ( int *a, int len ){
    int tmp;
    for ( int i=0; i<len; i++ ){
        for ( int j=len-1; j>i; j-- ){
            if( a[j-1] > a[j] ){
                tmp = a[j-1];
                a[j-1] = a[j];
                a[j] = tmp;
            }
        }
    }
    return *a;
}


#include<stdio.h>

#define SWAP(type,a,b) { type work = a; a = b; b = work; }

/*
    基準値を選ぶ
*/
long int select_pivot(long int* array, long int begin, long int end)
{
    return array[(begin + end) / 2];  // 中央の要素を基準値とする
}

/*
    クイックソート (再帰部分、昇順)
*/
void quick_sort_rec(long int* array, long int begin, long int end)
{
    long int pivot = select_pivot( array, begin, end );
    long int i = begin;
    long int j = end;

    while( 1 ){
        while( array[i] < pivot ){ ++i; }  // 基準値以上の値が見つかるまで右方向へ進めていく
        while( array[j] > pivot ){ --j; }  // 基準値以下の値が見つかるまで左方向へ進めていく

        if( i >= j ){ break; }  // 左右から進めてきたiとjがぶつかったらループを終える

        // 基準値の位置よりも左側にあり、基準値よりも大きい値 (array[i]) と、
        // 基準値の位置よりも右側にあり、基準値よりも小さい値 (array[j]) の
        // 位置関係を交換する。
        SWAP(long int, array[i], array[j] );

        // 次回に備えて、注目点をずらす
        i++;
        j--;
    }

    // 基準値の位置より左側に要素が２つ以上あれば、
    // その部分についてクイックソートを再帰させる
    if( i - begin >= 2 ){
        quick_sort_rec( array, begin, i - 1 );
    }

    // 基準値の位置より右側に要素が２つ以上あれば、
    // その部分についてクイックソートを再帰させる
    if( end - j >= 2 ){
        quick_sort_rec( array, j + 1, end );
    }
}

void sort(long int* array, long int size){
    // 配列全体を対象にする
    quick_sort_rec( array, 0, (long int)(size - 1) );
}

void set_data(long int data[], long int n){
    for(long int i = 0; i < n; i++){
        scanf("%ld", &data[i]);
    }
}

void calc_data(long int data[], long int pair[], long int n){
    long int x = 0;
    for(long int i = 0; i < n; i++){
        for(long int j = 0; j < i; j++){
            pair[x] = data[i] * data[j];
            /*`printf("%ld ", pair[x]);*/
            x++;
        }
    }
    /*printf("\n");*/
}

void print_array(long int data[], long int n){
    for(long int i = 0; i < n; i++){
        printf("%ld ", data[i]);
    }
    printf("\n");
}

int main(void){
    long int n, k;
    scanf("%ld""%ld", &n, &k);
    long int data[n], pairs[(n * (n - 1)) / 2];
    set_data(data, n);
    /*print_array(data, n);*/
    calc_data(data, pairs, n);

    /*print_array(pairs, (n * (n - 1)) / 2);*/
    sort(pairs, (n * (n - 1)) / 2 );

    printf("%ld\n", pairs[k - 1]);
}


#include <stdio.h>
int main(void)
{
    int A,B;
    scanf("%d %d",&A,&B);
    if(A > 9 || B > 9){
        printf("%d",-1);
    }else{
        printf("%d",A*B);
    }
}

Score : 100 points

Problem Statement

Having learned the multiplication table, Takahashi can multiply two integers between 1 and 9 (inclusive) together. He cannot do any other calculation.

Given are two integers A and B.

If Takahashi can calculate A \times B, print the result; if he cannot, print -1 instead.

Constraints

1 \leq A \leq 20

1 \leq B \leq 20

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If Takahashi can calculate A \times B, print the result; if he cannot, print -1.

Sample Input 1

2 5

Sample Output 1

10

2 \times 5 = 10.

Sample Input 2

5 10

Sample Output 2

-1

5\times 10 = 50, but Takahashi cannot do this calculation, so print -1 instead.

Sample Input 3

9 9

Sample Output 3

81


#include <stdio.h>

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


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


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

//#define NDEBUG

#ifdef NDEBUG
#define DEBUG_PRINT(fmt, ...) ((void)0)
#else
#define DEBUG_PRINT(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
#endif
#define ASSERT_RANGE(min,x,max) assert(((x)>=(min)) && ((x)<=(max)))

#define N_MIN 1
#define N_MAX 20

int sort_int(int *a, int N) {// {{{
	int i;
	for (i = 0; i < N-1; i++) {
		int j;
		for (j = i + 1; j < N; j++) {
			if (a[i] > a[j]) {
				int tmp = a[i];
				a[i] = a[j];
				a[j] = tmp;
			}
		}
	}
	return 0;
}// }}}

int main(int argc, char **argv) {
	int A,B;
	fscanf(stdin,"%d%d",&A,&B);
	ASSERT_RANGE(N_MIN,A,N_MAX);
	ASSERT_RANGE(N_MIN,B,N_MAX);

	if ((A<10)&&(B<10)) {
		fprintf(stdout,"%d\n",A*B);
	} else {
		fprintf(stdout,"-1\n");
	}
	return 0;
}


#include <stdio.h>

int main(void){
  long int a,b;
  long int res;
  scanf("%d %d",&a,&b);
  res = a * b;
  printf("%d\n",res);

  return 0;
}


#include <stdio.h>

int main(void){
	int a, b, ans;

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

  	ans = a * b;

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

	return 0;
}


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


#include <stdio.h>
int main(void) {
  int x,y;
  scanf("%d %d",&x,&y);
  if (x*y<82) {
    printf("%d\n",x*y);
  } else {
    printf("-1\n");
  }
  return 0;
}


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


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

int main()
{
   int A,B,c;
   printf("please input integerA B:");
   scanf("%d",&A);
   scanf("%d",&B);
   if(A>9||B>9)
     printf("c=-1\n");
   else
   {
     c=A*B;
   printf("%d\n",c);
   }
   return 0;
}


#include<stdio.h>

int main(void){
    int x,y;
    scanf("%d%d",&x,&y);
    if(x>=10 || y>=10){
        printf("-1\n");
    }else{
        printf("%d\n",x*y);
    }
    return 0;
}


#include<stdio.h>
int main()
{
    int n,x,y,o,e,i;
    scanf("%d",&n);
    for(i=sqrt(n); i>=1; i--)
    {
        if(n%i==0)
        {
            x=i;
            y=n/i;
            if(x>9||y>9)
            {
                printf("No");
                break;
            }
            else
            {
                printf("Yes");
                break;
            }
        }
    }
    return 0;
}


#include<stdio.h>
int main(void){
    int A,B,count=0;
    scanf("%d%d",&A,&B);
    count=A*B;
    if(A>10||A<0)
        count=-1;
    if(B>10||B<0)
        count=-1;
    printf("%d\n",count);
    return 0;
}


#include "stdio.h"

int main(){
  int x,y;
  scanf("%d %d",&x,&y);
  if(x<=9&&y<=9){printf("%d",x*y);}
  else{ printf("-1");}
  return 0;
}


#include <stdio.h>
#include <string.h>
typedef long long ll;
int main() {
	char s[101];
	int k;
	scanf("%s%d", s, &k);
	int n = strlen(s), cnt = 1;
	ll ans = 0;
	for (int i = 0; i < n; i++) {
		if (i != n - 1 && s[i] == s[i + 1]) {
			cnt++;
		} else {
			ans += cnt / 2;
			cnt = 1;
		}
	}
	ans = ans * k;
	if (s[0] == s[n - 1]) {
		int t = 1, h = 1;
		while (s[t] == s[0]) {
			t++;
			if (t == n) {
				printf("%lld\n", n * k / 2);
				return 0;
			}
		}
		while (s[n - h - 1] == s[n - 1]) {
			h++;
		}
		ans -= ((ll)(t / 2 + h / 2 - (t + h) / 2) * (k - 1));
	}
	printf("%lld\n", ans);
	return 0;
}

Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997


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

int main()
{
    char s[100];
    int i;
    long long k,ans = 0;

    scanf("%s %lld",s,&k);

    int len = strlen(s);
    int cnt = 0,a = 0,b = 0;

    for (i = 1; i < len; i++)
    {
        if(s[i - 1] != s[i])break;
    }
    a = i;
    if(i == len)
    {
        printf("%lld",k * len / 2);
    }
    else
    {
        for (i = 1; i < len; i++)
        {
            if(s[i - 1] == s[i])cnt++;
            else
            {
                ans += cnt / 2;
                cnt = 0;
            }
        }
        ans += cnt / 2;

        if(s[0] == s[len - 1])
        {
            for (i = len; i > 0 ;i--)
            {
                if(s[i - 1] != s[i])break;
            }
            b = len - i;
            printf("%lld",k * ans - (((a / 2 + b / 2) - (a + b) / 2)) * (k - 1));
        }
        else
        {
            printf("%lld",k * ans);
        }

    }

}


#include<stdio.h>

int main(){
    char s[101];
    scanf("%s", s);
    int n=0;
    while(s[n] != '\0'){
        n++;
    }
    long long int k;
    scanf("%lld", &k);
    if(n == 1){
        printf("%lld", k/2);
    }else if(n == 2 && s[0] == s[1]){
        printf("%lld", k);
    }else{
        long long int a=0, b=0, c=0;
        char buf[103];
        for(int i=0; i<=n; i++){
            buf[i] = s[i];
        }
        for(int i=1; i<n; i++){
            if(buf[i-1] == buf[i]){
                buf[i] = '&';
                a++;
            }
        }
        b = a;
        if(buf[n-1] == buf[0]){
            b++;
        }
        c = b;
        if(buf[n-1] == '&'){
            c--;
        }
        long long int ans;
        if(k == 1){
            ans = a;
        }else if(k == 2){
            ans = a+b;
        }else{
            ans = a + (c*(k-2)) + b;
        }
        printf("%lld", ans);
    }

    return 0;
}


#include<stdio.h>

int main(){
  char str[200] = {};
  long n = 0;
  long hofu[100] = {};
  long hofucnt = 0;
  long k = 0;
  long i = 0;
  long kaburi = 0;
  long ryotan = 0;
  long hoge = 0;
  long long count = 0;

  scanf(" %s",str);
  scanf("%ld",&k);
  //printf("%s",str);
  for(i=0;i<100;i++){
    if(str[i] != '\0'){
      n++;
    }else{
      break;
    }
  }
  //printf("%ld/",n);
  if(str[0] == str[n-1]){
    ryotan = 1;
  }

  hofu[0] = 1;
  hofucnt = 0;
  str[n] = str[0];
  for(i=1;i<n;i++){
    if(str[i-1] == str[i]){
      hofu[hofucnt]++;
    }else{
      hofucnt++;
      hofu[hofucnt]++;
    }
  }

  /*for(i=0;i<=hofucnt;i++){
    printf("%ld.",hofu[i]);
  } */
  if(!ryotan || hofucnt == 0){
    for(i=0;i<=hofucnt;i++){
      kaburi += hofu[i]/2;
    }
    count = k * kaburi;
  }else{
    for(i=1;i<=hofucnt-1;i++){
      kaburi += hofu[i]/2;
    }
    //printf("%ld/%ld/%ld/",kaburi,hofucnt,hofu[hofucnt] + hofu[0]);
    kaburi += (hofu[hofucnt] + hofu[0])/2;
    count = kaburi * (k) -((hofu[hofucnt] + hofu[0])/2)+ (hofu[0] / 2) + (hofu[hofucnt] / 2);
  }
  printf("%lld",count);
  return 0;
  //ssllsssssllsssssllsss
  //issiiissii
}


#include<stdio.h>

int main(){
  char str[200] = {};
  long n = 0;
  long hofu[100] = {};
  long hofucnt = 0;
  long k = 0;
  long i = 0;
  long kaburi = 0;
  long ryotan = 0;
  long hoge = 0;
  long long count = 0;

  scanf(" %s",str);
  scanf("%ld",&k);
  //printf("%s",str);
  for(i=0;i<100;i++){
    if(str[i] != '\0'){
      n++;
    }else{
      break;
    }
  }

  if(str[0] == str[n-1]){
    ryotan = 1;
  }

  hofu[0] = 1;
  hofucnt = 0;
  str[n] = str[0];
  for(i=1;i<=n;i++){
    if(str[i-1] == str[i]){
      hofu[hofucnt]++;
    }else{
      hofucnt++;
      hofu[hofucnt]++;
    }
  }

  for(i=0;i<=hofucnt;i++){
    kaburi += hofu[i]/2;
  }
  count = kaburi*(k);
  printf("%lld",count);
  return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
    char str[105];
    scanf("%s", str);
    long long int k;  scanf("%lld", &k);
    int len = strlen(str);
    long long int count = 0, count1 = 0;
    if(len==1)
    {
        printf("%lld\n", k/2);
        return 0;
    }
    for(int i=1; i<len; i++)
    {
        if(str[i]==str[i-1])
        {
            count++;
            str[i]='5';
        }
    }
    if(str[0]==str[len-1])
        count1++;
    long long ans = count*k + count1*(k-1);
    printf("%lld\n", ans);
    return 0;
}


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

int main(void){
  char s[110];
  long long k;
  scanf("%s%lld", s, &k);

  long long N = strlen(s);
  long long right = 0, left = 0, i = 0;
  while(s[0] == s[i]){
    i++;
    right++;
  }
  i = N-1;
  while(s[N-1] == s[i]){
    i--;
    left++;
  }

  long long ans1 = 0;
  if(right == N){
    printf("%lld\n", N*k/2);
    return 0;
  }else if(s[N-1] == s[0]){
    ans1 = (right+left)/2*(k-1);
  }
  ans1 += right/2+left/2;

  long long ans2 = 0, cnt = 1;
  for (int i = right+1; i < N-left+1; i++) {
    if(s[i] != s[i-1] || i == N-left){
      ans2 += cnt/2*k;
      cnt = 1;
    }else{
      cnt++;
    }
  }

  printf("%lld\n", ans1+ans2);

  return 0;
}


#include <stdio.h>

int main(void) {
	char s[110];
	long int n;
	long int count=0;
  	long int tmp;
	int i=0;

	scanf("%s",&s);
	scanf("%ld",&n);

	while(s[i+1]!=0||s[i]!=0){
		if(s[i]==s[i+1]){
			count++;
			i=i+2;
		}else{
			i++;
		}
	}
	count=count*n;

	if(s[0]==s[i]){
		count=count+(n-1);
	}
  if(s[1]==0){
  	count=count+(n)/2;
  }else{
    count=count;
  }
  i=1;
  while(s[i]!=0){
	if(s[i]==s[i-1]){
      tmp=1;
      i++;
    }else{
      tmp=0;
      break;
    }
  }
    if(tmp==1){
	tmp=(i*n)/2;
		if(count>tmp){
			count=tmp;
		}
    }
  printf("%ld",count);

return 0;
}


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

int main(void)
{
	long int i, j;
	long int n, k;
	long int count = 1;
	long int saigo = 0;
	long int saisyo = 0;
	long int sum = 0;
	long int ans = 0;
	char work;
	char str[200];

	scanf("%s%ld", str, &k);

	n = strlen(str);

	str[n] = '0';

	//printf("%ld %ld %s %ld\n", strlen(str), k, str, n);

	work = str[0];

	if(str[0] == str[n - 1]) {
		count = 1;
		for(i = n - 2; i > 0; i--) {
			if(work != str[i]) {
				break;
			}
			count++;
		}
		saigo = count;

		if(saigo == n - 1) {//全部同じ文字の場合
			ans = n * k / 2;
			printf("%ld\n", ans);
			return 0;
		} else {//最初と最後は同じ文字だけど間に必ず違う文字が入る場合
			count = 1;
			work = str[0];
			for(i = 1; i < n; i++) {
				if(work == str[i]) {
					count++;
				} else {
					break;
				}
			}

			saisyo = count;//最初の文字から同じ文字が何文字続いてるか
			count = 1;
			work = str[saisyo];

			sum = 0;
			//間の文字の連続具合
			for(i = saisyo + 1; i < n - saigo + 1; i++) {
				if(work == str[i]) {
					count++;
				} else {
					sum += count / 2;
					count = 1;
					work = str[i];
				}
			}

			ans = sum * k;
			ans +=  ( (saisyo + saigo) / 2 ) * (k - 1);
			ans += (saisyo / 2);
			ans += (saigo / 2);
			printf("%ld\n", ans);
			return 0;
		}
	} else {//最初と最後の文字が違う場合間の文字の連続具合を見ればいい
		sum = 0;
		count = 1;
		work = str[0];
		for(i = 1; i <= n; i++) {
			if(work == str[i]) {
				count++;
			} else {
				sum += count / 2;
				count = 1;
				work = str[i];
			}
		}
		ans = sum * k;
		printf("%ld\n", ans);
		return 0;
	}

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

	return 0;
}


#include<stdio.h>
int main(void){
    char s[102];
    unsigned long long int k;
    scanf("%s",s);
    scanf("%llu",&k);
    unsigned long long int cnt=0;
    int flg=0;
    int cnt2=0;
    for(int i=1;s[i]!='\0';i++){
        if(s[i]==s[i-1]){
            if(s[i+1]=='\0'&&s[i]==s[0]){
                cnt++;
                flg=1;
                break;
            }
            if(s[i]==s[i+1]){
                 cnt2++;
                 i++;
            }
            cnt++;

        }
    }
    /*
    if(flg==1){
        cnt+=k-1;
    }*/
    //printf("%d %d\n",cnt,cnt2);
    //cnt==cnt-cnt2;
    cnt=cnt*k;
    printf("%llu\n",cnt);
    return 0;
}


#include <stdio.h>
#include <string.h>
int main(void) {
    int i, len, cnt, mastercnt;
    unsigned long k;
    char s[102];
    scanf("%s", s);
    len = strlen(s);
    s[len] = s[0];
    s[len+1] = '\0';
    scanf("%lu", &k);

    cnt = 0;
    mastercnt=0;
    for (i=1; i<=len; i++) {
        if (s[i-1] == s[i]) {
            cnt++;
            if (s[i] == s[i+1]) {
                i++;
                if (s[i+2] == '\0') {
                    break;
                }
            }
            if (s[i+1] == '\0') {
                if (cnt == 1) {
                    mastercnt=1;
                } else {
                    cnt--;
                }
            }
        }
    }

    printf("%lu", k*cnt+mastercnt);

    return 0;
}


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

int
main(int argc, char *argv[])
{
    char S[101];
    long long K;
    long long tdup, idup, edup, duptmp;
    int i;

    tdup = 1;
    idup = 0;
    edup = 0;

    scanf("%s", S);
    scanf("%lld", &K);

    for (i=0; i<strlen(S)-1; i++) {
        if (S[i]==S[i+1]) {
            tdup++;
        }
        else
        {
            break;
        }

    }
    if(i<strlen(S)-1) {
        duptmp = 1;
        for(; i<strlen(S)-1; i++) {
            if (S[i]==S[i+1]){
                duptmp++;
            }
            else {
                idup += duptmp/2;
                duptmp = 1;
            }
        }
        if (duptmp>0) {
            edup = duptmp;
        }
    }
//    printf("t %lld i %lld e %lld tmp %lld\n", tdup, idup, edup, duptmp);

    if (tdup==strlen(S)) {
        printf("%lld\n", tdup*K/2);
    }
    else if (S[0]!=S[strlen(S)-1])
        printf("%lld\n", (tdup/2+idup+edup/2)*K);
    else {
//        printf("%lld %lld %lld %lld\n", tdup/2 ,idup * K, (tdup+edup)/2*(K-1) ,edup/2);
        printf("%lld\n", tdup/2 + idup * K + (tdup+edup)/2*(K-1) + edup/2);
    }

    return 0;
}


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

int main() {
    char *str = NULL;
    unsigned long long K = 0;
    scanf("%m[^\n]%Lu", &str, &K);
    int stringLength = strlen(str);
    int i = 1;
    unsigned long long sum = 0;
    while (i < 2 * stringLength) {
        if (i != (2 * stringLength - 1) && str[i % stringLength] == str[(i - 1) % stringLength] && str[i % stringLength] == str[(i + 1) % stringLength]) {
            sum++;
            i += 2;
        } else if (i != (2 * stringLength - 1) && str[i % stringLength] == str[(i + 1) % stringLength]) {
            i++;
        } else if (str[i % stringLength] == str[(i - 1) % stringLength]) {
            i += 2;
            sum++;
        } else {
            i += 2;
        }
    }

    if (sum % 2 == 1) {
        printf("%Lu", (K - 1) * (sum / 2 + 1) + sum / 2);
    } else {
        printf("%Lu", K * sum / 2);
    }
    free(str);
    str = NULL;
    return 0;
}


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

long long Count(char S[100], int K){
  int i, c, Sc;
  int s[100];
  long long C;
  c = C = 0;
  Sc = 1;
  for(i = 1;i < strlen(S);i++){
    if(S[i-1] == S[i]) Sc++;
    else{
      s[c++] = Sc;
      Sc = 1;
    }
  }
  s[c++] = Sc;
  if(strlen(S) != 1){
    for(i = 0;i < c;i++){
      C += (long long) (s[i] / 2) * K;
      s[i] %= 2;
    }
    if(s[0] * s[c-1] == 1 && S[0] == S[strlen(S)-1]) C += K - 1;
  }else C = K / 2;
  return C;
}
int main(void){
  int K;
  char S[100];
  scanf("%s", S);
  scanf("%d", &K);
  printf("%lld\n", Count(S, K));
  return 0;
}


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

typedef struct priceHeap
{
    long int *product;
    int head;
    int tail;
} heap;

void initialization(heap *h, int inputN)
{
    h->product = (long int *)malloc(sizeof(long int) * inputN);
    for (int index = 0; index < inputN; index++)
    {
        h->product[index] = 0;
    }
    h->head = 0;
    h->tail = 0;
}

int putInHeap(heap *h, long int price, int inputN)
{
    if (h->tail >= inputN)
    {
        return -1;
    }
    else
    {
        h->product[h->tail] = price;
        h->tail++;
        return 0;
    }
}

void pushDown(heap *h, int firstIndex)
{
    int parentIndex = firstIndex, childIndex;
    long int parent, child;

    while (parentIndex * 2 < h->tail - 1)
    {
        if (parentIndex == 0)
        {
            childIndex = 1;
        }
        else
        {
            childIndex = parentIndex * 2;
        }
        if (h->product[childIndex] < h->product[childIndex + 1])
        {
            childIndex++;
        }
        parent = h->product[parentIndex];
        child = h->product[childIndex];
        if (parent < child)
        {
            h->product[parentIndex] = child;
            h->product[childIndex] = parent;
            parentIndex = childIndex;
        }
        else
        {
            break;
        }
    }
}

void heapSort(heap *h)
{
    for (int count = (h->tail - 1) / 2; count >= 0; count--)
    {
        pushDown(h, count);
    }
}

long long int totalPrice(heap *h)
{
    long long int total = 0;
    for (int index = 0; index < h->tail; index++)
    {
        total += h->product[index];
    }
    return total;
}

int main(void)
{
    int inputN, inputM, flag = 0;
    long int *inputA;
    long long int total = 0;
    heap list;
    scanf("%d%d", &inputN, &inputM);
    inputA = (long int *)malloc(sizeof(long int) * inputN);
    initialization(&list, inputN);
    for (int count = 0; count < inputN; count++)
    {
        scanf("%ld", &inputA[count]);
        putInHeap(&list, inputA[count], inputN);
    }
    for (int count = 0; count < inputM; count++)
    {
        heapSort(&list);
        list.product[0] /= 2;
    }
    total = totalPrice(&list);
    printf("%lld", total);

    free(inputA);
    free(list.product);

    return 0;
}

Score : 400 points

Problem Statement

Takahashi is going to buy N items one by one.

The price of the i-th item he buys is A_i yen (the currency of Japan).

He has M discount tickets, and he can use any number of them when buying an item.

If Y tickets are used when buying an item priced X yen, he can get the item for \frac{X}{2^Y} (rounded down to the nearest integer) yen.

What is the minimum amount of money required to buy all the items?

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
A_1 A_2 ... A_N

Output

Print the minimum amount of money required to buy all the items.

Sample Input 1

3 3
2 13 8

Sample Output 1

9

We can buy all the items for 9 yen, as follows:

Buy the 1-st item for 2 yen without tickets.

Buy the 2-nd item for 3 yen with 2 tickets.

Buy the 3-rd item for 4 yen with 1 ticket.

Sample Input 2

4 4
1 9 3 5

Sample Output 2

6

Sample Input 3

1 100000
1000000000

Sample Output 3

0

We can buy the item priced 1000000000 yen for 0 yen with 100000 tickets.

Sample Input 4

10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 4

9500000000


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

void swap(long *x,long *y){
    long z;
    z=*x;
    *x=*y;
    *y=z;
}

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

void pushdown(long* a,int n){
    int i=1;
    while(1){
        if(a[i]>=max(a[2*i],a[2*i+1]))break;
        if(a[2*i]>a[2*i+1]){
            swap(&a[i],&a[2*i]);
            i*=2;
        }else{
            swap(&a[i],&a[2*i+1]);
            i=2*i+1;
        }
        if(i*2>=n)break;
    }
}

void add_num(long*a,int i){
    while(i>1){
        if(a[i]<=a[i/2])break;
        swap(&a[i],&a[i/2]);
        i/=2;
    }
}

int main(void){
    int N,M,tmp;
    scanf("%d%d",&N,&M);
    long a[100000];
    for(int i=0;i<100000;i++)a[i]=0;
    for(int i=1;i<N+1;i++){
        scanf("%ld",&a[i]);
        add_num(a,i);
    }
    for(int i=0;i<M;i++){
        a[1]=a[1]/2;
        pushdown(a,N);
    }
    long count=0;
    for(int i=1;i<N+1;i++){
        count+=a[i];
    }
    printf("%ld\n",count);
    return 0;
}


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

int compare (const void *a, const void *b)
{
    if(*(uintmax_t *)a < *(uintmax_t *)b)
        return 1;
    if(*(uintmax_t *)a > *(uintmax_t *)b)
        return -1;
    return 0;
}

int main (void)
{
    uintmax_t N, M, A[(int)1e5];
    uintmax_t ans = 0, msb = (INTMAX_C(1) << 63);
    size_t i;

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

    qsort(A, N, sizeof(uintmax_t), compare);

    while((msb & A[0]) == 0) msb >>= 1;

    i = 0;
    do {
        if (A[i] & msb) {
            A[i] >>= 1;
            i++;
        } else {
            qsort(A, (i + 1), sizeof(uintmax_t), compare);
            msb >>= 1;
            i = 0;
            A[i] >>= 1;
            i++;
        }
        if (i > N) i = 0;
    } while (--M || (msb == 0));

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

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

    return 0;
}


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

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

int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    int val[n];
    long ans;
    for(int i=0;i<n;i++){
        scanf("%d ",&val[i]);
    }
    qsort(val,n,sizeof(int),asc);
    for(int i=0;i<m;i++){
        val[n-1]=val[n-1]/2;
        int j=n-1;
        while(j>0&&val[j-1]>val[j]){
            int tmp=val[j];
            val[j]=val[j-1];
            val[j-1]=tmp;
            j--;
        }
    }
    ans=0;
    for(int i=0;i<n;i++){
        ans=ans+(long)val[i];
    }
    printf("%ld",ans);
    return 0;
}


#include <stdio.h>

#define N_MAX  5001

char s[N_MAX];
unsigned int ans[N_MAX][N_MAX];

int main(void)
{
    unsigned int n;
    unsigned int i, j, k;
    unsigned int len = 0;

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

    for (i = 0; i < n; i++) {
        j = i + 1;
        while (j < n) {
            k = 0;
            while ((s[i + k] == s[j + k]) && (j + k < n) && (i + k < j)) {
                k++;
            }
            ans[i][j] = k;
            if (len <= ans[i][j]) {
                len = ans[i][j];
            }
            if (k == 0) {
                j++;
            } else {
                j += k;
            }
        }
    }

    printf("%d", len);

    return 0;
}

Score : 500 points

Problem Statement

Given is a string S of length N.

Find the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping.

More formally, find the maximum positive integer len such that there exist integers l_1 and l_2 ( 1 \leq l_1, l_2 \leq N - len + 1 ) that satisfy the following:

l_1 + len \leq l_2

S[l_1+i] = S[l_2+i] (i = 0, 1, ..., len - 1)

If there is no such integer len, print 0.

Constraints

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

|S| = N

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the maximum length of a non-empty string that occurs twice or more in S as contiguous substrings without overlapping. If there is no such non-empty string, print 0 instead.

Sample Input 1

5
ababa

Sample Output 1

2

The strings satisfying the conditions are: a, b, ab, and ba. The maximum length among them is 2, which is the answer.
Note that aba occurs twice in S as contiguous substrings, but there is no pair of integers l_1 and l_2 mentioned in the statement such that l_1 + len \leq l_2.

Sample Input 2

2
xy

Sample Output 2

0

No non-empty string satisfies the conditions.

Sample Input 3

13
strangeorange

Sample Output 3

5


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.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 invp(a,p)pom(a,p-2,p)

int n;
char s[5010];
ll atai[5010];
int idx[5010];
int c(const void*p,const void*q){
	if(atai[*(int*)p]<atai[*(int*)q])return -1;
	if(atai[*(int*)p]>atai[*(int*)q])return 1;
	if(*(int*)p<*(int*)q)return -1;
	return 1;
}

int f(int m,int g,int MOD){
	ll ue=pom(g,m,MOD);
	ll has=0;
	rep(i,0,m)has=(has*g+s[i])%MOD;
	rep(i,m,n+1){
		atai[i-m]=has;
		has=(has*g+s[i])%MOD;
		has=((has-s[i-m]*ue)%MOD+MOD)%MOD;
	}

	rep(i,0,n-m+1)idx[i]=i;
	qsort(idx,n-m+1,sizeof(int),c);
//	printf("%d:",m);
//	rep(i,0,n-m+1)printf("%d ",atai[idx[i]]);puts("");

	int flag=0;

	rep(i,0,n-m+1){
		int t=i;
		while(t+1<n-m+1&&atai[idx[i]]==atai[idx[t+1]])t++;
		if(idx[i]+m<=idx[t]){
			flag=1;
			break;
		}
		i=t;
	}
	return flag;
}

int main(){
	scanf("%d %s",&n,s);
	int l=0,r=n;//lはok,rはng
	while(r-l>1){
		int m=(l+r)/2;
		if(f(m,3,1000000007)&&f(m,7,1000000021))l=m;
		else r=m;
	}
	printf("%d",l);
}


#include<stdio.h>
int main()
{
    int a;
    char s[10];
    scanf("%d\n%s",&a,&s);

    if(a<3200)
    {
        printf("%s",s);
    }
    else
        {
         printf("red");
        }
return 0;
}

Score : 100 points

Problem Statement

You will be given an integer a and a string s consisting of lowercase English letters as input.

Write a program that prints s if a is not less than 3200 and prints red if a is less than 3200.

Constraints

2800 \leq a < 5000

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

Each character of s is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

a
s

Output

If a is not less than 3200, print s; if a is less than 3200, print red.

Sample Input 1

3200
pink

Sample Output 1

pink

a = 3200 is not less than 3200, so we print s = pink.

Sample Input 2

3199
pink

Sample Output 2

red

a = 3199 is less than 3200, so we print red.

Sample Input 3

4049
red

Sample Output 3

red

a = 4049 is not less than 3200, so we print s = red.


#include<stdio.h>
int main(void){
  int i,n,a[110];
  double A,b;
  scanf("%d",&n);
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  for(i=0;i<n;i++)
    A+=(double)1/a[i];
    b=1/A;
  printf("%.5lf\n",b);
  return 0;
}


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

    char a[10];
    int s,d,f,g,h,j,k,len;
    scanf("%d",&f);
    scanf("%s",a);
if(f<3200){
    printf("red\n");
}
else{
    printf("%s\n",a);
}
    return 0;
}


#include<stdio.h>
int main(void)
{
int a;
  char s[103];
  scanf("%d%s",&a,s);
  if(a>3200)
   printf("%s",s);
  else
   printf("red");

return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
    int a;
    char s1[10],s2[10];
    scanf("%d %s",&a,s1);
    if (a>=3200)
    {
        strcpy (s2,s1);
        printf(s2);
    }
    if (a<3200)
    {
        if (strcmp(s1,s2)==0)
        {
            printf("red");
        }
        else
            printf("red");
    }
    return 0;
}


#include <stdio.h>;
//#include "Main.h"

int main(void)
{
	int a;
	char s[11];
	scanf("%d", &a);
	scanf("%s", s);
	if (a >= 3200)
	{
		printf("%s");
	}
	else
	{
		printf("red");
	}
	return 0;
}


//#include stdio.h

int main()
{
  char str[10];
  int a=0;

  scanf("%d", &a);
  scanf("%s", str);

  if(a>=3200)
    printf("%d", a);
  else if(a<3200)
  {
    printf("red");
  }

  return(0);

}


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

int main(){
  int a=0;
  char s[10];

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

  if(2800>a || a>=5000){
    exit (-1);
  }

  if(a>=3200){
    printf("%s",s);
  }
  else{
    printf("red");
  }
  return 0;
}


#include<stdio.h>
int main()
{
    int a;
    char s[100] ;
    scanf("%d%s",&a,s);
    if(a<3200)
    {
        printf("red\n");
    }
    else
    {
        printf("%s\n",s);
    }

    return 0;
}


#include<stdio.h>
int main(void){
 int a,s;
  if(a>=3200){
    printf(s);
  }else{
    printf("red");
  }
  return 0;
}


#include <stdio.h>

int main(){
    int a;
    char str[10];

    scanf("%d",&a);
    scanf("%s",str);

    if(a >= 3200){
        printf("%s\n",str);
    }else{
        printf("%s\n","red");
    }

}


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

int main()
{
  int a,i;
  char s[11];
  scanf("%d",&a);
  scanf("%s",s);
    if (a>=3200)
    {
      printf("%s",s);
    }
    else
    {
      printf("RED");
    }
return 0;
}


#include<stdio.h>

int main(void){
	int a;
	char s[10];

	printf("a = ");
	scanf("%d",&a);
	printf("s = ");
	scanf("%s",s);

	if(a >= 2800 && a < 5000){
		if(a >= 3200) printf("%s\n",s);
		else printf("red\n");
	}

	return 0;
}


#include<stdio.h>
int main(void)
{
    int a=0;
//char b[] ="helloworld";
    char text[10];
    scanf("%d",&a);
    scanf("%s",text);
    if(a>=3200){
        printf("%s\n",text);
    }else if(a<3200){
        printf("' red'\n");
    }

    return 0;
}


#include<stdio.h>

int main(){
  int a;
  char s[11];

  scanf("%d",&a);
  scanf("%s",&s);
  if(a>=3200){
    printf("%s",s);
  }else{
    printf("red");
  }
  return 0;
}


#include<stdio.h>
#include<string.h>
#include<math.h>
#define PRN(n) printf("%d\n",n)
#define PRS(s) printf("%s\n",s)
#define PRC(c) printf("%c",c)

int main(void){
  int n;
  char s[2][11]={"red",""};
  scanf("%d %s",&n,s[1]);
  PRS(s[n/3200]);
  return 0;
}


#include <stdio.h>

int main() {
    int n;
    char s[20];
    scanf("%d", &n);
    scanf("%s", s);
    printf("%s\n", (n < 3200) ? "red\n" : s);
}


#include <stdio.h>

int main( void )
{
    int a;              //整数a
    char s[11];         //文字列10＋末尾
    char red[4] = "red";//文字列redの初期化

    scanf( "%d",&a );   //整数aの取得
    scanf( "%s",s );   //文字列sの取得

    if( a >= 3200 )
    {
        printf( "%s", s );
    }
    else
    {
        printf( "%s", red );
    }

    return( 0 );
}


#include <stdio.h>

int main(void)
{
	int a;
	char s[10];

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

	if(a >= 3200){
		printf("%s\n", s);
	}
	else{
		printf("red\n");
	}
	return 0;
}


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

int main()
{
  int n;
  char str[5];

  scanf("%d %s",&n,str);

  if(n<3200)
    printf("red\n");
  else
    printf("pink\n");

  return 0;
}


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

int main(void){
    int i,j,k;
    int a;
    char s[20];

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

    if(a >= 3200){
        printf("%s\n",s);
    }else{
        printf("red\n");
    }

    return 0;
}


#include <stdio.h>
#include <stdlib.h>
int ans[200000];
int n, q;
int a[200000], b[200000], p[200000], x[200000];
int *tree[200000];
int cnt[200000];
int cnts[200000];

int DFS(int now_v, int before_v) {//v:現在の頂点　bv:前にいた頂点
    int i, new_v;

	for(i = 0; i < cnt[now_v]; i++){//iをvの個数分だけループ
		new_v = tree[now_v][i];//お隣の頂点
		if (before_v != new_v){//逆流を防ぐため，自分が前にいたところには行かない
			ans[new_v] += ans[now_v];
			DFS(new_v, now_v);//現在の自分の位置を，前にいたところに変え，お隣の頂点に移動
		}
	}
	return 0;
}

int main(void){
    scanf("%d%d", &n, &q);
    int i, j;
    for(i = 0; i < n; i++){
        cnt[i] = 0;
        ans[i] = 0;
        cnts[i] = 0;
    }

    for(i = 0; i < n - 1; i++){
        scanf("%d%d", &a[i], &b[i]);
        a[i]--;
        b[i]--;
        cnt[a[i]]++;
        cnt[b[i]]++;
    }
    for(i = 0; i < q; i++){
        scanf("%d%d", &p[i], &x[i]);
        p[i]--;
    }

    //普通にメモリ確保すると足りなくなるので，可変長配列で確保する
    for(i = 0; i < n; i++){
        tree[i] = (int *)malloc(sizeof(int)*(cnt[i] + 1));
        if(tree[i] == NULL){
            return 0;
        }
    }

    for(i = 0; i < n - 1; i++){
        tree[a[i]][cnts[a[i]]] = b[i];
        tree[b[i]][cnts[b[i]]] = a[i];
        cnts[a[i]]++;
        cnts[b[i]]++;
    }

    for(i = 0; i < q; i++) {
		ans[p[i]] += x[i];
	}

	DFS(0, -1);

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

    return 0;
}

Score : 400 points

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.
The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

Now, the following Q operations will be performed:

Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.

Find the value of the counter on each vertex after all operations.

Constraints

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

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

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
:
a_{N-1} b_{N-1}
p_1 x_1
:
p_Q x_Q

Output

Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.

Sample Input 1

4 3
1 2
2 3
2 4
2 10
1 100
3 1

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.

Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.

Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.

Sample Input 2

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

Sample Output 2

20 20 20 20 20 20


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

typedef struct {
  long counter;
	long connected;
} S_VERTICE;

typedef struct {
	long small;
  long large;
	long check;
} S_NODE;

int cmpAscVal(const void * n1, const void * n2)
{
	if (((S_NODE *)n1)->small > ((S_NODE *)n2)->small)
	{
		return 1;
	}
	else if (((S_NODE *)n1)->small < ((S_NODE *)n2)->small)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int cmpAscVal2(const void * n1, const void * n2)
{
	if (((S_NODE *)n1)->check > ((S_NODE *)n2)->check)
	{
		return 1;
	}
	else if (((S_NODE *)n1)->check < ((S_NODE *)n2)->check)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

int main(void) {

  long n,q;
  scanf("%ld %ld", &n, &q);
  S_NODE nodes[n-1];
  for (long i = 0; i < n-1; i++) {
    scanf("%ld %ld", &nodes[i].small, &nodes[i].large);
    nodes[i].check = 0;
  }
  qsort(nodes, n-1, sizeof(S_NODE), cmpAscVal);
  long p[q],x[q];
  for (long i = 0; i < q; i++) {
    scanf("%ld %ld", &p[i], &x[i]);
  }
  S_VERTICE vertices[n+1];
  for (long i = 0; i <= n; i++) {
    vertices[i].counter = 0;
  }
  for (long i = 0; i < q; i++) {
    vertices[p[i]].counter += x[i];
  }
  for (long i = 0; i <= n; i++) {
    vertices[i].connected = 0;
  }
  vertices[1].connected = 1;
  for (long i = 0; i < n-1; i++) {
    if (nodes[i].small == 1) {
      vertices[nodes[i].large].counter += vertices[1].counter;
      vertices[nodes[i].large].connected = 1;
      nodes[i].check = 1;
    }
  }
  while (1) {
    qsort(nodes, n-1, sizeof(S_NODE), cmpAscVal2);
    if (nodes[0].check == 1) {
      break;
    }
    for (long i = 0; i < n-1; i++) {
      if (nodes[i].check == 1) {
        break;
      }
      if (vertices[nodes[i].small].connected != 0) {
        vertices[nodes[i].large].counter += vertices[nodes[i].small].counter;
        vertices[nodes[i].large].connected = 1;
        nodes[i].check = 1;
      } else if (vertices[nodes[i].large].connected != 0) {
        vertices[nodes[i].small].counter += vertices[nodes[i].large].counter;
        vertices[nodes[i].small].connected = 1;
        nodes[i].check = 1;
      }
    }
  }
  for (long i = 1; i < n; i++) {
    printf("%ld ", vertices[i].counter);
  }
  printf("%ld\n", vertices[n].counter);

  return 0;
}


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

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

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

#define WHITE 0
#define GRAY 1
#define BLACK 2

//節点uからでる枝の構造体の定義
typedef struct slobj_{
    struct slobj_ *next;
    int v; //uから枝が伸びている節点v
}* slobj;

//slobj型のリストの定義
typedef struct{
    slobj head;
    slobj tail;
}* slist;

//グラフの構造体の定義
typedef struct{
    int n; //節点数
    slist* E; //枝リストの配列E[0]~E[n-1],未ソート単方向
}* graph;

//vにnを持つslobj型のリスト要素のメモリ確保+作成する関数
slobj slobj_new(int n){
    slobj p;
    NEW(p,1);
    p->v=n;
    p->next=NULL;
    return p;
}

//slist型のメモリを1個確保し、空リスト1つを作る関数
slist slist_new(void){
    slist L;
    NEW(L,1);
    L->head=NULL;
    L->tail=NULL;
    return L;
}

//各slistを空リストにした,節点nのgraph型のメモリを確保する関数(Eは0カウント)
graph graph_new(int n){
    graph G;
    NEW(G,1);

    G->n=n;

    NEW(G->E,G->n);
    int i;
    for(i=0;i<G->n;i++){
        G->E[i]=slist_new();
    }

    return G;
}

//リストLの末尾に要素rを追加する関数
void slist_append_tail(slist L,slobj r){
    if(L->head==NULL){
        L->head=r;
        L->tail=r;
    }
    else{
        L->tail->next=r;
        L->tail=r;
    }

    return;
}

//節点i(0~n-1)から節点j(0~n-1)に枝を追加する関数
void add_edge(graph G, int i, int j){
    //枝データを追加
    slobj p=slobj_new(j);
    slist_append_tail(G->E[i],p);

    return;
}

//根(節点i)からの長さをdfs
void dfs(graph G, int i, long* d, int* color){
    color[i]=GRAY;
	slobj p=G->E[i]->head;
	while(p!=NULL){
		if(color[p->v]==WHITE){ //未探索なら
			d[p->v]+=d[i];
			dfs(G,p->v,d,color);
		}
		p=p->next;
	}
    color[i]=BLACK;
	return;
}

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

    graph G = graph_new(N);

    //無向グラフに枝を追加
    for(int i=0;i<N-1;i++){
        int a,b;
        scanf("%d%d",&a,&b);
        add_edge(G,a-1,b-1);
        add_edge(G,b-1,a-1);
    }

    //頂点に付いているカウンターの値
    long d[N];
    for(int i=0;i<N;i++) d[i]=0;

    //カウンターの値を標準入力から入れる
    for(int i=0;i<Q;i++){
        int p;
        long x;
        scanf("%d%ld",&p,&x);
        d[p-1]+=x;
    }

    //頂点を訪れたかどうかを格納する配列
    int color[N];
    for(int i=0;i<N;i++) color[i]=0;

    dfs(G,0,d,color);

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

    return 0;
}


/*2019/08/25 */
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define MOD 1000000007
#define MIN 999999999
#define MAX -999999999
typedef long long int ll;
typedef unsigned long long int ull;
void llswap(ll *x,ll *y){ll temp;temp=*x;*x=*y;*y=temp;}
void swap(int *x,int *y){int tmp;tmp=*x;*x=*y;*y=tmp;}
int rmax(int x,int y){return x>y?x:y;}
int rmin(int x,int y){return x>y?y:x;}
ll llrmax(ll x,ll y){return x>y?x:y;}
ll llrmin(ll x,ll y){return x>y?y:x;}
int asc(const void *a,const void *b){return *(int*)a-*(int*)b;}
int desc(const void *a,const void *b){return *(int*)b-*(int*)a;}
int llasc(const void *a,const void *b){return *(ll*)a-*(ll*)b;}
int lldesc(const void *a,const void *b){return *(ll*)b-*(ll*)a;}

typedef struct node_{
    int parent;
    int child;
    ll sum;
    int flg;
}node_t;
typedef struct query_{
    int p;
    int x;
}query_t;

int asc_t(const void *a,const void *b){return ((query_t*)a)->p - ((query_t*)b)->p;}
//int desc_t(const void *a,const void *b){return ((struct_t*)b)->member - ((struct_t*)a)->member;}

int main(){
    int n,q;
    static query_t query[500000];
    static int a[500000],b[500000];
    static node_t node[500000];
    static int list[500000];

    scanf("%d %d",&n,&q);
    for(int i=0;i<n;i++){
        node[i].sum=0;
        node[i].flg=0;
    }
    node[0].parent=-1;
    for(int i=0;i<n-1;i++){
        scanf("%d %d",&a[i],&b[i]);
        node[b[i]-1].parent=a[i];
        node[a[i]-1].child=b[i];
    }
    int query_n;
    for(query_n=0;query_n<q;query_n++){
        scanf("%d %d",&query[query_n].p,&query[query_n].x);
        list[query[query_n].p-1]=1;
    }
    for(int i=0;i<n;i++){
        if(list[i]!=1){
            query[query_n].p=i+1;
            query[query_n].x=0;
            query_n++;
        }
    }
    qsort(query,query_n,sizeof(query_t),asc_t);

    /*
    for(int i=0;i<q;i++){
        printf("%d %d\n",query[i].p,query[i].x);
    }
    */
    int num=1;
    for(int i=0;i<query_n;i++){
        if(node[query[i].p-1].flg==0&&query[i].p!=1){
            node[query[i].p-1].sum=node[node[query[i].p-1].parent-1].sum;
            node[query[i].p-1].flg=1;
        }
        node[query[i].p-1].sum+=query[i].x;
        node[node[query[i].p-1].child-1].sum=node[query[i].p-1].sum;
    }
    for(int i=0;i<n;i++){
        if(node[i].flg==0&&i!=0){
            node[i].sum=node[node[i].parent-1].sum;
            node[i].flg=1;
        }
    }
    for(int i=0;i<n;i++){
        printf("%lld ",node[i].sum);
    }
    printf("\n");
    return 0;
}


#include <stdio.h>
#define N 200000

typedef struct node{
    int num;
    struct node *next;
}Node;

void countup(int counter[], Node roots[], Node lists[], int px[], int p, int x){
    Node *current;
    current = roots[p].next;
    while(current != NULL){
        counter[current->num] += x + px[current->num];
        countup(counter, roots, lists, px, current->num, x + px[current->num]);
        current = current->next;
    }
}

int main(void){
    int n,q;
    static Node roots[N+1] = {};
    static Node lists[N+1] = {};
    static int counter[N+1] = {};
    Node *current;
    static int px[N] = {};
    int i;
    int a,b,p,x;

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

    for(i = 0; i < n-1; i++){
        scanf("%d %d", &a, &b);
        lists[i].num = b;
        lists[i].next = NULL;
        if(roots[a].next == NULL){
            roots[a].next = &(lists[i]);
        }else{
            current = &(roots[a]);
            do{
                current = current->next;
            }while(current->next != NULL);
            current->next = &(lists[i]);
        }
    }

    for(i = 0; i < q; i++){
        scanf("%d %d", &p, &x);
        px[p] += x;
    }

    countup(counter, roots, lists, px, 1, px[1]);
    counter[1] += px[1];
    for(i = 1; i <= n; i++){
        printf("%d ", counter[i]);
    }

    return 0;
}


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

int cnt[200000], parent[200000];

int main() {
  int N, Q, result;
  int i, a, b, p, x, par;
  scanf ("%d %d", &N, &Q);

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

  for (i=0;i<N-1;i++) cnt[i] = 0;

  for (i=0;i<Q;i++) {
      scanf("%d %d", &p, &x);
      cnt[p-1] += x;
  }
#if 0
printf("---\n");
  for (i=0;i<N;i++) {
    printf("cnt[%d]=%d\n", i, cnt[i]);
  }
printf("---\n");
#endif

  for (i=0;i<N;i++) {
    result = cnt[i];
    p=i;
    while (p>0) {
        p = parent[p];
        result += cnt[p];
    }
    printf("%d ", result);
  }

  return 0;
}


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

int main(void)
{
	int n, ok = 1;
	long  int stair[100001] = {0};
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%ld", &stair[i]);
	}
	for (int i = 1; i <= n - 1; i++) {
		if (stair[i + 1] - stair[i] >= 0) {
			continue;
		}
		else if (stair[i + 1] - stair[i] == -1 && stair[i + 1] >= stair[i - 1]) {
			stair[i]--;
			continue;
		}
		else {
			ok--;
			break;
		}
	}
	if (ok) printf("Yes\n");
	else printf("No\n");

	return 0;
}

Score : 300 points

Problem Statement

There are N squares arranged in a row from left to right. The height of the i-th square from the left is H_i.

For each square, you will perform either of the following operations once:

Decrease the height of the square by 1.

Do nothing.

Determine if it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq H_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
H_1 H_2 ... H_N

Output

If it is possible to perform the operations so that the heights of the squares are non-decreasing from left to right, print Yes; otherwise, print No.

Sample Input 1

5
1 2 1 1 3

Sample Output 1

Yes

You can achieve the objective by decreasing the height of only the second square from the left by 1.

Sample Input 2

4
1 3 2 1

Sample Output 2

No

Sample Input 3

5
1 2 3 4 5

Sample Output 3

Yes

Sample Input 4

1
1000000000

Sample Output 4

Yes


#include<stdio.h>
int check(int h[],int n){
  int flag = 0,i;
  for(i=0;i<n-1;i++){
    if(h[i]>h[i+1]) flag=1;
  }
  return flag;
}
int main(void){
  int n,i;
  scanf("%d",&n);
  int h[n],t[n];
  for(i=0;i<n;i++){
    scanf("%d",&h[i]);
    t[i] = 0;
  }
  if(n==1){
    printf("Yes\n");
    return 0;
  }
  while(check(h,n)){
    for(i=n-2;i>-1;i--){
      if((h[i]>h[i+1]) && t[i]==0){
        h[i]--;
        t[i]++;
      }else if((h[i]>h[i+1]) && t[i]==1){
        printf("No\n");
        return 0;
      }
    }
  }
  printf("Yes\n");
  return 0;
}


#include <stdio.h>

int main(void) {
int N;
int count=0;
int i;
scanf("%d", &N);
int H[N], m=0;

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

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

if(i>1 && m!=0 && m-H[i]>=2){
 	count=2;
 }

	if(H[i]-H[i+1]>1){
			//printf("No");
			count=1;
			break;
			return 0;
	}
	if(H[i]-H[i+1]==1){
		if(m-H[i]<0){
			m=H[i];
		}
		if(H[i-1]-H[i]==1){
				count=1;

			}
	}
}
if(count==0){
	printf("Yes");
}else{
	printf("No");
}

return 0;

}


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

int main(void){
  int n;
  scanf("%d", &n);
  int *arr = calloc(n, sizeof(int));
  for(int i = 0; i < n;i++){
    scanf("%d", &arr[i]);
  }

  for (int i = 1;i < n;i++){
    if(arr[i] == arr[i-1]){
      continue;
    } else if (arr[i] < arr[i-1]){
      arr[i]--;
    } else {
      printf("No");
      return 0;
    }
  }
  printf("Yes");
  return 0;
}


#include <stdio.h>

typedef enum {false, true} bool;

int main(void)
{
    int N, H1, H2;
    char c;
    int i;
    bool flag = true;

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

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

        if(H1 > H2)
        {
            if(H1-1 > H2)
            {
                flag = false;

                break;
            }
        }

        H1 = H2;
    }

    if(flag)
    {
        printf("Yes\n");
    }
    else
    {
        printf("No\n");
    }

    //system("pause");

    return 0;
}


#include <stdio.h>

typedef enum {false, true} bool;

int main(void)
{
    int N, H1, H2;
    char c;
    int i;
    bool flag = true;

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

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

        if(H1 > H2)
        {
            if(H1-1 < H2 || !flag)
            {
                flag = false;

                break;
            }
            else
            {
                flag = false;
            }
        }

        H1 = H2;
    }

    if(i == N)
    {
        printf("Yes\n");
    }
    else
    {
        printf("No\n");
    }

    //system("pause");

    return 0;
}


#include <stdio.h>

int main(void) {
int N;
int count=0;
int i;
scanf("%d", &N);
int H[N];

for(i=0;i<N;i++){
	scanf("%lld",&H[i]);
}
for(i=0;i<N-1;i++){

	if(H[i]-H[i+1]>1){
			printf("No");
			count=1;
			break;
	}
	if(H[i]-H[i+1]==1){
			if(H[i-1]-H[i]==1){
				count=1;
				break;
			}
	}
}
if(count==0){
	printf("Yes");
}

return 0;

}


#include<stdio.h>
int main(){
  int n,bp=0;
  scanf("%d",&n);
  long int i,h[n+10],min1=0,cnt=0,down=0,start=0;
  for(i=0;i<n;i++){
    scanf("%ld",&h[i]);
    if(i==0)
      continue;
    if(i==1){
      if(h[0]>h[1]){
        down++;
        if(h[0]!=h[1]+1)
          start=1;
      }
      continue;
    }
    if(h[i-1]>h[i])
      down++;

    if(h[i-2]==h[i-1]-1)
      min1++;

    if(h[i-2]<h[i-1] && h[i-1]>h[i])
      cnt++;

  }
  if(n==1){
    printf("Yes\n");
    return 0;
  }

  if(start==1){
    printf("No\n");
    return 0;
  }

  if(cnt-min1<=0 && down!=n-1)
    printf("Yes\n");
  else
    printf("No\n");
  return 0;
}


#include<stdio.h>
int main(){
  int n,bp=0;
  scanf("%d",&n);
  long int i,h[n+10],min1=0,cnt=0,down=0;
  for(i=0;i<n;i++){
    scanf("%ld",&h[i]);
    if(i==0)
      continue;
    if(i==1){
      if(h[0]>h[1])
        down++;
      continue;
    }
    if(h[i-1]>h[i])
      down++;

    if(h[i-2]==h[i-1]-1)
      min1++;

    if(h[i-2]<h[i-1] && h[i-1]>h[i])
      cnt++;

  }
  if(cnt-min1<=0 && down!=n-1)
    printf("Yes\n");
  else
    printf("No\n");
  return 0;
}


#include<stdio.h>

int check(int height[],int n){
	for(int i=1; i<n; i++){
		if(height[i] < height[i-1]){
			height[i]++;
			if(height[i] < height[i-1]){
				return -1;
			}
		}
	}
	return 1;
}

int main(){
	int n;
	scanf("%d",&n);
	int height[n];
	for(int i=0; i<n; i++){
		scanf("%d", &height[i]);
	}
	if(check(height, n) == 1) printf("Yes");
	else printf("No");

	return 0;
}


#include <stdio.h>
int main(void){
    int i,j;            //  ループ文に使用
    int x,y,z;
    int n;
    int box[100001] = {};
    int mode = 0;
    int min = 1000000;
    int max = 0;
    int count,count2;   //  カウント用に使用
    char s[256];        //  文字列

    count = 0;
    count2 = 0;

    //  入力処理

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

        if (i != (n - 1)){
            if (min > box[i]){
                min = box[i];
            }
            if (box[i] > min + 1){
                mode = 2;
                max = box[i];
            }
        }
        if (mode == 2){
            if (max - 1> box[i]){
                mode = 1;
                break;
            }
        }
        if (i > 0 && i != (n - 1)){
            if (box[i - 1] + 1 < box[i]){
                mode = 1;
                break;
            }
        }
    }

    if (n == 2){
        if (box[0] - 1> box[1]){
            mode = 1;
        }
    }
    //  計算処理

    //  出力処理

    if (mode == 0 || mode == 2){
        printf("Yes");
    }else{
        printf("No");
    }

}


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

int main(){
  int n;
  scanf("%d",&n);
  long h[100000];
  for(int i=0;i<n;i++){
    scanf("%ld",&h[i]);
  }
  for(int i=0;i<n-1;i++){
    if(h[i] > h[i+1]){
      h[i] -= 1;
    }
  }
  int flag = 0;
  for(int i=0;i<n-1;i++){
    if(h[i] > h[i+1]){
      printf("No");
      return 0;
    }
  }
  printf("Yes");
  return 0;
}


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

typedef long long ll;
typedef int keytype;

int gcd(int a, int b){ return (a % b) ? gcd(b, a % b) : b;}
int lcm(int a, int b){ return (a / gcd(a, b)) * b;}

/* a[0], ..., a[n-1] の数の最大公約数 */
int ngcd(int n, int a[])
{
    int i, d;

    d = a[0];
    for (i = 1; i < n && d != 1; i++)
        d = gcd(a[i], d);
    return d;
}

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

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

int is_prime(int x){
    if(x <= 1) return 0;
    for(int i = 2; i * i <= x; i++){
        if(x % i == 0) return 0;
    }
    return 1;
}

int is_substr(char s[], char t[], int i){
    int flag = 1;

    if(s[i] == '\0') flag = 0;

    for(int j = 0; j < strlen(t); ++j, ++i){
        if(s[i] != t[j]){
            flag = 0;
            break;
        }
    }
    return flag;
}

// factor list, number of factors, natural number
void factorize(int ftr[], int *num, int n){
    int a = 2, cnt = 0;

    while(n >= a){
        if(n % a == 0){
            n = n / a;
            ftr[cnt++] = a;
        }else{
            a++;
        }
    }
    *num = cnt;
}

//#define N 5
//int p[N + 1];
int nextperm(int p[], int N)  /* 辞書式順序で次の順列 */
{
    int i, j, t;

    i = N - 1;
    p[0] = 0;  /* 番人 */
    while (p[i] >= p[i + 1]) i--;
    if (i == 0) return 0;  /* 完了 */
    j = N;
    while (p[i] >= p[j]) j--;
    t = p[i];  p[i] = p[j];  p[j] = t;
    i++;  j = N;
    while (i < j) {
        t = p[i];  p[i] = p[j];  p[j] = t;  i++;  j--;
    }
    return 1;  /* 未了 */
}

void comb(int size1, int size2, int v[size1][size2]){
  for(int i = 0; i < size1; i++){
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for(int k = 1; k < size1; k++){
    for(int j = 1; j < k; j++){
      v[k][j] = (v[k-1][j-1] + v[k-1][j]);
    }
  }
}

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

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

const int inf = 1012345678;
int main(){
    ll l, r;

    scanf("%lld %lld", &l, &r);

    int min = 2018;
    if((r - l) >= 2019){
        min = 0;
    }else{
        for(int i = l; i < r; ++i){
            for(int j = i + 1; j <= r; ++j){
                if(((i * j) % 2019) < min){
                    min = (i * j) % 2019;
                }
            }
        }
    }

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

    return 0;
}

Score : 300 points

Problem Statement

You are given two non-negative integers L and R.
We will choose two integers i and j such that L \leq i < j \leq R.
Find the minimum possible value of (i \times j) \mbox{ mod } 2019.

Constraints

All values in input are integers.

0 \leq L < R \leq 2 \times 10^9

Input

Input is given from Standard Input in the following format:

L R

Output

Print the minimum possible value of (i \times j) \mbox{ mod } 2019 when i and j are chosen under the given condition.

Sample Input 1

2020 2040

Sample Output 1

2

When (i, j) = (2020, 2021), (i \times j) \mbox{ mod } 2019 = 2.

Sample Input 2

4 5

Sample Output 2

20

We have only one choice: (i, j) = (4, 5).


//133_c
#include <stdio.h>

typedef long long ll;
#define rep(i,a,b,c) for(ll i=(a);i<(b);i+=(c))

ll a[2020]={};

int main(void){
    ll L,R,k=0,ans=1;
    scanf("%lld %lld",&L,&R);
    rep(i,L,R+1,1){
        if(i%2019==0){
            printf("0\n");
            return 0;
        }
        a[i%2019]++;
    }
    rep(i,1,2019,1){
        if(k>=2)goto end;
        if(a[i]>0){
            if(a[i]>=2&&k==0){
                ans=i*i;
                goto end;
            }else {
                ans*=i;
                k++;
            }
        }
    }
    end:;
    printf("%lld\n",ans);
    return 0;
}


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

int main(){

  long long i,j;
  long long min = 10000000;
  long long l,r;

  scanf("%lld %lld", &l, &r);

  for(i = l;i <= r;i++){
    for(j = l;j < i;j++){
      if(min > (i*j) % 2019) min = (i*j) % 2019;

      if(min == 0){
        printf("0\n");
        return 0;
      }
    }
  }

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


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

int main(){

  int i,j;
  int min = 100000;
  int l,r;

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

  l = l % 2019;
  r = r % 2019;

  for(i = l;i <= r;i++){
    for(j = i + 1;j <= r;j++){
      if(min > (i*j) % 2019) min = (i*j) % 2019;
    }
  }

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


#include <stdio.h>

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

  int i = L % 2019;
  int j;
  if (2019 - i + L <= R) {
    i = 0;
  } else {
    j = i + 1;
  }

  printf("%d\n", i * j);

  return 0;
}


#include <stdio.h>
int main(void) {
	long long l, r, min, sec;
	scanf("%lld %lld", &l, &r);
	min = 2018;
	sec = 2018;
	for (int i = r; i >= l; i--) {
		if (min > i % 2019) {
			sec = min;
			min = i % 2019;
		}
	}
	printf("%lld\n", (min * sec) % 2019);
	return 0;
}


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

	coding: utf-8
	lang: C (GCC 5.4.1)
*/

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

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

int main(int argc, char const *argv[])
{
	TLong l,r;
	TLong min = 2019;
	scanf("%lld%lld", &l,&r);
	if(r - l >= 2019)	puts("0");
	else{
		for (int i = l; i <= r ; ++i)
		{
			for (int j = i + 1; j <= r; ++r)
			{
				min = ((i * j) % 2019 < min) ? (i * j) % 2019 : min;
				if(min == 0){
					puts("0");
					return 0;
				}
			}
		}
		printf("%lld\n", min);
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <assert.h>
typedef int64_t ll;
typedef uint64_t ull;
int acs(const void *a, const void *b){return *(int*)a - *(int*)b;} /* 1,2,3,4.. */
int des(const void *a, const void *b){return *(int*)b - *(int*)a;} /* 8,7,6,5.. */
#define min(a,b) (a < b ? a: b)
#define max(a,b) (a > b ? a: b)

#define MAXN (10)
#define MOD (1000000007)

ll x[MAXN][MAXN];
int main(void)
{
    ll l,r;
    scanf("%ld %ld",&l,&r);

    ll min1 = 2019;
    ll min2 = 2019;
    for(ll i = l;i<=r;i++)
    {
        if(min1>(i%2019))
        {
            min2 = min1;
            min1 = i%2019;
        }
        else if(min2 > (i%2019)) min2 = i%2019;

        if(min1*min2 ==0) break;
    }

    printf("%ld\n",min1*min2);
    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
int acs(const void *a, const void *b){return *(int*)a - *(int*)b;} /* 1,2,3,4.. */
int des(const void *a, const void *b){return *(int*)b - *(int*)a;} /* 8,7,6,5.. */
#define min(a,b) (a < b ? a: b)
#define max(a,b) (a > b ? a: b)

#define mod 2019

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

	int ans;
	if (r - l < 2019 && r % mod - l % mod > 0)
	{
		ans = l % mod * (l % mod + 1);
	} else {
		ans = 0;
	}
	printf("%d\n", ans);
	return 0;
}


#include <stdio.h>

int main()
{
    int l, r;
    int i, j;
    long ijmod;
    int min =2019;

    char buf[256];
    fgets(buf, 256, stdin);
    sscanf(buf, "%d %d", &l, &r);

    if((r -l)/673 >= 1){ //2019 = 673 / 3
        // printf("673 must included.\n");
        // printf("%d", 0);
    }else{
        for (i = l; i <= r-1; i++){
            for (j = i+1; j <= r; j++){
                ijmod = ((long)i * j) % 2019;
                if (min > ijmod){
                    // printf("i = %d j = %d : change %d -> %ld\n", i, j ,min, ijmod);
                    min = ijmod;
                }
            }
        }
        // printf("%d", min);
    }
    printf("%d", min);

}


#include <stdio.h>
#define N 2019

int main(void){
  unsigned long int l,r,ans;
  int count;

  scanf("%ld %ld",&l,&r);

  ans = (l * (l + 1)) % N;
  count = 0;
  for(unsigned long int i = l; i < r; i++){
    for(unsigned long int j = i + 1; j <= r; j++){
      if(count >= (N - 1)) goto loopend;
      if(ans > (i * j) % N) ans = (i * j) % N;
      count++;
    }
  }
  loopend:
    printf("%ld",ans);
    return 0;
}


#include <stdio.h>

int main(){

    int L ,R ,i, j ;
    int isum ,jsum ,iminsum ,jminsum ,minsum;

    iminsum =2019;
    jminsum =2019;

    scanf("%d", &L);
    scanf("%d" ,&R);

    i=L;
    if(R-L>=2019){
        printf("%d\n",0);
    }else{

    for(j= i+1; j <= R ; j++){

        jsum = j%2019;

        if(jsum < jminsum){
            jminsum = jsum;
        }

        for(i = L;i <= j-1; i++){
            isum = i%2019;

            if(isum <iminsum){
                iminsum = isum;
            }

        }
    }

    minsum = jminsum*iminsum;

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


#include <stdio.h>

int main(){

    int L ,R ,i, j ;
    int isum ,jsum ,iminsum ,jminsum ,minsum;

    iminsum =2019;
    jminsum =2019;

    scanf("%d", &L);
    scanf("%d" ,&R);

    i=L;

    for(j= i+1; j <= R ; j++){

        jsum = i*j%2019;

        if(jsum < jminsum){
            jminsum = jsum;
        }

        for(i = L;i <= j-1; i++){
            isum = i*j%2019;

            if(isum <iminsum){
                iminsum = isum;
            }

        }

        if(iminsum < jminsum){
            minsum =iminsum;
        }
        else{
            minsum =jminsum;
        }
    }

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


#include <stdio.h>

int main(){
    int l,r;
    scanf("%d%d",&l,&r);
    int answer=2019;
    r=r-l<2019?r:l+2019;
    for(int i=l;i<=r;i++){
        for(int j=i+1;j<=r;j++){
            int a=i*j%2019;
            answer=a<answer?a:answer;
            if(answer==0) {
                i=r+1;
                j=r+1;
            }
        }
    }
    printf("%d",answer);
}


#include<stdio.h>
#include<stdlib.h>
int main()
{
	int L, R, i, min_temp ;
	int min= 2019;

	scanf("%d", &L);
	scanf("%d", &R);

	if(R-L >= 2019){
		min = 0;
		printf("%d", min);
		return 0;
	}

	for(i=L; i<R; i+=2){
		min_temp = (i*(i+1))%2019;
		if(min_temp < min){
			min = min_temp;
		}
	}

	printf("%d", min%2019);

	return 0;
}


#include<stdio.h>
int main(){
  int L,R;
  printf("L");
  scanf("%d",&L);
  printf("R");
  scanf("%d",&R);
  int max=0;
  if(L<0||R<0){
    printf("error");
  } else {
    for(int i=L;i<=R-1;i++){
      for(int j=i+1;j<=R;j++){
        if( max < (i*j) % 2019){
          max = (i*j)%2019;
        }
      }
    }
  }
    printf("%d\n",max);
}


#include <stdio.h>
#define MOD 2019
#define NUM

int main(void)
{

    long long int a,i,j,L,R,I,J;
    scanf("%lld%lld",&L,&R);
    a=(L%MOD)*((L+1)%MOD);
    if(R-L==2019){
        a=0;
    }

    for(i=L%MOD;i<=(R-1)%MOD;i++){
        I=i%MOD;
        for(j=(i+1)%MOD;j<=R%MOD;j++){
            J=j%MOD;
            if(a>I*J)
            a=I*J;
        }
    }

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


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

int main(){
    long long int L,R;
    scanf("%lld %lld",&L,&R);
    if(R-L>3000)printf("0\n");
    //L=L%2019;
    //R=R%2019;
    long long int min=2018;
    for(int i=L;i<R;i++){
        for(int j=L+1;j<R+1;j++){
            if(min > ((i*j)%2019))min=((i*j)%2019);
        }
    }
    printf("%d",min);

}


#include<stdio.h>
#include<math.h>
int main(){
    int L,R;
    scanf("%d %d",&L,&R);
    int i,j;
    int k,l;
    int min=2018;
    //int minj=2018;
    int J;
    if(R-L>=2018){
        printf("0\n");
    }
    else{
    for(k=L;k<=R;k++){
        for(l=k+1;l<=R;l++){
            J = (k*l)%2019;
            if(J<min){
                min=J;
            }
        }

    }
   // printf("%d %d\n",mini,minj);
    printf("%d\n",min);
    }
    return 0;
}


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

int main(void){

  long long int L,R,i,j,k;
  scanf("%lld",&L);
  scanf("%lld",&R);
  long long int tmp,min=2019;

  for(i=L;i<R;i++){
    for(j=i+1;j<R+1;j++)
    if(i<j){
     tmp = (i*j) % 2019;
     if(min > tmp){
       min = tmp;
     }
    }
  }

  printf("%lld",min);

  return 0;
}


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

int main() {
	int i, j, k;
	int	n, m, h, g, a, b, c, d, l, r;
	int f = 2020;
	int w;

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

	if (r < 2019) {
		f = l * (l + 1);
		goto end;
	}
	if (r - l > 2018) {
		f = 0;
		goto end;
	}

	for (i = l;i < r;i++) {
		w = i % 2019;
		if (w == 0) {
			f = 0;
			goto end;
		}
		for (j = i + 1;j < r + 1;j++) {
			h = j % 2019;
			if (h == 0) {
				f = 0;
				goto end;
			}
			g = (w*h) % 2019;
			if (g == 0) {
				f = 0;
				goto end;
			}
			if (g < f) {
				f = g;
			}
		}
	}

	end:

	printf("%d", f);

	return 0;
}


    //AtCoder Beginner Contest 133 目指せ3冠、勝ち取れ内定(⊙ꇴ⊙)

#include <stdio.h>
int main(void){
    //変数宣言ここから٩(ˊᗜˋ*)و

    char s[100001];
    int n,i,j,count;
    int l,r;
    int min = 2147483647;
    int mode = 0;

    //入力処理ここから٩(ˊᗜˋ*)و

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

    //処理部分ここから٩(ˊᗜˋ*)و

    if (l % 2019 == 0){
        mode = 1;
        min = 0;
    }

    if (mode == 0){
        for (i = l; i <= r - 1; i++){
            if (i % 2019 == 0){
                min = 0;
                break;
            }
            for (j = l + 1; j <= r; j++){
                count = ((i * j) % 2019);
                if (min > count){
                    min = count;
                    if (min == 0){
                        break;
                    }
                }
                if (j % 2019 == 0){
                    min = 0;
                    break;
                }
            }
            if (min == 0){
                break;
            }
        }
    }

    //出力処理ここから٩(ˊᗜˋ*)و

    printf("%d",min);

}


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

int main()
{
	long int L, R;
	int l, k, a, b, i, j, c;
	scanf("%d", &L);
	scanf("%d", &R);

	l = L % 2019;

	k = R - L;

	if (k > 2017)
	{
		printf("0");
		return 0;
	}

	c = l * (l + 1) % 2019;

	for (i = l; i < l + k; i++)
	{
		for (j = i + 1; j < l + k + 1; j++)
		{
			if ( i * j % 2019 < c)
			{
				c = i * j % 2019;
			}
		}

		if (c == 0)
		{
			break;
		}
	}
	printf("%d", c);
	return 0;
}


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

int main()
{
    long l,r,c,min=2019,tmp;
    long i,j;

    scanf("%ld %ld",&l,&r);
    c=r-l;
    if(c>=2019){
        printf("0\n");
        return 0;
    }
    l = l % 2019;
    r = r % 2019;
    for(i=l;i<=r;i++){
        tmp = ((i % 2019) * (j % 2019)) % 2019;
        if(min > tmp){
            min = tmp;
        }
    }

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

    return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define ll long long int
#define lim 100010
#define mod 1e9+7
#define INF 1e9
#define MIN(x,y) ((x)<(y)?(x):(y))
#define MAX(x,y) ((x)<(y)?(y):(x))
#define ABS(x) ((x)>0?(x):-(x))
#define num 2019
int main(void){
	ll l,r;
	scanf("%lld%lld",&l,&r);
	l %= num;
	r %= num;
	if(r<l){
		printf("0");
		return 0;
	}
	printf("%d",(l*(l+1))%num);

	return 0;
}


//#include "pch.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"

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

	int nDiff = R - L;
	L = L % 2019;
	if (nDiff > 2019)
		R = L + 2019;
	else
		R = L + nDiff;

	int nMod = (L * (L + 1)) % 2019;
	for (int i = L; i < R; i++)
	{
		for (int j = i + 1; j <= R; j++)
		if (nMod > (i * j) % 2019)
			nMod = (i * j) % 2019;
	}

	printf("%d", nMod);

	return 0;
}


#include<stdio.h>

int main(){
  int l;
  scanf("%d",&l);
  printf("%d",l*(l+1));
  return 0;
}


#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define min(x, y) ((x<y) ? (x) : (y))
#define abs(x)  ((x)>=0 ? (x) : ((x)*-1))

int main(){
	long long int L, R;
	scanf("%lld %lld", &L, &R);

	if (L+(2019-L%2019) < R){
		printf("0");
		return 0;
	}

	printf("%lld", (long long int)(L*(L+1)%2019));

	return 0;
}


#include<stdio.h>
int MIN(int a, int b){return a>b?b:a;}

typedef long long int ll;

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

    int time;
    if(r-l+1 >= 2019){
        time = 2019;
    }else{
        time = r-l+1;
    }

    int ans = 2019;
    for(int i=0; i < time; i++){
        ans = MIN(ans,(l*(l+i+1))%2019);
    }

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


#include <stdio.h>

int main(){
	int l,r,i,j,f,s;
	scanf("%d %d",&l,&r);
	f = l * r % 2019;
	for(i = l;i < r;i++){
		for(j = i + 1;j <= r;j++){
			s = i * j %2019;
			if(s<f) f = s;
		}
	}
	printf("%d",f);
	return 0;
}


#include <stdio.h>
#define MOD 2019

int main(){
  int L,R;
  int min = MOD*MOD;

  scanf("%d %d",&L,&R);
  L %= MOD;
  R %= MOD;
  if(L>R) R += MOD;

  for(int i=L;i<R;i++)
    for(int j=i+1;j<=R;j++)
      if(min > (i*j)%MOD)
	min = (i*j)%MOD;

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

  return 0;
}


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

int main(){

	int i, j, k, len, count = 0;
	char S[200000];

	scanf("%s", S);

	len = strlen(S);

	for (i = 1; i < len - 1; i++){
		if (S[i] == 'B' && S[i + 1] == 'C'){
			j = i;
			while (j - 1 >= 0 && S[j - 1] == 'A'){
				count++;
				j--;
			}
			S[j] = 'B';
			S[j + 1] = 'C';

			for (k = j + 2; k <= i + 1; k++){
				S[k] = 'A';
			}

			i++;
		}

//		printf("%s %d\n", S, i);
	}
	printf("%d\n", count);

  return 0;
}

Score : 600 points

Problem Statement

You are given a string s consisting of A, B and C.

Snuke wants to perform the following operation on s as many times as possible:

Choose a contiguous substring of s that reads ABC and replace it with BCA.

Find the maximum possible number of operations.

Constraints

1 \leq |s| \leq 200000

Each character of s is A, B and C.

Input

Input is given from Standard Input in the following format:

s

Output

Find the maximum possible number of operations.

Sample Input 1

ABCABC

Sample Output 1

3

You can perform the operations three times as follows: ABCABC → BCAABC → BCABCA → BCBCAA. This is the maximum result.

Sample Input 2

C

Sample Output 2

0

Sample Input 3

ABCACCBABCBCAABCB

Sample Output 3

6


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

int main(void){
    char s[200500];
    scanf("%s",s);
    long long ans=0,counta=0;
    for (int i=0; i<strlen(s); i++){
        if (s[i]=='A'){
            counta++;
        } else if (s[i]=='B'){
            if (s[i+1]=='C'){
                ans+=counta;
                i++;
            } else {
                counta=0;
            }
        } else {
            counta=0;
        }
    }
    printf("%lld\n",ans);
    return 0;
}


#include<stdio.h>

int main()
{
    char s[300000];
    scanf("%s",&s);
    int i,oldcounter=0,counter=0;

    do
    {
        oldcounter=counter;
        for(i=0;i<199998;i++)
        {
            if(s[i]=='A'&&s[i+1]=='B'&&s[i+2]=='C')
            {
                s[i]='B';
                s[i+1]='C';
                s[i+2]='A';
                counter++;
                //printf("qwertyui\n");
            }
        }
        //printf("%s\n",s);
    } while (counter!=oldcounter);
    printf("%d\n",counter);
    return 0;
}


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

int main(){
    char S[200000];
    scanf("%s", S);
    int N = strlen(S), ans=0, b_ans , i, j;

    int start=0, cnt=0, end=N-2, endf;
    for(i=0; 1; i++){
        b_ans = ans;
        cnt = 0;
        for(j=start; j<end; j++){
            if (S[j]=='A' && S[j+1] == 'B' && S[j+2] == 'C'){
                S[j]='B';
                S[j+1] = 'C';
                S[j+2] = 'A';
                ans++;
                if (cnt=0) start = j-2;
                if (start<0) start = 0;
                endf = j;
                cnt++;
            }
        }
        end = endf+2;
        if (end >= N-2) end = N-2;
        if (b_ans == ans) break;
    }

    printf("%d",  ans);

    return 0;
}


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

typedef int32_t i32;
typedef int64_t i64;

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

void run (void) {
  char *s = (char *) calloc (200000 + 1, sizeof (char));
  scanf ("%s", s);
  i32 n = strlen (s);
  s[n] = 'B';
  i64 ans = 0;
  i32 i = 0;
  while (i < n) {
    if (s[i] != 'A') {
      i++;
      continue;
    }
    i32 a = 0;
    while (i < n) {
      if (s[i] == 'A') {
	a++;
	i++;
      } else if (s[i] == 'B' && s[i + 1] == 'C') {
	ans += a;
	i += 2;
      } else {
	break;
      }
    }
  }
  printf ("%" PRIi64 "\n", ans);
}

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


Score : 300 points

Problem Statement

You are given a grid of squares with H horizontal rows and W vertical columns, where each square is painted white or black.
HW characters from A_{11} to A_{HW} represent the colors of the squares.
A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

We will repeatedly perform the following operation until all the squares are black:

Every white square that shares a side with a black square, becomes black.

Find the number of operations that will be performed.
The initial grid has at least one black square.

Constraints

1 \leq H,W \leq 1000

A_{ij} is # or ..

The given grid has at least one black square.

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the number of operations that will be performed.

Sample Input 1

3 3
...
.#.
...

Sample Output 1

2

After one operation, all but the corners of the grid will be black. After one more operation, all the squares will be black.

Sample Input 2

6 6
..#..#
......
#..#..
......
.#....
....#.

Sample Output 2

3