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

// 内部定数
#define D_ON			1										// 汎用フラグ - ON
#define D_OFF			0										// 汎用フラグ - OFF
#define D_VTX_MAX		50										// 最大頂点数

// 内部構造体 - 頂点情報
typedef struct Vtx {
	int mi1Vtx[D_VTX_MAX + 5];									// 接続頂点
	int miVCnt;													// 接続頂点数
	int miChk;													// チェック済フラグ
} Vtx;

// 内部構造体 - 辺情報
typedef struct Edge {
	int miVNo1;													// 頂点１
	int miVNo2;													// 頂点２
} Edge;

// 内部変数
static Vtx sz1Vtx[D_VTX_MAX + 5];								// 頂点情報
static int siVCnt;												// 頂点情報数
static Edge sz1Edge[D_VTX_MAX];									// 辺情報
static int siECnt;												// 辺情報数

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpT, *szpFpA;
#endif

// 頂点情報 - 辺追加
int
fVtxEdgeAdd(
	int piVNo1					// <I> 頂点１
	, int piVNo2				// <I> 頂点２
)
{
	// 対象データ
	Vtx *lzpVtx = &sz1Vtx[piVNo1];

	// 接続頂点
	lzpVtx->mi1Vtx[lzpVtx->miVCnt] = piVNo2;

	// 接続頂点数
	lzpVtx->miVCnt++;

	return 0;
}

// 頂点情報 - 辺削除
int
fVtxEdgeDel(
	int piVNo1					// <I> 頂点１
	, int piVNo2				// <I> 頂点２
)
{
	int i;

	// 対象データ
	Vtx *lzpVtx = &sz1Vtx[piVNo1];

	// 接続頂点数
	lzpVtx->miVCnt--;
	if (lzpVtx->miVCnt < 1) {
		return 0;
	}

	// 頂点２を検索
	int liNo;
	for (i = 0; i <= lzpVtx->miVCnt; i++) {
		if (lzpVtx->mi1Vtx[i] == piVNo2) {
			liNo = i;
			break;
		}
	}
	if (liNo >= lzpVtx->miVCnt) {		// 最終データ
		return 0;
	}

	// 接続頂点 - 最終データで上書き
	lzpVtx->mi1Vtx[liNo] = lzpVtx->mi1Vtx[lzpVtx->miVCnt];

	return 0;
}

// 頂点情報 - 連結チェック
int
fVtxConvChk(
	int piVNo1					// <I> 頂点１
	, int piVNo2				// <I> 頂点２
)
{
	int i, liRet;

	// 連結チェック
	if (piVNo1 == piVNo2) {
		return 0;
	}

	// 対象データ
	Vtx *lzpVtx = &sz1Vtx[piVNo1];

	// チェック済フラグ
	if (lzpVtx->miChk != D_OFF) {
		return -1;
	}
	lzpVtx->miChk = D_ON;

	// 頂点数でループ
	for (i = 0; i < lzpVtx->miVCnt; i++) {
		liRet = fVtxConvChk(lzpVtx->mi1Vtx[i], piVNo2);
		if (liRet == 0) {
			return 0;
		}
	}

	return -1;
}

// 実行メイン
int
fMain(
	int piTNo					// <I> テスト番号 1～
)
{
	int i, j, liRet;
	char lc1Buf[1024], lc1Out[1024];

	// データ初期化
	memset(sz1Vtx, D_OFF, sizeof(sz1Vtx));						// 頂点情報

	// テストファイルオープン
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", piTNo);
	szpFpT = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", piTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#endif

	// 頂点情報数・辺情報数取得
#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpT);
#else
	fgets(lc1Buf, sizeof(lc1Buf), stdin);
#endif
	sscanf(lc1Buf, "%d%d", &siVCnt, &siECnt);

	// 辺情報取得
	for (i = 0; i < siECnt; i++) {
#ifdef D_TEST
		fgets(lc1Buf, sizeof(lc1Buf), szpFpT);
#else
		fgets(lc1Buf, sizeof(lc1Buf), stdin);
#endif
		sscanf(lc1Buf, "%d%d", &sz1Edge[i].miVNo1, &sz1Edge[i].miVNo2);

		// 頂点情報 - 辺追加
		fVtxEdgeAdd(sz1Edge[i].miVNo1, sz1Edge[i].miVNo2);
		fVtxEdgeAdd(sz1Edge[i].miVNo2, sz1Edge[i].miVNo1);
	}

	// 橋の数取得
	int liCnt = 0;
	for (i = 0; i < siECnt; i++) {

		// 頂点情報 - 辺削除
		fVtxEdgeDel(sz1Edge[i].miVNo1, sz1Edge[i].miVNo2);
		fVtxEdgeDel(sz1Edge[i].miVNo2, sz1Edge[i].miVNo1);

		// 頂点情報 - チェック済フラグ初期化
		for (j = 0; j < siVCnt; j++) {
			sz1Vtx[j].miChk = D_OFF;
		}

		// 頂点情報 - 連結チェック
		liRet = fVtxConvChk(sz1Edge[i].miVNo1, sz1Edge[i].miVNo2);
		if (liRet != 0) {												// 非連結
			liCnt++;
		}

		// 頂点情報 - 辺追加
		fVtxEdgeAdd(sz1Edge[i].miVNo1, sz1Edge[i].miVNo2);
		fVtxEdgeAdd(sz1Edge[i].miVNo2, sz1Edge[i].miVNo1);
	}

	// 結果セット
	sprintf(lc1Out, "%d\n", liCnt);

	// 結果表示
#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, lc1Out)) {
		siRes = -1;
	}
#else
	printf("%s", lc1Out);
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpT);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", piTNo);
	}
	else {
		printf("NG %d\n", piTNo);
	}
#endif

	return 0;
}

int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		fMain(i);
	}
#else
	fMain(0);
#endif

	return 0;
}

Score : 300 points

Problem Statement

You are given an undirected connected graph with N vertices and M edges that does not contain self-loops and double edges.

The i-th edge (1 \leq i \leq M) connects Vertex a_i and Vertex b_i.

An edge whose removal disconnects the graph is called a bridge.

Find the number of the edges that are bridges among the M edges.

Notes

A self-loop is an edge i such that a_i=b_i (1 \leq i \leq M).

Double edges are a pair of edges i,j such that a_i=a_j and b_i=b_j (1 \leq i<j \leq M).

An undirected graph is said to be connected when there exists a path between every pair of vertices.

Constraints

2 \leq N \leq 50

N-1 \leq M \leq min(N(N−1)⁄2,50)

1 \leq a_i<b_i \leq N

The given graph does not contain self-loops and double edges.

The given graph is connected.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

Print the number of the edges that are bridges among the M edges.

Sample Input 1

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

Sample Output 1

4

The figure below shows the given graph:

The edges shown in red are bridges. There are four of them.

Sample Input 2

3 3
1 2
1 3
2 3

Sample Output 2

0

It is possible that there is no bridge.

Sample Input 3

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

Sample Output 3

5

It is possible that every edge is a bridge.


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

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

long long min(long long a,long long b){
		if(a<b)return a;
		return b;
}

struct xy{
		long long x;
		long long y;
};

int compare_intx(const void *a,const void *b){
		struct xy A=*(struct xy*)a,B=*(struct xy*)b;
		if(A.x>B.x)return 1;
		return -1;
}

int compare_inty(const void *a,const void *b){
		struct xy A=*(struct xy*)a,B=*(struct xy*)b;
		if(A.y>B.y)return 1;
		return -1;
}

int main(){
		struct xy xy[51];
		int n,k,i,j;
		long long ans=-1;
		scanf("%d%d",&n,&k);
		for(i=0;i<n;i++)scanf("%lld%lld",&xy[i].x,&xy[i].y);
		qsort(xy,n,sizeof(struct xy),compare_intx);
		long long h=0,w=0;
		for(i=0;i+k-1<n;i++){
				h=xy[i+k-1].x-xy[i].x;
				long long mn=xy[i].y,mx=xy[i].y;
				for(j=1;j<k;j++){
						mn=min(mn,xy[j+i].y);
						mx=max(mx,xy[j+i].y);
				}
				w=mx-mn;
				if(ans==-1)ans=h*w;
				ans=min(ans,h*w);
		}
		qsort(xy,n,sizeof(struct xy),compare_inty);
		for(i=0;i+k-1<n;i++){
				h=xy[i+k-1].y-xy[i].y;
				long long mn=xy[i].x,mx=xy[i].x;
				for(j=0;j<k;j++){
						mn=min(mn,xy[i+j].x);
						mx=max(mx,xy[i+j].x);
				}
				w=mx-mn;
				ans=min(ans,h*w);
		}
		printf("%lld\n",ans);
		return 0;
}

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

Score : 400 points

Problem Statement

We have N points in a two-dimensional plane.

The coordinates of the i-th point (1 \leq i \leq N) are (x_i,y_i).

Let us consider a rectangle whose sides are parallel to the coordinate axes that contains K or more of the N points in its interior.

Here, points on the sides of the rectangle are considered to be in the interior.

Find the minimum possible area of such a rectangle.

Constraints

2 \leq K \leq N \leq 50

-10^9 \leq x_i,y_i \leq 10^9 (1 \leq i \leq N)

x_i≠x_j (1 \leq i<j \leq N)

y_i≠y_j (1 \leq i<j \leq N)

All input values are integers. (Added at 21:50 JST)

Input

Input is given from Standard Input in the following format:

N K
x_1 y_1
:
x_{N} y_{N}

Output

Print the minimum possible area of a rectangle that satisfies the condition.

Sample Input 1

4 4
1 4
3 3
6 2
8 1

Sample Output 1

21

One rectangle that satisfies the condition with the minimum possible area has the following vertices: (1,1), (8,1), (1,4) and (8,4).

Its area is (8-1) × (4-1) = 21.

Sample Input 2

4 2
0 0
1 1
2 2
3 3

Sample Output 2

1

Sample Input 3

4 3
-1000000000 -1000000000
1000000000 1000000000
-999999999 999999999
999999999 -999999999

Sample Output 3

3999999996000000001

Watch out for integer overflows.

Submitted source-code file size, in bytes.

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

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


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

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

int main(){
    int n,p,q;
    scanf("%d", &n);
    int a[n], b[1000000];
    for(int i=0;i<n;i++){
        scanf("%d %d", &p, &q);
        a[i] = p;
        b[p] = q;
    }
    qsort(a, n-1, sizeof(int), cmp);
    printf("%d", a[n-1]+b[a[n-1]]);
    return 0;
}

Score : 200 points

Problem Statement

A group of people played a game. All players had distinct scores, which are positive integers.

Takahashi knows N facts on the players' scores. The i-th fact is as follows: the A_i-th highest score among the players is B_i.

Find the maximum possible number of players in the game.

Constraints

1 \leq N \leq 10^5

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

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

If i ≠ j, A_i ≠ A_j.

There exists a possible outcome of the game that are consistent with the facts.

All input values are integers.

Inputs

Input is given from Standard Input in the following format:

N
A_1 B_1
:
A_N B_N

Outputs

Print the maximum possible number of players in the game.

Sample Input 1

3
4 7
2 9
6 2

Sample Output 1

8

The maximum possible number of players is achieved when, for example, the players have the following scores: 12,9,8,7,5,2,1,0.

Sample Input 2

5
1 10
3 6
5 2
4 4
2 8

Sample Output 2

7

Sample Input 3

2
1 1000000000
1000000000 1

Sample Output 3

1000000001


#include <stdio.h>
int main() {
	long int m, n, w, p, q;
	scanf("%ld", &m);
	for (w = 1; w <= 3500; w+=m)
	{
		for (n = 1; n <= 3500; n++)
		{
			p = m*n*w;
			q = 4 * n*w - m*w - m*n;
			if (q > 0 && p % q == 0)
			{
				printf("%ld %ld %ld", p / q, n, w);
				return 0;
			}
		}
	}
	printf("Cant Solve");
	return 0;
}

Score : 300 points

Problem Statement

You are given an integer N.

Find a triple of positive integers h, n and w such that 4/N = 1/h + 1/n + 1/w.

If there are multiple solutions, any of them will be accepted.

Constraints

It is guaranteed that, for the given integer N, there exists a solution such that h,n,w \leq 3500.

Inputs

Input is given from Standard Input in the following format:

N

Outputs

Print a triple of positive integers h, n and w that satisfies the condition, in the following format:

h n w

Sample Input 1

2

Sample Output 1

1 2 2

4/2 = 1/1 + 1/2 + 1/2.

Sample Input 2

