Problem 01A. Maximum Subarray Sum ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array of n integers a0, a1, …, an − 1, some of which may be negative. Among all non-empty contiguous blocks al, al + 1, …, ar (with 0 ≤ l ≤ r ≤ n − 1), find the largest possible value of the sum al + al + 1 + …  + ar.

If every element is negative, the answer is the single largest element, because the block must be non-empty.

Input format

The first line contains one integer n — the length of the array. The second line contains n space-separated integers a0… an − 1.

Output format

Print one integer — the maximum sum of a non-empty contiguous block.

Constraints

1 ≤ n ≤ 2 ⋅ 105

 − 109 ≤ ai ≤ 109

The answer fits into a signed 64-bit integer.

Sample tests

No. Standard input Standard output
1
9
-2 1 -3 4 -1 2 1 -5 4
6
2
1
-5
-5
3
5
-1 -2 -3 -4 -5
-1

Problem 01B. Split the Array ≡

Author:Pavel Mesenev   Time limit:5 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array of n positive integers a0, a1, …, an − 1 and an integer k. Cut the array into exactly k non-empty contiguous parts: choose k − 1 split points so that every element belongs to exactly one part and the parts appear in the original order.

The cost of a cut is the largest part sum among the k parts. Find the minimum possible cost over all valid cuts.

Input format

The first line contains two integers n and k. The second line contains n space-separated integers a0… an − 1.

Output format

Print one integer — the minimum possible value of the largest part sum.

Constraints

1 ≤ k ≤ n ≤ 2 ⋅ 105

1 ≤ ai ≤ 109

The answer fits into a signed 64-bit integer.

Sample tests

No. Standard input Standard output
1
5 2
7 2 5 10 8
18
2
5 2
1 2 3 4 5
9
3
3 3
1 4 4
4

Problem 01C. Best Subarray Through a Point ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array of n integers a0, a1, …, an − 1, some of which may be negative, and q independent queries. Each query is an index p (0 ≤ p ≤ n − 1).

For a query p, consider every non-empty contiguous block al, …, ar that contains position p, that is l ≤ p ≤ r. Output the largest possible value of al + al + 1 + …  + ar among those blocks.

Input format

The first line contains two integers n and q. The second line contains n space-separated integers a0… an − 1. The third line contains q space-separated integers — the query indices.

Output format

Print q lines; line j is the answer to the j-th query.

Constraints

1 ≤ n ≤ 2 ⋅ 105

1 ≤ q ≤ 2 ⋅ 105

 − 109 ≤ ai ≤ 109

Every answer fits into a signed 64-bit integer.

Sample tests

No. Standard input Standard output
1
9 3
-2 1 -3 4 -1 2 1 -5 4
6 0 8
6
2
5
2
1 1
-5
0
-5
3
5 3
-1 -2 -3 -4 -5
0 2 4
-1
-3
-5

Problem 02A. Count Smaller Numbers After Self ≡

Author:Pavel Mesenev   Time limit:3 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array of n integers a0, a1, …, an − 1. For every index i, compute ci = \#{ j: j > i and aj < ai }, the number of elements to the right of position i that are strictly smaller than ai.

Input format

The first line contains one integer n. The second line contains n space-separated integers a0… an − 1.

Output format

Print n space-separated integers c0 c1 … cn − 1.

Constraints

1 ≤ n ≤ 2 ⋅ 105

 − 109 ≤ ai ≤ 109

Sample tests

No. Standard input Standard output
1
4
5 2 6 1
2 1 1 0
2
5
1 2 3 4 5
0 0 0 0 0
3
4
4 3 2 1
3 2 1 0

Problem 02B. Significant Reverse Pairs ≡

Author:Pavel Mesenev   Time limit:3 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array of n integers a0, a1, …, an − 1, possibly negative. A significant reverse pair is a pair of indices i < j with ai > 2 ⋅ aj.

Count the significant reverse pairs.

Input format

The first line contains one integer n. The second line contains n space-separated integers a0… an − 1.

Output format

Print one integer — the number of pairs i < j with ai > 2 ⋅ aj.

Constraints

