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

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

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

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

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

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

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

typedef struct{
    char str[32];
    long long val;
}data;

data hmap[1048576];

void hinit(){
    long long i;
    for(i=0;i<bas;i++){
        strcpy(hmap[i].str,"");
        hmap[i].val=0;
    }
}

long long chash(char s[]){
    long long l,i,r=0,k;
    l=strlen(s);
    k=1;
    for(i=0;i<l;i++){
        r+=(k*s[i]);r%=bas;
        k*=128;k%=bas;
    }
    return r;
}

long long search(char s[]){
    long long h,p;
    h=chash(s);
    p=h;
    while(1){
        if(strcmp(hmap[p].str,s)==0){return p;}
        if(strlen(hmap[p].str)==0){return -1;}
        p++;p%=bas;
        if(p==h){return -1;}
    }
}

void add(char s[]){
    long long h,p;
    h=search(s);
    if(h!=-1){hmap[h].val++;return;}
    h=chash(s);
    p=h;
    while(1){
        if(strlen(hmap[p].str)==0){
            strcpy(hmap[p].str,s);
            hmap[p].val++;
            return;
        }
        p++;p%=bas;
        if(p==h){return;}
    }
}

int main(void){
  hinit();
  long long i,j,n,m,k,a[524288],b,c,h,w,r=0,l,t;
  char s[32];
  scanf("%lld",&n);
  t=0;
  sprintf(s,"%lld",t);
  add(s);
  for(i=0;i<n;i++){
    scanf("%lld",&a[i]);
    t+=a[i];
    sprintf(s,"%lld",t);
    add(s);
  }
  for(i=0;i<bas;i++){
    w=hmap[i].val;
    r+=((w*(w-1))/2);
  }
  printf("%lld\n",r);
  return 0;
}


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

int combination(int n, int k) {
	if (n < k)return 0;
	if (k > 0) {
		return combination(n, k - 1) * (n - k + 1) / k;
	}
	else {
		return 1;
	}
}

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

int main(void) {
	int N;
	long S[200001];
	long sorted[200001];
	scanf("%d", &N);
	S[0] = 0;
	for (int i = 1; i <= N; i++) {
		long temp;
		scanf("%ld", &temp);
		S[i] = S[i - 1] + temp;
	}
	long max = -10000000;
	for (int i = 1; i <= N; i++) {
		if (max < S[i])max = S[i];
	}
	//counting_sort(S, sorted, max, N + 1);
	qsort(S, N + 1, sizeof(long), func);
	int before = 1;
	int res = 0;
	for (int i = 1; i <= N; i++) {
		if (S[i] == S[i - 1]) {
			before++;
		}
		else {
			res += combination(before, 2);
			before = 1;
		}
	}
	res += combination(before, 2);
	printf("%d\n", res);
	return 0;
}

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

Score : 200 points

Problem Statement

We have an integer sequence A, whose length is N.

Find the number of the non-empty contiguous subsequences of A whose sums are 0.
Note that we are counting the ways to take out subsequences.
That is, even if the contents of some two subsequences are the same, they are counted individually if they are taken from different positions.

Constraints

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

-10^9 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Find the number of the non-empty contiguous subsequences of A whose sum is 0.

Sample Input 1

6
1 3 -4 2 2 -2

Sample Output 1

3

There are three contiguous subsequences whose sums are 0: (1,3,-4), (-4,2,2) and (2,-2).

Sample Input 2

7
1 -1 1 -1 1 -1 1

Sample Output 2

12

In this case, some subsequences that have the same contents but are taken from different positions are counted individually.
For example, three occurrences of (1, -1) are counted.

Sample Input 3

5
1 -2 3 -4 5

Sample Output 3

0

There are no contiguous subsequences whose sums are 0.

Submitted source-code file size, in bytes.

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

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


Score : 100 points

Problem Statement

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

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

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

Constraints

S is a string of length 3.

Each character in S is o or x.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

oxo

Sample Output 1

900

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

Sample Input 2

ooo

Sample Output 2

1000

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

Sample Input 3

xxx

Sample Output 3

700

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


#include<stdio.h>
int main(){
int n,x,i,m,minm=1234567890,r;
scanf("%d%d",&n,&x);
r=n;
for(i=0;i<n;i++){
scanf("%d",&m);
x-=m;
if(m<minm){minm=m;}
}
printf("%d\n",r+(x/minm));
return 0;
}

Score : 200 points

Problem Statement

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

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

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

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

Constraints

2 ≤ N ≤ 100

1 ≤ m_i ≤ 1000

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N X
m_1
m_2
:
m_N

Output

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

Sample Input 1

3 1000
120
100
140

Sample Output 1

9

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

Sample Input 2

4 360
90
90
90
90

Sample Output 2

4

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

Sample Input 3

5 3000
150
130
150
130
110

Sample Output 3

26


#include <stdio.h>
#define MAX_N 100000
#define MAXEQ(m, x) m = (x)>m ? (x) : m

long x[MAX_N];
long v[MAX_N];
long leftw[MAX_N];
long rightw[MAX_N];

int main(void){
    long n, c;
    scanf("%ld %ld\n", &n, &c);
    long max_cal = 0;
    {
        long now_cal = 0;
        long pos = 0;
        // left
        for(long i=0; i<n; i++){
            scanf("%ld %ld\n", x+i, v+i);
            now_cal += v[i] - x[i] + pos;
            pos = x[i];
            leftw[i] = now_cal;
            MAXEQ(max_cal, now_cal);
        }
    }
    {
        long now_cal = 0;
        long pos = c;
        // right
        for(long i=n-1; i>=0; i--){
            now_cal += v[i] - pos + x[i];
            pos = x[i];
            rightw[i] = now_cal;
            MAXEQ(max_cal, now_cal);
        }
    }
    for(long i=0; i<n; i++){
        {
            long tmax = 0;
            for(long j=0; j<i; j++){
                MAXEQ(tmax, leftw[j]);
            }
            MAXEQ(max_cal, rightw[i] - c + x[i] + tmax);
        }
        {
            long tmax = 0;
            for(long j=i+1; j<n; j++){
                MAXEQ(tmax, rightw[j]);
            }
            MAXEQ(max_cal, leftw[i] - x[i] + tmax);
        }
    }
    printf("%ld\n", max_cal);
    return 0;
}

Score : 500 points

Problem Statement

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

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

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

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

Constraints

1 ≤ N ≤ 10^5

2 ≤ C ≤ 10^{14}

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

1 ≤ v_i ≤ 10^9

All values in input are integers.

Subscores

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

Input

Input is given from Standard Input in the following format:

N C
x_1 v_1
x_2 v_2
:
x_N v_N

Output

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

Sample Input 1

3 20
2 80
9 120
16 1

Sample Output 1

191

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

Sample Input 2

3 20
2 80
9 1
16 120

Sample Output 2

192

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

Sample Input 3

1 100000000000000
50000000000000 1

Sample Output 3

0

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

Sample Input 4

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

Sample Output 4

6500000000

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


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

int main(int argc, char *argv[])
{
    int A,B,C,X;

    if (scanf("%d %d %d", &A, &B, &X) < 3)
    {
        exit(1);
    }

    if (1 <= A && A <= 100 && 1 <= B && B <= 100 && 1 <= X && X <= 200)
    {

        C = A + B;

        if (X < A)
        {
            printf("NO\n");
        }
        else
        {
            if (X > C)
            {
                printf("NO\n");
            }
            else
            {
                printf("YES\n");
            }
        }
    }
    return 1;
}

Score : 100 points

Problem Statement

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

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

Constraints

1 \leq A \leq 100

1 \leq B \leq 100

1 \leq X \leq 200

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B X

Output

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

Sample Input 1

3 5 4

Sample Output 1

YES

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

Sample Input 2

2 2 6

Sample Output 2

NO

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

Sample Input 3

5 3 2

Sample Output 3

NO

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


#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
int n;
int max=0;
int max2=0;
int a[100001];
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(max<a[i])
{max=a[i];}
}
for(i=0;i<n;i++)
{
if(abs(max-max2*2)>abs(max-a[i]*2))
{max2=a[i];}
}
printf("%d",max);
printf("%d",max2);
return 0;}

Score : 400 points

Problem Statement

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

Constraints

2 \leq n \leq 10^5

0 \leq a_i \leq 10^9

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

All values in input are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

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

Sample Input 1

5
6 9 4 2 11

Sample Output 1

11 6

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

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

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

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

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

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

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

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

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

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

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

Thus, we should print 11 and 6.

Sample Input 2

2
100 0

Sample Output 2

100 0


#include<stdio.h>
#include<string.h>
int main()
{
    int i,l,j,ck=0;
    char ch[4],ch1[4]="abc",ch2[4]="bac",ch3[4]="cab",ch4[4]="bca",ch5[4]="acb",ch6[4]="cba";
    scanf("%s",ch);
    if(strcmp(ch,ch1)==0 || strcmp(ch,ch2)==0 || strcmp(ch,ch3)==0 || strcmp(ch,ch4)==0 || strcmp(ch,ch5)==0 || strcmp(ch,ch6)==0)
    {
        printf("Yes\n");
    }
    else
        printf("No\n");

    return 0;
}

Score : 100 points

Problem Statement

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

Constraints

|S|=3

