Revolving Sushi
Time limit2sMemory limit1024 MB
Given N setters with sushi counts and N+1 rotating cooks, find the minute when every setter has eaten all leftover sushi in sets of K, or -1 if never.
- Level
Medium6 of 10
- Topics
- Simulation, Math, Implementation
- Solved
- No attempts yet
Problem
The UCPC city has the most famous conveyor belt sushi restaurant in the world. Its name is "the restaurant where everything revolves"! The restaurant became popular on social media for its unusual concept: the cooks and the guests all revolve. The UCPC problem setters booked a table months ago, and today they finally visited.
A total of problem setters visited the restaurant, so the kitchen prepared cooks to match. The -th cook can make pieces of sushi per minute. At the start, the waiter seated the problem setters in a row and gave pieces of sushi to the -th setter. Then the following actions repeated in order every minute.
- For every with , the cook at position makes sushi for the setter at position . The amount made equals the number of pieces that cook can make per minute. The cook at position makes no sushi and rests.
- The cooks rotate once. That is, the cook originally at position 1 moves to position 2, the cook originally at position 2 moves to position 3, ..., and the cook originally at position moves to position 1.
- The setters rotate once. That is, the setter originally at position 1 moves to position 2, the setter originally at position 2 moves to position 3, ..., and the setter originally at position moves to position 1.
Each setter eats one set of sushi as soon as they have one set gathered. A set is a bundle of pieces, regardless of type. Time spent eating is ignored.
The setters hate wasting food, so they want to keep eating until everyone has 0 pieces left. How many minutes after the meal starts will the meal end?
Input
The first line contains integers and , separated by a space. (, )
The second line contains , separated by spaces. ()
The third line contains , separated by spaces. ()
Output
Print the number of minutes it takes the setters to finish the meal. If the setters never finish the meal, even after infinite time, print -1 instead.