1 ≤ n ≤ 2 ⋅ 105

 − 109 ≤ ai ≤ 109

The answer fits into a signed 64-bit integer.

Sample tests

No. Standard input Standard output
1
5
1 3 2 3 1
2
2
2
5 -1
1
3
4
4 3 2 1
2

Problem 02C. Counting Inversions ≡

Author:Pavel Mesenev   Time limit:3 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array of n integers a0, a1, …, an − 1. An inversion is a pair of indices i < j with ai > aj — a pair that appears out of order.

Count the inversions. A sorted array has 0; a strictly decreasing array has binomn2; the count equals the number of adjacent swaps bubble sort would make.

Input format

The first line contains one integer n. The second line contains n space-separated integers a0… an − 1.

Output format

Print one integer — the number of inversions.

Constraints

1 ≤ n ≤ 2 ⋅ 105

 − 109 ≤ ai ≤ 109

The answer fits into a signed 64-bit integer.

Sample tests

No. Standard input Standard output
1
8
5 2 6 1 3 4 8 7
9
2
5
1 2 3 4 5
0
3
4
4 3 2 1
6

Problem 03A. Aggressive Cows ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

There are n stalls at distinct integer positions a0, a1, …, an − 1 on a line, and C cows. Place the cows into C distinct stalls so that the minimum distance between any two cows is as large as possible.

Output that largest possible minimum distance.

Input format

The first line contains two integers n and C. The second line contains n distinct integers a0… an − 1 — the stall positions, in arbitrary order.

Output format

Print one integer — the maximum achievable minimum distance between two cows.

Constraints

2 ≤ C ≤ n ≤ 105

0 ≤ ai ≤ 109, all ai distinct

Sample tests

No. Standard input Standard output
1
6 3
1 2 5 8 9 15
7
2
5 3
1 2 8 4 9
3
3
2 2
10 0
10

Problem 03B. Rank Queries ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array of n integers a0, a1, …, an − 1 and q queries. Each query is an integer x; answer with the number of array elements that are strictly less than x.

Input format

The first line contains two integers n and q. The second line contains n integers a0… an − 1. The third line contains q integers — the queries.

Output format

Print q lines; line j is the answer to the j-th query.

Constraints

1 ≤ n ≤ 105

1 ≤ q ≤ 105

 − 109 ≤ ai ≤ 109,   − 109 ≤ x ≤ 109

Sample tests

No. Standard input Standard output
1
5 3
5 1 4 1 3
4 1 100
3
0
5
2
1 2
7
7 8
0
1
3
6 4
2 2 2 2 2 2
1 2 3 2
0
0
6
0

Problem 03C. Minimum Eating Speed ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

There are n piles of bananas; pile i has ai bananas. There are H hours available. If the eating speed is k bananas per hour, then in one hour one pile is chosen and min(k, remaining in that pile) bananas are eaten; a pile that is finished early does not carry the spare time over. Pile i therefore takes ⌈ ai / k⌉ hours.

Find the smallest integer speed k ≥ 1 for which all piles can be finished within H hours, i.e. ∑i⌈ ai / k⌉  ≤ H.

Input format

The first line contains two integers n and H. The second line contains n integers a0… an − 1.

Output format

Print one integer — the minimum eating speed.

Constraints

1 ≤ n ≤ 105

n ≤ H ≤ 109

1 ≤ ai ≤ 109

Sample tests

No. Standard input Standard output
1
4 8
3 6 7 11
4
2
3 3
30 11 23
30
3
1 2
1000000000
500000000

Problem 04A. Browser History ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are simulating a browser's history. The browser starts on the homepage, page 0. You are given n operations, each one of:

After every BACK and FORWARD operation, output the page you land on. VISIT produces no output.

Input format

The first line contains one integer n — the number of operations. Each of the next n lines is one operation in the format above.

Output format

For every BACK or FORWARD operation, print the resulting current page on its own line, in order.

Constraints

1 ≤ n ≤ 2 ⋅ 105

1 ≤ x ≤ 109 for VISIT x

1 ≤ k ≤ 109 for BACK k / FORWARD k

Sample tests