S consists of a, b and c.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

bac

Sample Output 1

Yes

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

Sample Input 2

bab

Sample Output 2

No

Sample Input 3

abc

Sample Output 3

Yes

Sample Input 4

aaa

Sample Output 4

No


#include<stdio.h>
int main()
{
	int i, j;
	int t;
	int max,z;
	scanf("%d %d %d", &i, &j, &t);
	max = j - i + 1;
	if (max <= 2 * t)
	{
		for (z = i; z<= j; z++)
		{
			printf("%d", z);
			if (z != j)printf("\n");
		}
	}
	else
	{
		for (z = i; z < i + t; z++)
		{
			printf("%d\n", z);
		}
		for (z = j-t+1; z <= j; z++)
		{
			printf("%d", z);
			if (z != j)printf("\n");
		}
	}
	return 0;
}

Score : 200 points

Problem Statement

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

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

Constraints

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

1 \leq K \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

A B K

Output

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

Sample Input 1

3 8 2

Sample Output 1

3
4
7
8

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

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

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

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

Sample Input 2

4 8 3

Sample Output 2

4
5
6
7
8

Sample Input 3

2 9 100

Sample Output 3

2
3
4
5
6
7
8
9


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

Score : 100 points

Problem Statement

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

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

Constraints

1 \leq A \leq 1 000

1 \leq B \leq 1 000

1 \leq C \leq 1 000

1 \leq D \leq 1 000

All input values are integers.

Input

Input is given from Standard Input in the following format:

A
B
C
D

Output

Print the minimum total fare.

Sample Input 1

600
300
220
420

Sample Output 1

520

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

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

Sample Input 2

555
555
400
200

Sample Output 2

755

Sample Input 3

549
817
715
603

Sample Output 3

1152


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

Score : 100 points

Problem Statement

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

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

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

Constraints

All input values are integers.

1 \leq A, B \leq 500

1 \leq C \leq 1000

Input

Input is given from Standard Input in the following format:

A B C

Output

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

Sample Input 1

50 100 120

Sample Output 1

Yes

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

Sample Input 2

500 100 1000

Sample Output 2

No

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

Sample Input 3

19 123 143

Sample Output 3

No

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

Sample Input 4

19 123 142

Sample Output 4

Yes


#include <stdio.h>

