#include <stdio.h>
int oddEven(int digit, char *s){
    int ansx = 0;
    int ansy = 0;
    char ansA[111];
    char ansB[111];

    if(digit == 1)
        return 0;
    for(int i = 0; i < digit; i++){
        if(i == 0 || (i % 2) == 0){
            ansA[i] = '1';
            ansB[i] = '0';
        }else{
            ansA[i] = '0';
            ansB[i] = '1';
        }
    }
    for(int i = 0; i < digit; i++){
        if(ansA[i] == *(s+i)){
            ansy += 1;
        }else{
            ansx += 1;
        }
    }
    if(ansx < ansy){
        return ansx;
    }else{
        return ansy;
    }
}
int main(void){
    int n;
    int digit = 1;
    char order[1001];

    fgets(order, 1000, stdin);
    while(1){
        if(order[digit] == '\n'){
            break;
        }
        digit += 1;
    }
    n = oddEven(digit, order);
    printf("%d\n", n);

    return 0;
}


#include <stdio.h>

int groups[100002];

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

	int gind = groups[0] = 0;
	for (int i = 0; i < n; i++) {
		int c = getchar() == '1';
		if (c == gind%2) groups[gind]++;
		else groups[++gind] = 1;
	}
	if (gind%2) groups[++gind] = 0;

	int max = 0;
	for (int i = 1; i <= gind; i += 2) {
		if (max < groups[i]) max = groups[i];
	}

	int sum = 0;
	for (int i = 0; i <= gind; i++) {
		if (i+1 > 2*k+1) sum -= groups[i-2*k-1];
		sum += groups[i];
		if (i%2 != gind%2 && max < sum) max = sum;
	}

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

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

Score : 400 points

Problem Statement

N people are arranged in a row from left to right.

You are given a string S of length N consisting of 0 and 1, and a positive integer K.

The i-th person from the left is standing on feet if the i-th character of S is 0, and standing on hands if that character is 1.

You will give the following direction at most K times (possibly zero):

Direction: Choose integers l and r satisfying 1 \leq l \leq r \leq N, and flip the l-th, (l+1)-th, ..., and r-th persons. That is, for each i = l, l+1, ..., r, the i-th person from the left now stands on hands if he/she was standing on feet, and stands on feet if he/she was standing on hands.

Find the maximum possible number of consecutive people standing on hands after at most K directions.

Constraints

N is an integer satisfying 1 \leq N \leq 10^5.

K is an integer satisfying 1 \leq K \leq 10^5.

The length of the string S is N.

Each character of the string S is 0 or 1.

Input

Input is given from Standard Input in the following format:

N K
S

Output

Print the maximum possible number of consecutive people standing on hands after at most K directions.

Sample Input 1

5 1
00010

Sample Output 1

4

We can have four consecutive people standing on hands, which is the maximum result, by giving the following direction:

Give the direction with l = 1, r = 3, which flips the first, second and third persons from the left.

Sample Input 2

14 2
11101010110011

Sample Output 2

8

Sample Input 3

1 1
1

Sample Output 3

1

No directions are necessary.

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: 200 points

Problem Statement

The restaurant AtCoder serves the following five dishes:

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

ARC Curry: takes B minutes to serve.

AGC Pasta: takes C minutes to serve.

APC Ramen: takes D minutes to serve.

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

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

This restaurant has the following rules on orders:

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

Only one dish can be ordered at a time.

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

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

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

Constraints

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

Input

Input is given from Standard Input in the following format:

A
B
C
D
E

Output

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

Sample Input 1

29
20
7
35
120

Sample Output 1

215

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

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

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

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

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

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

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

Sample Input 2

101
86
119
108
57

Sample Output 2

481

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

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

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

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

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

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

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

Sample Input 3

123
123
123
123
123

Sample Output 3

643

This is the largest valid case.


#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>
int acs(const void *a, const void *b) { return *(int *)a - *(int *)b; }                            /* 1,2,3,4.. */
int cmp_char(const void *a, const void *b) { return *(char *)a - *(char *)b; }                     /* a,b,c,d.. */
int cmp_str(const void *a, const void *b) { return strcmp(*(const char **)a, *(const char **)b); } /* aaa,aab.. */
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
#define rep(i, l, r) for (int i = l; i < r; i++)
#define MAX 10000000
#define MOD 1000000007

typedef long long int lli;

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

// 優先度付きキュー Priority Queue

typedef struct
{
  long long int pri;
  long long int val[3];
} que;

que pq[MAX];
int pq_size = 0; // 最大要素

void PQadd(long long int pri, long long int val1,
           long long int val2, long long int val3)
{
  int last = ++pq_size;
  que new;
  new.pri = pri;
  new.val[0] = val1;
  new.val[1] = val2;
  new.val[2] = val3;
  pq[pq_size] = new;

  while (last > 1 && pq[last].pri > pq[last / 2].pri)
  {
    que temp;
    temp = pq[last];
    pq[last] = pq[last / 2];
    pq[last / 2] = temp;
    last /= 2;
  }
  return;
}

que PQget(void)
{
  que root = pq[1];
  pq[1] = pq[pq_size--];

  int last = 1, bigger_child;
  while (last * 2 <= pq_size)
  {
    if (last * 2 == pq_size)
      bigger_child = last * 2;
    else
      bigger_child = pq[last * 2].pri > pq[last * 2 + 1].pri ? last * 2 : last * 2 + 1;

    if (pq[last].pri < pq[bigger_child].pri)
    {
      que temp;
      temp = pq[last];
      pq[last] = pq[bigger_child];
      pq[bigger_child] = temp;
      last = bigger_child;
    }
    else
      break;
  }
  return root;
}

int main(void)
{
  lli a[1001], b[1001], c[1001];
  int x, y, z, k;
  scanf("%d %d %d %d", &x, &y, &z, &k);
  rep(i, 0, x) scanf("%lld", &a[i]);
  rep(i, 0, y) scanf("%lld", &b[i]);
  rep(i, 0, z) scanf("%lld", &c[i]);

  qsort(a, x, sizeof(lli), des);
  qsort(b, y, sizeof(lli), des);
  qsort(c, z, sizeof(lli), des);

  PQadd(a[0] + b[0] + c[0], 0, 0, 0);
  rep(i, 0, k)
  {
    que max = PQget();

    int ai = max.val[0];
    int bi = max.val[1];
    int ci = max.val[2];

    PQadd(a[ai + 1] + b[bi] + c[ci], ai + 1, bi, ci);
    PQadd(a[ai] + b[bi + 1] + c[ci], ai, bi + 1, ci);
    PQadd(a[ai] + b[bi] + c[ci + 1], ai, bi, ci + 1);

    printf("%lld\n", max.pri);
  }

  return 0;
}

Score: 400 points

Problem Statement

The Patisserie AtCoder sells cakes with number-shaped candles.
There are X, Y and Z kinds of cakes with 1-shaped, 2-shaped and 3-shaped candles, respectively.
Each cake has an integer value called deliciousness, as follows:

The deliciousness of the cakes with 1-shaped candles are A_1, A_2, ..., A_X.

The deliciousness of the cakes with 2-shaped candles are B_1, B_2, ..., B_Y.

The deliciousness of the cakes with 3-shaped candles are C_1, C_2, ..., C_Z.

Takahashi decides to buy three cakes, one for each of the three shapes of the candles, to celebrate ABC 123.

There are X \times Y \times Z such ways to choose three cakes.

We will arrange these X \times Y \times Z ways in descending order of the sum of the deliciousness of the cakes.

Print the sums of the deliciousness of the cakes for the first, second, ..., K-th ways in this list.

Constraints

1 \leq X \leq 1 \ 000

1 \leq Y \leq 1 \ 000

1 \leq Z \leq 1 \ 000

1 \leq K \leq \min(3 \ 000, X \times Y \times Z)

1 \leq A_i \leq 10 \ 000 \ 000 \ 000

1 \leq B_i \leq 10 \ 000 \ 000 \ 000

1 \leq C_i \leq 10 \ 000 \ 000 \ 000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y Z K
A_1 \ A_2 \ A_3 \ ... \ A_X
B_1 \ B_2 \ B_3 \ ... \ B_Y
C_1 \ C_2 \ C_3 \ ... \ C_Z

Output

Print K lines. The i-th line should contain the i-th value stated in the problem statement.

Sample Input 1

2 2 2 8
4 6
1 5
3 8

Sample Output 1

19
17
15
14
13
12
10
8

There are 2 \times 2 \times 2 = 8 ways to choose three cakes, as shown below in descending order of the sum of the deliciousness of the cakes:

(A_2, B_2, C_2): 6 + 5 + 8 = 19

(A_1, B_2, C_2): 4 + 5 + 8 = 17

(A_2, B_1, C_2): 6 + 1 + 8 = 15

(A_2, B_2, C_1): 6 + 5 + 3 = 14

(A_1, B_1, C_2): 4 + 1 + 8 = 13

(A_1, B_2, C_1): 4 + 5 + 3 = 12

(A_2, B_1, C_1): 6 + 1 + 3 = 10

(A_1, B_1, C_1): 4 + 1 + 3 = 8

Sample Input 2

3 3 3 5
1 10 100
2 20 200
1 10 100

Sample Output 2

400
310
310
301
301

There may be multiple combinations of cakes with the same sum of the deliciousness. For example, in this test case, the sum of A_1, B_3, C_3 and the sum of A_3, B_3, C_1 are both 301.
However, they are different ways of choosing cakes, so 301 occurs twice in the output.

Sample Input 3

10 10 10 20
7467038376 5724769290 292794712 2843504496 3381970101 8402252870 249131806 6310293640 6690322794 6082257488
1873977926 2576529623 1144842195 1379118507 6003234687 4925540914 3902539811 3326692703 484657758 2877436338
4975681328 8974383988 2882263257 7690203955 514305523 6679823484 4263279310 585966808 3752282379 620585736

Sample Output 3

23379871545
22444657051
22302177772
22095691512
21667941469
21366963278
21287912315
21279176669
21160477018
21085311041
21059876163
21017997739
20703329561
20702387965
20590247696
20383761436
20343962175
20254073196
20210218542
20150096547

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


#include<stdio.h>
int main()
{
    char b;
    scanf("%c", &b);
    if(b=='A'){
        printf("T\n");
    }
    else if(b=='T'){
        printf("A\n");
    }
    else if(b=='C'){
        printf("G\n");
    }
    else if(b=='G'){
        printf("C\n");
    }
    return 0;
}

Score : 100 points

Problem Statement

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

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

Constraints

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

Input

Input is given from Standard Input in the following format:

b

Output

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

Sample Input 1

A

Sample Output 1

T

Sample Input 2

G

Sample Output 2

C


#include<stdio.h>

int main(void)
{
 char a[2];
 scanf("%s",a);
  if(a[0]=='A')
    printf("T");
   if(a[0]=='T')
    printf("A");
   if(a[0]=='C')
    printf("G");
   if(a[0]=='G')
    printf("C");
 return 0;
}


