Under construction

Still being built. Things may change

Yonsei Mask Factory

Time limit1sMemory limit1024 MB

Summary
Assign each one-way passage a flow between its minimum s_i and maximum e_i so every site's net flow matches p_i, or report that it is impossible.
Level

Medium6 of 10

Topics
Graph, BFS, DFS
Solved
No attempts yet

Problem

Yonsei University built a mask factory to prepare for COVID-19.

The factory has NN production sites and MM one-way passages connecting them. There is also an underground space that sends masks to each site or receives masks from it. No two distinct sites are connected by more than one passage, and no site is connected to itself.

Each site ii has an integer pip_i. If pip_i is positive, the site receives pip_i masks per second from the underground space. If pip_i is negative, the site sends −pi-p_i masks per second to the underground space. Each passage ii is given a minimum number of masks sis_i and a maximum number of masks eie_i that may be sent per second.

The production sites and the underground space must keep the number of masks they hold unchanged every second.

Find how many masks each passage must carry per second under these conditions.

Input

The input is given as follows.

N M
p_1 ... p_N
u_1 v_1 s_1 e_1
...
u_M v_M s_M e_M

Output

On the first line, print 1 if a valid answer exists and -1 if it does not.

If a valid answer exists, print MM lines. The ii-th line contains the number of masks passage ii carries per second. If several answers exist, print any one of them.

Constraints

  • 1≤N≤2001 \le N \le 200.
  • 0≤M≤min⁡(N(N−1)2,1000)0 \le M \le \min(\frac{N(N-1)}{2},1000)
  • −10,000,000×max⁡(min⁡(N−1,M),1)≤pi≤10,000,000×max⁡(min⁡(N−1,M),1)-10{,}000{,}000 \times \max(\min(N-1, M), 1) \le p_i \le 10{,}000{,}000 \times \max(\min(N-1, M), 1)
  • 1≤ui,vi≤N1 \le u_i, v_i \le N, ui≠viu_i \ne v_i (1≤i≤M1 \le i \le M)
  • 0≤si≤ei≤10,000,0000 \le s_i \le e_i \le 10{,}000{,}000 (1≤i≤M1 \le i \le M)

Examples5

  1. Example 1

    Input
    6 6
    -2 2 -2 2 -2 2
    1 2 2 4
    2 3 4 6
    3 4 3 5
    4 5 5 7
    5 6 1 3
    6 1 3 5
    
    Expected output
    1
    3
    5
    3
    5
    3
    5
    
  2. Example 2

    Input
    6 6
    -2 2 -2 2 -2 2
    1 2 2 4
    2 3 4 6
    3 4 3 5
    4 5 5 7
    5 6 1 2
    6 1 3 5
    
    Expected output
    -1
    
  3. Example 3

    Input
    1 0
    0
    
    Expected output
    1
    
  4. Example 4

    Input
    1 0
    1
    
    Expected output
    -1
    
  5. Example 5

    Input
    5 5
    0 0 0 0 0
    1 2 0 0
    2 3 0 0
    3 4 0 0
    4 5 0 0
    5 1 0 0
    
    Expected output
    1
    0
    0
    0
    0
    0