int main(int argc, char const *argv[])
{
	char c =0;
	char str[4];
 	for(int i = 0;i < 3;i++){
	scanf("%s",str);
	printf("%c",str[i]);
 	}

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

Score : 100 points

Problem Statement

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

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

Constraints

Input consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

ant
obe
rec

Sample Output 1

abc

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

Sample Input 2

edu
cat
ion

Sample Output 2

ean


/*
 AtCoder XXX 000
 */

#include <stdio.h>

int main(void){
  char a,b,c;
  char out[4];

  scanf("%c%c%c\n",&a,&b,&c);
  out[0]=a;
  scanf("%c%c%c\n",&a,&b,&c);
  out[1]=b;
  scanf("%c%c%c\n",&a,&b,&c);
  out[2]=c;

  printf("%c%c%c",out[0],out[1],out[2]);
  return 0;
}


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

int main(void){

	int in[2]={0};

	char tmp;
	int i,j;
	int count=0;
	int rev=0,num=0;
	scanf("%d %d",&in[0],&in[1]);
	//printf("%d %d\n",in[0],in[1]);

	for(i=in[0];i<in[1];i++){

		num=i;
		rev=0;
		while(num>0){
			rev=rev*10+num%10;
			num/=10;
		}

		if(i==rev){
			count++;
		}
	}

	printf("%d",count);

	return 0;
}

Score : 200 points

Problem Statement

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

Constraints

10000 \leq A \leq B \leq 99999

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

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

Sample Input 1

11009 11332

Sample Output 1

4

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

Sample Input 2

31415 92653

Sample Output 2

612


#include <stdio.h>

int main(){
	long long int N,M;
	scanf("%lld%lld",&N,&M);
	if(N==1 && M==1){
		printf("1\n");
	}else if(N==1 && M>1){
		printf("%lld\n",M-2);
	}else if(N>1 && M==1){
		printf("%lld",N-2);
	}else{
		printf("%lld\n",(M-2)*(N-2));
	}
	return 0;
}

Score : 300 points

Problem Statement

There is a grid with infinitely many rows and columns. In this grid, there is a rectangular region with consecutive N rows and M columns, and a card is placed in each square in this region.
The front and back sides of these cards can be distinguished, and initially every card faces up.

We will perform the following operation once for each square contains a card:

For each of the following nine squares, flip the card in it if it exists: the target square itself and the eight squares that shares a corner or a side with the target square.

It can be proved that, whether each card faces up or down after all the operations does not depend on the order the operations are performed.
Find the number of cards that face down after all the operations.

Constraints

1 \leq N,M \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of cards that face down after all the operations.

Sample Input 1

2 2

Sample Output 1

0

We will flip every card in any of the four operations. Thus, after all the operations, all cards face up.

Sample Input 2

1 7

Sample Output 2

5

After all the operations, all cards except at both ends face down.

Sample Input 3

314 1592

Sample Output 3

496080


#include<stdio.h>
int main()
{
    long long sum,n,m;
    scanf("%lld%lld",&n,&m);
    if(n==1)
    {
        if(m==1)
            printf("1\n");
        else
            printf("%lld\n",m-2);
    }
    else if(m==1)
    {
        printf("%lld\n",n-2);
    }
    else
    {
        sum=n*m;
        sum-=(n+m-2)*2;
        printf("%lld\n",sum);
    }
    return 0;
}


#include<stdio.h>

int main(void){
    int N,K;
    int count = 0;
    scanf("%d %d",&N,&K);
    for(int i=1;i<=N;i++){
        for(int j=1;j<=N;j++){
            if(i%j>=K){
                count ++;
            }
        }
    }
    printf("%d",count);
    return 0;
}

Score : 400 points

Problem Statement

Takahashi had a pair of two positive integers not exceeding N, (a,b), which he has forgotten.
He remembers that the remainder of a divided by b was greater than or equal to K.
Find the number of possible pairs that he may have had.

Constraints

1 \leq N \leq 10^5

0 \leq K \leq N-1

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of possible pairs that he may have had.

Sample Input 1

5 2

Sample Output 1

7

There are seven possible pairs: (2,3),(5,3),(2,4),(3,4),(2,5),(3,5) and (4,5).

Sample Input 2

10 0

Sample Output 2

100

Sample Input 3

31415 9265

Sample Output 3

287927211


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

int main(){
	long long int n, m, i, j;
	scanf("%lld", &n);
	scanf("%lld", &m);
	if(n==1 && m==1){
		printf("%lld\n", n*m);
	}
	else if(n==1 || m==1){
		printf("%lld\n", n*m-2);
	}
	else {
		printf("%lld\n", (n-2)*(m-2));
	}
	return 0;
}


#include <stdio.h>
int main(void){
   int N;
   scanf("%d",&N);
   printf("%d",N/3);
   return 0;
}

Score : 100 points

Problem Statement

There are N students in a school.

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

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

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

Constraints

1 \leq N \leq 1000

All input values are integers.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

8

Sample Output 1

2

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

Sample Input 2

2

Sample Output 2

0

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

Sample Input 3

9

Sample Output 3

3


#include <stdio.h>

int main(void)
{
    int n,s[100],i;

    scanf("%d",&n);

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

    int flag=0;
    for(i=0;i<n-1;i++){
        if(s[i]=='Y'){
           flag=1;
           break;
        }
    }

    if(flag==0)    printf("Three\n");
    else        printf("four\n");

    return 0;
}

Score : 200 points

Problem Statement

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

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

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

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

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

Constraints

1 \leq N \leq 100

S_i is P, W, G or Y.

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

Input

Input is given from Standard Input in the following format:

N
S_1 S_2 ... S_N

Output

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

Sample Input 1

6
G W Y P Y W

Sample Output 1

Four

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

Sample Input 2

9
G W W G P W P G G

Sample Output 2

Three

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

Sample Input 3

8
P Y W G Y W Y Y

Sample Output 3

Four


#include <stdio.h>

int main(void){
  int N;
  scanf("%d",&N);
  char S;
  int flag = 0;

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

  if(flag){
    printf("Four\n");
  }else{
    printf("Three\n");
  }
  return 0;
}


#include <stdio.h>

int main(void)
{
        int n,i;
        char s[101];

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

        scanf("%s",s);

        for (i=1;i<=n;i++){
                if (s[i]=='Y'){
                        printf("Four\n");
                        return 0;
                }
        }

        printf("Three\n");

        return 0;
}


/*
 AtCoder XXX 000
 */

#include <stdio.h>

int abs(int x){
	return (x>0) ? x : (-x);
}

int main(void){

	int n;
	int width,height,dist;
	int x[60000];
	int y[60000];
	int q;
	int l,r;
	int i,j,k;
	int maryoku;

	scanf("%d",&height);
	scanf("%d",&width);
	scanf("%d",&dist);

	for(j=0; j<height; j++) for(i=0; i<width; i++){
		scanf("%d",&n);
		x[n] = i;
		y[n] = j;
	}

	scanf("%d",&q);
	for(k=0; k<q; k++){
		maryoku=0;
		scanf("%d",&l);
		scanf("%d",&r);

		while(l < r-dist){
			maryoku += abs(x[l] - x[l+dist]) + abs(y[l] - y[l+dist]);
			l += dist;
		}

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

	return 0;
}

Score : 400 points

Problem Statement

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

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

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

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

The i-th test will be conducted as follows:

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

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

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

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

Constraints

1 \leq H,W \leq 300

1 \leq D \leq H×W

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

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

1 \leq Q \leq 10^5

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

(R_i-L_i) is a multiple of D.

Input

Input is given from Standard Input in the following format:

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

Output

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

Output should be in the order the tests are conducted.

Sample Input 1

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

Sample Output 1

5

4 is written in Square (1,2).

6 is written in Square (3,3).

8 is written in Square (3,1).

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

Sample Input 2

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

Sample Output 2

0
0

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

Sample Input 3

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

Sample Output 3

0
5
0


#include <stdio.h>
#include <stdlib.h>
int h, w, d;
int xs[90001];
int ys[90001];

int main() {
  int q;
  scanf("%d%d%d", &h, &w, &d);
  for (int y = 0; y < h; y++) {
    for (int x = 0; x < w; x++) {
      int v;
      scanf("%d", &v);
      xs[v] = x;
      ys[v] = y;
    }
  }
  scanf("%d", &q);
  for (int _ = 0; _ < q; _++) {
    int l, r;
    scanf("%d%d", &l, &r);
    int ans = 0;
    for (int l0 = l; l0 < r; l0 += d) {
      int r0 = l0 + d;
      ans += abs(xs[r0] - xs[l0]) + abs(ys[r0] - ys[l0]);
    }
    printf("%d\n", ans);
  }
  return 0;
}


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

int compare(const void * a, const void * b){
	return *(int *)b - *(int *)a;
}
int main(){
  int n;
  scanf("%d\n", &n);
  int *a = (int *)calloc(n, sizeof(int));
  char buff[400] = {0};
  fgets(buff, 401, stdin);
  a[0] = atoi(strtok(buff, " "));
  for(int i =1; i < n; ++i)
	  a[i] = atoi(strtok(NULL, " "));
  qsort(a, n, sizeof(int),compare);
  int alice=0;int bob=0;
  for(int i = 0; i< n; ++i){
	  if(i%2)
		  bob += a[i];
	  else
		  alice += a[i];
  }
  printf("%d", alice - bob);
  free(a);
  return 0;
}

Score: 200 points

Problem Statement

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

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

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

Constraints

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

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

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 a_3 ... a_N

Output

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

Sample Input 1

2
3 1

Sample Output 1

2

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

Sample Input 2

3
2 7 4

Sample Output 2

5

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

Sample Input 3

4
20 18 2 18

Sample Output 3

18


Score : 300 points

Problem Statement

You are given two integer sequences of length N: a_1,a_2,..,a_N and b_1,b_2,..,b_N.
Determine if we can repeat the following operation zero or more times so that the sequences a and b become equal.

Operation: Choose two integers i and j (possibly the same) between 1 and N (inclusive), then perform the following two actions simultaneously:

Add 2 to a_i.

Add 1 to b_j.

Constraints

1 ≤ N ≤ 10 000

0 ≤ a_i,b_i ≤ 10^9 (1 ≤ i ≤ N)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 .. a_N
b_1 b_2 .. b_N

Output

If we can repeat the operation zero or more times so that the sequences a and b become equal, print Yes; otherwise, print No.

Sample Input 1

3
1 2 3
5 2 2

Sample Output 1

Yes

For example, we can perform three operations as follows to do our job:

First operation: i=1 and j=2. Now we have a = \{3,2,3\}, b = \{5,3,2\}.

Second operation: i=1 and j=2. Now we have a = \{5,2,3\}, b = \{5,4,2\}.

Third operation: i=2 and j=3. Now we have a = \{5,4,3\}, b = \{5,4,3\}.

Sample Input 2

5
3 1 4 1 5
2 7 1 8 2

Sample Output 2

No

Sample Input 3

5
2 7 1 8 2
3 1 4 1 5

Sample Output 3

No


#include<stdio.h>
int main()
{
	int N,A[2][101],sum[101],max;
	int i,j,k;

	//// Scan
	scanf("%d",&N);
	for (i=1;i<=N;i++){scanf("%d",&A[0][i]);}
	for (i=1;i<=N;i++){scanf("%d",&A[1][i]);}

	/// the initial sum is the "L" shaped path:
	sum[1]=A[0][1];
	for (i=1;i<=N;i++){sum[1]+=A[1][i];}
	max=sum[1];

	//// the other sums are modification of the previous one:
	for (j=2;j<=N;j++){sum[j]=sum[j-1]+A[0][j]-A[1][j-1];
		if(max<sum[j]) max=sum[j]; else ;
	}

	//////// answer
	printf("%d",max);

	return 0;
}

Score : 300 points

Problem Statement

We have a 2 \times N grid. We will denote the square at the i-th row and j-th column (1 \leq i \leq 2, 1 \leq j \leq N) as (i, j).

You are initially in the top-left square, (1, 1).
You will travel to the bottom-right square, (2, N), by repeatedly moving right or down.

The square (i, j) contains A_{i, j} candies.
You will collect all the candies you visit during the travel.
The top-left and bottom-right squares also contain candies, and you will also collect them.

At most how many candies can you collect when you choose the best way to travel?

Constraints

1 \leq N \leq 100

1 \leq A_{i, j} \leq 100 (1 \leq i \leq 2, 1 \leq j \leq N)

Input

Input is given from Standard Input in the following format:

N
A_{1, 1} A_{1, 2} ... A_{1, N}
A_{2, 1} A_{2, 2} ... A_{2, N}

Output

Print the maximum number of candies that can be collected.

Sample Input 1

5
3 2 2 4 1
1 2 2 2 1

Sample Output 1

14

The number of collected candies will be maximized when you:

move right three times, then move down once, then move right once.

Sample Input 2

4
1 1 1 1
1 1 1 1

Sample Output 2

5

You will always collect the same number of candies, regardless of how you travel.

Sample Input 3

7
3 3 4 5 4 5 3
5 3 4 4 2 3 2

Sample Output 3

29

Sample Input 4

1
2
3

Sample Output 4

5


#include <stdio.h>
int main(void)
{
    long int n,m;
    scanf("%ld %ld",&n,&m);
    long int l[200005]={};
    long int r[200005]={};
    long int d[200005]={};
    for(int p=0;p<m;p++)scanf("%ld %ld %d",&l[p],&r[p],&d[p]);
    long int gr[100005]={0};
    long long int pl[100005]={};
    long int rer[100005]={114514};
    long int rel[100005]={114514};
    long int c=0;
    long long int x=0;
    for(int q=0;q<m;q++)
    {
        rer[l[q]]=r[q];
        rel[r[q]]=l[q];
        if(gr[l[q]]==gr[r[q]] && gr[r[q]]!=0)
        {
            if(pl[r[q]]-pl[l[q]]!=d[q])
            {
                printf("No");
                return 0;
            }
        }
        else if(gr[l[q]]!=0 && gr[r[q]]!=0)
        {
            gr[r[q]]=gr[l[q]];
            long int len=pl[l[q]]+d[q]-pl[r[q]];
            x=r[q];
            while(x!=114514)
            {
                gr[x]=gr[l[q]];
                pl[x]=pl[x]+len;
                x=rer[x];
            }
            x=r[q];
            while(x!=114514)
            {
                gr[x]=gr[l[q]];
                pl[x]=pl[x]+len;
                x=rel[x];
            }
        }
        else if(gr[l[q]]!=0)
        {
            gr[r[q]]=gr[l[q]];
            pl[r[q]]=pl[l[q]]+d[q];
        }
        else if(gr[r[q]]!=0)
        {
            gr[l[q]]=gr[r[q]];
            pl[l[q]]=pl[r[q]]-d[q];
        }
        else
        {
            pl[l[q]]=0;
            pl[r[q]]=d[q];
            c++;
            gr[l[q]]=c;
            gr[r[q]]=c;
        }
    }
    long long int max=pl[0];
    long long int min=pl[0];
    for(int ppp=0;ppp<n;ppp++)
    {
        if(max<pl[ppp])max=pl[ppp];
        if(min>pl[ppp])min=pl[ppp];
    }
    if(max-min>1000000000)
    {
        printf("No");
        return 0;
    }
    printf("Yes");
}

Score : 400 points

Problem Statement

There are N people standing on the x-axis.
Let the coordinate of Person i be x_i.
For every i, x_i is an integer between 0 and 10^9 (inclusive).
It is possible that more than one person is standing at the same coordinate.

You will given M pieces of information regarding the positions of these people.
The i-th piece of information has the form (L_i, R_i, D_i).
This means that Person R_i is to the right of Person L_i by D_i units of distance, that is, x_{R_i} - x_{L_i} = D_i holds.

It turns out that some of these M pieces of information may be incorrect.
Determine if there exists a set of values (x_1, x_2, ..., x_N) that is consistent with the given pieces of information.

Constraints

1 \leq N \leq 100 000

0 \leq M \leq 200 000

1 \leq L_i, R_i \leq N (1 \leq i \leq M)

0 \leq D_i \leq 10 000 (1 \leq i \leq M)

L_i \neq R_i (1 \leq i \leq M)

If i \neq j, then (L_i, R_i) \neq (L_j, R_j) and (L_i, R_i) \neq (R_j, L_j).

D_i are integers.

Input

Input is given from Standard Input in the following format:

N M
L_1 R_1 D_1
L_2 R_2 D_2
:
L_M R_M D_M

Output

If there exists a set of values (x_1, x_2, ..., x_N) that is consistent with all given pieces of information, print Yes; if it does not exist, print No.

Sample Input 1

3 3
1 2 1
2 3 1
1 3 2

Sample Output 1

Yes

Some possible sets of values (x_1, x_2, x_3) are (0, 1, 2) and (101, 102, 103).

Sample Input 2

3 3
1 2 1
2 3 1
1 3 5

Sample Output 2

No

If the first two pieces of information are correct, x_3 - x_1 = 2 holds, which is contradictory to the last piece of information.

Sample Input 3

4 3
2 1 1
2 3 5
3 4 2

Sample Output 3

Yes

Sample Input 4

10 3
8 7 100
7 9 100
9 8 100

Sample Output 4

No

Sample Input 5

100 0

Sample Output 5

Yes


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

    char a[7],b[4];
    double n=0,k;

    scanf("%s%s",a,b);
    strcat(a,b);

    for(int i=0;i<strlen(a);i++){
        n+=(a[i]-'0')*(pow(10,strlen(a)-i-1));
    }
    k=round(sqrt(n));

    if(n==k*k) printf("Yes\n");
    else printf("No\n");

    return 0;
}

Score : 200 points

Problem Statement

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

Constraints

1 ≤ a,b ≤ 100

a and b are integers.

Input

Input is given from Standard Input in the following format:

a b

Output

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

Sample Input 1

1 21

Sample Output 1

Yes

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

Sample Input 2

100 100

Sample Output 2

No

100100 is not a square number.

Sample Input 3

12 10

Sample Output 3

No


#include<stdio.h>
#include<string.h>
int main(void) {
	int a, b,c,i;
	char A[4] = {"No"};

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

	if (b >= 10) {
		c = a * 100 + b;
	}
	else {
	c = a * 10 + b;
	}

	for (i = 1; i < c; i++) {
		if (c%i == 0) {
			if (c / i == i) {
				strcpy(A, "Yes");
			}
		}
	}

	printf("%s", A);
	return 0;
}


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int a,b,c,n = 1,d = 1;
    scanf("%d%d",&a,&b);
    c = b;
    while(b!=0){
        n *= 10;
        b /= 10;
    }
    c = c + a * n;
    d = (int)sqrt((double)c);
    if(c == d*d)
        printf("Yes\n");
    else
        printf("No\n");
    return 0;
}


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