#include <stdio.h>

int main(void)
{
	char c;
	scanf("%c",&c);

	if(c == 'A'){
		printf("T\n");
	}else if(c == 'T'){
		printf("A\n");
	}else if(c == 'C'){
		printf("G\n");
	}else if(c == 'G'){
		printf("C\n");
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
int main (void){
    char str[11];
    scanf("%s",str);

  	int i;
    int length = 0;
    int length_max = 0;
    for(i=0;i<strlen(str);i++){
        if(str[i] != 'A' &&
            str[i] != 'C' &&
            str[i] != 'G' &&
            str[i] != 'T'){
              length = 0;
        }else{
            length++;
            if(length >length_max){
                length_max = length;
            }
        }
    }
    printf("%d",length_max);

}

Score : 200 points

Problem Statement

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

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

Notes

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

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

Constraints

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

Each character in S is an uppercase English letter.

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

ATCODER

Sample Output 1

3

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

Sample Input 2

HATAGAYA

Sample Output 2

5

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

Sample Input 3

SHINJUKU

Sample Output 3

0

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


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

  char S[11];
  int count=0;
  int max=0;

  scanf("%s",S);

  for(int i=0;i<10;i++){
  	  if(S[i]=='A' || S[i]=='C' || S[i]=='G' || S[i]=='T'){
        while(S[i]=='A' || S[i]=='C' || S[i]=='G' || S[i]=='T'){
        	count++;
        }
        if(count>max){
        	max=count;
        }else{
        	count=0;
        }
      }
  }

  printf("%d",max);

 return 0;
}


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

int main(){
    int i,max=0,slen;
    char s[20], str[10];
    int ans=0;

    scanf("%s", &s);
    slen = strlen(s);

    for(i=0; i<slen; i++){
        if(s[i] == 'A'){
            ans += 1;
        }else if( s[i] == 'C' ){
            ans += 1;
        }else if(s[i] == 'G'){
            ans += 1;
        }else if(s[i] == 'T'){
            ans += 1;
        }else{
            ans = 0;
        }
        if(max<ans){
            max = ans;
        }
    }

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

}


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

void check (char *s, int n, int *a) {
    int flag = 0;
    for (int i=0; i<n; i++) {
        if (s[i] == 'A') {
            flag = 1;
        }
        else if (s[i] == 'C') {
            if (flag) {
                a[i] = 1;
                flag = 0;
            }
        }
        else {
            flag = 0;
        }
    }
}

int main() {
    int n = 0,q = 0;
    scanf("%d %d\n", &n, &q);
    char s[n+2];
    fgets(s, n+2, stdin);
    int a[n];
    for (int i=0; i<n; i++) {
        a[i] = 0;
    }
    check(s, n, a);
    int *find = (int *)malloc(sizeof(int)*n*n);
    for (int i=0; i<n; i++) {
        for (int j=i; j<n; j++) {
            if (j == i) *(find+n*i+j) = 0;
            else *(find+n*i+j) = *(find+n*i+j-1) + a[j];
        }
    }
    int l = 0, r = 0;
    for (int i=0; i<q; i++) {
        scanf("%d %d\n", &l, &r);
        printf("%d\n", *(find+n*(l-1)+r-1));
    }
    free(find);
    return 0;
}

Score : 300 points

Problem Statement

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

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

Notes

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

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

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

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

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

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

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

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

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

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


#include<stdio.h>
int main(void)
{
	int N, Q,i,j;
	scanf("%d %d",&N,&Q);
	char s[N+1];
	int l[Q+1],r[Q+1];
	scanf("%s",s);
	for(i=0;i<Q;i++){
		scanf("%d %d",&l[i],&r[i]);
	}
	int count;
	for(i=0;i<Q;i++){
		count = 0;
		for(j=l[i]-1;j<r[i]-1;j++){
			if('A' == s[j] && 'C' == s[j+1])count++;
		}
		if(i == Q-1){printf("%d",count);
		}else{
		printf("%d\n",count);
		}
	}
	return 0;
}


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

#define REP(i,n) for(i=0;i<n;i++)
#define REVREP(i,n) for(i=n-1;i>=0;i--)

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

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

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

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

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

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

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

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

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

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

void check(char *S, int *C) {
    int i, j;
    REVREP(i,strlen(S)) {
        j = i + 1;
        if (j > strlen(S)) continue;
//        cout(S[i]);
        if (S[i] != 'A') {
            C[i] += C[j];
        } else {
            if (S[j] != 'C') {
                C[i] += C[j];
            } else {
                C[i] += (C[j] + 1);
            }
        }
    }
}

int main() {
    int N = iin(), Q = iin();
    char S[N];
    int C[N];
    int i;
    stin(S);
    memset(C, 0, sizeof C);
//    stout(S);
    check(S, C);
//    REP(i,N) { iout(C[i]); }
    REP(i,Q) {
        int l = iin(), r = iin();
        iout(C[l - 1] - C[r - 1]);
    }
    return 0;
}


Score : 200 points

Problem Statement

You are given a string S of length N.
Among its subsequences, count the ones such that all characters are different, modulo 10^9+7. Two subsequences are considered different if their characters come from different positions in the string, even if they are the same as strings.

Here, a subsequence of a string is a concatenation of one or more characters from the string without changing the order.

Constraints

1 \leq N \leq 100000

S consists of lowercase English letters.

|S|=N

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the number of the subsequences such that all characters are different, modulo 10^9+7.

Sample Input 1

4
abcd

Sample Output 1

15

Since all characters in S itself are different, all its subsequences satisfy the condition.

Sample Input 2

3
baa

Sample Output 2

5

The answer is five: b, two occurrences of a, two occurrences of ba. Note that we do not count baa, since it contains two as.

Sample Input 3

5
abcab

Sample Output 3

17


#include <stdio.h>

int main(void)
{
  int h_size, w_size;
  int h, w;
  int answer;
  scanf("%d%d", &h_size, &w_size);
  scanf("%d%d", &h, &w);

  answer
    = h_size * w_size
    - h * w_size
    - h_size * w
    + h * w;

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

Score : 100 points

Problem Statement

There are H rows and W columns of white square cells.

You will choose h of the rows and w of the columns, and paint all of the cells contained in those rows or columns.

How many white cells will remain?

It can be proved that this count does not depend on what rows and columns are chosen.

Constraints

All values in input are integers.

1 \leq H, W \leq 20

1 \leq h \leq H

1 \leq w \leq W

Input

Input is given from Standard Input in the following format:

H W
h w

Output

Print the number of white cells that will remain.

Sample Input 1

3 2
2 1

Sample Output 1

1

There are 3 rows and 2 columns of cells. When two rows and one column are chosen and painted in black, there is always one white cell that remains.

Sample Input 2

5 5
2 3

Sample Output 2

6

Sample Input 3

2 4
2 4

Sample Output 3

0


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

int main()
{

  int a, b, c, d, e, X, Y;
  //  double  c;
  int  A[100001]={};
  int  B[100001]={};
//  int  B[100003]={};
  int  L[1003]={};

  int  flag[103]={};

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

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

 printf("%d\n", (a*b)-(c*a)-(d*b)+(c*d));

  //    if(max < cnt - max)  printf("Yes\n");
//    else

return 0;
}


#include <stdio.h>

#define DEBUG 1
#define NINF -1
#define PINF 1E9+1

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

	ret = 0;

	for (i=0;i<c;i++) {
		ret += b;
	}
	for (i=0;i<d;i++) {
		ret += a-c;
	}

	printf("%d\n",a*b-ret);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
typedef struct{
  long long int value;
  int cost;
} box;
int cmp( const void *p, const void *q ) {
    return ((box*)p)->cost - ((box*)q)->cost;
}

int main(void) {
  int a,b;
  long long wallet=0;
  scanf("%d %d",&a,&b);
  box store[a];
  for(int i=0;i<a;i++){
    scanf("%d %d",&(store[i].cost),&(store[i].value));
}
  qsort(store,a,sizeof(box),cmp);

  int num=0,want=0;
  while(b-want>0){
      if(store[num].value>0){
	wallet+=store[num].cost;
	store[num].value--;
	want++;}
      else num++;

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

Score : 300 points

Problem Statement

Hearing that energy drinks increase rating in those sites, Takahashi decides to buy up M cans of energy drinks.

There are N stores that sell energy drinks. In the i-th store, he can buy at most B_i cans of energy drinks for A_i yen (the currency of Japan) each.

What is the minimum amount of money with which he can buy M cans of energy drinks?

It is guaranteed that, in the given inputs, a sufficient amount of money can always buy M cans of energy drinks.

Constraints

All values in input are integers.

1 \leq N, M \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^5

B_1 + ... + B_N \geq M

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the minimum amount of money with which Takahashi can buy M cans of energy drinks.

Sample Input 1

2 5
4 9
2 4

Sample Output 1

12

With 12 yen, we can buy one drink at the first store and four drinks at the second store, for the total of five drinks. However, we cannot buy 5 drinks with 11 yen or less.

Sample Input 2

4 30
6 18
2 5
3 10
7 9

Sample Output 2

130

Sample Input 3

1 100000
1000000000 100000

Sample Output 3

100000000000000

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


#include <stdio.h>
int func(int a,int b,int c){
  int min = a>b?b:a;
  int array[99];
  int count = 0;
  for(int i = 1 ; i < min ; i ++){
    if ((a % i == 0) && (b % i == 0)){
      array[count] = i;
      count ++;
    }
  }
  return array[count - c];

}
int main(){
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);
    printf("%d\n",func(a,b,c));
    return 0;
}

Score : 200 points

Problem Statement

You are given positive integers A and B.

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

The input guarantees that there exists such a number.

Constraints

All values in input are integers.

1 \leq A, B \leq 100

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

K \geq 1

Input

Input is given from Standard Input in the following format:

A B K

Output

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

Sample Input 1

8 12 2

Sample Output 1

2

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

Sample Input 2

100 50 4

Sample Output 2

5

Sample Input 3

1 1 1

Sample Output 3

1


#include <stdio.h>

int N(int A, int B, int K){
  int n, c;
  c = 0;
  for(n = 100;n > 0;n--){
    if(A % n + B % n == 0) c++;
    if(c == K) break;
  }
  return n;
}

int main(void){
  int A, B, K;
  scanf("%d %d %d", &A, &B, &K);
  printf("%d\n", N(A, B, K));
  return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#define LL long long
#define INF 1<<31-1
#define MIN(x,y) ((x)<(y)?(x):(y))
#define MAX(x,y) ((x)<(y)?(y):(x))
#define FOR(i,a,n) for(i=a;i<n;i++)
#define MOD 1000000007
#define NUM 100001

void swap(int* a,int* b){int tmp=*a;*a=*b;*b=tmp;}
int GCD(int a,int b){int c=1,tmp=MAX(a,b);b=MIN(a,b);a=tmp;while(c!=0){c=a%b;a=b;b=c;}return a;}
int LCM(int a,int b){return a*b/GCD(a,b);}
int POW(LL a,LL n){LL ans=1;while(n>0){if(n&1)ans=ans*a%MOD;a=a*a%MOD;n>>=1;}return (int)ans;}
int icmp(const void* a,const void* b){return *(int*)a-*(int*)b;}
int ccmp(const void* a,const void* b){return *(char*)a-*(char*)b;}
int scmp(const void* a,const void* b){return strcmp(*(char**)a,*(char**)b);}
LL fac[NUM],finv[NUM],inv[NUM];
void comi(){int i;fac[0]=fac[1]=1;finv[0]=finv[1]=1;inv[1]=1;FOR(i,2,NUM){fac[i]=fac[i-1]*i%MOD;inv[i]=MOD-inv[MOD%i]*(MOD/i)%MOD;finv[i]=finv[i-1]*inv[i]%MOD;}}
int com(int n,int k){if(n<k||n<0||k<0)return 0;return fac[n]*(finv[k]*finv[n-k]%MOD)%MOD;}

typedef struct{int s,e,c;}Edge;
int par[NUM];
int UF(){for(int i=0;i<NUM;i++)par[i]=-1;}
int Root(int x){if(par[x]<0)return x;else return par[x]=Root(par[x]);}
int Size(int x){return -par[Root(x)];}
bool Same(int x,int y){return Root(x)==Root(y);}
void Unite(int x,int y){x=Root(x);y=Root(y);if(x!=y){if(Size(x)<Size(y))swap(&x,&y);par[x]+=par[y];par[y]=x;}}
int cmpc(const void* a,const void* b){return ((Edge*)a)->c-((Edge*)b)->c;}
int Kruscal(Edge* edge,int n){int i,ans=0;Edge e;qsort(edge,n,sizeof(Edge),cmpc);UF();FOR(i,0,n){e=edge[i];if(!Same(e.s,e.e)){Unite(e.s,e.e);ans+=e.c;}}return ans;}

int main(){
  int i,a=0,b=0,s,ans=0;
	char S[NUM];
  scanf("%s",S);
	s=strlen(S);
	FOR(i,0,s){
		if(S[i]=='0')a++;
		else b++;
	}

  printf("%d\n",MIN(a,b)*2);
	return 0;
}

Score : 300 points

Problem Statement

There are N cubes stacked vertically on a desk.

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

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

At most how many cubes can be removed?

Constraints

1 \leq N \leq 10^5

|S| = N

Each character in S is 0 or 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum number of cubes that can be removed.

Sample Input 1

0011

Sample Output 1

4

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

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

Remove the first and second cubes from the bottom.

Sample Input 2

11011010001011

Sample Output 2

12

Sample Input 3

0

Sample Output 3

0


#include<stdio.h>

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

        scanf("%s", s);

        for(i=0; s[i]!='\0'; i++){
                if(s[i]=='0') a++;
                else if(s[i]=='1') b++;
        }
        if(a<b) printf("%d\n", a*2);
        else printf("%d\n", b*2);

        return 0;
}


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

int main(){

  int i, count0=0, count1;
  char S[100000];

  if(scanf("%s", S)==0) return -1;

  count1=strlen(S);

  for(i=0;S[i]!='\0';i++){
  	if(S[i]=='0') count0++;
  }

  count1-=count0;

  if(count1>=count0) printf("%d", count0*2);
  else printf("%d", count1*2);

  return 0;
}


#include <stdio.h>

int main(void)
{
  int y, m, d;
  char a,b;
  scanf("%d%c%d%c%d", &y, &a, &m, &b, &d);
  printf("%d%c%d%c%d",y,a,m,b,d);
  if (m <= 4){
    printf("Heisei\n");
  }
  else{
    printf("TBD\n");
  }
  return 0;
}

Score : 100 points

Problem Statement

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

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

Constraints

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

Input

Input is given from Standard Input in the following format:

S

Output

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

Sample Input 1

2019/04/30

Sample Output 1

Heisei

Sample Input 2

2019/11/01

Sample Output 2

TBD


#include<stdio.h>

int main(){
  int year,mon,day,lastd,scan;
  int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

  //入力取得
  scan = scanf("%d/%d/%d",&year,&mon,&day);

  lastd = days[mon-1];
  //うるう年
  if(mon == 2){
    if( year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ) lastd = 29;
  }

  if(0 < day && day < lastd){
    //月が１～4か
    if(mon<5){
      //2019年までであれば'Heisei'
      if(year < 2020){
        printf("Heisei\n");
      }
      //月が5以上12以下で2018年までであれば'Heisei'
    }else if(mon < 13 && year < 2019){
      printf("Heisei\n");
    }else{
      //条件に当てはまらなければ'TBD'
      printf("TBD\n");
    }
  }else{
    printf("入力された日付が存在しません。\n");
  }
  return 0;
}


#include <stdio.h>

int main(){
  int n,sum = 0;
  char a[40] = "JPY";
  scanf("%d",&n);
  for (int i = 0; i < n; i++) {
    float x;
    char u[100];
    scanf("%f %s",&x,u);
    if (u[0] == a[0]) {
      sum += x;
    }else{
      sum += x * 380000.0;
    }
  }
  printf("%d\n",sum);
}

Score : 200 points

Problem Statement

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

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

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

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

Constraints

2 \leq N \leq 10

u_i = JPY or BTC.

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

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

Input

Input is given from Standard Input in the following format:

N
x_1 u_1
x_2 u_2
:
x_N u_N

Output

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

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

Sample Input 1

2
10000 JPY
0.10000000 BTC

Sample Output 1

48000.0

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

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

Sample Input 2

3
100000000 JPY
100.00000000 BTC
0.00000001 BTC

Sample Output 2

138000000.0038

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


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

int main (void){
    int N;
    char str[256];
    double x,sum;
    sum = 0.0;
    scanf("%d",&N);
    for(int i=0; i<N; i++){
        scanf("%lf",&x);
        scanf("%s", str);
        if(!strcmp(str,"JPY")){
            sum += x;
        }
        if(!strcmp(str,"BTC")){
            sum += 380000.0*x;
        }
    }
    printf("%lf\n",sum);
    return 0;
}


#include<stdio.h>

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

Score : 100 points

Problem Statement

You are given positive integers A and B.

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

Constraints

All values in input are integers.

1 \leq A \leq B \leq 20

Input

Input is given from Standard Input in the following format:

A B

Output

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

Sample Input 1

4 12

Sample Output 1

16

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

Sample Input 2

8 20

Sample Output 2

12

Sample Input 3

1 1

Sample Output 3

2

1 is a divisor of 1.


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

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

	if(B%A == 0) printf("%d\n",A+B);
	else printf("%d\n",B-A);

	return(0);
}


#include<stdio.h>
int main(){
  int n,m;
  scanf("%d",&n);
  scanf("%d",&m);
  int q;
  int a[m],count[m];
  for(int i=0;i<m;i++){
    count[i]=0;
  }
  for(int i=0;i<n;i++){
    q=0;
    scanf("%d",&q);
    for(int j=0;j<q;j++){
      scanf("%d",&a[j]);
      for(int k=0;k<m;k++){
        if(a[j]==k+1){
          count[k]++;
        }
      }
    }
  }
  int ans;
  ans=0;
  for(int i=0;i<m;i++){
    if(count[i]==n)
      ans++;
  }
  printf("%d",ans);
}

Score : 200 points

Problem Statement

Katsusando loves omelette rice.

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

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

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

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

Constraints

All values in input are integers.

1 \leq N, M \leq 30

1 \leq K_i \leq M

1 \leq A_{ij} \leq M

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

Constraints

Input is given from Standard Input in the following format:

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

Output

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

Sample Input 1

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

Sample Output 1

1

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

Sample Input 2

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

Sample Output 2

0

Katsusando's hypothesis turned out to be wrong.

Sample Input 3

1 30
3 5 10 30

Sample Output 3

3


#include <stdio.h>

int yakusuu(int a, int b)
{
  while(1) {
    if (a > b) {
      a %= b;
      if (a == 0) {
	return b;
        break;
      }
    } else {
      b %= a;
      if (b == 0) {
	return a;
        break;
      }
    }
  }
}

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

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

  int min = yakusuu(a[0], a[1]);
  for (int i = 2; i < n; i++)
    min = yakusuu(a[i], min);

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

  return 0;
}

Score : 300 points

Problem Statement

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

Initially, the health of Monster i is A_i.

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

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

A random alive monster attacks another random alive monster.

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

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

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

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

Sample Input 1

4
2 10 8 40

Sample Output 1

2

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

Sample Input 2

4
5 13 8 1000000000

Sample Output 2

1

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

1000000000


#include <stdio.h>

int gcd(int a, int b)
{
    if(b == 0) {
        return a;
    }

    return gcd(b, a%b);
}

int main()
{
    int N;
    int A1;
    int A2;
    int i;

    scanf("%d", &N);
    scanf("%d", &A1);
    for(i = 1; i < N; i++) {
        scanf("%d", &A2);
        A1 = gcd(A1, A2);
    }

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

    return 0;
}


#include<stdio.h>
int main(void){
	int n,k,i,j,count=0;

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

	for(i=1;i<n;i++){
		for(j=1;i!=1&&j<i;j++){
			if(i!=j+1&&i!=j-1){
				count++;
			}
		}
	}
	if(count>=k){
		puts("YES");
	}else{
		puts("NO");
	}

	return 0;
}

Score : 100 points

Problem Statement

Determine if we can choose K different integers between 1 and N (inclusive) so that no two of them differ by 1.

Constraints

1\leq N,K\leq 100

N and K are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

If we can choose K integers as above, print YES; otherwise, print NO.

Sample Input 1

3 2

Sample Output 1

YES

We can choose 1 and 3.

Sample Input 2

5 5

Sample Output 2

NO

Sample Input 3

31 10

Sample Output 3

YES

Sample Input 4

10 90

Sample Output 4

NO


#include<stdio.h>

int main(void){
    int count[4]={};
    int a,b;
    for(int i=0; i<3; i++){
        scanf("%d %d", &a, &b);
        count[a]++;
        count[b]++;
    }
    for(int i=0; i<4; i++)
        if(count[i] >= 3 || count[i]==0){
            printf("NO\n");
            return 0;
        }

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

Score : 200 points

Problem Statement

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

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

Constraints

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

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

No two roads connect the same pair of towns.

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

Input

Input is given from Standard Input in the following format:

a_1 b_1
a_2 b_2
a_3 b_3

Output

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

Sample Input 1

4 2
1 3
2 3

Sample Output 1

YES

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

Sample Input 2

3 2
2 4
1 2

Sample Output 2

NO

Sample Input 3

2 1
3 2
4 3

Sample Output 3

YES


#include<stdio.h>

int main()
{
    int T,X;
    float t;

    scanf("%d %d", &T, &X);

    t = (float)T/X;

    printf("%f\n",t);

    return 0;
}

Score : 100 points

Problem Statement

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

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

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

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

Constraints

All values in input are integers.

1 \leq T \leq 100

1 \leq X \leq 100

Input

Input is given from Standard Input in the following format:

T X

Output

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

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

Sample Input 1

8 3

Sample Output 1

2.6666666667

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

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

Sample Input 2

99 1

Sample Output 2

99.0000000000

Sample Input 3

1 100

Sample Output 3

0.0100000000


#include <stdio.h>

int main(){
  int n,i,max,sum=0;
  scanf("%d",&n);
  int l[n];
  for(i=0; i<n; i++){
    scanf("%d",&l[i]);
  }
  for(i=0; i<n; i++){
    if(l[i]>max) max=l[i];
    sum+=l[i];
  }
  if(max>=sum-max) printf("No");
  else printf("Yes");
  return 0;
}

Score : 200 points

Problem Statement

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

You can use the following theorem:

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

Constraints

All values in input are integers.

3 \leq N \leq 10

1 \leq L_i \leq 100

Input

Input is given from Standard Input in the following format:

N
L_1 L_2 ... L_N

Output

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

Sample Input 1

4
3 8 5 1

Sample Output 1

Yes

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

Sample Input 2

4
3 8 4 1

Sample Output 2

No

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

Sample Input 3

10
1 8 10 5 8 12 34 100 11 3

Sample Output 3

No


#include <stdio.h>
int main(void){
    int N,L,S=0,max=0;
    scanf("%d",&N);
    for (int i=0 ; i<N; i++) {
        scanf("%d",&L);
        S+=L;
        if(L>max){
            max=L;
        }
    }
    if(S-(2*max)>0){
        printf("Yes");
    }
    else{
        printf("No");
    }
    printf("\n");

    return 0;
}


#include <stdio.h>

int main(void){

   int N;

   scanf("%d",&N);

   int i,sum,max;
   int L[N];

   sum = 0;
   for(i = 0;i < N;i++){
      scanf("%d",&L[i]);
      sum = sum + L[i];
      if(i == 0 || max <= L[i]){
         max = L[i];
      }
   }

   if(max < sum - max){
      printf("Yes\n");
   }
   else {
      printf("No\n");
   }

  return 0;
}


#include <stdio.h>
#include <math.h>
int main(void){
  int n,x,y;
  scanf("%d%d%d",&n,&x,&y);
  if(n>=x+y) printf("%d %d",(int)fmin(x,y),0);
  else printf("%d %d",(int)fmin(x,y),x+y-n);
  return 0;
}

Score : 100 points

Problem Statement

We conducted a survey on newspaper subscriptions.
More specifically, we asked each of the N respondents the following two questions:

Question 1: Are you subscribing to Newspaper X?

Question 2: Are you subscribing to Newspaper Y?

As the result, A respondents answered "yes" to Question 1, and B respondents answered "yes" to Question 2.

What are the maximum possible number and the minimum possible number of respondents subscribing to both newspapers X and Y?

Write a program to answer this question.

Constraints

1 \leq N \leq 100

0 \leq A \leq N

0 \leq B \leq N

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the maximum possible number and the minimum possible number of respondents subscribing to both newspapers, in this order, with a space in between.

Sample Input 1

10 3 5

Sample Output 1

3 0

In this sample, out of the 10 respondents, 3 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 3 and at least 0.

Sample Input 2

10 7 5

Sample Output 2

5 2

In this sample, out of the 10 respondents, 7 answered they are subscribing to Newspaper X, and 5 answered they are subscribing to Newspaper Y.

Here, the number of respondents subscribing to both newspapers is at most 5 and at least 2.

Sample Input 3

100 100 100

Sample Output 3

100 100


#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[]) {
    int n;
    char a[101], b[101], c[101];
    int min;
    int i;

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

    min = n*3;

    for(i=0; i<n; i++) {
    	if(a[i] == b[i]) {
    		min -= 2;
    		if(a[i] == c[i]) {
    			min--;
    		}
    	} else if(b[i] == c[i]) {
    		min -= 2;
    		if(b[i] == a[i]) {
    			min--;
    		}
    	} else if(c[i] == a[i]) {
    		min -= 2;
    		if(c[i] == b[i]) {
    			min--;
    		}
    	} else {
    		min--;
    	}
    }
    printf("%d\n", min);
    return 0;
}

Score : 200 points

Problem Statement

You are given three strings A, B and C. Each of these is a string of length N consisting of lowercase English letters.

Our objective is to make all these three strings equal. For that, you can repeatedly perform the following operation:

Operation: Choose one of the strings A, B and C, and specify an integer i between 1 and N (inclusive). Change the i-th character from the beginning of the chosen string to some other lowercase English letter.

What is the minimum number of operations required to achieve the objective?

Constraints

1 \leq N \leq 100

Each of the strings A, B and C is a string of length N.

Each character in each of the strings A, B and C is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

N
A
B
C

Output

Print the minimum number of operations required.

Sample Input 1

4
west
east
wait

Sample Output 1

3

In this sample, initially A = west、B = east、C = wait. We can achieve the objective in the minimum number of operations by performing three operations as follows:

Change the second character in A to a. A is now wast.

Change the first character in B to w. B is now wast.

Change the third character in C to s. C is now wast.

Sample Input 2

9
different
different
different

Sample Output 2

0

If A, B and C are already equal in the beginning, the number of operations required is 0.

Sample Input 3

7
zenkoku
touitsu
program

Sample Output 3

13


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

int main(){
    int n, i, j, k, work, sum_a=0, sum_b=0;
    long *a,*b;

    scanf("%d",&n);

    a = (long*)malloc(sizeof(long)*n);
    b = (long*)malloc(sizeof(long)*n);

    for(i=0;i<n;i++){
        scanf("%ld %ld",&a[i],&b[i]);
    }
    for(j=0;j<n;j++){
        for(k=j+1;k<n;k++){
            if(a[j]<a[k]){
                work = a[j];
                a[j] = a[k];
                a[k] = work;
            }
            if(b[j]<b[k]){
                work = b[j];
                b[j] = b[k];
                b[k] = work;
            }
        }
    }
    for(i=0;i<n;i++){
        if(i%2==0){
            sum_a+=a[i];
        }else{
            sum_b+=b[i];
        }
    }
    printf("%d",sum_a-sum_b);
    free(a);
    free(b);

    return 0;
}

Score : 400 points

Problem Statement

There are N dishes of cuisine placed in front of Takahashi and Aoki.
For convenience, we call these dishes Dish 1, Dish 2, ..., Dish N.

When Takahashi eats Dish i, he earns A_i points of happiness; when Aoki eats Dish i, she earns B_i points of happiness.

Starting from Takahashi, they alternately choose one dish and eat it, until there is no more dish to eat.
Here, both of them choose dishes so that the following value is maximized: "the sum of the happiness he/she will earn in the end" minus "the sum of the happiness the other person will earn in the end".

Find the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Constraints

1 \leq N \leq 10^5

1 \leq A_i \leq 10^9

1 \leq B_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Output

Print the value: "the sum of the happiness Takahashi earns in the end" minus "the sum of the happiness Aoki earns in the end".

Sample Input 1

3
10 10
20 20
30 30

Sample Output 1

20

In this sample, both of them earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since Takahashi and Aoki have the same "taste", each time they will choose the dish with which they can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (30 + 10) - 20 = 20.

Sample Input 2

3
20 10
20 20
20 30

Sample Output 2

20

In this sample, Takahashi earns 20 points of happiness by eating any one of the dishes 1, 2 and 3, but Aoki earns 10 points of happiness by eating Dish 1, 20 points by eating Dish 2, and 30 points by eating Dish 3.

In this case, since only Aoki has likes and dislikes, each time they will choose the dish with which Aoki can earn the greatest happiness. Thus, first Takahashi will choose Dish 3, then Aoki will choose Dish 2, and finally Takahashi will choose Dish 1, so the answer is (20 + 20) - 20 = 20.

Sample Input 3

6
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000
1 1000000000

Sample Output 3

-2999999997

Note that the answer may not fit into a 32-bit integer.


#include<stdio.h>

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

Score : 100 points

Problem Statement

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

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

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

Constraints

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

All values in input are integers.

The area of the triangle ABC is an integer.

Input

Input is given from Standard Input in the following format:

|AB| |BC| |CA|

Output

Print the area of the triangle ABC.

Sample Input 1

3 4 5

Sample Output 1

6

This triangle has an area of 6.

Sample Input 2

5 12 13

Sample Output 2

30

This triangle has an area of 30.

Sample Input 3

45 28 53

Sample Output 3

630

This triangle has an area of 630.


Score : 200 points

Problem Statement

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

The first term s is given as input.

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

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

Find the minimum integer m that satisfies the following condition:

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

Constraints

1 \leq s \leq 100

All values in input are integers.

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

Input

Input is given from Standard Input in the following format:

s

Output

Print the minimum integer m that satisfies the condition.

Sample Input 1

8

Sample Output 1

5

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

Sample Input 2

7

Sample Output 2

18

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

Sample Input 3

54

Sample Output 3

114


#include<stdio.h>
#include<stdbool.h>
int main(void){
  int n=0;
  int h[n];
  int mizuyariCount=0;
  int l=0;
  int r=n;
  bool allZero=false;

  bool eluJudge=false;
  bool aruJudge=false;

  //本数指定
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    //要素ごとの高さ指定
    scanf("%d",&h[i]);
  }

  //ここからmizuyari を Count
  while(allZero==true){

  	// l の指定
  	while(eluJudge==true){
      for(int j=0;j<n;j++){
    	if(h[n]!=0){
      	  l=j;
          eluJudge = true;
          j = 101;
    	}
      }
    }
      // r の指定
    while(aruJudge==true){
      for(int k=0;k<n;k++){
        if(h[n]==0){
          r = k -1;
          aruJudge = true;
          k = 101;
        }
      }
    }

    //0でない部分で水やり
    for(l;l<r;l++){
      h[l] = h[l]-1;
    }
    mizuyariCount++;

    //要素全部 0 になったか調べる
    if(eluJudge==false&&aruJudge==false){
      allZero=true;
    }else{
    //値の初期化
    l=0;
    r=n;
    eluJudge=false;
    aruJudge=false;
    }

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

Score : 300 points

Problem Statement

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

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

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

Constraints

1 \leq N \leq 100

0 \leq h_i \leq 100

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 h_3 ...... h_N

Output

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

Sample Input 1

4
1 2 2 1

Sample Output 1

2

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

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

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

Sample Input 2

5
3 1 2 3 1

Sample Output 2

5

Sample Input 3

8
4 23 75 0 23 96 50 100

Sample Output 3

221


#include <stdio.h>

int dp[100010] = {0};
int M;
int l[100010][2];

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

int f(int a){
	if(dp[a]>=0) return dp[a];
	int max = 0;
	for(int i=0;i<M;i++){
		if(a==l[i][0]) max = maxNum(f(l[i][1])+1,max);
	}
	dp[a] = max;
	return dp[a];
}

int main(void) {
	int N;
	scanf("%d%d",&N,&M);
	for(int i=0;i<M;i++) scanf("%d%d",&l[i][0],&l[i][1]);
	for(int i=0;i<=N;i++) dp[i] = -1;
	int ans=0;
	for(int i=1;i<=N;i++){
		ans = maxNum(f(i),ans);
	}
	printf("%d",ans);
	/*for(int i=1;i<=N;i++){
		printf("%d\n",dp[i]);
	}*/
	return 0;
}

Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

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

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

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

Sample Output 3

3

The red directed path in the following figure is one of the longest:


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

int N,M,x,y;
int **G,*aa,*z;

int num(int n)
{
  int k,temp;

  if(aa[n]!=-1)
  {
    return aa[n];
  }
  else
  {
    temp=0;

    for(k=0;k<z[n];k++)
    {
      temp=fmax(temp,num(G[n][k])+1);
    }
    aa[n]=temp;
  }

  return temp;

}

int main()
{

  int i,j,s,t;
  scanf("%d %d",&N,&M);

  aa=(int *)malloc(sizeof(int)*(N+1));
  z=(int *)malloc(sizeof(int)*(N+1));

  for(i=1;i<N+1;i++)
  {
    aa[i]=-1;
    z[i]=0;

  }

  G=(int **)malloc(sizeof(int)*(N+1));
  for(i=0;i<N+1;i++)
  {
    G[i]=(int *)malloc(sizeof(int)*(M+1));
  }

  for(i=1;i<M+1;i++)
  {
    scanf("%d %d",&x,&y);

    G[x][z[x]]=y;
    z[x]++;
  }

  j=0;
  for(i=1;i<N+1;i++)
  {
    j=fmax(j,num(i));
  }

  printf("%d",j);

  return 0;
}


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

int main() {
    int N;
    scanf("%d", &N);
    float *p = malloc((N + 1) * sizeof(float));
    float **DP = malloc((N + 1) * sizeof(float*));
    for (int i = 1; i <= N; i++) {
        scanf("%f", &p[i]);
    }
    for (int i = 0; i <= N; i++) {
        DP[i] = malloc((N + 1) * sizeof(float));
        for (int j = 0; j <= N; j++) {
            DP[i][j] = 0.0;
        }
    }

    // initialize
    DP[1][0] = 1.0 - p[1];
    DP[1][1] = p[1];

    //DP[i][j]: i枚目まででj枚が面の確率 (jは最大i)
    for (int i = 2; i <= N; i++) {
        DP[i][0] = DP[i-1][0] * (1.0 - p[i]); // i-1枚目で表が一枚も出てない状態で裏が出る
        for (int j = 1; j <= i; j++) {
            DP[i][j] = (DP[i-1][j] * (1.0 -p[i]) + DP[i-1][j-1] * p[i]);  // i-1枚目までで、j枚表が出てる時に裏が出る or j-1枚表が出てる状態で、表が出る
        }
    }

    float ans = 0.0;
    // ansは(N-1)/2 + 1ちょうど一枚表が多い確率からN枚表になる確率を足した値
    for (int i = (N - 1)/2 + 1; i <= N; i++) {
        ans += DP[N][i];
    }
    printf("%f\n", ans);

    return 0;
}

Score : 100 points

Problem Statement

Let N be a positive odd number.

There are N coins, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), when Coin i is tossed, it comes up heads with probability p_i and tails with probability 1 - p_i.

Taro has tossed all the N coins.
Find the probability of having more heads than tails.

Constraints

N is an odd number.

1 \leq N \leq 2999

p_i is a real number and has two decimal places.

0 < p_i < 1

Input

Input is given from Standard Input in the following format:

N
p_1 p_2 \ldots p_N

Output

Print the probability of having more heads than tails.
The output is considered correct when the absolute error is not greater than 10^{-9}.

Sample Input 1

3
0.30 0.60 0.80

Sample Output 1

0.612

The probability of each case where we have more heads than tails is as follows:

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Head) is 0.3 × 0.6 × 0.8 = 0.144;