No. Standard input Standard output
1
10
VISIT 1
VISIT 2
VISIT 3
BACK 1
BACK 1
FORWARD 1
VISIT 4
FORWARD 2
BACK 2
BACK 7
2
1
2
4
1
0
2
1
BACK 5
0
3
2
VISIT 100
FORWARD 3
100

Problem 04B. Queue from Two Stacks ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

Implement a FIFO queue supporting n operations of two kinds:

It is guaranteed that POP is never called on an empty queue. You must use only a Stack ADT (push / pop / top from one end) as your underlying storage — no deque, no list indexing from the front, no built-in queue.

Input format

The first line contains one integer n — the number of operations. Each of the next n lines is either PUSH x or POP.

Output format

For each POP, print the removed value on its own line, in order.

Constraints

1 ≤ n ≤ 2 ⋅ 105

1 ≤ x ≤ 109

Sample tests

No. Standard input Standard output
1
8
PUSH 1
PUSH 2
POP
PUSH 3
POP
POP
PUSH 4
POP
1
2
3
4
2
2
PUSH 42
POP
42
3
6
PUSH 1
PUSH 2
PUSH 3
POP
POP
POP
1
2
3

Problem 04C. Sliding Window Sum ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

A stream of n integers a0, a1, …, an − 1 arrives one at a time, together with a fixed window size k (1 ≤ k ≤ n), known in advance. For every position i with i ≥ k − 1, report the sum of the last k values seen so far: ai − k + 1 + ai − k + 2 + …  + ai.

Input format

The first line contains two integers n and k. The second line contains n space-separated integers a0… an − 1.

Output format

Print n − k + 1 lines: the sliding-window sum for i = k − 1,k,…,n − 1, in order.

Constraints

1 ≤ k ≤ n ≤ 2 ⋅ 105

 − 109 ≤ ai ≤ 109

Every printed sum fits into a signed 64-bit integer.

Sample tests

No. Standard input Standard output
1
8 3
1 2 3 4 5 6 7 8
6
9
12
15
18
21
2
5 1
-1 2 -3 4 -5
-1
2
-3
4
-5
3
4 4
10 -10 10 -10
0

Problem 05A. Range Sum Queries ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array a0, a1, …, an − 1 of integers and q queries. Each query is a pair (l, r) with 0 ≤ l ≤ r ≤ n − 1; answer with al + al + 1 + …  + ar.

Input format

The first line contains two integers n and q. The second line contains n space-separated integers a0… an − 1. Each of the next q lines contains two integers l and r.

Output format

Print q lines: the answer to each query, in order.

Constraints

1 ≤ n, q ≤ 2 ⋅ 105

 − 109 ≤ ai ≤ 109

Every answer fits into a signed 64-bit integer.

Sample tests

No. Standard input Standard output
1
6 3
1 2 3 4 5 6
0 2
3 5
0 5
6
15
21
2
5 2
-3 -1 4 -2 5
1 3
0 0
1
-3

Problem 05B. Submatrix Sum Queries ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an n × m grid of integers and q queries. Each query is a rectangle given by its top-left corner (r1, c1) and bottom-right corner (r2, c2) (0 ≤ r1 ≤ r2 ≤ n − 1, 0 ≤ c1 ≤ c2 ≤ m − 1); answer with the sum of every cell in that rectangle, inclusive.

Input format

The first line contains three integers n, m and q. Each of the next n lines contains m space-separated integers — the grid. Each of the next q lines contains four integers r1, c1, r2, c2.

Output format

Print q lines: the sum of each query rectangle, in order.

Constraints

1 ≤ n, m, \ n ⋅ m ≤ 4 ⋅ 105

1 ≤ q ≤ 2 ⋅ 105

 − 109 ≤ grid[i][j] ≤ 109

Every answer fits into a signed 64-bit integer.

Sample tests

No. Standard input Standard output
1
3 4 3
1 2 3 4
5 6 7 8
9 10 11 12
0 0 1 1
1 1 2 3
0 0 2 3
14
54
78
2
1 1 1
7
0 0 0 0
7
3
2 2 1
-1 -2
-3 -4
0 0 1 1
-10