int main(){
  long int n;
  int check = 0;
  scanf("%ld",&n);
  long int x[n+1],y[n+1],t[n+1];
  x[0]=0;
  y[0]=0;
  t[0]=0;
  for(int i=1;i<=n;++i){
    scanf("%ld %ld %ld",&t[i],&x[i],&y[i]);
    long int del_t=t[i]-t[i-1];
    long int del_x=labs(x[i]-x[i-1]);
    long int del_y=labs(y[i]-y[i-1]);
    if(del_t+del_x+del_y % 2 == 1){
      check = 1;
      break;
    }else if(del_t<del_x+del_y){
      check = 1;
      break;
    }
  }
  if(check == 0) printf("Yes");
  else printf("No");
  return 0;
}

Score : 300 points

Problem Statement

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

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

Constraints

1 ≤ N ≤ 10^5

0 ≤ x_i ≤ 10^5

0 ≤ y_i ≤ 10^5

1 ≤ t_i ≤ 10^5

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
t_1 x_1 y_1
t_2 x_2 y_2
:
t_N x_N y_N

Output

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

Sample Input 1

2
3 1 2
6 1 1

Sample Output 1

Yes

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

Sample Input 2

1
2 100 100

Sample Output 2

No

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

Sample Input 3

2
5 1 1
100 1 1

Sample Output 3

No


#include <stdio.h>

int main(void){
  int a;
  scanf("%d",&a);
  int t[10000];
  int x[10000];
  int y[10000];
  int xn=0;
  int yn=0;
  int flag=0;
  for(int i=1;i<=a;i++){
    scanf("%d%d%d",t[i],x[i],y[i]);
    xn+=1;
    if(xn==x[i]){
      for(int k=1;k<=t[i]-i;k++){
        yn+=1;
        if(yn==y[i]){
          flag+=1;
        }
      }
    }
  }
  if(flag==a){
    printf("Yes");
  }else{
    printf("No");
  }

}


#include <stdio.h>

void solve(void)
{
	int n;
	int x0, y0, t0;
	int x1, y1, t1;
	int i;
	char ngTxt[] = "No", okTxt[] = "Yes", *pTxt = okTxt;

	x1 = y1 = t1 = 0;

	scanf("%d", &n);
	for(i=0;i<n;++i){
		int d;
		x0 = x1;
		y0 = y1;
		t0 = t1;

		scanf("%d %d %d", &t1, &x1, &y1);
		d = (x1>x0 ? x1-x0 : x0-x1) + (y1>y0 ? y1-y0 : y0-y1);
		if( d > t1 - t0 ){
			pTxt = ngTxt;
			break;
		}
		if( (t1-t0-d)%2 == 1 ){
			pTxt = ngTxt;
			break;
		}
	}
	printf("%s\n", pTxt);
}
int main(int argc, char **argv)
{
	solve();
	return 0;
}


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

int main(void)
{
	//変数の宣言
	int n;
	int a[100010];
	long long int min,max;

	//よく使う変数
	int i,j,k,l;
	int flag=0;
	int ans=0;
	int count=0;
	int temp,temp1,temp2;
	//データの読み込み
	scanf("%d",&n);

	for(i=n-1;i>=0;i--){
		scanf("%d",&a[i]);
	}

//	printf("nは%dです\n", n);
//	printf("データの読み込み終了\n");
	//実際の処理

	min=2;
	max=2;

	for(i=0;i<n;i++){
		if((min+a[i]-1)/a[i]>max/a[i]){
			printf("-1");
			return 0;
		}
		min=((min+a[i]-1)/a[i])*a[i];
		max=(max/a[i])*a[i]+a[i]-1;
		printf("min=%lld,max=%lld\n",min,max);
	}

//	printf("計算部分終了\n");
	//出力

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

//	printf("結果の出力終了\n");

	return 0;

}

Score : 500 points

Problem Statement

An adult game master and N children are playing a game on an ice rink.
The game consists of K rounds.
In the i-th round, the game master announces:

Form groups consisting of A_i children each!

Then the children who are still in the game form as many groups of A_i children as possible.
One child may belong to at most one group.
Those who are left without a group leave the game. The others proceed to the next round.
Note that it's possible that nobody leaves the game in some round.

In the end, after the K-th round, there are exactly two children left, and they are declared the winners.

You have heard the values of A_1, A_2, ..., A_K. You don't know N, but you want to estimate it.

Find the smallest and the largest possible number of children in the game before the start, or determine that no valid values of N exist.

Constraints

1 \leq K \leq 10^5

2 \leq A_i \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

K
A_1 A_2 ... A_K

Output

Print two integers representing the smallest and the largest possible value of N, respectively,
or a single integer -1 if the described situation is impossible.

Sample Input 1

4
3 4 3 2

Sample Output 1

6 8

For example, if the game starts with 6 children, then it proceeds as follows:

In the first round, 6 children form 2 groups of 3 children, and nobody leaves the game.

In the second round, 6 children form 1 group of 4 children, and 2 children leave the game.

In the third round, 4 children form 1 group of 3 children, and 1 child leaves the game.

In the fourth round, 3 children form 1 group of 2 children, and 1 child leaves the game.

The last 2 children are declared the winners.

Sample Input 2

5
3 4 100 3 2

Sample Output 2

-1

This situation is impossible.
In particular, if the game starts with less than 100 children, everyone leaves after the third round.

Sample Input 3

10
2 2 2 2 2 2 2 2 2 2

Sample Output 3

2 3


Score : 100 points

Problem Statement

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

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

Constraints

S is a string of length 10.

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

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

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

2017/01/07

Sample Output 1

2018/01/07

Sample Input 2

2017/01/31

Sample Output 2

2018/01/31


#include <stdio.h>

void quicksort(int number[],int first,int last)
{
   int i, j, pivot, temp;

   if(first < last){
      pivot = first;
      i = first;
      j = last;

      while(i < j){
         while((number[pivot] >= number[i]) && (i < last))
         {
            i++;
         }
         while(number[pivot] < number[j])
         {
            j--;
         }
         if(i < j){
            temp = number[i];
            number[i] = number[j];
            number[j] = temp;
         }
      }

      temp = number[pivot];
      number[pivot] = number[j];
      number[j] = temp;
      quicksort(number,first,j - 1);
      quicksort(number,j+1,last);

   }
}