The probability of having (Coin 1, Coin 2, Coin 3) = (Tail, Head, Head) is 0.7 × 0.6 × 0.8 = 0.336;

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Tail, Head) is 0.3 × 0.4 × 0.8 = 0.096;

The probability of having (Coin 1, Coin 2, Coin 3) = (Head, Head, Tail) is 0.3 × 0.6 × 0.2 = 0.036.

Thus, the probability of having more heads than tails is 0.144 + 0.336 + 0.096 + 0.036 = 0.612.

Sample Input 2

1
0.50

Sample Output 2

0.5

Outputs such as 0.500, 0.500000001 and 0.499999999 are also considered correct.

Sample Input 3

5
0.42 0.01 0.42 0.99 0.42

Sample Output 3

0.3821815872


Score : 100 points

Problem Statement

There is a simple directed graph G with N vertices, numbered 1, 2, \ldots, N.

For each i and j (1 \leq i, j \leq N), you are given an integer a_{i, j} that represents whether there is a directed edge from Vertex i to j.
If a_{i, j} = 1, there is a directed edge from Vertex i to j; if a_{i, j} = 0, there is not.

Find the number of different directed paths of length K in G, modulo 10^9 + 7.
We will also count a path that traverses the same edge multiple times.

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 10^{18}

a_{i, j} is 0 or 1.