3485

Sample Output 2

872 1012974 1539173474040

It is allowed to use an integer exceeding 3500 in a solution.

Sample Input 3

4664

Sample Output 3

3498 3498 3498


/* ex2_3
   KKOO  */

#include <stdio.h>

int main(void) {
    int ALL,WHITE;

    printf("ALL=");
    //マス目の一辺の入力
    scanf("%d",&ALL);

    printf("WHITE=");
     //白いマスの入力
    scanf("%d",&WHITE);

    //黒いマスの入力
    printf("BLACK=%d\n",ALL*ALL-WHITE);

    return 0;
}

Score : 100 points

Problem Statement

We have an N \times N square grid.

We will paint each square in the grid either black or white.

If we paint exactly A squares white, how many squares will be painted black?

Constraints

1 \leq N \leq 100

0 \leq A \leq N^2

Inputs

Input is given from Standard Input in the following format:

N
A

Outputs

Print the number of squares that will be painted black.

Sample Input 1

3
4

Sample Output 1

5

There are nine squares in a 3 \times 3 square grid.
Four of them will be painted white, so the remaining five squares will be painted black.

Sample Input 2

19
100

Sample Output 2

261

Sample Input 3

10
0

Sample Output 3

100

As zero squares will be painted white, all the squares will be painted black.


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


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

    scanf("%d\n", &N);
    if(N / 10 == 9 || N % 10 == 9){
        printf("Yes\n");
    }else{
        printf("No\n");
    }

}

Score : 100 points

Problem Statement

It is September 9 in Japan now.

You are given a two-digit integer N. Answer the question: Is 9 contained in the decimal notation of N?

Constraints

10≤N≤99

Input

Input is given from Standard Input in the following format:

N

Output

If 9 is contained in the decimal notation of N, print Yes; if not, print No.

Sample Input 1

29

Sample Output 1

Yes

The one's digit of 29 is 9.

Sample Input 2

72

Sample Output 2

No

72 does not contain 9.

Sample Input 3

91

Sample Output 3

Yes


#include<stdio.h>
#define MAX 100000

void sort(int A[], int N)
{
        int i, j, tmp;
        for(i=0; i<N; i++)
        {
                for(j=i; j<N; j++)
                {
                        if(A[i] > A[j])
                        {
                                tmp = A[i];
                                A[i] = A[j];
                                A[j] = tmp;
                        }
                }
        }
}

int main()
{
        int A[MAX];
        int N;
        int i, j;
        int cnt;

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

        sort(A, N);

        //for(i=0; i<N; i++)printf("%d ", A[i]);

        cnt = 0;
        for(i=0; i<N-1; i++)
        {
                if(A[i]==A[i+1])i++;
                else cnt++;
        }

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

        return 0;
}

Score : 300 points

Problem Statement

You are playing the following game with Joisino.

Initially, you have a blank sheet of paper.

Joisino announces a number. If that number is written on the sheet, erase the number from the sheet; if not, write the number on the sheet. This process is repeated N times.

Then, you are asked a question: How many numbers are written on the sheet now?

The numbers announced by Joisino are given as A_1, ... ,A_N in the order she announces them. How many numbers will be written on the sheet at the end of the game?

Constraints

1≤N≤100000

1≤A_i≤1000000000(=10^9)

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print how many numbers will be written on the sheet at the end of the game.

Sample Input 1

3
6
2
6

Sample Output 1

1

The game proceeds as follows:

6 is not written on the sheet, so write 6.

2 is not written on the sheet, so write 2.

6 is written on the sheet, so erase 6.

Thus, the sheet contains only 2 in the end. The answer is 1.

Sample Input 2

4
2
5
5
2

Sample Output 2

0

It is possible that no number is written on the sheet in the end.

Sample Input 3

6
12
22
16
22
18
12

Sample Output 3

2


#include <stdio.h>

int main (void)
{
    long X,t,ans;
    scanf("%ld",&X);
    scanf("%ld",&t);
    if(X<t){
        ans=0;
    }else{
        ans=X-t;
    }
    printf("%ld",ans);
    return 0;
}

Score : 100 points

Problem Statement

We have a sandglass that runs for X seconds. The sand drops from the upper bulb at a rate of 1 gram per second. That is, the upper bulb initially contains X grams of sand.

How many grams of sand will the upper bulb contains after t seconds?

Constraints

1≤X≤10^9

1≤t≤10^9

X and t are integers.

Input

The input is given from Standard Input in the following format:

X t

Output

Print the number of sand in the upper bulb after t second.

Sample Input 1

100 17

Sample Output 1

83

17 out of the initial 100 grams of sand will be consumed, resulting in 83 grams.

Sample Input 2

48 58

Sample Output 2

0

All 48 grams of sand will be gone, resulting in 0 grams.

Sample Input 3

1000000000 1000000000

Sample Output 3

0


#include <stdio.h>
int main(void)
{
        int i=0;
        char s[10001000];

        scanf("%s",s);
        if(s[0]==' ')   return 0;

        while(s[i]!='\0') {
                 printf("%c",s[i]);  i=i+2;
                 }
        printf("\n");
                return 0;
}

Score : 200 points

Problem Statement

You are given a string s consisting of lowercase English letters. Extract all the characters in the odd-indexed positions and print the string obtained by concatenating them. Here, the leftmost character is assigned the index 1.

Constraints

Each character in s is a lowercase English letter.

1≤|s|≤10^5

Input

The input is given from Standard Input in the following format:

s

Output

Print the string obtained by concatenating all the characters in the odd-numbered positions.

Sample Input 1

atcoder

Sample Output 1

acdr

Extract the first character a, the third character c, the fifth character d and the seventh character r to obtain acdr.

Sample Input 2

aaaa

Sample Output 2

aa

Sample Input 3

z

Sample Output 3

z

Sample Input 4

fukuokayamaguchi

Sample Output 4

fkoaaauh