Problem 05C. Product of Array Except Self ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array a0, a1, …, an − 1 of integers, which may include zero and negative values. For every index i, output the product of every element except ai (the product of an empty set of factors, when n = 1, is 1).

You may not use division.

Input format

The first line contains one integer n. The second line contains n space-separated integers a0… an − 1.

Output format

Print n space-separated integers: the answer for each index, in order.

Constraints

1 ≤ n ≤ 800

 − 50 ≤ ai ≤ 50

Sample tests

No. Standard input Standard output
1
4
1 2 3 4
24 12 8 6
2
5
-1 1 0 -3 3
0 0 9 0 0
3
1
7
1

Problem 06A. Container With Most Water ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given n non-negative integers h0, h1, …, hn − 1, where hi is the height of a vertical line drawn at position i. Choose two lines i < j; together with the x-axis they form a container that can hold (j − i) ⋅ min(hi,hj) units of water. Find the maximum amount of water any pair of lines can hold.

Input format

The first line contains one integer n. The second line contains n space-separated integers h0… hn − 1.

Output format

Print one integer — the maximum water any pair of lines can hold.

Constraints

2 ≤ n ≤ 2 ⋅ 105

0 ≤ hi ≤ 109

Sample tests

No. Standard input Standard output
1
9
1 8 6 2 5 4 8 3 7
49
2
2
1 1
1
3
4
1 2 1 2
4

Problem 06B. Next Greater Element ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array a0, a1, …, an − 1 of integers. For every index i, find the first index j > i with aj > ai, and report aj. If no such j exists, report  − 1.

Input format

The first line contains one integer n. The second line contains n space-separated integers a0… an − 1.

Output format

Print n space-separated integers: the answer for each index, in order.

Constraints

1 ≤ n ≤ 2 ⋅ 105

1 ≤ ai ≤ 109

Sample tests

No. Standard input Standard output
1
6
2 1 2 4 3 1
4 2 4 -1 -1 -1
2
4
4 3 2 1
-1 -1 -1 -1
3
1
5
-1

Problem 06C. Trapping Rain Water ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given n non-negative integers h0, h1, …, hn − 1 representing an elevation map, each bar having width 1. Compute how much water it can trap after raining.

Input format

The first line contains one integer n. The second line contains n space-separated integers h0… hn − 1.

Output format

Print one integer — the total trapped water.

Constraints

1 ≤ n ≤ 2 ⋅ 105

0 ≤ hi ≤ 106

Sample tests

No. Standard input Standard output
1
12
0 1 0 2 1 0 1 3 2 1 2 1
6
2
6
4 2 0 3 2 5
9
3
3
1 1 1
0

Problem 07A. Sliding Window Maximum ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array a0, a1, …, an − 1 of integers and a window size k. For every contiguous window of size k, output the maximum value in that window. There are n − k + 1 windows in total.

Input format

The first line contains two integers n and k. The second line contains n space-separated integers a0… an − 1.

Output format

Print n − k + 1 space-separated integers: the maximum of each window, in order.

Constraints

1 ≤ k ≤ n ≤ 2 ⋅ 105

 − 109 ≤ ai ≤ 109

Sample tests

No. Standard input Standard output
1
8 3
1 3 -1 -3 5 3 6 7
3 3 5 5 6 7
2
1 1
5
5
3
5 5
4 4 4 4 4
4

Problem 07B. Window Range ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array a0, a1, …, an − 1 of integers and a window size k. For every contiguous window of size k, output max(window) − min(window).

Input format

The first line contains two integers n and k. The second line contains n space-separated integers a0… an − 1.

Output format

Print n − k + 1 space-separated integers: the range of each window, in order.

Constraints

1 ≤ k ≤ n ≤ 2 ⋅ 105

 − 109 ≤ ai ≤ 109

Sample tests

No. Standard input Standard output
1
8 3
1 3 -1 -3 5 3 6 7
4 6 8 8 3 4
2
1 1
5
0
3
5 5
4 4 4 4 4
0

Problem 07C. Shortest Subarray with Sum at Least K ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given an array a0, a1, …, an − 1 of integers (may include negatives) and an integer k. Find the length of the shortest non-empty contiguous subarray whose sum is  ≥ k, or report  − 1 if no such subarray exists.