int main()
{
    int n,d[100];

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

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

Score : 200 points

Problem Statement

An X-layered kagami mochi (X ≥ 1) is a pile of X round mochi (rice cake) stacked vertically where each mochi (except the bottom one) has a smaller diameter than that of the mochi directly below it. For example, if you stack three mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, you have a 3-layered kagami mochi; if you put just one mochi, you have a 1-layered kagami mochi.

Lunlun the dachshund has N round mochi, and the diameter of the i-th mochi is d_i centimeters. When we make a kagami mochi using some or all of them, at most how many layers can our kagami mochi have?

Constraints

1 ≤ N ≤ 100

1 ≤ d_i ≤ 100

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
d_1
:
d_N

Output

Print the maximum number of layers in a kagami mochi that can be made.

Sample Input 1

4
10
8
8
6

Sample Output 1

3

If we stack the mochi with diameters of 10, 8 and 6 centimeters from bottom to top in this order, we have a 3-layered kagami mochi, which is the maximum number of layers.

Sample Input 2

3
15
15
15

Sample Output 2

1

When all the mochi have the same diameter, we can only have a 1-layered kagami mochi.

Sample Input 3

7
50
30
50
100
50
80
30

Sample Output 3

4


#include <stdio.h>

int main(void){
  int N,d;
  int ans = 0;
  int flag[100];
  scanf("%d",&N);

  for(int i= 0;i < 100;i++){
    flag[i] = 0;
  }

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

  for(int i = 0;i < 100;i++){
    if(flag[i]) ans++;
  }

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


#include<stdio.h>

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

    printf("We have %d hours until New Year at 21 o'clock on 30th, December.\n",(24 - m) + 24);

    return 0;
}

Score : 100 points

Problem Statement

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

Constraints

1≤M≤23

M is an integer.

Input

Input is given from Standard Input in the following format:

M

Output

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

Sample Input 1

21

Sample Output 1

27

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

Sample Input 2

12

Sample Output 2

36


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

int main(){
  int A, B, len = 0;
  char s[12];

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

  while(s[len]){
    if(len == A && s[len] != '-'){
      break;
    } else if(len != A && (s[len] < '0' || s[len] > '9')){
      break;
    }
    len++;
  }

  if(len == strlen(s)){
    printf("Yes\n");
  } else {
    printf("No\n");
  }

  return 0;
}

Score : 200 points

Problem Statement

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

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

Constraints

1≤A,B≤5

|S|=A+B+1

S consists of - and digits from 0 through 9.

Input

Input is given from Standard Input in the following format:

A B
S

Output

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

Sample Input 1

3 4
269-6650

Sample Output 1

Yes

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

Sample Input 2

1 1
---

Sample Output 2

No

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

Sample Input 3

1 2
7444

Sample Output 3

No


#include <stdio.h>

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

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

        scanf("%s",s);

        if (s[a]!='-'){
                printf("No\n");
                return 1;
        }

        for (i=0;i<a;i++){
                if (s[i]=='-'){
                        printf("No\n");
                        return 1;
                }
        }

        printf("Yes\n");
return 0;

}


Score : 400 points

Problem Statement

We say that a odd number N is similar to 2017 when both N and (N+1)/2 are prime.

You are given Q queries.

In the i-th query, given two odd numbers l_i and r_i, find the number of odd numbers x similar to 2017 such that l_i ≤ x ≤ r_i.

Constraints

1≤Q≤10^5

1≤l_i≤r_i≤10^5

l_i and r_i are odd.

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.

Sample Input 1

1
3 7

Sample Output 1

2

3 is similar to 2017, since both 3 and (3+1)/2=2 are prime.

5 is similar to 2017, since both 5 and (5+1)/2=3 are prime.

7 is not similar to 2017, since (7+1)/2=4 is not prime, although 7 is prime.

Thus, the response to the first query should be 2.

Sample Input 2

4
13 13
7 11
7 11
2017 2017

Sample Output 2

1
0
0
1

Note that 2017 is also similar to 2017.

Sample Input 3

6
1 53
13 91
37 55
19 51
73 91
13 49

Sample Output 3

4
4
1
1
1
2


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Q_MAX 100000

int main(void) {
  int Q, *l, *r;
  int n[Q_MAX+1] = {}, p[Q_MAX+1] = {};
  scanf("%d", &Q);
  l = (int*)calloc(Q, sizeof(int));
  r = (int*)calloc(Q, sizeof(int));
  for (int i = 0; i < Q; i++) {
    scanf("%d %d", &l[i], &r[i]);
  }
  for (int i = 2; i <= Q_MAX; i++) {
    p[i] = 1;
  }

  for (int i = 2; i <= Q_MAX; i++) {
    if (p[i] && i <= Q_MAX / 2) {
      for (register int k = 2; i*k <= Q_MAX; k++) {
        p[i*k] = 0;
      }
    }
    n[i] = n[i-1] + (p[i] && p[(i+1)/2]);
  }

  for (int i = 0; i < Q; i++) {
    printf("%d\n", n[r[i]] - n[l[i]-1]);
  }

  return 0;
}


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

Score : 100 points

Problem Statement

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

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

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

Constraints

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

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

Sample Input 1

3 8 7 1

Sample Output 1

Left

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

Sample Input 2

3 4 5 2

Sample Output 2

Balanced

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

Sample Input 3

1 7 6 4

Sample Output 3

Right

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


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))

int main(){
	int a,b,c,d;
	scanf("%d%d%d%d",&a,&b,&c,&d);
	puts(a+b==c+d?"Balanced":a+b<c+d?"Right":"Left");
}


#include <stdio.h>

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

Score : 100 points

Problem Statement

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

Constraints

a and b are integers.

1 \leq a, b \leq 100

Input

Input is given from Standard Input in the following format:

a b

Output

Print x rounded up to the nearest integer.

Sample Input 1

1 3

Sample Output 1

2

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

Sample Input 2

7 4

Sample Output 2

6

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

Sample Input 3

5 5

Sample Output 3

5


#include <stdio.h>

int main(void) {
    int a, b;
    scanf("%d %d", &a, &b);
    int sum = a + b;
    if(sum % 2 != 0){
        printf("%d\n", sum / 2 + 1);
    }else{
        printf("%d\n", sum / 2);
    }
}


#include <stdio.h>

int main(void){
    int a,b;
    scanf("%d %d",&a,&b);
    printf("%d\n",(a+b+1)/2);
    return 0;
}


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

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

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

int main(void){

    int lens, lent;
    char s[max], t[max], tmp;

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

    lens=strlen(s);
    lent=strlen(t);

    for (int i=0; i<lens; i++) {
        for (int j=i+1; j<lens; j++) {
            if (s[i]>s[j]) {
                tmp=s[i];
                s[i]=s[j];
                s[j]=tmp;
            }
        }
    }

    for (int i=0; i<lent; i++) {
        for (int j=i+1; j<lent; j++) {
            if (t[i]<t[j]) {
                tmp=t[i];
                t[i]=t[j];
                t[j]=tmp;
            }
        }
    }

    if (strcmp(s, t)<0) {
        printf("Yes");
    } else {
        printf("No");
    }

    return 0;
}

Score : 200 points

Problem Statement

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

Notes

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

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

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

For example, xy < xya and atcoder < atlas.

Constraints

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

s and t consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s
t

Output

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

Sample Input 1

yx
axy

Sample Output 1

Yes

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

Sample Input 2

ratcode
atlas

Sample Output 2

Yes

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

Sample Input 3

cd
abc

Sample Output 3

No

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

Sample Input 4

w
ww

Sample Output 4

Yes

Sample Input 5

zzz
zzz

Sample Output 5

No


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

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

bool dp1[5010][9010], dp2[5010][9010];

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

  int k = 0, l = 0;
  int n = strlen(s);
  while (k < n) {
    int j = k;
    while (s[j] == 'F') j++;
    a[l++] = j - k;
    k = j + 1;
  }
  for (int i = 0; i < l; i++) printf("%d ", a[i]); endl;

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

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

  printf("No\n");

  return 0;
}

Score : 500 points

Problem Statement

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

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

F : Move in the current direction by distance 1.

T : Turn 90 degrees, either clockwise or counterclockwise.

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

Constraints

s consists of F and T.

1 \leq |s| \leq 8 000

x and y are integers.

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

Input

Input is given from Standard Input in the following format:

s
x y

Output

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

Sample Input 1

FTFFTFFF
4 2

Sample Output 1

Yes

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

Sample Input 2

FTFFTFFF
-2 -2

Sample Output 2

Yes

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

Sample Input 3

FF
1 0

Sample Output 3

No

Sample Input 4

TF
1 0

Sample Output 4

No

Sample Input 5

FFTTFF
0 0

Sample Output 5

Yes

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

Sample Input 6

TTTT
1 0

Sample Output 6

No


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

Score : 200 points

Problem Statement

There are N positive integers written on a blackboard: A_1, ..., A_N.

Snuke can perform the following operation when all integers on the blackboard are even:

Replace each integer X on the blackboard by X divided by 2.

Find the maximum possible number of operations that Snuke can perform.

Constraints

1 \leq N \leq 200

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum possible number of operations that Snuke can perform.

Sample Input 1

3
8 12 40

Sample Output 1

2

Initially, [8, 12, 40] are written on the blackboard.
Since all those integers are even, Snuke can perform the operation.

After the operation is performed once, [4, 6, 20] are written on the blackboard.
Since all those integers are again even, he can perform the operation.