#include <stdio.h>
int main(void)
{
        int a[30],i=0,j;
        char s[100];

        scanf("%s",s);

        while(s[i]=='\0') {printf("%s",s[i]);  i=i+2; }

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


#include <stdio.h>
int main(void)
{
  char s[200000];
  fgets(s, sizeof(s), stdin);
  for (int i=0; s[i] != '\n'; i++) {
    if (i%2 == 0) {
      printf("%c", s[i]);
    }
  }
      printf("\n");
  return 0;
}


Score : 400 points

Problem Statement

You are given a permutation p_1,p_2,...,p_N consisting of 1,2,..,N.
You can perform the following operation any number of times (possibly zero):

Operation: Swap two adjacent elements in the permutation.

You want to have p_i ≠ i for all 1≤i≤N.
Find the minimum required number of operations to achieve this.

Constraints

2≤N≤10^5

p_1,p_2,..,p_N is a permutation of 1,2,..,N.

Input

The input is given from Standard Input in the following format:

N
p_1 p_2 .. p_N

Output

Print the minimum required number of operations

Sample Input 1

5
1 4 3 5 2

Sample Output 1

2

Swap 1 and 4, then swap 1 and 3. p is now 4,3,1,5,2 and satisfies the condition.
This is the minimum possible number, so the answer is 2.

Sample Input 2

2
1 2

Sample Output 2

1

Swapping 1 and 2 satisfies the condition.

Sample Input 3

2
2 1

Sample Output 3

0

The condition is already satisfied initially.

Sample Input 4

9
1 2 4 9 5 8 7 3 6

Sample Output 4

3


Score : 300 points

Problem Statement

You've come to your favorite store Infinitesco to buy some ice tea.

The store sells ice tea in bottles of different volumes at different costs.
Specifically, a 0.25-liter bottle costs Q yen, a 0.5-liter bottle costs H yen, a 1-liter bottle costs S yen, and a 2-liter bottle costs D yen.
The store has an infinite supply of bottles of each type.

You want to buy exactly N liters of ice tea. How many yen do you have to spend?

Constraints

1 \leq Q, H, S, D \leq 10^8

1 \leq N \leq 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

Q H S D
N

Output

Print the smallest number of yen you have to spend to buy exactly N liters of ice tea.

Sample Input 1

20 30 70 90
3

Sample Output 1

150

Buy one 2-liter bottle and two 0.5-liter bottles. You'll get 3 liters for 90 + 30 + 30 = 150 yen.

Sample Input 2

10000 1000 100 10
1

Sample Output 2

100

Even though a 2-liter bottle costs just 10 yen, you need only 1 liter.
Thus, you have to buy a 1-liter bottle for 100 yen.

Sample Input 3

10 100 1000 10000
1

Sample Output 3

40

Now it's better to buy four 0.25-liter bottles for 10 + 10 + 10 + 10 = 40 yen.

Sample Input 4

12345678 87654321 12345678 87654321
123456789

Sample Output 4

1524157763907942


#include <stdio.h>
int main()
{
   int r, g, b;
   scanf("%d %d" , &r, &g);
   b = (g*2)-r;
   printf("%d", b);
   return 0;
}

Score : 100 points

Problem Statement

Snuke lives at position x on a number line.
On this line, there are two stores A and B, respectively at position a and b, that offer food for delivery.

Snuke decided to get food delivery from the closer of stores A and B.
Find out which store is closer to Snuke's residence.

Here, the distance between two points s and t on a number line is represented by |s-t|.

Constraints

1 \leq x \leq 1000

1 \leq a \leq 1000

1 \leq b \leq 1000

x, a and b are pairwise distinct.

The distances between Snuke's residence and stores A and B are different.

Input

Input is given from Standard Input in the following format:

x a b

Output

If store A is closer, print A; if store B is closer, print B.

Sample Input 1

5 2 7

Sample Output 1

B

The distances between Snuke's residence and stores A and B are 3 and 2, respectively.
Since store B is closer, print B.

Sample Input 2

1 999 1000

Sample Output 2

A


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#define MOD 1000000007
#define INFTY 2147400000
#define END printf("\n");return 0;
#define QS09(how_data,data) qsort(data,how_data,sizeof(long),(int (*)(const void *,const void *))qsort_09);
#define QS90(how_data,data) qsort(data,how_data,sizeof(long),(int (*)(const void *,const void *))qsort_90);
#define In(a) long a;scanf("%ld",&a);
#define Out(a) printf("%ld\n",a);
#define MAKE_ARRAY_DATA In(how_data);long data[how_data];input_array(how_data,data);
void input_array(long how_data,long *data);
void output_array(long how_data,long *data);
void input_array2(long first , long second , long data[][2]);
void format_array(long how_data ,long *data,long what);
long get_random(long min, long max);
long factorial(long n);
long fibonacci(long n);
int qsort_09(const int *sys1 , const int *sys2);
int qsort_90(const int *sys1 , const int *sys2);
long sel_max(long a , long b);
long sel_min(long a , long b);
long array_max(long how_data,long *data);
long array_min(long how_data,long *data);
long array_search(long how_data,long *data,long what);
long can_DP(long how_data,long *data,long how_can,bool *can);
long array_sum(long how_data,long *data);
long Leven_dist(char *now , char *target);
long get_digit(long target);
long rounding(double target);
long roundingdown(double target);
long roundingup(double target);
long ncr(long n , long r);
long npr(long n , long r);
long nhr(long n , long r);
int fifw(long add,long *data);
long fifr(long *data);
void GRAPH_input(void);
void GRAPH_dfs(long now);
bool check_prime(long target);

//グローバル変数一覧
long fifo[1000]={0};//fifo[0]はデータ件数
long loop1,loop2,loop3,loop4,loop5,i_temp;
long g_ans=0; //answerグローバル変数用

typedef struct{
    long how_vertex;
    long how_edge;
    long edge_data[1000][2];
    bool aleady_searched_vertex[500];
    bool all_searched;
} _GD;//GraphData

_GD GRAPH={0,0,{{0}},{0},0};

char c_temp;
//loop5 loop4 loop3 loop2 loop1
//--------------ここから

int main(void){
    In(x);In(a);In(b);
    if(abs(x-a)>abs(x-b)){
        printf("B");
    }else{
        printf("A");
    }
END;}

//--------------ここまで

void input_array(long how_data,long *data){
    long loop;
    for(loop=0;loop<how_data;loop++){
        scanf("%ld",&data[loop]);
    }
    return ;
}
void output_array(long how_data,long *data){
    long loop;
    for(loop=0;loop<how_data-1;loop++){
        printf("%ld ",data[loop]);
    }
    return ;
}
void input_array2(long first,long second,long data[][2]){
    long loopA,loopB;
    for(loopA=0;loopA<first;loopA++){
        for(loopB=0;loopB<second;loopB++){
            scanf("%ld",&data[loopA][loopB]);
        }
    }
    return ;
}
void format_array(long how_data ,long *data,long what){
    long loopA;
    for(loopA=0;loopA<how_data;loopA++){
        data[loopA]=what;
    }
    return ;
}
long get_random(long min, long max){   //指定の範囲から乱数を1つ返すやつ
    //srand((unsigned int)time(0));   //現在時刻で疑似乱数初期化
    rand();rand();rand();rand();    //乱数を空打ち
    return (rand()%(max+1-min))+min;
}
long factorial(long n){//n!のMOD10^9+7を返すやつ
    unsigned long long int ret=1;
    for(long i=1;i<=n;i++)ret=(ret*i)%1000000007;
    return (long)ret;
}
int qsort_09(const int *sys1 , const int *sys2){ //小さいのが上にくるやつ
    //qsort(data,how_data,sizeof(long),(int (*)(const void *,const void *))qsort_09);
    if(*sys1<*sys2){
        return -1;
    }else if(*sys1==*sys2){
        return 0;
    }else{
        return 1;
    }
}
int qsort_90(const int *sys1 , const int *sys2){ //大きいのが上にくるやつ
    //qsort(data,how_data,sizeof(long),(int (*)(const void *,const void *))qsort_90);
    if(*sys1<*sys2){
        return 1;
    }else if(*sys1==*sys2){
        return 0;
    }else{
        return -1;
    }
}
long fibonacci(long n){
    switch(n){
        case 0:return 0;
        case 1:return 1;
        default :return fibonacci(n-1)+fibonacci(n-2);
    }
}
long sel_max(long a,long b){
    if(a>b)return a;
    return b;
}
long sel_min(long a,long b){
    if(a>b)return b;
    return a;
}
long can_DP(long how_data,long *data,long how_can,bool *can){//Typical DP Contest A
    //data内で組み合わせられる和をcanに0 or 1で入れる
    //返り値はパターン数
    long loopA,loopB;
    long ret=0;
    for(loopA=0;loopA<how_can;loopA++){//初期化
        can[loopA]=0;
    }
    can[0]=1;//絶対にありえる
    for(loopA=0;loopA<how_data;loopA++){
        for(loopB=how_can-1;loopB>=0;loopB--){
            if(can[loopB]==1 && loopB+data[loopA]<how_can){
                can[loopB+data[loopA]]=1;
            }
        }
    }
    for(loopA=0;loopA<how_can;loopA++){
        if(can[loopA]==1){
            ret++;
        }
    }
    return ret;
}
long array_max(long how_data,long *data){
    long loop;
    long ret=data[0];
    for(loop=0;loop<how_data;loop++){
        if(ret<data[loop])ret=data[loop];
    }
    return ret;
}
long array_min(long how_data,long *data){
    long loop;
    long ret=data[0];
    for(loop=0;loop<how_data;loop++){
        if(ret>data[loop])ret=data[loop];
    }
    return ret;
}
long array_sum(long how_data,long *data){
    long ret=0;
    long loop;
    for(loop=0;loop<how_data;loop++){
        ret+=data[loop];
    }
    return ret;
}
long array_search(long how_data,long *data,long what){
    long loop;
    for(loop=0;loop<how_data;loop++){
        if(data[loop]==what){
            return loop;
        }
    }
    return -1;
}
long Leven_dist(char *now , char *target){
    long loopA,loopB;
    //レーベンシュタイン距離を求める
    // 参考文献
    // http://nw.tsuda.ac.jp/class/algoB/c13.html  (アルゴリズム理解)
    // http://d.hatena.ne.jp/ohnishiakira/20090809/1249845529  (実装)
    long len_now=strlen(now)+1;
    long len_target=strlen(target)+1;
    long d[len_now][len_target]; //計算用
    for(loopA=0;loopA<len_now;loopA++) d[loopA][0]=loopA;
    for(loopA=0;loopA<len_target;loopA++) d[0][loopA]=loopA;
    for(loopA=1;loopA<len_now;loopA++){
        for(loopB=1;loopB<len_target;loopB++){
            long cost=(now[loopA-1]==target[loopB-1] ? 0:1);
            d[loopA][loopB]=sel_min(sel_min(d[loopA-1][loopB]+1,d[loopA][loopB-1]+1),d[loopA-1][loopB-1]+cost);
        }
    }
    return d[len_now-1][len_target-1];
}
long get_digit(long target){
    return (long)(log10(target)+1);
}
long ncr(long n , long r){
    //パスカルの三角形
    long loopA,loopB;
    long pascal[100][102]={{0}};
    pascal[1][0]=1;
    pascal[1][1]=1;
    for(loopA=2;loopA<100;loopA++){
        pascal[loopA][0]=1;
        for(loopB=1;loopB<loopA;loopB++){
            pascal[loopA][loopB]=pascal[loopA-1][loopB-1]+pascal[loopA-1][loopB];
        }
        pascal[loopA][loopA]=1;
    }
    return pascal[n][r];
}
long npr(long n, long r){
    return ncr(n,r)*factorial(r);
}
long nhr(long n , long r){
    return ncr(n+r-1,r);
}
long rounding(double target){
    return (long)target+0.50;
}
long roundingup(double target){
    return (long)ceil(target);
}
long roundingdown(double target){
    return (long)floor(target);
}
int fifw(long add,long *data){
    data[data[0]+1]=add;
    data[0]++;
    return data[0]; //現在のデータ件数を返す
}
long fifr(long *data){
    long ret=data[1];
    data[0]--;
    for(int i=1;i<999;i++){
        data[i]=data[i+1];
    }
    return ret;
}
void GRAPH_input(void){
    scanf("%ld %ld\n",&GRAPH.how_vertex,&GRAPH.how_edge);
    for(long loopA=0;loopA<GRAPH.how_edge;loopA++){
        scanf("%ld %ld\n",&GRAPH.edge_data[loopA][0],&GRAPH.edge_data[loopA][1]);
    }
    return;
}

void GRAPH_dfs(long now){
    GRAPH.aleady_searched_vertex[now]=1;
    for(int loopA=0;loopA<GRAPH.how_edge;loopA++){
        if(GRAPH.edge_data[loopA][0]==now && GRAPH.aleady_searched_vertex[GRAPH.edge_data[loopA][1]]==0){
            GRAPH_dfs(GRAPH.edge_data[loopA][1]);
        }
        if(GRAPH.edge_data[loopA][1]==now && GRAPH.aleady_searched_vertex[GRAPH.edge_data[loopA][0]]==0){
            GRAPH_dfs(GRAPH.edge_data[loopA][0]);
        }
    }
    return ;
}

bool check_prime(long target){//素数判定、素数なら1
    if(target==2)return 1;
    if(target<=1 || target%2==0)return 0;
    for(int loopA=3;loopA<=sqrt(target)+1;loopA+=2)if(target%loopA==0)return 0;
    return 1;
}


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

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

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

#define MOD 1000000007

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

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

    //A[0]~A[N-1]に格納する
    int* A;
    NEW(A,N);
    for(int i=0;i<N;i++){
        scanf("%d",&A[i]);
    }
    qsort(A,N,sizeof(int),desc);

    int H=0;
    int W=0;
    for(int i=1;i<N;i++){
        if(A[i]==A[i-1]){
            if(H==0) H=A[i];
            else{
                W=A[i];
                break;
            }
            i++;
        }
    }

    long ans=H*W;
    printf("%ld\n",ans);

    return 0;
}

Score : 300 points

Problem Statement

We have N sticks with negligible thickness.
The length of the i-th stick is A_i.

Snuke wants to select four different sticks from these sticks and form a rectangle (including a square), using the sticks as its sides.
Find the maximum possible area of the rectangle.

Constraints

4 \leq N \leq 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 area of the rectangle.
If no rectangle can be formed, print 0.

Sample Input 1

6
3 1 2 4 2 1

Sample Output 1

2

1 \times 2 rectangle can be formed.

Sample Input 2

4
1 2 3 4

Sample Output 2

0

No rectangle can be formed.

Sample Input 3

10
3 3 3 3 4 4 4 5 5 5

Sample Output 3

20


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

int
main(int argc, char *argv[])
{
  int n;
  char s[2][53];
  scanf("%d", &n);
  scanf("%s", s[0]);
  scanf("%s", s[1]);

  int i, f;
  ll ans = 1;
  if (s[0][0] == s[1][0]) {
    i = 1; f = 0;
    ans *= 3;
  } else {
    i = 2; f = 1;
    ans *= 3;
  }

  while (i < n) {
    if (f) {
      if (s[0][i] != s[1][i]) {
	ans *= (ll)3;
	ans %= MOD;
	i += 2;
	f = 1;
      } else {
	i += 1;
	f = 0;
      }
    } else {
      if (s[0][i] != s[1][i]) {
	ans *= (ll)2;
	ans %= MOD;
	i += 2;
	f = 1;
      } else {
	ans *= (ll)2;
	ans %= MOD;
	i += 1;
	f = 0;
      }
    }
  }

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

  return 0;
}

Score : 400 points

Problem Statement

We have a board with a 2 \times N grid.
Snuke covered the board with N dominoes without overlaps.
Here, a domino can cover a 1 \times 2 or 2 \times 1 square.

Then, Snuke decided to paint these dominoes using three colors: red, cyan and green.
Two dominoes that are adjacent by side should be painted by different colors.
Here, it is not always necessary to use all three colors.

Find the number of such ways to paint the dominoes, modulo 1000000007.

The arrangement of the dominoes is given to you as two strings S_1 and S_2 in the following manner:

Each domino is represented by a different English letter (lowercase or uppercase).

The j-th character in S_i represents the domino that occupies the square at the i-th row from the top and j-th column from the left.

Constraints

1 \leq N \leq 52

|S_1| = |S_2| = N

S_1 and S_2 consist of lowercase and uppercase English letters.

S_1 and S_2 represent a valid arrangement of dominoes.

Input

Input is given from Standard Input in the following format:

N
S_1
S_2

Output

Print the number of such ways to paint the dominoes, modulo 1000000007.

Sample Input 1

3
aab
ccb

Sample Output 1

6

There are six ways as shown below:

Sample Input 2

1
Z
Z

Sample Output 2

3

Note that it is not always necessary to use all the colors.

Sample Input 3

52
RvvttdWIyyPPQFFZZssffEEkkaSSDKqcibbeYrhAljCCGGJppHHn
RLLwwdWIxxNNQUUXXVVMMooBBaggDKqcimmeYrhAljOOTTJuuzzn

Sample Output 3

958681902


#include<stdio.h>

int main(void) {
	int N;
	scanf("%d", &N);
	if (N/100==N%10) printf("Yes");
	else printf("No");

	return 0;
}

Score : 100 points

Problem Statement

You are given a three-digit positive integer N.

Determine whether N is a palindromic number.

Here, a palindromic number is an integer that reads the same backward as forward in decimal notation.

Constraints

100≤N≤999

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If N is a palindromic number, print Yes; otherwise, print No.

Sample Input 1

575

Sample Output 1

Yes

N=575 is also 575 when read backward, so it is a palindromic number. You should print Yes.

Sample Input 2

123

Sample Output 2

No

N=123 becomes 321 when read backward, so it is not a palindromic number. You should print No.

Sample Input 3

812

Sample Output 3

No


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

int a,b,c;
a=n/100;
b=(n%100)/10;
c=(n%100)%10;

if(a==c)  puts("Yes");
else
puts("No");

return 0;
}


#include <stdio.h>
int main(void)
{
    int a,b,c,d,i,s,g,t=0;

    scanf("%d%d%d%d",&a,&b,&c,&d);
    if(a<c) s=a;
    else s=c;
    if(b>d) g=b;
    else g=d;
    for(i=s;i<=g;i++)
        if(((a<=i)&&(i<b))&&((c<=i)&&(i<d)))
            t++;
    printf("%d\n",t);
    return 0;
}

Score : 200 points

Problem Statement

Alice and Bob are controlling a robot. They each have one switch that controls the robot.

Alice started holding down her button A second after the start-up of the robot, and released her button B second after the start-up.

Bob started holding down his button C second after the start-up, and released his button D second after the start-up.

For how many seconds both Alice and Bob were holding down their buttons?

Constraints

0≤A<B≤100

0≤C<D≤100

All input values are integers.

Input

Input is given from Standard Input in the following format:

A B C D

Output

Print the length of the duration (in seconds) in which both Alice and Bob were holding down their buttons.

Sample Input 1

0 75 25 100

Sample Output 1

50

