This page is still under construction.

Parts of this page are still being built. What you see may change.

Mooo Moo

Time limit1sMemory limit128 MB

Summary
Find the fewest cows whose breed volumes explain the recorded volumes when each field spills its total minus one into the next field.
Level

Medium6 of 10

Topics
Dynamic programming, Greedy
Solved
No attempts yet

Problem

Farmer John forgot how many cows he owns. He placed microphones in NN fields along a road to estimate cow counts from mooing volume.

Breed ii moos at volume V(i)V(i). Wind blows left to right: if a field's total volume is XX, the next field gains max(0,X−1)max(0, X-1) from carryover.

Given recorded total volumes, find the minimum possible number of cows, or −1-1 if impossible.

Input

Line 1: NN, BB.

Next BB lines: V(i)V(i).

Next NN lines: recorded volume in each field.

Output

Minimum number of cows, or −1-1.

Examples1

  1. Example 1

    Input
    5 2
    5
    7
    0
    17
    16
    20
    19
    
    Expected output
    4