atcoder#ASAPOROA. 魔法使い高橋君
魔法使い高橋君
Score : points
Problem Statement
Takahashi is a magician. He can cast a spell on an integer sequence with terms, to turn it into another sequence , where is the sum of the first terms in the original sequence.
One day, he received integer sequences, each with terms, and named those sequences . He will try to cast the spell on those sequences so that will hold, where sequences are compared lexicographically. Let the action of casting the spell on a selected sequence be one cast of the spell. Find the minimum number of casts of the spell he needs to perform in order to achieve his objective.
Here, for two sequences with terms each, holds lexicographically if and only if there exists such that and .
Constraints
- Let the -th term in be , then .
Partial Scores
- In the test set worth points, Takahashi can achieve his objective by at most casts of the spell, while keeping the values of all terms at most .
- In the test set worth another points, .
Input
The input is given from Standard Input in the following format:
…
…
:
…
Output
Print the minimum number of casts of the spell Takahashi needs to perform. If he cannot achieve his objective, print -1
instead.
3 3
2 3 1
2 1 2
2 6 3
1
Takahashi can achieve his objective by casting the spell on once to turn it into .
3 3
3 2 10
10 5 4
9 1 9
-1
In this case, Takahashi cannot achieve his objective by casting the spell any number of times.
5 5
2 6 5 6 9
2 6 4 9 10
2 6 8 6 7
2 1 7 3 8
2 1 4 8 3
11