Alice started holding down her button 0 second after the start-up of the robot, and released her button 75 second after the start-up.

Bob started holding down his button 25 second after the start-up, and released his button 100 second after the start-up.

Therefore, the time when both of them were holding down their buttons, is the 50 seconds from 25 seconds after the start-up to 75 seconds after the start-up.

Sample Input 2

0 33 66 99

Sample Output 2

0

Alice and Bob were not holding their buttons at the same time, so the answer is zero seconds.

Sample Input 3

10 90 20 80

Sample Output 3

60


#include <stdio.h>
long long int yaku(long long int a,long long int b){
  if(a < b){
    long long int tmp = a;
    a = b;
    b = tmp;
  }
  long long int r = a % b;
  while(r > 0){
    a = b;
    b = r;
    r = a % b;
  }
  return b;
}
int main(){
  int n;
  scanf("%d",&n);
  long long int a[n];
  for(int i = 0;i < n;i++){
    scanf("%lld",&a[i]);
    if(i > 0){
      a[i] = a[i] / yaku(a[i],a[i - 1]) * a[i - 1];
    }
  }
  printf("%lld\n",a[n - 1]);
  return 0;
}

Score : 300 points

Problem Statement

We have N clocks. The hand of the i-th clock (1≤i≤N) rotates through 360° in exactly T_i seconds.

Initially, the hand of every clock stands still, pointing directly upward.

Now, Dolphin starts all the clocks simultaneously.

In how many seconds will the hand of every clock point directly upward again?

Constraints

1≤N≤100

1≤T_i≤10^{18}

All input values are integers.

The correct answer is at most 10^{18} seconds.

Input

Input is given from Standard Input in the following format:

N
T_1
:
T_N

Output

Print the number of seconds after which the hand of every clock point directly upward again.

Sample Input 1

2
2
3

Sample Output 1

6

We have two clocks. The time when the hand of each clock points upward is as follows:

Clock 1: 2, 4, 6, ... seconds after the beginning

Clock 2: 3, 6, 9, ... seconds after the beginning

Therefore, it takes 6 seconds until the hands of both clocks point directly upward.

Sample Input 2

5
2
5
10
1000000000000000000
1000000000000000000

Sample Output 2

1000000000000000000


long g(a,b)long a,b;{return b?g(b,a%b):1;}
long m,a;main(n){for(scanf("%d",&n);~scanf("%ld",&a);m=m/g(m,a)*a)m==0&&(m=a);printf("%ld",m);}


#include<stdio.h>

int main()
{
 int n, m, blocks;
 scanf("%d %d",&n,&m);
 blocks = (n-1)*(m-1);
 printf("%d\n",blocks);

return 0;
}

Score : 100 points

Problem Statement

In K-city, there are n streets running east-west, and m streets running north-south. Each street running east-west and each street running north-south cross each other. We will call the smallest area that is surrounded by four streets a block. How many blocks there are in K-city?

Constraints

2 ≤ n, m ≤ 100

Input

Input is given from Standard Input in the following format:

n m

Output

Print the number of blocks in K-city.

Sample Input 1

3 4

Sample Output 1

6

There are six blocks, as shown below:

Sample Input 2

2 2

Sample Output 2

1

There are one block, as shown below:


#include<stdio.h>
#include<string.h>
int main(){
  char s[101];
  scanf("%s",s);
  printf("%c%d%c",s[0],strlen(s)-2,s[strlen(s)-1]);
  return 0;}

Score : 200 points

Problem Statement

The word internationalization is sometimes abbreviated to i18n.
This comes from the fact that there are 18 letters between the first i and the last n.

You are given a string s of length at least 3 consisting of lowercase English letters.
Abbreviate s in the same way.

Constraints

3 ≤ |s| ≤ 100 (|s| denotes the length of s.)

s consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

s

Output

Print the abbreviation of s.

Sample Input 1

internationalization

Sample Output 1

i18n

Sample Input 2

smiles

Sample Output 2

s4s

Sample Input 3

xyz

Sample Output 3

x1z


#include<stdio.h>
int main()
{
  int	n,max=1;

  scanf("%d", &n);

  while(max < n){
    max*=2;
  }

  if(max>n) max=max/2;

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

Score : 200 points

Problem Statement

Takahashi loves numbers divisible by 2.

You are given a positive integer N. Among the integers between 1 and N (inclusive), find the one that can be divisible by 2 for the most number of times. The solution is always unique.

Here, the number of times an integer can be divisible by 2, is how many times the integer can be divided by 2 without remainder.

For example,

6 can be divided by 2 once: 6 -> 3.

8 can be divided by 2 three times: 8 -> 4 -> 2 -> 1.

3 can be divided by 2 zero times.

Constraints

1 ≤ N ≤ 100

Input

Input is given from Standard Input in the following format:

N

Output

Print the answer.

Sample Input 1

7

Sample Output 1

4

4 can be divided by 2 twice, which is the most number of times among 1, 2, ..., 7.

Sample Input 2

32

Sample Output 2

32

Sample Input 3

1

Sample Output 3

1

Sample Input 4

100

Sample Output 4

64


#include <stdio.h>
#include <stdlib.h>
#define MAX_NUM 200000
#define BIRTHDAY 1009

typedef struct hash{
	int num;
	struct hash *next;
}hash;

void append(hash**, int);
void search(hash**, int);
void possible();

int a_list[MAX_NUM] = {0};
int a_index = 0;
hash *hash_list[MAX_NUM/BIRTHDAY] = {NULL};

int main(void){
	int N, M;
	int a, b;
	int i;
	hash *p;

	scanf("%d %d", &N, &M);
	for(i = 0; i < M; i++){
		scanf("%d %d", &a, &b);
		if(a == 1){
			append(hash_list, b);
		}
		if(b == N){
			a_list[a_index++] = a;
		}
	}

	for(i=0; i<a_index; i++){
		//search(hash_list, a_list[i]);
	}

	printf("IMPOSSIBLE");
	return 0;
}

void append(hash **list, int num){
	hash *p, *tmp;
	hash *item;
	tmp = NULL;
	for(p = list[num%BIRTHDAY]; p != NULL && p->num < num; p = p->next)
		tmp = p;

	item = (hash*)malloc(sizeof(hash));
	item->num = num;
	item->next = p;
	if(tmp == NULL)
		list[num%BIRTHDAY] = item;
	else
		tmp->next = item;
}

void search(hash **list, int num){
	hash *p;
	for(p = list[num%BIRTHDAY]; p != NULL && p->num <= num; p = p->next){
		if(p->num == num)
			possible();
	}
}

void possible(){
	printf("POSSIBLE");
	exit(1);
}

Score : 300 points

Problem Statement

In Takahashi Kingdom, there is an archipelago of N islands, called Takahashi Islands.
For convenience, we will call them Island 1, Island 2, ..., Island N.

There are M kinds of regular boat services between these islands.
Each service connects two islands. The i-th service connects Island a_i and Island b_i.

Cat Snuke is on Island 1 now, and wants to go to Island N.
However, it turned out that there is no boat service from Island 1 to Island N, so he wants to know whether it is possible to go to Island N by using two boat services.

Help him.

Constraints

3 ≤ N ≤ 200 000

1 ≤ M ≤ 200 000

1 ≤ a_i < b_i ≤ N

(a_i, b_i) \neq (1, N)

If i \neq j, (a_i, b_i) \neq (a_j, b_j).

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1
a_2 b_2
:
a_M b_M

Output

If it is possible to go to Island N by using two boat services, print POSSIBLE; otherwise, print IMPOSSIBLE.

Sample Input 1

3 2
1 2
2 3

Sample Output 1

POSSIBLE

Sample Input 2

4 3
1 2
2 3
3 4

Sample Output 2

IMPOSSIBLE

You have to use three boat services to get to Island 4.

Sample Input 3

100000 1
1 99999

Sample Output 3

IMPOSSIBLE

Sample Input 4

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

Sample Output 4

POSSIBLE

You can get to Island 5 by using two boat services: Island 1 -> Island 4 -> Island 5.


Score : 600 points

Problem Statement

We have a sequence of length N consisting of non-negative integers. Consider performing the following operation on this sequence until the largest element in this sequence becomes N-1 or smaller.

Determine the largest element in the sequence (if there is more than one, choose one). Decrease the value of this element by N, and increase each of the other elements by 1.

It can be proved that the largest element in the sequence becomes N-1 or smaller after a finite number of operations.

You are given an integer K. Find an integer sequence a_i such that the number of times we will perform the above operation is exactly K. It can be shown that there is always such a sequence under the constraints on input and output in this problem.

Constraints

0 ≤ K ≤ 50 \times 10^{16}

Input

Input is given from Standard Input in the following format:

K

Output

Print a solution in the following format:

N
a_1 a_2 ... a_N

Here, 2 ≤ N ≤ 50 and 0 ≤ a_i ≤ 10^{16} + 1000 must hold.

Sample Input 1

0

Sample Output 1

4
3 3 3 3

Sample Input 2

1

Sample Output 2

3
1 0 3

Sample Input 3

2

Sample Output 3

2
2 2

The operation will be performed twice: [2, 2] -> [0, 3] -> [1, 1].

Sample Input 4

3

Sample Output 4

7
27 0 0 0 0 0 0

Sample Input 5

1234567894848

Sample Output 5

10
1000 193 256 777 0 1 1192 1234567891011 48 425


#include <stdio.h>
int main(void){
    int a,b;
    scanf("%d%d",&a,&b);
    if(a%3==0 || b%3==0 || (a+b)%3==0) printf("Possible");
    else printf("Impossible");
    return 0;
}

Score : 100 points

Problem Statement

Snuke is giving cookies to his three goats.

He has two cookie tins. One contains A cookies, and the other contains B cookies. He can thus give A cookies, B cookies or A+B cookies to his goats (he cannot open the tins).

Your task is to determine whether Snuke can give cookies to his three goats so that each of them can have the same number of cookies.

Constraints

1 \leq A,B \leq 100

Both A and B are integers.

Input

Input is given from Standard Input in the following format:

A B

Output

If it is possible to give cookies so that each of the three goats can have the same number of cookies, print Possible; otherwise, print Impossible.

Sample Input 1

4 5

Sample Output 1

Possible

If Snuke gives nine cookies, each of the three goats can have three cookies.

Sample Input 2

1 1

Sample Output 2

Impossible

Since there are only two cookies, the three goats cannot have the same number of cookies no matter what Snuke gives to them.


#include<stdio.h>
int main(){
    int a,b,s,i,j;
    scanf("%d%d",&a,&b);
    s=a+b;
    i=s/3;
    //printf("%d\n",i);
    j=i*3;
    //printf("%d\n",j);
    i=s-j;
    //printf("%d\n",i);
    if(s!=0){
        if(i==0){
            printf("Possible\n");
        }else{
            printf("Impossible\n");
        }
    }else{
        printf("Impossible\n");
    }
    return 0;
}


#include<stdio.h>
#include<string.h>
int main(void)
{
int a,b;

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

if((a+b)%3==0)
  printf("Possible\n");
else
   printf("Impossible\n");

return 0;
}


#include <stdlib.h>

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

main()
{ int K,N;
	scanf("%d%d",&N,&K);
    int l[50];
    int i,j,k;
    for(k=0;k<=N-1;k++){
    	scanf("%d",&l[k]);
    }
    int sum;
    sum=0;

    qsort(l, N, sizeof(int), compare_int);
    for (i = N-1; i >=N-K ; i--) {
    	sum+=l[i];
    }
   printf("%d",sum);
}

Score : 200 points

Problem Statement

Snuke has N sticks.
The length of the i-th stick is l_i.

Snuke is making a snake toy by joining K of the sticks together.

The length of the toy is represented by the sum of the individual sticks that compose it.
Find the maximum possible length of the toy.

Constraints

1 \leq K \leq N \leq 50

1 \leq l_i \leq 50

l_i is an integer.

Input

Input is given from Standard Input in the following format:

N K
l_1 l_2 l_3 ... l_{N}

Output

Print the answer.

Sample Input 1

5 3
1 2 3 4 5

Sample Output 1

12

You can make a toy of length 12 by joining the sticks of lengths 3, 4 and 5, which is the maximum possible length.

Sample Input 2

15 14
50 26 27 21 41 7 42 35 7 5 5 36 39 1 45

Sample Output 2

386


#include<stdio.h>

int N, K;
int Snake[1000000];

int main(void){
int i, j, c, sum;
scanf("%d %d", &N, &K);

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

for( i = 0; i < N-1; i++){
for(j = i + 1; j < N; j++){
if(Snake[i] <= Snake[j] ){
c = Snake[j];
Snake[i] = Snake[j];
Snake[j] = c;
}
}}

sum = 0;
for(i = 0; i < K; i++){
sum += Snake[i];
}
return 0;
}


#include <stdio.h>
int main(void){
     long long int n,a[200000]={0},sum = 0,sunuke = 0,araiguma = 0,min = 10000000000;
    scanf("%lld", &n);
    for(int i = 0; i< n; i++){
        scanf("%lld",&a[i]);
        sum += a[i];
    }
    for(int j = 0; j < n-1; j++){
        sunuke +=a[j];
        araiguma = sum - sunuke;

        if(min > sunuke-araiguma && sunuke-araiguma >0){
            min = sunuke - araiguma;
        }else if(min > araiguma-sunuke && araiguma-sunuke >0){
            min = araiguma - sunuke;
        }else if(sunuke == araiguma){
            min = 0;
            break;
        }
    }
    if(min == 10000000000){
        min = 0;
    }
    printf("%lld", min);
    return 0;
}

Score : 300 points

Problem Statement

Snuke and Raccoon have a heap of N cards. The i-th card from the top has the integer a_i written on it.

They will share these cards.
First, Snuke will take some number of cards from the top of the heap, then Raccoon will take all the remaining cards.
Here, both Snuke and Raccoon have to take at least one card.

Let the sum of the integers on Snuke's cards and Raccoon's cards be x and y, respectively.
They would like to minimize |x-y|.
Find the minimum possible value of |x-y|.

Constraints

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

-10^{9} \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 answer.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

1

If Snuke takes four cards from the top, and Raccoon takes the remaining two cards, x=10, y=11, and thus |x-y|=1. This is the minimum possible value.

Sample Input 2

2
10 -10

Sample Output 2

20

Snuke can only take one card from the top, and Raccoon can only take the remaining one card. In this case, x=10, y=-10, and thus |x-y|=20.


#include<stdio.h>
#include<math.h>
int main(void)
{
    int n,i,x,num=0,num1=0,num2;
    scanf("%d",&n);
    int a[n];
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        num+=a[i];
    }
    x=abs(num-2*a[0]);
    for(i=0;i<n-1;i++)
    {
        num1+=a[i];
        num2=num-num1;
        if(abs(num1-num2)<x)    x=abs(num1-num2);
    }
        printf("%d",x);
        return 0;
}


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