After the operation is performed twice, [2, 3, 10] are written on the blackboard.
Now, there is an odd number 3 on the blackboard, so he cannot perform the operation any more.

Thus, Snuke can perform the operation at most twice.

Sample Input 2

4
5 6 8 10

Sample Output 2

0

Since there is an odd number 5 on the blackboard already in the beginning, Snuke cannot perform the operation at all.

Sample Input 3

6
382253568 723152896 37802240 379425024 404894720 471526144

Sample Output 3

8


#include <stdio.h>
#include <stdlib.h>
#define maxN 200000

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

int
main(int argc, char *argv[])
{
  int n, k, a, c[maxN] = {}, max = 0;
  scanf("%d %d", &n, &k);
  for(int i = 0; i < n; i++){
    scanf("%d", &a);
    max = (max > a ? max : a);
    c[a - 1]++;
  }

  qsort(c, max, sizeof(int), asc);

  int i = 0; int l = 0; int z = 0;
  while(max > k){
    if(c[i] == 0) max--;
    else{
      l += c[i];
      max--;
    }
    i++;
  }

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

  return 0;
}

Score : 300 points

Problem Statement

Takahashi has N balls. Initially, an integer A_i is written on the i-th ball.

He would like to rewrite the integer on some balls so that there are at most K different integers written on the N balls.

Find the minimum number of balls that Takahashi needs to rewrite the integers on them.

Constraints

1 \leq K \leq N \leq 200000

1 \leq A_i \leq N

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1 A_2 ... A_N

Output

Print the minimum number of balls that Takahashi needs to rewrite the integers on them.

Sample Input 1

5 2
1 1 2 2 5

Sample Output 1

1

For example, if we rewrite the integer on the fifth ball to 2, there are two different integers written on the balls: 1 and 2.
On the other hand, it is not possible to rewrite the integers on zero balls so that there are at most two different integers written on the balls, so we should print 1.

Sample Input 2

4 4
1 1 2 2

Sample Output 2

0

Already in the beginning, there are two different integers written on the balls, so we do not need to rewrite anything.

Sample Input 3

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

Sample Output 3

3


#include <stdio.h>
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,n)  FOR(i,0,n)

int main() {
	int n, k, a;
	int num[200000], max, arg, sum = 0;
	scanf("%d%d",&n,&k);
	REP(i,n) num[i] = 0;
	REP(i,n) {
		scanf("%d",&a);
		num[a-1]++;
	}
	int tmp[n];
	REP(i,n) tmp[i] = 0;
	REP(i,n) tmp[num[i]]++;
	arg = 0;
	REP(i,n) REP(j,tmp[n-1-i]) num[arg++] = n-1-i;
	REP(i,k) sum += num[i];
	printf("%d",n - sum);
	return 0;
}


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

    int i,a=0,b=0;
    char s[10];

    scanf("%s",s);

    for(i=0;i<strlen(s);i++){
        a=a+(s[i]-'0')*(int)pow(10,strlen(s)-i);
        b=b+s[i]-'0';
    }

    if(a%b==0) printf("Yes\n");
    else printf("No\n");

    return 0;
}

Score : 200 points

Problem Statement

An integer X is called a Harshad number if X is divisible by f(X), where f(X) is the sum of the digits in X when written in base 10.

Given an integer N, determine whether it is a Harshad number.

Constraints

1?N?10^8

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print Yes if N is a Harshad number; print No otherwise.

Sample Input 1

12

Sample Output 1

Yes

f(12)=1+2=3. Since 12 is divisible by 3, 12 is a Harshad number.

Sample Input 2

57

Sample Output 2

No

f(57)=5+7=12. Since 57 is not divisible by 12, 12 is not a Harshad number.

Sample Input 3

148

Sample Output 3

No


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


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int n, c[10], f[100][10], p[100][11];
long long int max=-1000000000;
void check(int o){
  if(o==0) return;
  int i;
  long long int sum=0;
  for(i=0;i<n;i++) sum+=p[i][c[i]];
  if(max<sum) max=sum;
}

void func(int j, int o){
  if(j==10){check(o); return;}
  int i;
  for(i=0;i<n;i++) if(f[i][j]==1) c[i]++;
  func(j+1, o+1);
  for(i=0;i<n;i++) if(f[i][j]==1) c[i]--;
  func(j+1, o);
}

int main(void){
  int i, j;
  scanf("%d",&n);
  for(i=0;i<n;i++) for(j=0;j<10;j++) scanf("%d",&f[i][j]);
  for(i=0;i<n;i++) for(j=0;j<11;j++) scanf("%d",&p[i][j]);
  for(i=0;i<10;i++) c[i]=0;
  func(0, 0);
  printf("%lld\n",max);
  return 0;
}

Score : 300 points

Problem Statement

Joisino is planning to open a shop in a shopping street.

Each of the five weekdays is divided into two periods, the morning and the evening. For each of those ten periods, a shop must be either open during the whole period, or closed during the whole period. Naturally, a shop must be open during at least one of those periods.

There are already N stores in the street, numbered 1 through N.

You are given information of the business hours of those shops, F_{i,j,k}. If F_{i,j,k}=1, Shop i is open during Period k on Day j (this notation is explained below); if F_{i,j,k}=0, Shop i is closed during that period. Here, the days of the week are denoted as follows. Monday: Day 1, Tuesday: Day 2, Wednesday: Day 3, Thursday: Day 4, Friday: Day 5. Also, the morning is denoted as Period 1, and the afternoon is denoted as Period 2.

Let c_i be the number of periods during which both Shop i and Joisino's shop are open. Then, the profit of Joisino's shop will be P_{1,c_1}+P_{2,c_2}+...+P_{N,c_N}.

Find the maximum possible profit of Joisino's shop when she decides whether her shop is open during each period, making sure that it is open during at least one period.

Constraints

1≤N≤100

0≤F_{i,j,k}≤1

For every integer i such that 1≤i≤N, there exists at least one pair (j,k) such that F_{i,j,k}=1.

-10^7≤P_{i,j}≤10^7

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
F_{1,1,1} F_{1,1,2} ... F_{1,5,1} F_{1,5,2}
:
F_{N,1,1} F_{N,1,2} ... F_{N,5,1} F_{N,5,2}
P_{1,0} ... P_{1,10}
:
P_{N,0} ... P_{N,10}

Output

Print the maximum possible profit of Joisino's shop.

Sample Input 1

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

Sample Output 1

8

If her shop is open only during the periods when Shop 1 is opened, the profit will be 8, which is the maximum possible profit.

Sample Input 2

2
1 1 1 1 1 0 0 0 0 0
0 0 0 0 0 1 1 1 1 1
0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1
0 -2 -2 -2 -2 -2 -1 -1 -1 -1 -1

Sample Output 2

-2

Note that a shop must be open during at least one period, and the profit may be negative.

Sample Input 3

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

Sample Output 3

23


#include<stdio.h>
int main()
{
    unsigned long long int k, a, b;
    scanf("%lld %lld %lld", &k, &a, &b);
    if (a <= b)
    {
        if (k <= a)
            printf("1\n");
        else
            printf("-1\n");
        return 0;
    }
    k -= b;
    a -= b;
    printf("%lld\n", 2 * ((k + a - 1) / a) - 1);
    return 0;
}

Score : 100 points

Problem Statement

ButCoder Inc. runs a programming competition site called ButCoder. In this site, a user is given an integer value called rating that represents his/her skill, which changes each time he/she participates in a contest. The initial value of a new user's rating is 0, and a user whose rating reaches K or higher is called Kaiden ("total transmission"). Note that a user's rating may become negative.

Hikuhashi is a new user in ButCoder. It is estimated that, his rating increases by A in each of his odd-numbered contests (first, third, fifth, ...), and decreases by B in each of his even-numbered contests (second, fourth, sixth, ...).

According to this estimate, after how many contests will he become Kaiden for the first time, or will he never become Kaiden?

Constraints

1 ≤ K, A, B ≤ 10^{18}

All input values are integers.

Input

Input is given from Standard Input in the following format:

K A B

Output

If it is estimated that Hikuhashi will never become Kaiden, print -1. Otherwise, print the estimated number of contests before he become Kaiden for the first time.

Sample Input 1

4000 2000 500

Sample Output 1

5

Each time Hikuhashi participates in a contest, his rating is estimated to change as 0 → 2000 → 1500 → 3500 → 3000 → 5000 → … After his fifth contest, his rating will reach 4000 or higher for the first time.

Sample Input 2

4000 500 2000

Sample Output 2

-1

Each time Hikuhashi participates in a contest, his rating is estimated to change as 0 → 500 → -1500 → -1000 → -3000 → -2500 → … He will never become Kaiden.

Sample Input 3

1000000000000000000 2 1

Sample Output 3

1999999999999999997

The input and output values may not fit into 32-bit integers.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void){
char s[100];
int count,flg=0,counthoji;
char tmp;
char ans[]="KIHBR";
scanf("%s",s);
for(;;){
flg=0;
for(count=0;s[count]!='\0';count++){
if(s[count]=='A'){s[count]='0';flg=1;counthoji=count;break;}
}
if(flg==0){break;}
for(count=counthoji;;count++){
s[count]=s[count+1];
if(s[count]=='\0'){break;}
}

}
if(strcmp(s,ans) == 0){printf("YES");}
else{printf("NO");}

return 0;
}