a_{i, i} = 0

Input

Input is given from Standard Input in the following format:

N K
a_{1, 1} \ldots a_{1, N}
:
a_{N, 1} \ldots a_{N, N}

Output

Print the number of different directed paths of length K in G, modulo 10^9 + 7.

Sample Input 1

4 2
0 1 0 0
0 0 1 1
0 0 0 1
1 0 0 0

Sample Output 1

6

G is drawn in the figure below:

There are six directed paths of length 2:

1 → 2 → 3

1 → 2 → 4

2 → 3 → 4

2 → 4 → 1

3 → 4 → 1

4 → 1 → 2

Sample Input 2

3 3
0 1 0
1 0 1
0 0 0

Sample Output 2

3

G is drawn in the figure below:

There are three directed paths of length 3:

1 → 2 → 1 → 2

2 → 1 → 2 → 1

2 → 1 → 2 → 3

Sample Input 3

6 2
0 0 0 0 0 0
0 0 1 0 0 0
0 0 0 0 0 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 0 0 0 0

Sample Output 3

1

G is drawn in the figure below:

There is one directed path of length 2:

4 → 5 → 6

Sample Input 4

1 1
0

Sample Output 4

0

Sample Input 5

10 1000000000000000000
0 0 1 1 0 0 0 1 1 0
0 0 0 0 0 1 1 1 0 0
0 1 0 0 0 1 0 1 0 1
1 1 1 0 1 1 0 1 1 0
0 1 1 1 0 1 0 1 1 1
0 0 0 1 0 0 1 0 1 0
0 0 0 1 1 0 0 1 0 1
1 0 0 0 1 0 1 0 0 0
0 0 0 0 0 1 0 0 0 0
1 0 1 1 1 0 1 1 1 0