long Combination(long n, long r) {
  long answer = 1;
  if (n-r < r) {
    r = n-r;
  }
  for (long i = 1; i <= r; i++) {
    answer *= n+1-i;
    answer /= i;
  }
  return answer;
}

int main(void) {

  int n,p;
  scanf("%d %d", &n, &p);
  int a[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  int odd = 0;
  for (int i = 0; i < n; i++) {
    if (a[i]%2 != 0) {
      odd++;
    }
  }
  int even = n-odd;
  long answer = 0;
  for (int i = 0; i <= odd; i += 2) {
    if (i == 0 && p == 1) {
      i++;
    }
    answer += Combination(odd,i);
  }
  answer *= pow(2,even);
  printf("%ld\n", answer);

  return 0;
}

Score : 200 points

Problem Statement

There are N bags of biscuits. The i-th bag contains A_i biscuits.

Takaki will select some of these bags and eat all of the biscuits inside.
Here, it is also possible to select all or none of the bags.

He would like to select bags so that the total number of biscuits inside is congruent to P modulo 2.
How many such ways to select bags there are?

Constraints

1 \leq N \leq 50

P = 0 or 1

1 \leq A_i \leq 100

Input

Input is given from Standard Input in the following format:

N P
A_1 A_2 ... A_N

Output

Print the number of ways to select bags so that the total number of biscuits inside is congruent to P modulo 2.

Sample Input 1

2 0
1 3

Sample Output 1

2

There are two ways to select bags so that the total number of biscuits inside is congruent to 0 modulo 2:

Select neither bag. The total number of biscuits is 0.

Select both bags. The total number of biscuits is 4.

Sample Input 2

1 1
50

Sample Output 2

0

Sample Input 3

3 0
1 1 1

Sample Output 3

4

Two bags are distinguished even if they contain the same number of biscuits.

Sample Input 4

45 1
17 55 85 55 74 20 90 67 40 70 39 89 91 50 16 24 14 43 24 66 25 9 89 71 41 16 53 13 61 15 85 72 62 67 42 26 36 66 4 87 59 91 4 25 26

Sample Output 4

17592186044416


#include <stdio.h>

int main(void) {
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);
	int max = a;
	if(b > max) max = b;
	if(c > max) max = c;
	printf("%d\n", a+b+c-max);
	return 0;
}

Score : 100 points

Problem Statement

Snuke is buying a bicycle.
The bicycle of his choice does not come with a bell, so he has to buy one separately.

He has very high awareness of safety, and decides to buy two bells, one for each hand.

The store sells three kinds of bells for the price of a, b and c yen (the currency of Japan), respectively.
Find the minimum total price of two different bells.

Constraints

1 \leq a,b,c \leq 10000

a, b and c are integers.

Input

Input is given from Standard Input in the following format:

a b c

Output

Print the minimum total price of two different bells.

Sample Input 1

700 600 780

Sample Output 1

1300

Buying a 700-yen bell and a 600-yen bell costs 1300 yen.

Buying a 700-yen bell and a 780-yen bell costs 1480 yen.

Buying a 600-yen bell and a 780-yen bell costs 1380 yen.

The minimum among these is 1300 yen.

Sample Input 2

10000 10000 10000

Sample Output 2

20000

Buying any two bells costs 20000 yen.


#include <stdio.h>

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

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

  if( (a >= b)&&(a >= c) )
    {
      printf("%d\n",b + c);
      return 0;
    }
  else if( (b >= a)&&(b >= c) )
    {
      printf("%d/n",a + c);
      return 0;
    }
  else
    printf("%d\n",a + b);

  return 0;
}


Score : 300 points

Problem Statement

You are given an integer sequence of length n, a_1, ..., a_n.
Let us consider performing the following n operations on an empty sequence b.

The i-th operation is as follows:

Append a_i to the end of b.

Reverse the order of the elements in b.

Find the sequence b obtained after these n operations.

Constraints

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

0 \leq a_i \leq 10^9

n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_n

Output

Print n integers in a line with spaces in between.
The i-th integer should be b_i.

Sample Input 1

4
1 2 3 4

Sample Output 1

4 2 1 3

After step 1 of the first operation, b becomes: 1.

After step 2 of the first operation, b becomes: 1.

After step 1 of the second operation, b becomes: 1, 2.

After step 2 of the second operation, b becomes: 2, 1.

After step 1 of the third operation, b becomes: 2, 1, 3.

After step 2 of the third operation, b becomes: 3, 1, 2.

After step 1 of the fourth operation, b becomes: 3, 1, 2, 4.

After step 2 of the fourth operation, b becomes: 4, 2, 1, 3.

Thus, the answer is 4 2 1 3.

Sample Input 2

3
1 2 3

Sample Output 2

3 1 2

As shown above in Sample Output 1, b becomes 3, 1, 2 after step 2 of the third operation. Thus, the answer is 3 1 2.

Sample Input 3

1
1000000000

Sample Output 3

1000000000

Sample Input 4

6
0 6 7 6 7 0

Sample Output 4

0 6 6 0 7 7


#include<stdio.h>
#include<stdlib.h>
int main() {
	int n;
	scanf("%d", &n);
	int *a, *b, *c;
	a = (int *)malloc(n*sizeof(int));
	b = (int *)malloc(n*sizeof(int));
	c = (int *)malloc(n*sizeof(int));
	for (int i = 0; i < n; i++) {
		int count = 0;
		scanf("%d", &a[i]);
		b[i] = a[i];
		for (int j = i; j >= 0; j--) {
			c[count] = b[j];
			count++;
		}
		for (int j = 0; j <= i; j++) {
			b[j] = c[j];
		}
	}
	for (int i = 0; i < n; i++) {
		printf("%d ", b[i]);
	}
	return 0;
}


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

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

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

ll *factors[2] , fac_cnt=0 , is_factor_prepared=0;
ll factor_pre(){  rep(i,0,1){ if(is_factor_prepared++) return 0; }  ll tmp=(1e6)/2+1, fac_tmp[tmp];  rep(i,0,tmp){fac_tmp[i]=i?2*i+1:2;}  rep(i,1,tmp){if(fac_tmp[i]){  repp(j,3,tmp/(2*i+1)+1,2 ){  if( j*(2*i+1)<tmp ) fac_tmp[ (j*(2*i+1)-1)/2 ]=0;  }  }else continue;}  rep(i,0,tmp){if(fac_tmp[i]){  rep(j,0,2){ factors[j] = realloc( factors[j] , sizeof(ll)*( fac_cnt +1 ) );  factors[j][j?fac_cnt++:fac_cnt]=j?0:fac_tmp[i];  }  } }  return 0;  }
ll factor(ll n, ll new_common_plus){ factor_pre();  rep(i,0,fac_cnt){ ll cnt=0; rep(j,0,1){ while( ( cnt+= n %factors[0][i]==0 ? 1 : 0 )  && (n/=factors[0][i]) %factors[0][i]==0 ) continue; } factors[1][i]= new_common_plus==1 ? cnt :  new_common_plus==2 ? max2(factors[1][i],cnt) : factors[1][i]+cnt ;  if( factors[0][i]> n ) break; } return n; }
ll judge_prime(ll n){ factor_pre(); rep(i,0,fac_cnt){ if(n<factors[0][i]*factors[0][i] || n==factors[0][i]) break; else if(n%factors[0][i]==0) n/=n; }  return n==1?0:1; }

ll *mf_arr,*inv_arr,*finv_arr,is_minv_made=0,is_mf_made=0,num_of_inv=1e5+10;
ll makeinv(ll n , ll mod){  rep(i,0,1){if(is_minv_made++) return 0;}  inv_arr = realloc(inv_arr, sizeof(ll)*2 );  finv_arr = realloc(finv_arr, sizeof(ll)*2 );  inv_arr[1]=1;finv_arr[0]=finv_arr[1]=1;  rep(i,2,n+1){  inv_arr = realloc(inv_arr, sizeof(ll)*(i+1) );  finv_arr = realloc(finv_arr, sizeof(ll)*(i+1) );  inv_arr[i]= mod - inv_arr[mod%i] * (mod / i) % mod;  finv_arr[i] = finv_arr[i - 1] * inv_arr[i] % mod;  }  return 0; }
ll make_mf(ll n, ll mod){  rep(i,0,1){ if(is_mf_made++) return 0; }  mf_arr = realloc(mf_arr, sizeof(ll)*2 );  ll x=1;  mf_arr[0]=mf_arr[1]=x;  rep(i,2,n+1){   x=x*i%mod;  mf_arr = realloc(mf_arr, sizeof(ll)*(i+1) );  mf_arr[i]=x; }  return 0;  }
ll m_inv(ll x, ll mod, ll is_fac ){ makeinv(num_of_inv,mod);  return is_fac?finv_arr[x]:inv_arr[x]; }
ll m_f(ll x, ll mod){  make_mf(num_of_inv,mod);  return mf_arr[x];  }
ll mod_nck(ll n, ll k, ll mod){
    return m_f(n,mod)*m_inv(k,mod,1)%mod*m_inv(n-k,mod,1)%mod;
    // return m_f[n]*finv_arr[k]%mod*finv_arr[n-k]%mod;
    }
ll m_p(ll r,ll n,ll mod){  ll t=1,s=r;  while(n>0){ t = (n&1) ? t*s%mod : t;  s=s*s%mod; n>>=1; }  return r?t:0;  }
ll m_mul2(ll a, ll b, ll mod){ return a*b%mod; }
ll m_mul3(ll a, ll b, ll c, ll mod){ return m_mul2(a*b%mod,c,mod); }
ll m_mul4(ll a, ll b, ll c, ll d, ll mod){ return m_mul3(a*b%mod,c,d,mod); }
ll m_mul5(ll a, ll b, ll c, ll d, ll e, ll mod){ return m_mul4(a*b%mod,c,d,e,mod); }

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

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