Score : 300 points

Problem Statement

You are given a string S.

Takahashi can insert the character A at any position in this string any number of times.

Can he change S into AKIHABARA?

Constraints

1 \leq |S| \leq 50

S consists of uppercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If it is possible to change S into AKIHABARA, print YES; otherwise, print NO.

Sample Input 1

KIHBR

Sample Output 1

YES

Insert one A at each of the four positions: the beginning, immediately after H, immediately after B and the end.

Sample Input 2

AKIBAHARA

Sample Output 2

NO

The correct spell is AKIHABARA.

Sample Input 3

AAKIAHBAARA

Sample Output 3

NO


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

int main(void)
{
	int i,len,same=0,cor=0,j;
	char s[51],kihbr[]={'K','I','H','B','R'};
	char correct[]={'K','I','H','.','B','.','R'};
	scanf("%s",s);len=strlen(s);
	if(len>10)
	{
		printf("NO");
		return 0;
	}
	for(i=0;i<len;i++)
	{
	    //printf("%c %c (cor=%d same=%d)\n",s[i],correct[cor],cor,same);
		if(s[i]==correct[cor])
		{
		    for(j=same;j<5;j++) if(s[i]==kihbr[j]) same++;
		    cor++;
		}
		else if(correct[cor]=='.')
		{
			cor++;
			for(j=same;j<5;j++)
				if(s[i]==kihbr[j])
				{
					i--;
					break;
				}
		}
		else
		{
		    if(same!=0) i--;
		    same=cor=0;
		}

		if(same==5) break;
	}
	if(same==5) printf("YES");
	else printf("NO");
	return 0;
}


#include <stdio.h>

int main()
{
    char N[5];
    int i,yn=1;

    scanf("%s",N);

    for(i=0;i<2;i++){
        if(N[i]!=N[i+1]){
            yn = 0;
            break;
        }
        if(i==1){
            yn = 1;
            printf("Yes\n");
            return 0;
        }
    }

    for(i=3;i>1;i--){
        if(N[i]!=N[i-1]){
            yn = 0;
            break;
        }
        if(i==2){
            yn = 1;
            printf("Yes\n");
            return 0;
        }
    }

    printf("No\n");

    return 0;
}

Score : 100 points

Problem Statement

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

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

Constraints

1000 ≤ N ≤ 9999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

1118

Sample Output 1

Yes

N is good, since it contains three consecutive 1.

Sample Input 2

7777

Sample Output 2

Yes

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

Sample Input 3

1234

Sample Output 3

No


#include <stdio.h>

int main(void) {
	long long int luc[86];
	int N;
	int i;
	luc[0] = 2;
	luc[1] = 1;

	scanf("%d", &N);
	for(i = 2; i < 86; i++) {
		luc[i] = luc[i-1] + luc[i-2];
	}
	printf("%d", luc[N-1]);

	return 0;
}

Score : 200 points

Problem Statement

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

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

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

L_0=2

L_1=1

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

Constraints

1≤N≤86

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

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the N-th Lucas number.

Sample Input 1

5

Sample Output 1

11

L_0=2

L_1=1

L_2=L_0+L_1=3

L_3=L_1+L_2=4

L_4=L_2+L_3=7

L_5=L_3+L_4=11

Thus, the 5-th Lucas number is 11.

Sample Input 2

86

Sample Output 2

939587134549734843


Score : 300 points

Problem Statement

Sitting in a station waiting room, Joisino is gazing at her train ticket.

The ticket is numbered with four digits A, B, C and D in this order, each between 0 and 9 (inclusive).

In the formula A op1 B op2 C op3 D = 7, replace each of the symbols op1, op2 and op3 with + or - so that the formula holds.

The given input guarantees that there is a solution. If there are multiple solutions, any of them will be accepted.

Constraints

0≤A,B,C,D≤9

All input values are integers.

It is guaranteed that there is a solution.

Input

Input is given from Standard Input in the following format:

ABCD

Output

Print the formula you made, including the part =7.

Use the signs + and -.

Do not print a space between a digit and a sign.

Sample Input 1

1222

Sample Output 1

1+2+2+2=7

This is the only valid solution.

Sample Input 2

0290

Sample Output 2

0-2+9+0=7

0 - 2 + 9 - 0 = 7 is also a valid solution.

Sample Input 3

3242

Sample Output 3

3+2+4-2=7


#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdio.h>
int main()
{
    int a,b,c,d,n;
    while(scanf("%d",&n)!=EOF)
    {   a=n/1000;
        b=(n-1000*a)/100;
        c=n%100/10;
        d=n%10;
        if(a+b+c+d==7)printf("%d+%d+%d+%d=7\n",a,b,c,d);
        else if(a+b+c-d==7)printf("%d+%d+%d-%d=7\n",a,b,c,d);
        else if(a+b-c+d==7)printf("%d+%d-%d+%d=7\n",a,b,c,d);
        else if(a-b+c+d==7)printf("%d-%d+%d+%d=7\n",a,b,c,d);
        else if(a+b-c-d==7)printf("%d+%d-%d-%d=7\n",a,b,c,d);
        else if(a-b-c+d==7)printf("%d-%d-%d+%d=7\n",a,b,c,d);
        else if(a-b+c-d==7)printf("%d-%d+%d-%d=7\n",a,b,c,d);
    }
    return 0;
}


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

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

int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  int c[10][10], a[h][w];
  for (int i=0; i<10; i++)
    for (int j=0; j<10; j++)
      scanf("%d", c[i]+j);
  for (int i=0; i<h; i++)
    for (int j=0; j<w; j++)
      scanf("%d", a[i]+j);
  int cost[10];
  for (int i=0; i<10; i++)
    cost[i] = c[i][1];
  for (int i=0; i<10; i++) {
    for (int j=0; j<10; j++) {
      for (int k=0; k<10; k++) {
        cost[j] = Min(cost[j], cost[k]+c[j][k]);
      }
    }
  }
  int ans = 0;
  for (int i=0; i<h; i++) {
    for (int j=0; j<w; j++) {
      if (a[i][j] == -1)
        continue;
      else
        ans += cost[a[i][j]];
    }
  }
  printf("%d\n", ans);
  return 0;
}

Score : 400 points

Problem Statement

Joisino the magical girl has decided to turn every single digit that exists on this world into 1.

Rewriting a digit i with j (0≤i,j≤9) costs c_{i,j} MP (Magic Points).

She is now standing before a wall. The wall is divided into HW squares in H rows and W columns, and at least one square contains a digit between 0 and 9 (inclusive).

You are given A_{i,j} that describes the square at the i-th row from the top and j-th column from the left, as follows:

If A_{i,j}≠-1, the square contains a digit A_{i,j}.

If A_{i,j}=-1, the square does not contain a digit.

Find the minimum total amount of MP required to turn every digit on this wall into 1 in the end.

Constraints

1≤H,W≤200

1≤c_{i,j}≤10^3 (i≠j)

c_{i,j}=0 (i=j)

-1≤A_{i,j}≤9

All input values are integers.

There is at least one digit on the wall.

Input

Input is given from Standard Input in the following format:

H W
c_{0,0} ... c_{0,9}
:
c_{9,0} ... c_{9,9}
A_{1,1} ... A_{1,W}
:
A_{H,1} ... A_{H,W}

Output

Print the minimum total amount of MP required to turn every digit on the wall into 1 in the end.

Sample Input 1

2 4
0 9 9 9 9 9 9 9 9 9
9 0 9 9 9 9 9 9 9 9
9 9 0 9 9 9 9 9 9 9
9 9 9 0 9 9 9 9 9 9
9 9 9 9 0 9 9 9 9 2
9 9 9 9 9 0 9 9 9 9
9 9 9 9 9 9 0 9 9 9
9 9 9 9 9 9 9 0 9 9
9 9 9 9 2 9 9 9 0 9
9 2 9 9 9 9 9 9 9 0
-1 -1 -1 -1
8 1 1 8

Sample Output 1

12

To turn a single 8 into 1, it is optimal to first turn 8 into 4, then turn 4 into 9, and finally turn 9 into 1, costing 6 MP.

The wall contains two 8s, so the minimum total MP required is 6×2=12.

Sample Input 2

5 5
0 999 999 999 999 999 999 999 999 999
999 0 999 999 999 999 999 999 999 999
999 999 0 999 999 999 999 999 999 999
999 999 999 0 999 999 999 999 999 999
999 999 999 999 0 999 999 999 999 999
999 999 999 999 999 0 999 999 999 999
999 999 999 999 999 999 0 999 999 999
999 999 999 999 999 999 999 0 999 999
999 999 999 999 999 999 999 999 0 999
999 999 999 999 999 999 999 999 999 0
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1

Sample Output 2

0

Note that she may not need to change any digit.

Sample Input 3

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

Sample Output 3

47


#include <stdio.h>

int main(void){

  char x,y;
	scanf("%c %c", &x, &y);
	if (x < y) {
		printf("<\n");
	} else if (y < x) {
		printf(">\n");
	} else {
		printf("=\n");
	}

  return 0;
}

Score : 100 points

Problem Statement

In programming, hexadecimal notation is often used.

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

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

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

Constraints

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

Input

Input is given from Standard Input in the following format:

