Trait Growth Leap
Time limit1sMemory limit1024 MB
Given N trait levels, each trait's item count, and M Growth Leaps, find the highest level T that every trait can reach.
- Level
Medium6 of 10
- Topics
- Math, Binary search, Greedy
- Solved
- No attempts yet
Problem
'MapleStory' has a system called 'traits'. Raising the levels of traits such as 'Charisma', 'Charm', and 'Craftsmanship' lets you build up the abilities assigned to each trait. Jihun has played this game for a long time, so every trait on his main character is at max level. When all trait levels are equal, the trait graph becomes a regular polygon, as in the picture below.

A trait level is raised with 'trait items'. To raise any trait from level to level , you need of that trait's items. Alternatively,
the special trait item 'Growth Leap' raises the level of any chosen trait by exactly . A trait at max level cannot use a trait item, so Jihun decided to move his spare trait items to storage and use them to raise trait levels on another character.
The character that will use the trait items has traits, and the traits have no max level, so their levels can keep rising. Jihun also likes large regular polygons, so he wants the trait graph of this character to be the largest regular -gon possible. Given the number of Growth Leaps , the current level of each trait, and the number of trait items for each trait, find the size of the largest regular -gon Jihun can make, that is, the highest level at which a regular -gon can be formed.
Input
The first line contains two integers and , separated by a space. (; )
The second line contains integers , separated by spaces, giving the current level of each trait. ()
The third line contains integers , separated by spaces, giving the number of trait items for each trait. ()
Output
Print the size of the largest regular -gon Jihun can make, that is, the highest level at which a regular -gon can be formed.
If no regular polygon can be formed, print -1.
Hint
Log in to MapleStory and look for the hidden item in this problem!