// char s[1151154];
ll ans[115115];
ll d[115115];
int main(void){
    // fgets(s,sizeof(s),stdin);
    ll n;
    // ll ans=0;
    cin(&n);
    // scanf("%s",s);
    ll a[n+1],p;
    rep(i,0,n){
        cin(&a[i]);
        d[a[i]]++;
        if(d[a[i]]==2) p=a[i];
    }
    ll l=-1,r;
    rep(i,0,n+1){
        if(a[i]==p){
            if(l==-1) l=i;
            else r=i;
        }
    }
    // printf("%lld %lld\n",l,r);
    // ans[0]=n;
    // ans[n]=1;
    rep(i,0,n+1){

        ans[i]+=mod_nck(n+1,i+1,MOD1);
        if(n-r+l>=i){
            ll t=mod_nck(n-r+l,i,MOD1);
            ans[i]+=mod_MOD1(-t);
        }

        ans[i]%=MOD1;
    }
    rep(i,0,n+1){
        printf("%lld\n",ans[i]);
    }
    return 0;
}

Score : 600 points

Problem Statement

You are given an integer sequence of length n+1, a_1,a_2,...,a_{n+1}, which consists of the n integers 1,...,n.
It is known that each of the n integers 1,...,n appears at least once in this sequence.

For each integer k=1,...,n+1, find the number of the different subsequences (not necessarily contiguous) of the given sequence with length k, modulo 10^9+7.

Notes

If the contents of two subsequences are the same, they are not separately counted even if they originate from different positions in the original sequence.

A subsequence of a sequence a with length k is a sequence obtained by selecting k of the elements of a and arranging them without changing their relative order. For example, the sequences 1,3,5 and 1,2,3 are subsequences of 1,2,3,4,5, while 3,1,2 and 1,10,100 are not.

Constraints

1 \leq n \leq 10^5

1 \leq a_i \leq n

Each of the integers 1,...,n appears in the sequence.

n and a_i are integers.

Input

Input is given from Standard Input in the following format:

n
a_1 a_2 ... a_{n+1}

Output

Print n+1 lines.
The k-th line should contain the number of the different subsequences of the given sequence with length k, modulo 10^9+7.

Sample Input 1

3
1 2 1 3

Sample Output 1

3
5
4
1

There are three subsequences with length 1: 1 and 2 and 3.

There are five subsequences with length 2: 1,1 and 1,2 and 1,3 and 2,1 and 2,3.

There are four subsequences with length 3: 1,1,3 and 1,2,1 and 1,2,3 and 2,1,3.

There is one subsequence with length 4: 1,2,1,3.

Sample Input 2

1
1 1

Sample Output 2

1
1

There is one subsequence with length 1: 1.

There is one subsequence with length 2: 1,1.

Sample Input 3

32
29 19 7 10 26 32 27 4 11 20 2 8 16 23 5 14 6 12 17 22 18 30 28 24 15 1 25 3 13 21 19 31 9

Sample Output 3

32
525
5453
40919
237336
1107568
4272048
13884156
38567100
92561040
193536720
354817320
573166440
818809200
37158313
166803103
166803103
37158313
818809200
573166440
354817320
193536720
92561040
38567100
13884156
4272048
1107568
237336
40920
5456
528
33
1

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


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

int main(void)
{
    int i;
    long n, tmp;
    long *button;
    int flag = 0;
    int now = 1;
    int push_num = 0;
    int purpose = -1;

    scanf("%ld", &n);

    button = malloc(n * sizeof(long));

    for(i = 0 ; i < n ; i++){
        scanf("%ld", &tmp);
        button[i] = tmp;
        if(tmp == 2){
            flag = 1;
        }
    }

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

    while(1){
        push_num++;
        now = button[now-1];
        if(now == 2){
            flag++;
            break;
        } else if(now == 1){
            break;
        }
    }

    if(flag == 1){
        printf("%d\n", push_num);
    } else {
        printf("-1\n");
    }
    free(button);
    return 0;
}

Score : 100 points

Problem Statement

Takahashi has a strong stomach. He never gets a stomachache from eating something whose "best-by" date is at most X days earlier.
He gets a stomachache if the "best-by" date of the food is X+1 or more days earlier, though.

Other than that, he finds the food delicious if he eats it not later than the "best-by" date. Otherwise, he does not find it delicious.

Takahashi bought some food A days before the "best-by" date, and ate it B days after he bought it.

Write a program that outputs delicious if he found it delicious, safe if he did not found it delicious but did not get a stomachache either, and dangerous if he got a stomachache.

Constraints

1 ≤ X,A,B ≤ 10^9

Input

Input is given from Standard Input in the following format:

X A B

Output

Print delicious if Takahashi found the food delicious; print safe if he neither found it delicious nor got a stomachache; print dangerous if he got a stomachache.

Sample Input 1

4 3 6

Sample Output 1

safe

He ate the food three days after the "best-by" date. It was not delicious or harmful for him.

Sample Input 2

6 5 1

Sample Output 2

delicious

He ate the food by the "best-by" date. It was delicious for him.

Sample Input 3

3 7 12

Sample Output 3

dangerous

He ate the food five days after the "best-by" date. It was harmful for him.


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

int main(void) {
    int n;
    scanf("%d", &n);
    int a[n+1];
    for(int i=1; i<n+1; i++) {
        scanf("%d", &a[i]);
    }
    int memo=1;
    int counter=0;
    int result;
    while(true) {
        memo = a[memo];
        counter++;
        if(memo == 2) {
            result = counter;
            break;
        }
        if(counter >= n) {
            result = -1;
            break;
        }
    }
    printf("%d", result);
    return 0;
}

Score : 200 points

Problem Statement

Takahashi wants to gain muscle, and decides to work out at AtCoder Gym.

The exercise machine at the gym has N buttons, and exactly one of the buttons is lighten up.
These buttons are numbered 1 through N.
When Button i is lighten up and you press it, the light is turned off, and then Button a_i will be lighten up. It is possible that i=a_i.
When Button i is not lighten up, nothing will happen by pressing it.

Initially, Button 1 is lighten up. Takahashi wants to quit pressing buttons when Button 2 is lighten up.

Determine whether this is possible. If the answer is positive, find the minimum number of times he needs to press buttons.

Constraints

2 ≤ N ≤ 10^5

1 ≤ a_i ≤ N

Input

Input is given from Standard Input in the following format:

N
a_1
a_2
:
a_N

Output

Print -1 if it is impossible to lighten up Button 2.
Otherwise, print the minimum number of times we need to press buttons in order to lighten up Button 2.

Sample Input 1

3
3
1
2

Sample Output 1

2

Press Button 1, then Button 3.

Sample Input 2

4
3
4
1
2

Sample Output 2

-1

Pressing Button 1 lightens up Button 3, and vice versa, so Button 2 will never be lighten up.

Sample Input 3

5
3
3
4
2
4

Sample Output 3

3


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int acs(const void *a, const void *b) {
  return *(int*)a - *(int*)b;
} /* 1,2,3,4.. */
int des(const void *a, const void *b) {
  return *(int*)b - *(int*)a;
} /* 8,7,6,5.. */
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 100001
#define MOD 1000000007
#define INF 1000000009
typedef long long int lli;

lli f[MAX];
void fact(int n) {
  for (lli i = 1; i <= n; i++ )
    f[i] = f[i-1] * i % MOD;
}

int main(void) {
  lli N, M;
  scanf("%lld %lld", &N, &M);

  f[0] = 1;
  fact(100000);
  lli dif = llabs(N - M);
  if ( dif != 0 && dif != 1 ) {
    printf("0\n");
    return 0;
  }
  if ( dif == 0 ) {
    printf("%lld\n", ((f[N] * f[M]) % MOD) * 2 % MOD);
    return 0;
  }
  printf("%lld\n", f[N] * f[M] % MOD);
  return 0;
}

Score : 300 points

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

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

Sample Input 1

2 2

Sample Output 1

8

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477


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

typedef struct binaryHeap{
  void *h;
  size_t heapSize;
  size_t maxSize;
  size_t size;
  int (*cmp)(const void *,const void *);
} heap;

heap* newHeap(const size_t size,int (*cmpF)(const void *,const void *)){
  const size_t maxSize=1;
  heap *res=(heap *)malloc(sizeof(heap));
  res->h=malloc(size*(maxSize+1));
  res->heapSize=0;
  res->maxSize=maxSize;
  res->size=size;
  res->cmp=cmpF;
  return res;
}

int isEmpty(const heap *h){
  return h->heapSize==0;
}

void freeHeap(heap *h){
  free(h->h);
  free(h);
  return;
}

void initHeap(heap *h){
  h->heapSize=0;
  return;
}

static inline void heapFunc_swap(void * restrict a,void * restrict b,void * restrict tmp,size_t size){
  memcpy(tmp,a,size);
  memcpy(a,b,size);
  memcpy(b,tmp,size);
  return;
}

void push(heap *q,const void *p){
  if(q->heapSize==q->maxSize){
    q->maxSize*=2;
    q->h=realloc(q->h,q->size*(q->maxSize+1));
  }
  q->heapSize+=1;
  char *h=(char *)(q->h);
  int k=q->heapSize;
  const size_t size=q->size;
  int (*cmp)(const void *,const void *)=q->cmp;
  memcpy(h+k*size,p,size);
  while(k>1){
    size_t parent=k/2;
    if(cmp(h+parent*size,h+k*size)<=0) return;
    heapFunc_swap(h+parent*size,h+k*size,h,size);
    k=parent;
  }
  return;
}

void pop(heap *q,void *res){
  char *h=(char *)(q->h);
  const size_t size=q->size;
  if(res!=NULL) memcpy(res,h+size,size);
  memcpy(h+size,h+size*q->heapSize,size);
  q->heapSize-=1;
  int (*cmp)(const void *,const void *)=q->cmp;
  const size_t n=q->heapSize;
  size_t k=1;
  while(2*k+1<=n){
    size_t next=cmp(h+2*k*size,h+(2*k+1)*size)<=0?2*k:2*k+1;
    if(cmp(h+k*size,h+next*size)<=0) return;
    heapFunc_swap(h+k*size,h+next*size,h,size);
    k=next;
  }
  if(2*k<=n && cmp(h+k*size,h+2*k*size)>0) heapFunc_swap(h+k*size,h+2*k*size,h,size);
  return;
}

void top(const heap *q,void *res){
  memcpy(res,(char *)q->h+q->size,q->size);
  return;
}

typedef long long int int64;

typedef struct edge{
  int a,b;
  int c;
} edge;

typedef struct indexNode{
  int index;
  int val;
} node;

int cmpNode(const void *a,const void *b){
  return ((node *)a)->val-((node *)b)->val;
}

int cmpEdge(const void *a,const void *b){
  return ((edge *)a)->c-((edge *)b)->c;
}

int root(int *u,int x){
  if(u[x]==x) return x;
  return u[x]=root(u,u[x]);
}

int same(int *u,int x,int y){
  return root(u,x)==root(u,y);
}

void unite(int *u,int x,int y){
  x=root(u,x);
  y=root(u,y);
  if(x==y) return;
  u[x]=y;
}

void run(void){
  int n;
  scanf("%d",&n);
  node *x=(node *)calloc(n,sizeof(node));
  node *y=(node *)calloc(n,sizeof(node));
  int i;
  for(i=0;i<n;i++){
    int a,b;
    scanf("%d%d",&a,&b);
    x[i]=(node){i,a};
    y[i]=(node){i,b};
  }
  qsort(x,n,sizeof(node),cmpNode);
  qsort(y,n,sizeof(node),cmpNode);
  heap *h=newHeap(sizeof(edge),cmpEdge);
  for(i=0;i<n-1;i++){
    push(h,&((edge){x[i].index,x[i+1].index,x[i+1].val-x[i].val}));
    push(h,&((edge){y[i].index,y[i+1].index,y[i+1].val-y[i].val}));
  }
  int cnt=0;
  int *u=(int *)calloc(n,sizeof(int));
  for(i=0;i<n;i++) u[i]=i;
  int64 ans=0;
  for(i=0;cnt<n-1;i++){
    edge e;
    pop(h,&e);
    if(same(u,e.a,e.b)) continue;
    unite(u,e.a,e.b);
    ans+=e.c;
    cnt++;
  }
  printf("%lld\n",ans);
}

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

Score : 500 points

Problem Statement

There are N towns on a plane. The i-th town is located at the coordinates (x_i,y_i). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a-c|,|b-d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?

Constraints

2 ≤ N ≤ 10^5

0 ≤ x_i,y_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
x_1 y_1
x_2 y_2
:
x_N y_N

Output

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1

3
1 5
3 9
7 8

Sample Output 1

3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2

6
8 3
4 9
12 19
18 1
13 5
7 6

Sample Output 2

8


#include <stdio.h>

