Program time limit: 1 second
Program memory limit: 256 MB
Noah the studious guy is buying caterpillows(a special type of caterpillar that is soft like pillows) for his M ducklings.
There are N caterpillows to choose from, each with a different price $A_i$, but the data from his ship’s scanner is messy and unsorted.
Help Noah figure out which how much he have to spend to buy at least to buy M different caterpillows, so there is at least one for each of his ducklings.
If impossible, output -1.
You should read from standard input.
In Python, you could use the following code.
N, M = map(int, input().split())
A = list(map(int, input().split()))
In C or C++, you could use the following code.
const int LEN = 1e5+10;
int N, M;
scanf("%d%d", &N, &M);
int a[LEN];
for (int i = 0; i < N; i++) {
scanf("%d", &a[i]);
}
For all test cases:
Additionally:
one integer, the least amount of money.
You should write to standard output.
In Python, you could use the line print(answer).
In C or C++, you could use the line printf("%d\n", answer);.
5 2
6 7 4 1 2
3
Answer is optimal if Noah buys the last two caterpillows.
5 6
6 7 4 1 2
-1
There are not enough caterpillows! Noah will sadly fail to feed all his ducklings.
For each subtasks (worth $50\%$, and $50\%$ of points, as per the Constraints section), your program will be run on multiple secret test cases one after another, and if it produces the correct output for all test cases, it solves that subtask. Your program will receive the points for each subtask it solves. Recall that your final score on the task is the score of your highest scoring submission.
The contest ended at 8:16 p.m. AEST on Thursday 2nd July. All submissions will be unofficial .