Sample Output 5

957538352

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


Score : 100 points

Problem Statement

Find the number of integers between 1 and K (inclusive) satisfying the following condition, modulo 10^9 + 7:

The sum of the digits in base ten is a multiple of D.

Constraints

All values in input are integers.

1 \leq K < 10^{10000}

1 \leq D \leq 100

Input

Input is given from Standard Input in the following format:

K
D

Output

Print the number of integers satisfying the condition, modulo 10^9 + 7.

Sample Input 1

30
4

Sample Output 1

6

Those six integers are: 4, 8, 13, 17, 22 and 26.

Sample Input 2

1000000009
1

Sample Output 2

2

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

Sample Input 3

98765432109876543210
58

Sample Output 3

635270834


#include <stdio.h>

typedef struct List {
	struct List *next;
	int v;
	long long num;
} list;

int main()
{
	int i, N, M, u, w;
	list *adj[100001] = {}, e[200001], *p;
	scanf("%d %d", &N, &M);
	for (i = 0; i < N - 1; i++) {
		scanf("%d %d", &u, &w);
		e[i*2].v = w;
		e[i*2+1].v = u;
		e[i*2].num = -1;
		e[i*2+1].num = -1;
		e[i*2].next = adj[u];
		e[i*2+1].next = adj[w];
		adj[u] = &(e[i*2]);
		adj[w] = &(e[i*2+1]);
	}

	int par[100001] = {}, q[100001], head, tail;
	long long ans, dp[100001] = {};
	for (i = 1; i <= N; i++) {
		ans = 1;
		q[0] = i;
		par[i] = i;
		for (head = 0, tail = 1; head < tail; head++) {
			u = q[head];
			for (p = adj[u]; p != NULL; p = p->next) {
				if (p->num == -1 && par[p->v] == 0) {
					par[p->v] = u;
					q[tail++] = p->v;
				}
			}
		}
		for (head--; head >= 0; head--) {
			u = q[head];
			dp[u] = 1;
			for (p = adj[u]; p != NULL; p = p->next) {
				if (p->v == par[u]) continue;
				else {
					if (p->num == -1) p->num = dp[p->v];
					dp[u] = dp[u] * (p->num + 1) % M;
				}
			}
		}

		printf("%lld\n", dp[i]);
		for (head++; head < tail; head++) {
			u = q[head];
			par[u] = 0;
			dp[u] = 0;
		}
	}

	fflush(stdout);
	return 0;
}

Score : 100 points

Problem Statement

There is a tree with N vertices, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.

Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices.

You are given a positive integer M.
For each v (1 \leq v \leq N), answer the following question:

Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

2 \leq M \leq 10^9

1 \leq x_i, y_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print N lines.
The v-th (1 \leq v \leq N) line should contain the answer to the following question:

Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Sample Input 1

3 100
1 2
2 3

Sample Output 1

3
4
3

There are seven ways to paint the vertices, as shown in the figure below.
Among them, there are three ways such that Vertex 1 is black, four ways such that Vertex 2 is black and three ways such that Vertex 3 is black.

Sample Input 2

4 100
1 2
1 3
1 4

Sample Output 2

8
5
5
5

Sample Input 3

1 100

Sample Output 3

1

Sample Input 4

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

Sample Output 4

0
0
1
1
1
0
1
0
1
1

Be sure to print the answers modulo M.


Score : 100 points

Problem Statement

Consider a string of length N consisting of 0 and 1.
The score for the string is calculated as follows:

For each i (1 \leq i \leq M), a_i is added to the score if the string contains 1 at least once between the l_i-th and r_i-th characters (inclusive).

Find the maximum possible score of a string.

Constraints

All values in input are integers.

1 \leq N \leq 2 × 10^5

1 \leq M \leq 2 × 10^5

1 \leq l_i \leq r_i \leq N

|a_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N M
l_1 r_1 a_1
l_2 r_2 a_2
:
l_M r_M a_M

Output

Print the maximum possible score of a string.

Sample Input 1

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

Sample Output 1

20

The score for 10001 is a_1 + a_3 = 10 + 10 = 20.

Sample Input 2

3 4
1 3 100
1 1 -10
2 2 -20
3 3 -30

Sample Output 2

90