int main(void){
	int num1;
	int num2;
	int num3;
	scanf("%d %d %d", &num1, &num2, &num3);

	printf("%d",num1);
	printf("%d",num2);
	printf("%d",num3);

	if((num1*100+num2*10+num3)%4 == 0){
		printf("YES");
	} else {
		printf("NO");
	}

	return 0;
}

Score : 100 points

Problem Statement

AtCoDeer has three cards, one red, one green and one blue.

An integer between 1 and 9 (inclusive) is written on each card: r on the red card, g on the green card and b on the blue card.

We will arrange the cards in the order red, green and blue from left to right, and read them as a three-digit integer.

Is this integer a multiple of 4?

Constraints

1 ≤ r, g, b ≤ 9

Input

Input is given from Standard Input in the following format:

r g b

Output

If the three-digit integer is a multiple of 4, print YES (case-sensitive); otherwise, print NO.

Sample Input 1

4 3 2

Sample Output 1

YES

432 is a multiple of 4, and thus YES should be printed.

Sample Input 2

2 3 4

Sample Output 2

NO

234 is not a multiple of 4, and thus NO should be printed.


#include <stdio.h>
#include <string.h>
int main(void){
    int n;
    scanf("%d", &n);
    int rate[9+5]={0};

    for(int i=0; i<n; i++){
        int a;
        scanf("%d", &a);
        if(a<3200)rate[a/400]=1;
        else rate[8]+=1;
    }

    int min=0;
    for(int i=0; i<8; i++)min+=rate[i];

    if(min==0){
        min++;
        rate[8]--;
    }
    printf("%d %d\n", min, min+rate[8]);

    return 0;
}

Score : 300 points

Problem Statement

In AtCoder, a person who has participated in a contest receives a color, which corresponds to the person's rating as follows:

Rating 1-399 : gray

Rating 400-799 : brown

Rating 800-1199 : green

Rating 1200-1599 : cyan

Rating 1600-1999 : blue

Rating 2000-2399 : yellow

Rating 2400-2799 : orange

Rating 2800-3199 : red

Other than the above, a person whose rating is 3200 or higher can freely pick his/her color, which can be one of the eight colors above or not.

Currently, there are N users who have participated in a contest in AtCoder, and the i-th user has a rating of a_i.

Find the minimum and maximum possible numbers of different colors of the users.

Constraints

1 ≤ N ≤ 100

1 ≤ a_i ≤ 4800

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 minimum possible number of different colors of the users, and the maximum possible number of different colors, with a space in between.

Sample Input 1

4
2100 2500 2700 2700

Sample Output 1

2 2

The user with rating 2100 is "yellow", and the others are "orange". There are two different colors.

Sample Input 2

5
1100 1900 2800 3200 3200

Sample Output 2

3 5

The user with rating 1100 is "green", the user with rating 1900 is blue and the user with rating 2800 is "red".

If the fourth user picks "red", and the fifth user picks "blue", there are three different colors. This is one possible scenario for the minimum number of colors.

If the fourth user picks "purple", and the fifth user picks "black", there are five different colors. This is one possible scenario for the maximum number of colors.

Sample Input 3

20
800 810 820 830 840 850 860 870 880 890 900 910 920 930 940 950 960 970 980 990

Sample Output 3

1 1

All the users are "green", and thus there is one color.


Score : 400 points

Problem Statement

You are given a string S of length N consisting of ( and ). Your task is to insert some number of ( and ) into S to obtain a correct bracket sequence.

Here, a correct bracket sequence is defined as follows:

() is a correct bracket sequence.

If X is a correct bracket sequence, the concatenation of (, X and ) in this order is also a correct bracket sequence.

If X and Y are correct bracket sequences, the concatenation of X and Y in this order is also a correct bracket sequence.

Every correct bracket sequence can be derived from the rules above.

Find the shortest correct bracket sequence that can be obtained. If there is more than one such sequence, find the lexicographically smallest one.

Constraints

The length of S is N.

1 ≤ N ≤ 100

S consists of ( and ).

Input

Input is given from Standard Input in the following format:

N
S

Output

Print the lexicographically smallest string among the shortest correct bracket sequences that can be obtained by inserting some number of ( and ) into S.

Sample Input 1

3
())

Sample Output 1

(())

Sample Input 2

6
)))())

Sample Output 2

(((()))())

Sample Input 3