X Y

Output

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

Sample Input 1

A B

Sample Output 1

<

10 < 11.

Sample Input 2

E C

Sample Output 2

>

14 > 12.

Sample Input 3

F F

Sample Output 3

=

15 = 15.


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

int main(){
  int x,y;
  scanf("%d %d",&x,&y);
  //printf("%d\n", (int)pow(2,5));
  printf("%d\n",(int)(((x-y)*100+y*1900)*pow(2,y)));
}

Score : 300 points

Problem Statement

Takahashi is now competing in a programming contest, but he received TLE in a problem where the answer is YES or NO.

When he checked the detailed status of the submission, there were N test cases in the problem, and the code received TLE in M of those cases.

Then, he rewrote the code to correctly solve each of those M cases with 1/2 probability in 1900 milliseconds, and correctly solve each of the other N-M cases without fail in 100 milliseconds.

Now, he goes through the following process:

Submit the code.

Wait until the code finishes execution on all the cases.

If the code fails to correctly solve some of the M cases, submit it again.

Repeat until the code correctly solve all the cases in one submission.

Let the expected value of the total execution time of the code be X milliseconds. Print X (as an integer).

Constraints

All input values are integers.

1 \leq N \leq 100

1 \leq M \leq {\rm min}(N, 5)

Input

Input is given from Standard Input in the following format:

N M

Output

Print X, the expected value of the total execution time of the code, as an integer. It can be proved that, under the constraints in this problem, X is an integer not exceeding 10^9.

Sample Input 1

1 1

Sample Output 1

3800

In this input, there is only one case. Takahashi will repeatedly submit the code that correctly solves this case with 1/2 probability in 1900 milliseconds.

The code will succeed in one attempt with 1/2 probability, in two attempts with 1/4 probability, and in three attempts with 1/8 probability, and so on.

Thus, the answer is 1900 \times 1/2 + (2 \times 1900) \times 1/4 + (3 \times 1900) \times 1/8 + ... = 3800.

Sample Input 2

10 2

Sample Output 2

18400

The code will take 1900 milliseconds in each of the 2 cases, and 100 milliseconds in each of the 10-2=8 cases. The probability of the code correctly solving all the cases is 1/2 \times 1/2 = 1/4.

Sample Input 3

100 5

Sample Output 3

608000


#include<stdio.h>

int main(){

   int n,i;

   scanf("%d",&n);

   for(i = 1; i * i <= n; i++);

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

   return 0;
}

Score : 200 points

Problem Statement

Find the largest square number not exceeding N. Here, a square number is an integer that can be represented as the square of an integer.

Constraints

1 \leq N \leq 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the largest square number not exceeding N.

Sample Input 1

10

Sample Output 1

9

10 is not square, but 9 = 3 × 3 is. Thus, we print 9.

Sample Input 2

81

Sample Output 2

81

Sample Input 3

271828182

Sample Output 3

271821169


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

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

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

#define MOD 1000000007

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

//(初めて以上になる添字を0始まりで出力)
//(もしそういうのがないならNを出力)
int binary_search_right(int* array, int N, int key){
    int i=0;
    int low=0;
    int high=N-1;
    int mid;
    while(low<=high){
        mid=(low+high)/2;
        if(array[mid]<key){
            low=mid+1;
            i=mid+1;
        }
        else{
            high=mid-1;
            i=mid;
        }
    }
    return i;
}

//(以下になる添字の中で一番でかいものを0始まりで出力)
//(もしそういうのがないなら-1を出力)
int binary_search_left(int* array, int N, int key){
    int i=0;
    int low=0;
    int high=N-1;
    int mid;
    while(low<=high){
        mid=(low+high)/2;
        if(array[mid]<=key){
            low=mid+1;
            i=mid+1;
        }
        else{
            high=mid-1;
            i=mid;
        }
    }
    return i-1;
}

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

    int* A;
    NEW(A,N);
    for(int i=0;i<N;i++) scanf("%d",&A[i]);
    qsort(A,N,sizeof(int),asc);

    int* B;
    NEW(B,N);
    for(int i=0;i<N;i++) scanf("%d",&B[i]);
    qsort(B,N,sizeof(int),asc);

    int* C;
    NEW(C,N);
    for(int i=0;i<N;i++) scanf("%d",&C[i]);
    qsort(C,N,sizeof(int),asc);

    long ans=0;
    for(int i=0;i<N;i++){
        int a=binary_search_left(A,N,B[i]-1);
        int c=binary_search_right(C,N,B[i]+1);
        if(a>=0 && c<N) ans+=(a+1)*(N-c);
    }
    printf("%ld\n",ans);

    return 0;
}

Score : 300 points

Problem Statement

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

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

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

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

Constraints

1 \leq N \leq 10^5

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

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

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

All input values are integers.

Input

Input is given from Standard Input in the following format:

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

Output

Print the number of different altars that Ringo can build.

Sample Input 1

2
1 5
2 4
3 6

Sample Output 1

3

The following three altars can be built:

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

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

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

Sample Input 2

3
1 1 1
2 2 2
3 3 3

Sample Output 2

27

Sample Input 3

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

Sample Output 3

87


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

    int main ()
    {
            printf ("Rating System\n");

            char d [10];
            printf ("Please insert name\n",d);
            gets (d);

            int x,y ;
            printf ("Please insert current rating and performance \n",x,y);
            scanf ("%d %d",&x,&y);

            fflush (stdin);

            if (x <=4500 && x >= 0 && y <=4500 && y >= 0)
           {

            printf ("%d \n",x+y);
            puts (d);
            printf (" current rating is %d\n ",x);
            printf ("If his performance in the contest is %d ",y);
            printf (", his rating will be the average of %d and %d ",x,y);
            printf (", which is equal to the desired rating, %d",(x+y)/2);
                }
                else
                {
                    printf ("Invalid");
                }
            }

Score : 100 points

Problem Statement

Takahashi is a user of a site that hosts programming contests.

When a user competes in a contest, the rating of the user (not necessarily an integer) changes according to the performance of the user, as follows:

Let the current rating of the user be a.

Suppose that the performance of the user in the contest is b.

Then, the new rating of the user will be the avarage of a and b.

For example, if a user with rating 1 competes in a contest and gives performance 1000, his/her new rating will be 500.5, the average of 1 and 1000.

Takahashi's current rating is R, and he wants his rating to be exactly G after the next contest.

Find the performance required to achieve it.

Constraints

0 \leq R, G \leq 4500

All input values are integers.

Input

Input is given from Standard Input in the following format:

R
G

Output

Print the performance required to achieve the objective.

Sample Input 1

2002
2017

Sample Output 1

2032

Takahashi's current rating is 2002.

If his performance in the contest is 2032, his rating will be the average of 2002 and 2032, which is equal to the desired rating, 2017.

Sample Input 2

4500
0

Sample Output 2

-4500

Although the current and desired ratings are between 0 and 4500, the performance of a user can be below 0.


#include <stdio.h>

int main(){

	int R,G;

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

	printf("%d\n",(2*G)-R);

	return 0;
}


Score : 700 points

Problem Statement

We have a string s consisting of lowercase English letters.
Snuke is partitioning s into some number of non-empty substrings.
Let the subtrings obtained be s_1, s_2, ..., s_N from left to right. (Here, s = s_1 + s_2 + ... + s_N holds.)
Snuke wants to satisfy the following condition:

For each i (1 \leq i \leq N), it is possible to permute the characters in s_i and obtain a palindrome.

Find the minimum possible value of N when the partition satisfies the condition.

Constraints

1 \leq |s| \leq 2 \times 10^5

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum possible value of N when the partition satisfies the condition.

Sample Input 1

aabxyyzz

Sample Output 1

2

The solution is to partition s as aabxyyzz = aab + xyyzz.
Here, aab can be permuted to form a palindrome aba, and xyyzz can be permuted to form a palindrome zyxyz.

Sample Input 2

byebye

Sample Output 2

1

byebye can be permuted to form a palindrome byeeyb.

Sample Input 3

abcdefghijklmnopqrstuvwxyz

Sample Output 3

26

Sample Input 4

abcabcxabcx

Sample Output 4

3

The solution is to partition s as abcabcxabcx = a + b + cabcxabcx.


#include <stdio.h>

int a,b,c;

int main() {
  scanf("%d %d %d",&a,&b,&c);
 if(a!=b&a!=c)
   {printf("%d",a);}

   if(b!=a&b!=c)
     {printf("%d",b);}

     if(c!=b&c!=a)
       {printf("%d",c);}

 return 0;
}

Score : 100 points

Problem Statement

You are given three integers, A, B and C.

Among them, two are the same, but the remaining one is different from the rest.

For example, when A=5,B=7,C=5, A and C are the same, but B is different.

Find the one that is different from the rest among the given three integers.

Constraints

-100 \leq A,B,C \leq 100

A, B and C are integers.

The input satisfies the condition in the statement.

Input

Input is given from Standard Input in the following format:

A B C

Output

Among A, B and C, print the integer that is different from the rest.

Sample Input 1

5 7 5

Sample Output 1

7

This is the same case as the one in the statement.

Sample Input 2

1 1 7

Sample Output 2

7

In this case, C is the one we seek.

Sample Input 3

-100 100 100

Sample Output 3

-100