The score for 100 is a_1 + a_2 = 100 + (-10) = 90.

Sample Input 3

1 1
1 1 -10

Sample Output 3

0

The score for 0 is 0.

Sample Input 4

1 5
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 1 1000000000
1 1 1000000000

Sample Output 4

5000000000

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

Sample Input 5

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

Sample Output 5

10

For example, the score for 101000 is a_2 + a_3 + a_4 + a_5 + a_7 = 10 + (-8) + 5 + 9 + (-6) = 10.


#include <stdio.h>
int main(void){
  int a,b,c,max;
    fscanf("%d,%d,%d",&a,&b,&c);
    //2連続で毒入り食べると死
    if(a+b>=c) max=b+c;
    else max=a+2*b;
  return 0;
}

Score : 200 points

Problem Statement

Takahashi has A untasty cookies containing antidotes, B tasty cookies containing antidotes and C tasty cookies containing poison.

Eating a cookie containing poison results in a stomachache, and eating a cookie containing poison while having a stomachache results in a death.
As he wants to live, he cannot eat one in such a situation.
Eating a cookie containing antidotes while having a stomachache cures it, and there is no other way to cure stomachaches.

Find the maximum number of tasty cookies that Takahashi can eat.

Constraints

0 \leq A,B,C \leq 10^9

A,B and C are integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

Print the maximum number of tasty cookies that Takahashi can eat.

Sample Input 1

3 1 4

Sample Output 1

5

We can eat all tasty cookies, in the following order:

A tasty cookie containing poison

An untasty cookie containing antidotes

A tasty cookie containing poison

A tasty cookie containing antidotes

A tasty cookie containing poison

An untasty cookie containing antidotes

A tasty cookie containing poison

Sample Input 2

5 2 9

Sample Output 2

10

Sample Input 3

8 8 1

Sample Output 3

9


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