Input format

The first line contains two integers n and k. The second line contains n space-separated integers a0… an − 1.

Output format

Print one integer: the shortest length, or  − 1.

Constraints

1 ≤ n ≤ 2 ⋅ 105

 − 1000 ≤ ai ≤ 1000

 − 109 ≤ k ≤ 109

Sample tests

No. Standard input Standard output
1
1 1
1
1
2
2 4
1 2
-1
3
3 3
2 -1 2
3

Problem 08A. Kth Largest Element in a Stream ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

Values arrive one at a time (n of them), and you are given k. After each arrival, once at least k values have arrived, report the k-th largest value seen so far. Arrivals before the k-th produce no output.

Input format

The first line contains two integers n and k. The second line contains n space-separated integers — the values, in arrival order.

Output format

Print one line per arrival from the k-th onward: the k-th largest value seen so far at that point.

Constraints

1 ≤ k ≤ n ≤ 2 ⋅ 105

 − 109 ≤ value ≤ 109

Sample tests

No. Standard input Standard output
1
6 2
4 5 8 2 3 5
4
5
5
5
5
2
3 1
7 2 9
7
7
9

Problem 08B. Merge K Sorted Arrays ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

You are given k arrays, each already sorted in non-decreasing order (an array may be empty). Output all elements from all arrays, merged into one non-decreasing sequence.

Input format

The first line contains one integer k. Then, for each of the k arrays: one line with its length m, followed by a line with m space-separated integers (the line may be empty if m = 0).

Output format

Print all elements, merged and sorted, space-separated on one line (an empty line if there are none).

Constraints

1 ≤ k ≤ 2 ⋅ 105

0 ≤  total number of elements across all arrays  ≤ 2 ⋅ 105

 − 109 ≤ element ≤ 109

Sample tests

No. Standard input Standard output
1
3
3
1 4 5
3
1 3 4
2
2 6
1 1 2 3 4 4 5 6
2
1
4
1 2 3 4
1 2 3 4

Problem 08C. Find Median from Data Stream ≡

Author:Pavel Mesenev   Time limit:2 sec
Input file:Standard input   Memory limit:256 Mb
Output file:Standard output  

Statement

Values arrive one at a time (n of them). After each arrival, report the median of all values seen so far. If the count so far is even, report the lower of the two middle values (this keeps every answer an integer).

Input format

The first line contains one integer n. The second line contains n space-separated integers — the values, in arrival order.

Output format

Print n lines: the median after each arrival.

Constraints

1 ≤ n ≤ 2 ⋅ 105

 − 109 ≤ value ≤ 109

Sample tests

No. Standard input Standard output
1
4
5 15 1 3
5
5
5
3
2
1
7
7

Problem 99A. Митинг (две характеристики) ≡

Author:mesenev   Time limit:2 sec
Input file:input.txt   Memory limit:256 Mb
Output file:output.txt  

Statement

The virtual student council has decided to hold a rally against freedom of speech. This raised the question of choosing a chairperson. The student council is full of people, yet there is no one to choose from. After deliberating, the virtual characters numbered themselves, and for the character numbered i defined the following numbers:

The larger the number, the higher the character's skill level in the corresponding field.

Character i is considered a candidate for chairperson if and only if there is no character j such that simultaneously mj > mi and pj > pi.

Write a program that compiles the list of candidates for chairperson.

Input file format

The input file contains a natural number n  — the number of characters. It is followed by n pairs of natural numbers mi pi.

Output file format

Output to the output file the numbers of the selected characters in increasing order.

Constraints

1 ≤ n ≤ 105

1 ≤ mi, pi ≤ 109

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
4
2 3 
1 1 
2 1 
3 2
1 4

Problem 99B. Митинг ≡

Author:Виртуальная реальность   Time limit:2 sec
Input file:input.txt   Memory limit:256 Mb
Output file:output.txt  

Statement

The virtual student council has decided to hold a rally against freedom of speech. This raised the question of choosing a chairperson. The student council is full of people, yet there is no one to choose from. After deliberating, the virtual characters numbered themselves, and for the character numbered i defined the following numbers:

