Gems (GEM)
Time limit2sMemory limit512 MB
Given range-sum constraints modulo 10 on an array of N values each in [0,100], find the lexicographically smallest valid array or report -1 if the constraints conflict.
- Level
Medium7 of 10
- Topics
- Union-find, Prefix sum, Graph, Greedy
- Solved
- No attempts yet
Problem
Seonhong keeps a flower bed in the front yard. The bed is a row of plots numbered 1 to from the left, and every plot holds between 0 and 100 gems.
One day Seonhong heard a rumor that gems might be buried in the bed and decided to check with a metal detector. Given two numbers and , the detector sweeps plot through plot and reports the total number of gems buried in that range.
Seonhong swept the bed times and wrote down every result. Yeongmin walked by, tampered with the notes for fun, and left only the last digit of each result. Seonhong then told Yeongmin to recover the gem count of every plot from those digits alone. Help Yeongmin and find counts that do not contradict the notes.
Input
The first line has the number of plots and the number of sweeps , separated by a space. (, )
Each of the next lines has , and separated by spaces. Sweep covered the range , and the last digit of the total number of gems in that range was . (, )
Output
Print the gem count of plot 1 through plot on one line, separated by spaces. Every value must be an integer between 0 and 100, and all sweeps must come out right.
If several arrays satisfy the sweeps, print only the lexicographically smallest one. Array is lexicographically smaller than array when at the first position where the two arrays differ.
If the notes contradict each other and no array works, print -1 alone.