int main(void){
  int a, b, c;
  int total=0;

  scanf("%d %d %d", &a, &b, &c);
  while((a+b+1) < c){
    c -= 1;
  }
  total = b + c;
  printf("%d\n", total);
  return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main() {
	long long int  N, s[1005][1005] = {},p=2;
	scanf("%lld", &N);
  	printf("%lld\n", N);
	for (int i = 1; i <= N; i++) {
		for (int j = 1; j <= N; j++) {
			printf("%d ", i);
		}
      	printf("\n");
	}
}

Score : 1000 points

Problem Statement

For an n \times n grid, let (r, c) denote the square at the (r+1)-th row from the top and the (c+1)-th column from the left.
A good coloring of this grid using K colors is a coloring that satisfies the following:

Each square is painted in one of the K colors.

Each of the K colors is used for some squares.

Let us number the K colors 1, 2, ..., K. For any colors i and j (1 \leq i \leq K, 1 \leq j \leq K), every square in Color i has the same number of adjacent squares in Color j. Here, the squares adjacent to square (r, c) are ((r-1)\; mod\; n, c), ((r+1)\; mod\; n, c), (r, (c-1)\; mod\; n) and (r, (c+1)\; mod\; n) (if the same square appears multiple times among these four, the square is counted that number of times).

Given K, choose n between 1 and 500 (inclusive) freely and construct a good coloring of an n \times n grid using K colors.
It can be proved that this is always possible under the constraints of this problem,

Constraints

1 \leq K \leq 1000

Input

Input is given from Standard Input in the following format:

K

Output

Output should be in the following format:

n
c_{0,0} c_{0,1} ... c_{0,n-1}
c_{1,0} c_{1,1} ... c_{1,n-1}
:
c_{n-1,0} c_{n-1,1} ... c_{n-1,n-1}

n should represent the size of the grid, and 1 \leq n \leq 500 must hold.
c_{r,c} should be an integer such that 1 \leq c_{r,c} \leq K and represent the color for the square (r, c).

Sample Input 1

2

Sample Output 1

3
1 1 1
1 1 1
2 2 2

Every square in Color 1 has three adjacent squares in Color 1 and one adjacent square in Color 2.

Every square in Color 2 has two adjacent squares in Color 1 and two adjacent squares in Color 2.

Output such as the following will be judged incorrect:

2
1 2
2 2

3
1 1 1
1 1 1
1 1 1

Sample Input 2

9

Sample Output 2

3
1 2 3
4 5 6
7 8 9


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

int main(int argc, char *argv)
{
    long long n, p;

    long long maxCommon;
    long long maxN;
    long long i;
    long long num;

    scanf("%lld %lld", &n, &p);

    maxCommon = 1;
    maxN = pow(p, 1/((double)n));

    // i = 2のとき
    while (1) {
        num = pow(maxCommon * 2, n);

        if (p % num == 0) {
            maxCommon *= 2;
        } else {
            break;
        }
    }

    i = 3;
    while(i <= maxN) {
        num = pow(maxCommon * i, n);
        if (num > p) {break;}

        if (p % num == 0) {
            maxCommon *= i;
        } else {
            i += 2;
        }
    }

    printf("%lld\n", maxCommon);

    return 0;
}

Score : 300 points

Problem Statement

There are N integers a_1, a_2, ..., a_N not less than 1.
The values of a_1, a_2, ..., a_N are not known, but it is known that a_1 \times a_2 \times ... \times a_N = P.

Find the maximum possible greatest common divisor of a_1, a_2, ..., a_N.

Constraints

1 \leq N \leq 10^{12}

1 \leq P \leq 10^{12}

Input

Input is given from Standard Input in the following format:

N P

Output

Print the answer.

Sample Input 1

3 24

Sample Output 1

2

The greatest common divisor would be 2 when, for example, a_1=2, a_2=6 and a_3=2.

Sample Input 2

5 1

Sample Output 2

1

As a_i are positive integers, the only possible case is a_1 = a_2 = a_3 = a_4 = a_5 = 1.

Sample Input 3

1 111

Sample Output 3

111

Sample Input 4

4 972439611840

Sample Output 4

206


Score : 300 points

Problem Statement

There are N Reversi pieces arranged in a row. (A Reversi piece is a disc with a black side and a white side.)
The state of each piece is represented by a string S of length N.
If S_i=B, the i-th piece from the left is showing black;
If S_i=W, the i-th piece from the left is showing white.

Consider performing the following operation:

Choose i (1 \leq i < N) such that the i-th piece from the left is showing black and the (i+1)-th piece from the left is showing white, then flip both of those pieces. That is, the i-th piece from the left is now showing white and the (i+1)-th piece from the left is now showing black.

Find the maximum possible number of times this operation can be performed.

Constraints

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

S_i=B or W

Input

Input is given from Standard Input in the following format:

S

Output

Print the maximum possible number of times the operation can be performed.

Sample Input 1

BBW

Sample Output 1

2

The operation can be performed twice, as follows:

Flip the second and third pieces from the left.

Flip the first and second pieces from the left.

Sample Input 2

BWBWBW

Sample Output 2

6


#if 0
cat <<EOF >mistaken-paste
#endif
// thanks for @rsk0315_h4x

#pragma GCC diagnostic ignored "-Wincompatible-pointer-types"
#define _USE_MATH_DEFINES

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

#define BIG 2000000007
#define VERYBIG 2000000000000007LL

#define MOD 1000000007
#define FOD  998244353
typedef uint64_t ull;
typedef  int64_t sll;

#define N_MAX 1000000

#ifdef __cplusplus
#include <queue>
#include <stack>
#include <tuple>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <functional>
#include <array>

using std::queue;
using std::priority_queue;
using std::stack;
using std::tuple;
using std::set;
using std::map;
using std::vector;
using std::greater;
using std::pair;
using std::string;
using std::get;

template <typename T, typename U>
pair<T, U> operator+(pair<T, U> l, pair<T, U> r) {
	return pair<T, U>(l.first + r.first, l.second + r.second);
}

#endif

typedef struct {
	int32_t a;
	int32_t b;
} hw;

typedef struct {
	sll a;
	sll b;
} hwll;

typedef struct {
	sll a;
	sll b;
	sll c;
} hwllc;

typedef struct {
	hwll a;
	hwll b;
} linell;

ull n, m;
ull h, w;
ull k;
ull q;
sll va, vb, vc, vd, ve, vf;
ull ua, ub, uc, ud, ue, uf;
long double vra, vrb, vrc;
double vda, vdb, vdc;
char ch, dh;

ull umin (ull x, ull y) {
	return (x < y) ? x : y;
}

ull umax (ull x, ull y) {
	return (x > y) ? x : y;
}

sll smin (sll x, sll y) {
	return (x < y) ? x : y;
}

sll smax (sll x, sll y) {
	return (x > y) ? x : y;
}

ull gcd (ull x, ull y) {
	if (x < y) {
		return gcd(y, x);
	} else if (y == 0) {
		return x;
	} else {
		return gcd(y, x % y);
	}
}

ull bitpow (ull a, ull x, ull modulo) {
	ull result = 1;
	while (x) {
		if (x & 1) {
			result *= a;
			result %= modulo;
		}
		x /= 2;
		a = (a * a) % modulo;
	}
	return result;
}

ull divide (ull a, ull b, ull modulo) {
	return (a * bitpow(b, modulo - 2, modulo)) % modulo;
}

ull udiff (ull a, ull b) {
	if (a >= b) {
		return a - b;
	} else {
		return b - a;
	}
}

sll sdiff (sll a, sll b) {
	if (a >= b) {
		return a - b;
	} else {
		return b - a;
	}
}

int bitcount (ull n) {
	int result = 0;
	while (n) {
		if (n & 1) result++;
		n /= 2;
	}
	return result;
}

#define BEGCMP(NAME) int32_t NAME (const void *left, const void *right)
#define DEFLR(TYPE) TYPE l=*(TYPE*)left,r=*(TYPE*)right
#define CMPRET(L, R) if((L)<(R))return-1;if((L)>(R))return+1

// int32_t pullcomp (const void *left, const void *right) {
// 	ull l = *(ull*)left;
// 	ull r = *(ull*)right;
// 	if (l < r) {
// 		return -1;
// 	}
// 	if (l > r) {
// 		return +1;
// 	}
// 	return 0;
// }
BEGCMP(pullcomp){
	DEFLR(ull);
	CMPRET(l, r);
	return 0;
}
BEGCMP(prevcomp){
	DEFLR(ull);
	CMPRET(r, l);
	return 0;
}
BEGCMP(psllcomp){
	DEFLR(sll);
	CMPRET(l, r);
	return 0;
}
BEGCMP(pcharcomp){
	DEFLR(char);
	CMPRET(l, r);
	return 0;
}
BEGCMP(pdoublecomp){
	DEFLR(double);
	CMPRET(l, r);
	return 0;
}

int32_t pstrcomp (const void *left, const void *right) {
	char* l = *(char**)left;
	char* r = *(char**)right;

	return strcmp(l, r);
}

BEGCMP(phwllABcomp){
	DEFLR(hwll);
	CMPRET(l.a, r.a);
	CMPRET(l.b, r.b);
	return 0;
}
BEGCMP(phwllREVcomp){
	DEFLR(hwll);
	CMPRET(l.b, r.b);
	CMPRET(l.a, r.a);
	return 0;
}
BEGCMP(ptriplecomp){
	DEFLR(hwllc);
	CMPRET(l.a, r.a);
	CMPRET(l.b, r.b);
	CMPRET(l.c, r.c);
	return 0;
}
BEGCMP(ptripleREVcomp){
	DEFLR(hwllc);
	CMPRET(l.b, r.b);
	CMPRET(l.a, r.a);
	CMPRET(l.c, r.c);
	return 0;
}

int32_t pquadcomp (const void *left, const void *right) {
	linell l = *(linell*)left;
	linell r = *(linell*)right;

	sll ac = phwllABcomp(&(l.a), &(r.a));
	if (ac) return ac;
	sll bc = phwllABcomp(&(l.b), &(r.b));
	if (bc) return bc;

	return 0;
}

bool isinrange (sll left, sll x, sll right) {
	return (left <= x && x <= right);
}

bool isinrange_soft (sll left, sll x, sll right) {
	return (left <= x && x <= right) || (left >= x && x >= right);
}

sll a[N_MAX + 5];
// ull a[N_MAX + 5];
// sll a[3001][3001];
sll b[N_MAX + 5];
// ull b[N_MAX + 5];
// sll b[3001][3001];
ull c[N_MAX + 5];
ull d[N_MAX + 5];
sll e[N_MAX];
char s[N_MAX + 1];
// char s[3010][3010];
char t[N_MAX + 1];
// char t[3010][3010];
// char u[N_MAX + 1];
hwll xy[N_MAX + 5];
hwllc tup[N_MAX + 5];
sll table[3000][3000];
// here we go

sll find (ull ok, ull ng, ull x) {
	while (ok + 1 < ng) {
		ull med = (ok + ng) / 2;
		if (tup[med].b <= x) {
			ok = med;
		} else {
			ng = med;
		}
	}
	if (tup[ok].b == x) {
		return ok;
	} else {
		return -1;
	}
}

ull f (ull x) {
	if (x & 1) return 0;
	return f(x >> 1) + 1;
}
ull g (ull x) {
	ull b = 1;
	while (b <= x) b <<= 1;

	return (b - 1) - x;
}

ull solve () {
	sll i, j, ki, li;
	// ull result = 0;
	sll result = 0;
	double dresult = 0;
	// ull maybe = 0;
	sll maybe = 0;
	// ull sum = 0;
	sll sum = 0;
	sll item;
	ull *dpcell;

	for (i = 0; i < n; i++) {
		ull s = f(a[i]);
		xy[i] = (hwll){s, (a[i] >> (s + 1))};
	}
	qsort(xy, n, sizeof(hwll), phwllABcomp);
	ki = 0;
	i = 0;
	while (i < n) {
		j = i;
		while (i < n && xy[i].b == xy[j].b && xy[i].a == xy[j].a) i++;

		ull cnt = i - j;
		tup[ki++] = (hwllc){xy[j].a, xy[j].b, cnt};
	}

	i = 0;
	while (i < ki) {
		j = i;
		while (i < ki && tup[i].a == tup[j].a) i++;

		// for (li = i - 1; li >= j; li--) {

		// 	printf("%llu, %llu, %llu\n", tup[li].a, tup[li].b, tup[li].c);
		// }

		li = i;
		while (--li >= j) {
			if (tup[li].c <= 0) continue;
			if (tup[li].b <= 0) break;
			sll x = find(j, i, g(tup[li].b));
			if (x < 0) continue;

			result += smin(tup[x].c, tup[li].c);
			tup[x].c -= smin(tup[x].c, tup[li].c);

			// printf("%llu..\n", result);
		}
	}

	printf("%lld\n", result);
	// printf("%.15lf\n", dresult);
	// puts(s);

	return 0;

	success:
	// puts("YES");
	puts("Yes");
	// printf("%llu\n", result);
	// puts("0");
	// puts("Yay!");
	return 0;

	fail:
	// puts("NO");
	// puts("No");
	// puts("0");
	puts("-1");
	// puts("-1 -1 -1");
	// puts("Impossible");
	return 1;
}

int32_t main (void) {
	int32_t i, j;
	int32_t x, y;

	// scanf("%llu%llu", &h, &w);
	scanf("%llu", &n, &m);
	// scanf("%llu", &k, &n, &m);
	// scanf("%llu%llu", &h, &w);
	// scanf("%llu", &q);
	// scanf("%s", s);
	// scanf("%lld%lld", &va, &vb, &vc, &vd);
	// scanf("%llu%llu%llu", &ua, &ub, &uc, &ud);
	// scanf("%s", t);
	// scanf("%lld", &m);
	for (i = 0; i < n; i++) {
		// scanf("%lld%lld", &xy[i].a, &xy[i].b);
		// scanf("%lld%lld%lld", &tup[i].a, &tup[i].b, &tup[i].c);
		scanf("%lld", &a[i]);
		// scanf("%lld", &b[i]);
		// scanf("%lld", &c[i]);
		// scanf("%lld", &d[i]);
		// a[i]--;
		// b[i]--;
		// c[i]--;
		// d[i]--;
		// tup[i].a--;
		// tup[i].b--;
	}
	// scanf("%llu", &m, &k);
	// scanf("%llu", &q);
	// scanf("%s", s);
	// for (i = 0; i < n; i++) {
	// 	scanf("%llu", &c[i]);
	// 	// c[i]--;
	// }
	// scanf("%llu", &q);

	// for (i = 0; i < h; i++) {
	// 	for (j = 0; j < w; j++) {
	// 		scanf("%lld", &table[i][j]);
	// 		// table[i][j]--;
	// 	}
	// }
	// for (i = 0; i < h; i++) {
	// 	scanf("%s", s[i]);
	// }
	// scanf("%llu", &q);

	solve();

	return 0;
}

Score : 600 points

Problem Statement

Takahashi has N balls with positive integers written on them. The integer written on the i-th ball is A_i.
He would like to form some number of pairs such that the sum of the integers written on each pair of balls is a power of 2.
Note that a ball cannot belong to multiple pairs.
Find the maximum possible number of pairs that can be formed.

Here, a positive integer is said to be a power of 2 when it can be written as 2^t using some non-negative integer t.

Constraints

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

1 \leq A_i \leq 10^9

A_i is an integer.

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 pairs such that the sum of the integers written on each pair of balls is a power of 2.

Sample Input 1

3
1 2 3

Sample Output 1

1

We can form one pair whose sum of the written numbers is 4 by pairing the first and third balls.
Note that we cannot pair the second ball with itself.

Sample Input 2

5
3 11 14 5 13

Sample Output 2

2


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

int cmp(const void*a,const void*b){
	ll aa=*(ll*)a;
	ll bb=*(ll*)b;
	ll aaa=aa&-aa;
	ll bbb=bb&-bb;
	if(aaa<bbb)return -1;
	if(aaa>bbb)return 1;
	if(aa<bb)return -1;
	return 1;
}

ll n,ans;
ll a[200010];
int flag[200010];
int nibutan(int l,int r,int x){
	//x以下の最大のものを[l,r)で探す
	if(a[l]>x)return -1;
	while(r-l>1){
		int m=(l+r)/2;
		if(a[m]<x)l=m;
		else if(a[m]>x)r=m;
		else if(flag[m])r=m;
		else l=m;
	}
	return l;
}

int main(){
	scanf("%lld",&n);
	rep(i,0,n)scanf("%lld",a+i);
	qsort(a,n,sizeof(ll),cmp);
	for(int i=0;i<n;){
		int r=i;
		int t=a[i]&-a[i];
		while((a[r]&-a[r])==t)r++;
		//[i,r)で組にする
		for(int j=r-1;j>i;j--)if(!flag[j]){
			//a[j]と組に出来るものは、a[j]より真に大きい最小の2ベキをXとしてX-a[j]のみ
			//これを[i,j)でにぶたんして探す
			flag[j]=1;
			int x=1;
			while(x<=a[j])x+=x;
			int idx=nibutan(i,r,x-a[j]);
			if(idx!=-1&&a[idx]+a[j]==x&&!flag[idx]){
				ans++;
				flag[idx]=1;
			}
		}
		i=r;
	}
	printf("%lld",ans);
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//q_dort(double,int,int);

void q_sort(double numbers[], int left, int right)
{
    int l_hold, r_hold;
    double pivot;

    l_hold = left;
    r_hold = right;
    pivot = numbers[left];
    while (left < right)
    {
        while ((numbers[right] >= pivot) && (left < right))
            right--;
        if (left != right)
        {
            numbers[left] = numbers[right];
            left++;
        }
        while ((numbers[left] <= pivot) && (left < right))
            left++;
        if (left != right)
        {
            numbers[right] = numbers[left];
            right--;
        }
    }
    numbers[left] = pivot;
    pivot = left;
    left = l_hold;
    right = r_hold;
    if (left < pivot)
        q_sort(numbers, left, pivot-1);
    if (right > pivot)
        q_sort(numbers, pivot+1, right);
}

int main(void){
  int N,K,i,j,m;//,temp,min;
  double temp,min;
  double h[100000]={};
  min=1000000000;
  if(scanf("%d%d",&N,&K)>2){
    exit(1);
  }
  for(i=0;i<N;i++){
    if(scanf("%lf",&h[i])>1){
    exit(1);
  }
  }
  /*
  for(i=0; i<N; i++){
    for(j=N-1; j>i; j--){
      if(h[j] < h[j-1]){
        temp = h[j];
        h[j] = h[j-1];
        h[j-1] = temp;
      }
    }
  }
  */
  q_sort(h,0,N-1);
  for(i=0;i<N-K+1;i++){
    if(min>h[i+K-1]-h[i]){
    min=h[i+K-1]-h[i];
    if(min==0)break;
  }
  }
  m=min;
  printf("%d",m);
  return 0;
}

Score : 300 points

Problem Statement

In some other world, today is Christmas Eve.

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

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

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

Constraints

2 \leq K < N \leq 10^5

1 \leq h_i \leq 10^9

h_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
h_1
h_2
:
h_N

Output

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

Sample Input 1

5 3
10
15
11
14
12

Sample Output 1

2

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

Sample Input 2

5 3
5
7
5
7
7

Sample Output 2

0

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

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


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

    if(x==3||x==5||x==7)
    {
        printf("YES");
    }
    else
    {
        printf("NO");
    }
}

Score : 100 points

Problem Statement

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

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

Constraints

1 ≤ X ≤ 9

X is an integer.

Input

Input is given from Standard Input in the following format:

X

Output

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

Sample Input 1

5

Sample Output 1

YES

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

Sample Input 2

6

Sample Output 2

NO

See you next year.


#include <stdio.h>

int main()
{
    int x;

    scanf("%d", &x);

    if (x == 3 || x == 5 || x == 7) {
        printf("YES\n");
    } else {
        printf("NO\n");
    }

    return 0;
}


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

int abs(int num){
	if(num < 0)	num *= -1;
	return num;
}

int main(int argc, char const *argv[])
{
	int min = 999,val;
	char S[12];
	scanf("%s",S);
	for (int i = 0; i < strlen(S) - 2; ++i){
		val = abs(753 - (100 * (S[i] - '0') + 10 * (S[i + 1] - '0') + (S[i + 2] - '0')));
		if(min > val)	min = val;
	}
	printf("%d\n", min);
	return 0;
}

Score : 200 points

Problem Statement

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

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

Constraints

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

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

Input

Input is given from Standard Input in the following format:

S

Output

Print the minimum possible difference between X and 753.

Sample Input 1

1234567876

Sample Output 1

34

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

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

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

Sample Input 2

35753

Sample Output 2

0

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

Sample Input 3

1111111111

Sample Output 3

642

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


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

int main(){
  int i,j;
  int n;
  long long int m = 1;
  long long int x;
  int y = 0;
  int l=0;

  scanf("%d",&n);

  for(i=1; i<=n; i++){
    m = m * i;
  }

  for(i=1; i<=m; i++){
    if(m%i == 0){
      x = i;
      l = 0;

    for(j=1; j<=x; j++){
      if(x%j == 0){
        l++;
      }
    }
    if(l==75){
      y++;
    }
  }
  }
  printf("%d\n",y);

  return 0;
}

Score : 400 points

Problem Statement

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

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

Note

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

Constraints

1 \leq N \leq 100

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

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

Sample Input 1

9

Sample Output 1

0

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

Sample Input 2

10

Sample Output 2

1

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

Sample Input 3

100

Sample Output 3

543


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

int main()
{
    int m,a,b;
    while(~scanf("%d%d",&a,&b))
    {
        b=b/2;
        printf("%d\n",a+b);
    }
    return 0;
}

Score: 100 points

Problem Statement

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

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

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

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

Constraints

1 \leq X,Y \leq 100

Y is an even number.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

X Y

Output

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

Sample Input 1

81 58

Sample Output 1

110

The train fare is 81 yen.

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

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

Sample Input 2

4 54

Sample Output 2

31


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

int main()
{
    long long n;
    long long t, a;
    long long ans;
    long long hi;
    long long minv = 1ll << 50ll;
    long long h;

    scanf("%lld", &n);
    scanf("%lld %lld", &t, &a); t *= 1000; a *= 1000;

    for (long long i = 0; i < n; i++) {
        scanf("%lld", &h);

        hi = t - h * 6;

        if (minv > abs(a - hi)) {
            minv = abs(a - hi);
            ans = i + 1;
        }
    }

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

    return 0;
}

Score: 200 points

Problem Statement

A country decides to build a palace.

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

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

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

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

It is guaranteed that the solution is unique.

Constraints

1 \leq N \leq 1000

0 \leq T \leq 50

-60 \leq A \leq T

0 \leq H_i \leq 10^5

All values in input are integers.

The solution is unique.

Input

Input is given from Standard Input in the following format:

N
T A
H_1 H_2 ... H_N

Output

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

Sample Input 1

2
12 5
1000 2000

Sample Output 1

1

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

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

Thus, the palace should be built at Place 1.

Sample Input 2

3
21 -11
81234 94124 52141

Sample Output 2

3


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

double fabs(double x);

int main(){
    int n;
    int t,a;
    scanf("%d",&n);
    scanf("%d %d",&t,&a);
    double h[n];
    int i;
    double tmp;
    int maxplcnum[2]={100000,0};
    for(i=0; i<n; i++){
        scanf("%lf",&h[i]);
        tmp=t-h[i]*0.006;
        h[i]=a-tmp;
        h[i]=fabs(h[i]);
        if(maxplcnum[0]>h[i]){
            maxplcnum[0]=h[i];
            maxplcnum[1]=i+1;
        }
    }

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

    return 0;
    }


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

int main()
{   int a,b,y,k,n,i,t,h[1001];
    double th[1001],x=114514191;
    scanf("%d%d%d",&n,&t,&a);
    for(i=0;i<n;i++){
        scanf("%d",&h[i]);
        th[i]=t-h[i]*0.006;
    }
    for(i=0;i<n;i++){
        if(abs(th[i]-a)<x){
            x=abs(th[i]-a);
            y=i;
        }
    }
    printf("%d",y+1);

    return 0;
}


Score : 400 points

Problem Statement

You are given N integers; the i-th of them is A_i.
Find the maximum possible sum of the absolute differences between the adjacent elements after arranging these integers in a row in any order you like.

Constraints

2 \leq N \leq 10^5

1 \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_N

Output

Print the maximum possible sum of the absolute differences between the adjacent elements after arranging the given integers in a row in any order you like.

Sample Input 1

5
6
8
1
2
3

Sample Output 1

21

When the integers are arranged as 3,8,1,6,2, the sum of the absolute differences between the adjacent elements is |3 - 8| + |8 - 1| + |1 - 6| + |6 - 2| = 21. This is the maximum possible sum.

Sample Input 2

6
3
1
4
1
5
9

Sample Output 2

25

Sample Input 3

3
5
5
1

Sample Output 3

8


#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int gcd(int x, int y);
int main(void){
  long n,m,i,x,y,g=0;
  scanf("%ld %ld", &n, &m);
  if(n<m){
    i=n;
    n=m;
    m=i;
    g=1;
  }
  char s[n], t[m];
  if(g==1){
   scanf("%s", t);
   scanf("%s",s);
  }
  else {
   scanf("%s", s);
   scanf("%s", t);
  }
  g = gcd(n,m);
  x = n/g;
  y = m/g;
  for(i=0; m >= y*i ; i++){

    if(*(s+x*i)==*(t+y*i));
    else{
     x=1;
     y=1;
     g=-1;
     break;
     }
   }
  printf("%ld",x*y*g);
  return 0;
}
int gcd(int x, int y){

  if(x%y==0) return y;
  else {
    int t;
    t = x%y;
    x = y;
    y = t;
    return gcd(x, y);
  }
}

Score : 300 points

Problem Statement

You are given a string S of length N and another string T of length M.
These strings consist of lowercase English letters.

A string X is called a good string when the following conditions are all met:

Let L be the length of X. L is divisible by both N and M.

Concatenating the 1-st, (\frac{L}{N}+1)-th, (2 \times \frac{L}{N}+1)-th, ..., ((N-1)\times\frac{L}{N}+1)-th characters of X, without changing the order, results in S.

Concatenating the 1-st, (\frac{L}{M}+1)-th, (2 \times \frac{L}{M}+1)-th, ..., ((M-1)\times\frac{L}{M}+1)-th characters of X, without changing the order, results in T.

Determine if there exists a good string. If it exists, find the length of the shortest such string.

Constraints

1 \leq N,M \leq 10^5

S and T consist of lowercase English letters.

|S|=N

|T|=M

Input

Input is given from Standard Input in the following format:

N M
S
T

Output

If a good string does not exist, print -1; if it exists, print the length of the shortest such string.

Sample Input 1

3 2
acp
ae

Sample Output 1

6

For example, the string accept is a good string.
There is no good string shorter than this, so the answer is 6.

Sample Input 2

6 3
abcdef
abc

Sample Output 2

-1

Sample Input 3

15 9
dnsusrayukuaiia
dujrunuma

Sample Output 3

45


#include <stdio.h>

int get_lcm(int a, int b);

int main(void)
{
	int i, n, m, lcm;
	scanf("%d %d%*c", &n, &m);
	lcm = get_lcm(n,m);

	char s[100001], t[100001];
	for(i=0;i<n;i++)
		scanf("%c", &s[i]);
	s[i] = '\0';
	printf("%s\n", s);
	scanf("%*c");
	for(i=0;i<m;i++)
		scanf("%c", &t[i]);
	t[i] = '\0';
	printf("%s\n", t);

	if(s[0] != t[0]) {
		printf("%d\n", -1);
		return 0;
	}
	if(n%m == 0) {
		int flag = 0;
		for(i=0;i<m;i++) {
			if(s[i*(n/m)] != t[i]) {
				flag = 1;
				break;
			}
		}
		if(flag) {
			printf("%d\n", -1);
			return 0;
		} else {
			printf("%d\n", lcm);
			return 0;
		}
	}
	else if(m%n == 0) {
		int flag = 0;
		for(i=0;i<n;i++) {
			if(t[i*(m/n)] != s[i]) {
				flag = 1;
				break;
			}
		}
		if(flag) {
			printf("%d\n", -1);
			return 0;
		} else {
			printf("%d\n", lcm);
			return 0;
		}
	}

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

	return 0;
}

int get_lcm(int a, int b)
{
	int g, l, lcm;
	if(a>b) {
		g = a; l = b;
	} else {
		g = b; l = a;
	}
	lcm = g;

	while(lcm % l != 0) lcm += g;

	return lcm;
}


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

int gcd(int a, int b) {
    if (b == 0)
        return a;

    return gcd(b, a % b);
}

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

int main(){
  int N,M;
  scanf("%d%d",&N,&M);
  char S[N];
  char T[M];
  scanf("%s",S);
  scanf("%s",T);
  int i;
  int flag=0;

  int ans=-1;
  if(S[0]!=T[0]){
    flag=1;
  }
  if(N==M){
    for(i=0;i<N;i++){
      if(S[i]!=T[i]){
	flag=1;
      }
    }
  }else{
    for(i=0;i<N;i++){
      if(i*M%N==0){
	if(S[i]!=T[i*M/N]){
	  flag=1;
	}
      }
    }
  }
  if(flag==0){
    ans=lcm(N,M);
  }
  printf("%d",ans);
  return 0;
}


#include<stdio.h>

int main()
{
	int N, T;
	scanf("%d %d", &N, &T);
	int ary[2][N];

	int i, j = 0;
	for(i = 0; i < N; ++i){
		scanf("%d%d", &ary[0][j], &ary[1][j]);
		if(ary[1][j] <= T) ++j;
	}
	printf(" j = %d\n", j);
	if(j == 0) printf("TEL\n");
	else{
		int Min = 1001;
		for(i = 0; i < j; ++i){
			if(Min > ary[0][i]) Min = ary[0][i];
		}
		printf("%d\n", Min);
	}

	return 0;
}

Score : 200 points

Problem Statement

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

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

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

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

Constraints

All values in input are integers.

1 \leq N \leq 100

1 \leq T \leq 1000

1 \leq c_i \leq 1000

1 \leq t_i \leq 1000

The pairs (c_i, t_i) are distinct.

Input

Input is given from Standard Input in the following format:

N T
c_1 t_1
c_2 t_2
:
c_N t_N

Output

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

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

Sample Input 1

3 70
7 60
1 80
4 50

Sample Output 1

4

The first route gets him home at cost 7.

The second route takes longer than time T = 70.

The third route gets him home at cost 4.

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

Sample Input 2

4 3
1 1000
2 4
3 1000
4 500

Sample Output 2

TLE

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

Sample Input 3

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

Sample Output 3

5