The larger the number, the higher the character's skill level in the corresponding field.

Character i is considered a candidate for chairperson if and only if there is no character j such that simultaneously mj > mi, pj > pi and tj > ti.

Write a program that compiles the list of candidates for chairperson.

Input file format

The input file contains a natural number n  — the number of characters. It is followed by n triples of natural numbers mi pi ti.

Output file format

Output to the output file the numbers of the selected characters in increasing order.

Constraints

1 ≤ n ≤ 105

1 ≤ mi, pi, ti ≤ 109

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
4
2 3 4
1 1 3
2 1 8
3 2 10
1 4

Problem 99C. Aggressive Cows: General Pasture ≡

Author:Pavel Mesenev   Time limit:3 sec
Input file:Standard input   Memory limit:512 Mb
Output file:Standard output  

Statement

The same farmer as before, the same aggressive cows — but this time the n pastures are not stalls on a tidy line. They are connected by a tangle of fences, ditches and informal cow gossip, so all the farmer can tell you is a number di,j for every pair of pastures: "how far apart I'd call i and j". These numbers need not come from any map or line, and need not satisfy the triangle inequality.

Place C cows into C distinct pastures so that the minimum distance between any two cows is as large as possible.

Output that largest possible minimum distance.

Input format

The first line contains two integers n and C. Each of the next n lines contains n integers — row i of the distance matrix di,0… di,n − 1. The matrix is symmetric (di,j = dj,i); the diagonal entries di,i are present but irrelevant and may be ignored.

Output format

Print one integer — the maximum achievable minimum distance between two cows.

Constraints

2 ≤ C ≤ n ≤ 3000

0 ≤ di,j ≤ 109 for i ≠ j, di,j = dj,i

the matrix need not satisfy the triangle inequality

Sample tests

No. Standard input Standard output
1
4 2
0 5 1 1
5 0 1 1
1 1 0 5
1 1 5 0
5
2
5 3
0 2 2 1 1
2 0 2 1 1
2 2 0 1 1
1 1 1 0 2
1 1 1 2 0
2
3
2 2
0 7
7 0
7

Problem 99C. Colors of the Correspondence ≡

Author:П.Р. Месенёв   Time limit:6 sec
Input file:Standard input   Memory limit:512 Mb
Output file:Standard output  

Statement

No Message May Ever Go Undelivered.

Blazing Stars — the Judgements — incessantly erect the law of the universe with each deliverance, prescribing the possibility of existence for all through Correspondence.

This incinerating language is so hazardous to living witnesses that messages written in it must be insulated upon the hard carapace of the postmen — Messengers, whose holy destiny is to promulgate edicts of the Stars.

The Sacred Message must reach its audience in undimmed brightness. Every Correspondence sigil must preserve its distinctness within the shared immanence of the whole. Thus each postman is endowed with a palette fitted to its burden: Violant, Irrigo, Apocyan, Peligin, Viric, Cosmogone, Gant, and others with no distinctive names. Every additional color demands another commitment, one hardly spared in the service of the Gods.

Two sigils inscribed upon touching regions of the carapace must never share a color: should their hues merge, their meanings will merge also, and the law delivered will not be the law ordained.

Choose a sigil to change its hue. Violant and Apocyan answer this.

In the first example, two colors suffice.

The Messenger knows which message its carapace carries. The incarnation of the ink is an immediate problem — one it will gladly transfer to the algorithm you are about to write — because No Message May Ever Go Undelivered.

Input format

The first line contains n and m: the number of carapace regions and the number of touching pairs. Each of the following m lines contains a b, 1 ≤ a,b ≤ n: the regions with these numbers touch. No region touches itself, and no pair is listed twice.

Output format

Print one integer: the minimum number of colors required to deliver the Sacred Message unchanged.

Constraints

1 ≤ n ≤ 24, 0 ≤ m ≤ n(n − 1) / 2.

Sample tests

No. Standard input Standard output
1
4 3
1 2
2 3
3 4
2
2
3 3
1 2
1 3
2 3
3

0.418s 0.010s 70