

GGSDDUTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1293 Accepted Submission(s): 628 Problem Description Do you think this is a strange problem name? That is because you don't know its full name'Good Good Study and Day Day Up!". Very famous sentence! Isn't it? Now "GGSDDU" is lzqxh's target! He has N courses and every course is divided into a plurality of levels. Just like College English have Level 4 and Level 6. To simplify the problem, we suppose that the ith course has Levels from level 0 to level a[i]. And at the beginning, lzqxh is at Level 0 of every course. Because his target is "GGSDDU", lzqxh wants to reach the highest Level of every course. Fortunately, there are M tutorial classes. The ith tutoial class requires that students must reach at least Level L1[i] of course c[i] before class begins. And after finishing the ith tutorial class, the students will reach Level L2[i] of course d[i]. The ith tutoial class costs lzqxh money[i]. For example, there is a tutorial class only students who reach at least Level 5 of "Tiyu" can apply. And after finishing this class, the student's "MeiShu" will reach Level 10 if his "MeiShu"'s Level is lower than 10. (Don't ask me why! Supernatural class!!!") Now you task is to help lzqxh to compute the minimum cost! Input The input contains multiple test cases. The first line of each case consists of two integers, N (N<=50) and M (M<=2000). The following line contains N integers, representing a[1] to a[N]. The sum of a[1] to a[N] will not exceed 500. The next M lines, each have five integers, indicating c[i], L1[i], d[i], L2[i] and money[i] (1<=c[i], d[i]<=N, 0<=L1[i]<=a[c[i]], 0<=L2[i]<=a[d[i]], money[i]<=1000) for the ith tutorial class. The courses are numbered from 1 to N. The input is terminated by N = M = 0. Output Output the minimum cost for achieving lzqxh's target in a line. If his target can't be achieved, just output 1. Sample Input
Sample Output
Author SYSU Source  