8
))))((((

Sample Output 3

(((())))(((())))


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

int test(char *str) {
    int i, e=0, len=strlen(str);
    for (i=0; i<len; i++) {
        if (str[i] == '(')
            e++;
        else
            e--;
        if (e < 0)
            return -1;
    }
    if (e != 0)
        return 1;
    else
        return 0;
}

void pushfront(char *str) {
    char abc[128];
    strcpy(abc, str);
    str[0] = '(';
    strcpy(str+1, abc);
}
void pushback(char *str) {
    int itr = strlen(str);
    str[itr] = ')';
    str[itr+1] = '\0';
}

int main() {
    int n, ret;
    char str[128];
    scanf("%d\n", &n);
    fgets(str, sizeof(str), stdin);
    str[n] = '\0';
    while ((ret = test(str)) != 0) {
        if (ret > 0)
            pushback(str);
        else
            pushfront(str);
    }
    printf("%s\n", str);
    return 0;
}


#include <stdio.h>

int main(void)

{
  int a , b;

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

  if(a + b >= 10){
  printf("error");
  } else {
  a = a + b;
  printf("%d\n",a);
  }
  return 0;
}

Score : 100 points

Problem Statement

You are given two integers A and B as the input. Output the value of A + B.

However, if A + B is 10 or greater, output error instead.

Constraints

A and B are integers.

1 ≤ A, B ≤ 9

Input

Input is given from Standard Input in the following format:

A B

Output

If A + B is 10 or greater, print the string error (case-sensitive); otherwise, print the value of A + B.

Sample Input 1

6 3

Sample Output 1

9

Sample Input 2

6 4

Sample Output 2

error


#include<stdio.h>

int main()
{
    int a,b;
    scanf("%d %d",&a,&b);
    int sum=a+b;
    if(sum>=10){
        printf("error");
    }else{
        printf("%d",sum);
    }

    return 0;
}


#include <unistd.h>
#include <stdio.h>

int	main(void)
{
  char	str[27] = {0};
  int	i = 0;
  int	j = 0;

  read(0, str, 26);
  while (str[i])
  {
    j = i + 1;
    while (str[j])
    {
      if (str[i] == str[j])
      {
        printf("no");
        return (0);
      }
      j++;
    }
    i++;
  }
  printf("yes");
  return (0);
}

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters. Determine whether all the characters in S are different.

Constraints

2 ≤ |S| ≤ 26, where |S| denotes the length of S.

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

If all the characters in S are different, print yes (case-sensitive); otherwise, print no.

Sample Input 1

uncopyrightable

Sample Output 1

yes

Sample Input 2

different

Sample Output 2

no

Sample Input 3

no

Sample Output 3

yes


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

int main()
{
  char a[27];
  int i, j, n, flag=0;
  //入力
  scanf("%s", &a);
  n = strlen(a);
  //処理
  for(i=0; i<n; i++)
  {
    for(j=0; j<n; j++)
    {
      if(a[i] == a[j] && i != j)
      {
        flag = 1;
      }
    }
  }
  //出力
  if(flag == 0)
  {
    printf("yes\n");
  }
  else
  {
    printf("no\n");
  }
  return 0;
}


#include <stdio.h>
#include <string.h>
 int main()
 {
     int i,j,k=0;
	 char str[26];
     gets(str);
     for (i = 0; i < strlen(str); i++)
	 	{
         for (j =i + 1; j < strlen(str);j++)
		 	{
             if (str[i] == str[j])
			 k=1;
            }
        }
     if(k==1) printf("no");
     else printf("yes");
 }


#include <stdio.h>
#include <stdlib.h>
void qsort(
  void *base, size_t num, size_t size,
  int (*compare)(const void *a, const void *b)
);
int asc(const void *a, const void *b) {
  return *(int *)a - *(int *)b;
}

int main (){
  int i,a[102]={0},n=0,sum=0;
  scanf ("%d",&n);
  for (i=0;i<n;i++){
  scanf ("%d",&a[i]);
   sum=a[i]+sum;
  }
 qsort(a, n / sizeof(int), sizeof(int), asc);

  for (i=0;i<n;i++){
  if (sum%10 == 0){
    if (a[i]%10 != 0){
  sum = sum-a[i];
    }
    }
    else {break;}
  }
  printf ("%d",sum);
return 0;
}

Score : 300 points

Problem Statement

You are taking a computer-based examination. The examination consists of N questions, and the score allocated to the i-th question is s_i. Your answer to each question will be judged as either "correct" or "incorrect", and your grade will be the sum of the points allocated to questions that are answered correctly. When you finish answering the questions, your answers will be immediately judged and your grade will be displayed... if everything goes well.

However, the examination system is actually flawed, and if your grade is a multiple of 10, the system displays 0 as your grade. Otherwise, your grade is displayed correctly. In this situation, what is the maximum value that can be displayed as your grade?

Constraints

All input values are integers.

1 ≤ N ≤ 100

1 ≤ s_i ≤ 100

Input

Input is given from Standard Input in the following format:

N
s_1
s_2
:
s_N

Output

Print the maximum value that can be displayed as your grade.

Sample Input 1

3
5
10
15

Sample Output 1

25

Your grade will be 25 if the 10-point and 15-point questions are answered correctly and the 5-point question is not, and this grade will be displayed correctly. Your grade will become 30 if the 5-point question is also answered correctly, but this grade will be incorrectly displayed as 0.

Sample Input 2

3
10
10
15

Sample Output 2

35

Your grade will be 35 if all the questions are answered correctly, and this grade will be displayed correctly.

Sample Input 3

3
10
20
30

Sample Output 3

0

Regardless of whether each question is answered correctly or not, your grade will be a multiple of 10 and displayed as 0.


#include <stdio.h>

int main()
{
long int n,sum,c[10000],i;
scanf("%ld",&n);
sum=0;
for(i=0;i<n;i++)
{
    scanf("%ld",&c[i]);

        sum+=c[i];

}
if(sum%10==0)
{
for(i=0;i<n;i++)
{
    sum-=c[i];
    if(sum%10!=0)
    {
        break;
    }
    else{
        sum+=c[i];
    }
}
}else{
    sum=0;
}
printf("%ld",sum);
    return 0;
}


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

int main(){
	int n;
	scanf(" %d", &n);
	int i, all = 0, s[n];
	for(i = 0; i < n; i++){
		scanf(" %d", &s[i]);
	}
	for(i = 0; i < n; i++){
		all += s[i];
	}
	int min=999;
    for(i = 0; i < n; i++){
		if(s[i] %10 != 0 && s[i] < min){
			min = s[i];
		}
    }
	if(min == 999){
		printf("%d", 0);
	}
	else{
		printf("%d", all-min);
	}
	return 0;
}


#include <stdio.h>

int po(int n,int h[n],int p)
{
	for(int i=0;i<n;i++)
		if(h[i]%p!=0)return 0;
	return 1;
}

int compact(int N,int A,int B,int h[N])
{
	int x=1;
	while(po(N,h,A*B))
	{
		for(int i=0;i<N;i++)
		{
			h[i]/=A*B;
		}
		x*=A*B;
	}
	return x;
}

int max(int n,int a[n])
{
	int max=-114154;
	int maxi;
	for(int i=0;i<n;i++)
	{
		if(max<a[i])
		{
			max=a[i];
			maxi=i;
		}
	}
	return maxi;
}

int death(int N,int h[N])
{
	for(int i=0;i<N;i++)
		if(h[i]>0)return 1;
	return 0;
}

void bomb(int N,int A,int B,int h[N])
{
	int maxi=max(N,h);
	for(int i=0;i<N;i++)
		h[i]+=-B;
	h[maxi]+=B-A;
}

int main(void)
{
	int N,A,B;
	scanf("%d%d%d",&N,&A,&B);
	int h[N];
	for(int i=0;i<N;i++)
		scanf("%d",&h[i]);
	int x=compact(N,A,B,h);
	int i=0;
	for(;death(N,h);i++)
		bomb(N,A,B,h);
	printf("%d\n",i*x);
	return 0;
}

Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter N monsters. Each monster has a parameter called health, and the health of the i-th monster is h_i at the moment of encounter. A monster will vanish immediately when its health drops to 0 or below.

Fortunately, you are a skilled magician, capable of causing explosions that damage monsters. In one explosion, you can damage monsters as follows:

Select an alive monster, and cause an explosion centered at that monster. The health of the monster at the center of the explosion will decrease by A, and the health of each of the other monsters will decrease by B. Here, A and B are predetermined parameters, and A > B holds.

At least how many explosions do you need to cause in order to vanish all the monsters?

Constraints

All input values are integers.

1 ≤ N ≤ 10^5

1 ≤ B < A ≤ 10^9

1 ≤ h_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N A B
h_1
h_2
:
h_N

Output

Print the minimum number of explosions that needs to be caused in order to vanish all the monsters.

Sample Input 1

4 5 3
8
7
4
2

Sample Output 1

2

You can vanish all the monsters in two explosion, as follows:

First, cause an explosion centered at the monster with 8 health. The healths of the four monsters become 3, 4, 1 and -1, respectively, and the last monster vanishes.

Second, cause an explosion centered at the monster with 4 health remaining. The healths of the three remaining monsters become 0, -1 and -2, respectively, and all the monsters are now vanished.

Sample Input 2

2 10 4
20
20

Sample Output 2

4

You need to cause two explosions centered at each monster, for a total of four.

Sample Input 3

5 2 1
900000000
900000000
1000000000
1000000000
1000000000

Sample Output 3

800000000


#include<stdio.h>
int main(void){
  int i,j,m,n;
  char a;
  scanf("%d %d",&m,&n);
  for(i=0;i<m+2;i++){
    for(j=0;j<n+2;j++){
      if(i==0||i==m+2){
        printf("#");
      }
      if(j==0||j==n+2){
        printf("#");
      }else{
        scanf("%c",&a);
        printf("%c",a);
      }
    }
    printf("\n");
  }
  return 0;
}

Score : 200 points

Problem Statement

You are given a image with a height of H pixels and a width of W pixels.
Each pixel is represented by a lowercase English letter.
The pixel at the i-th row from the top and j-th column from the left is a_{ij}.

Put a box around this image and output the result. The box should consist of # and have a thickness of 1.

Constraints

1 ≤ H, W ≤ 100

a_{ij} is a lowercase English letter.

Input

Input is given from Standard Input in the following format:

H W
a_{11} ... a_{1W}
:
a_{H1} ... a_{HW}

Output

Print the image surrounded by a box that consists of # and has a thickness of 1.

Sample Input 1

2 3
abc
arc

Sample Output 1

#####
#abc#
#arc#
#####

Sample Input 2

1 1
z

Sample Output 2

###
#z#
###


#include<stdio.h>
#include<string.h>
int main()
{
	int m,n,i,j,k;
	char a[10000];
	scanf("%d%d",&m,&n);
	getchar();
	for(i=0;i<m;i++)
    gets(a);
	for(k=0;k<n+2;k++)
	printf("#");
	printf("\n");
	for(i=0;i<m;i++)
	printf("#%s#",a[i]);
	for(j=0;j<n+2;j++)
	printf("#");
	printf("\n");
	return 0;

}


#include <stdio.h>

long long min(long long a, long long b);
long long max(long long a, long long b);
long long ab(long long a);

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

    if((unsigned long long)H*(unsigned long long)W%3 == 0){
        printf("0\n");
        return 0;
    }

    if(H>W){
        sh = W;
        lo = H;
    }
    else{
        sh = H;
        lo = W;
    }

    int a, b=sh/2;
    if(sh%2 == 0){
        a = b;
    }
    else{
        a = b + 1;
    }

    int i=lo/2;
    long long d=H*W;

    while(i<lo){
        long long S1=a*i, S2=b*i, S3=sh*(lo-i);
        if(a==b){
            d=min(d, ab(S3-S1));
            //printf("d = %d\n", d);
        }
        else{
            long long la = max(ab(S3-S1), ab(S3-S2));
            d=min(d, la);
        }
        i++;
    }

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

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

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

long long ab(long long a){
    if(a>=0){
        return a;
    }
    else{
        return a *(-1);
    }
}

Score : 400 points

Problem Statement

There is a bar of chocolate with a height of H blocks and a width of W blocks.
Snuke is dividing this bar into exactly three pieces.
He can only cut the bar along borders of blocks, and the shape of each piece must be a rectangle.

Snuke is trying to divide the bar as evenly as possible.
More specifically, he is trying to minimize S_{max} - S_{min}, where S_{max} is the area (the number of blocks contained) of the largest piece, and S_{min} is the area of the smallest piece.
Find the minimum possible value of S_{max} - S_{min}.

Constraints

2 ≤ H, W ≤ 10^5

Input

Input is given from Standard Input in the following format:

H W

Output

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

Sample Input 1

3 5

Sample Output 1

0

In the division below, S_{max} - S_{min} = 5 - 5 = 0.

Sample Input 2

4 5

Sample Output 2

2

In the division below, S_{max} - S_{min} = 8 - 6 = 2.

Sample Input 3

5 5

Sample Output 3

4

In the division below, S_{max} - S_{min} = 10 - 6 = 4.

Sample Input 4

100000 2

Sample Output 4

1

Sample Input 5

100000 100000

Sample Output 5

50000


#include <stdio.h>

long long int min(long long int a, long long int b) { return a < b ? a : b; }
long long int max(long long int a, long long int b) { return a > b ? a : b; }
long long int abs(long long int a) { return a > 0 ? a : -a; }

int main(void) {
	long long int H, W, i, tmp, ans;
	scanf("%lld%lld", &H, &W);

	ans = H * W;
	if (H % 3 == 0 || W % 3 == 0) {
		ans = 0;
	}
	else if (H == 1 || W == 1) {
		ans = 1;
	}
	else {
		for (i = 1; i < W / 2 + 1; i++) {
			if (H % 2 == 0)
				tmp = abs(i * H - H / 2 * (W - i));
			else
				tmp = max(abs(i * H - H / 2 * (W - i)), abs(i * H - (H / 2 + 1) * (W - i)));
			ans = min(tmp, ans);
		}
	}

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


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

Score : 100 points

Problem Statement

You are given three integers A, B and C.
Determine whether C is not less than A and not greater than B.

Constraints

-100≤A,B,C≤100

A, B and C are all integers.

Input

Input is given from Standard Input in the following format:

A B C

Output

If the condition is satisfied, print Yes; otherwise, print No.

Sample Input 1

1 3 2

Sample Output 1

Yes

C=2 is not less than A=1 and not greater than B=3, and thus the output should be Yes.

Sample Input 2

6 5 4

Sample Output 2

No

C=4 is less than A=6, and thus the output should be No.

Sample Input 3

2 2 2

Sample Output 3

Yes


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

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

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

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

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

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

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

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

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

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

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

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

int dfs(int v, int n, int *visited, int table[n][n]) {
    int all_visited = 1, i;

    REP(i,n) {
        if (!visited[i]) {
            all_visited = 0;
            break;
        }
    }

    if (all_visited) {
        return 1;
    }

    int count = 0;

    REP(i,n) {
        if (!table[v][i]) continue;
        if (visited[i]) continue;

        visited[i] = 1;
        count += dfs(i, n, visited, table);
        visited[i] = 0;
    }

    return count;
}

int main() {
    int n = iin(), k = iin(), i, a, b;
    long long c[INT_MAX];
    memset(c, 0, sizeof c);
    REP(i, n) {
        a = iin(), b = iin();
        c[a] += b;
    }
    long long count = 0;
    i = 0;
    while(count < k) {
        i++;
        count += c[i];
    }
    iout(i);
    return 0;
}

Score : 300 points

Problem Statement

There is an empty array.
The following N operations will be performed to insert integers into the array.
In the i-th operation (1≤i≤N), b_i copies of an integer a_i are inserted into the array.
Find the K-th smallest integer in the array after the N operations.
For example, the 4-th smallest integer in the array \{1,2,2,3,3,3\} is 3.

Constraints

1≤N≤10^5

1≤a_i,b_i≤10^5

1≤K≤b_1…+…b_n

All input values are integers.

Input

Input is given from Standard Input in the following format:

N K
a_1 b_1
:
a_N b_N

Output

Print the K-th smallest integer in the array after the N operations.

Sample Input 1

3 4
1 1
2 2
3 3

Sample Output 1

3

The resulting array is the same as the one in the problem statement.

Sample Input 2

10 500000
1 100000
1 100000
1 100000
1 100000
1 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000

Sample Output 2

1


#include <stdio.h>

int main(void){

    long long int i,n,a,b,k;
    long long int num[100001]={0};

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

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

    }

    for( i = 1; i < 100001; i++ ){
        k = k - num[i];
        if( k <= 0 ){
            printf( "%lld",i );
            break;
        }
    }

    return 0;
}


#include <stdio.h>
#include<stdlib.h>
int int_sort( const void * a , const void * b ) {
  if( *( long long int * )a < *( long long int * )b ) {
    return -1;
  }
  else
  if( *( long long int * )a == *(long long  int * )b ) {
    return 0;
  }
  return 1;
}

int main(void){
    int N,K;
    scanf("%d %d",&N,&K);
    long long int a[114514]={};
      size_t data_cnt=sizeof( a )/sizeof( a[0] );
    for(int i=0;i<114514;i++)a[i]=2000000000000;
    for(int i=0;i<N;i++){
        int s,t;
        scanf("%d %d",&s,&t);
        a[i]=1000000*s+t;
    }
      qsort(( void * )a, data_cnt , sizeof( a[0] )   , int_sort );
      int count=0;
      for(int i=0;i<N;i++){
          count+=a[i]%1000000;
          if(count>=K){
              printf("%lld",a[i]/1000000);
              break;
          }
      }

    return 0;
}


#include<stdio.h>
#define INF -1000000000000000000
#define ll long long

int main() {
  int n, m, f;
  int i, j;
  ll score[1010] = {};
  scanf("%d %d", &n, &m);
  ll a[2010] = {};
  ll b[2010] = {};
  ll c[2010] = {};
  for (i = 1; i < m+1; i++) {
    scanf("%lld %lld %lld", &a[i], &b[i], &c[i]);
  }
  for (i = 2; i < n+1; i++) {
    score[i] = INF;
  }
  for (i = 1; i < n+2; i++) {
    f = 0;
    for (j = 1; j < m+1; j++) {
      int from = a[j];
      int to = b[j];
      /*基本的に、修正が入ったらf=1
       */
      if (score[to] < score[from] + c[j]) {
	score[to] = score[from] + c[j];
	f = 1;
      }
    }
    /*修正が入らなくなれば(f==0)そこで終わり*/
    if (f == 0) {
      break;
    }
    /*修正が入り続け、最後も修正が起こればinf*/
    if (i == n+1) {
      printf("inf\n");
      return 0;
    }
  }
  printf("%lld\n", score[n]);
  return 0;
}

Score : 400 points

Problem Statement

There is a directed graph with N vertices and M edges.
The i-th edge (1≤i≤M) points from vertex a_i to vertex b_i, and has a weight c_i.
We will play the following single-player game using this graph and a piece.

Initially, the piece is placed at vertex 1, and the score of the player is set to 0.
The player can move the piece as follows:

When the piece is placed at vertex a_i, move the piece along the i-th edge to vertex b_i. After this move, the score of the player is increased by c_i.

The player can end the game only when the piece is placed at vertex N.
The given graph guarantees that it is possible to traverse from vertex 1 to vertex N.

When the player acts optimally to maximize the score at the end of the game, what will the score be?
If it is possible to increase the score indefinitely, print inf.

Constraints

2≤N≤1000

1≤M≤min(N(N-1),2000)

1≤a_i,b_i≤N (1≤i≤M)

a_i≠b_i (1≤i≤M)

a_i≠a_j or b_i≠b_j (1≤i<j≤M)

-10^9≤c_i≤10^9 (1≤i≤M)

c_i is an integer.

In the given graph, there exists a path from vertex 1 to vertex N.

Input

Input is given from Standard Input in the following format:

N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M

Output

Print the maximum possible score at the end of the game, if it is finite. If it is possible to increase the score indefinitely, print inf.

Sample Input 1

3 3
1 2 4
2 3 3
1 3 5

Sample Output 1

7

There are two ways to move the piece to vertex N=3:

vertex 1 → vertex 2 → vertex 3 : score 4+3=7

vertex 1 → vertex 3 : score 5

Thus, the maximum possible score at the end of the game is 7.

Sample Input 2

2 2
1 2 1
2 1 1

Sample Output 2

inf

It is possible to increase the score indefinitely by alternating between vertex 1 and 2.

Sample Input 3

6 5
1 2 -1000000000
2 3 -1000000000
3 4 -1000000000
4 5 -1000000000
5 6 -1000000000

Sample Output 3

-5000000000