Charlie and the Chocolate Factory

All submissions for this problem are available.
Problem description:
Charlie Bucket is a kind and loving boy living in poverty with his parents and four bedridden grandparents.Down the street are Willy Wonka's ‘N’ chocolate factories & ‘M’ houses.
A Godown lying adjacent to the factories holds the raw materials for the factories. It is assumed that 1g of raw material produces 1 chocolate. The transfer of raw materials to the factories is to take place for ‘Q’ days.For each day, the factories lying between the two indices L and R(inclusive), are provided with ‘K’ grams of raw materials each.(Values of L and R could differ for each day)
The houses in the city are also believed to have supply of raw materials which they can transfer it to the factories and get chocolates in return.No other house could get the chocolates made from these raw materials, except the supplying one.
There are ‘E’ eligible factories which can transfer chocolates to households. For each of the eligible factories, the indices of houses for which it is allowed to provide chocolates and the additional amount of raw materials that the houses wish to provide them are listed.
After the raw materials’ retrieval gets completed , the process of transferring chocolates from factories to the houses take place. Due to some old custom, each factory could transfer its chocolates to at most one house and each house could get the chocolates from at most one factory.
Wonka announces a contest whereby children, who find the optimal arrangement, such that the sum of the chocolates delivered to all the houses is maximum, will be given a tour of the factories and win a chance to be presented with an unknown grand prize.
Help Charlie Bucket win the contest.
Input:
The first line contains an integer ‘T’ , the number of test cases. ‘T’ test cases follow :
The first line of each test case contains two integers: N,the number of factories and M,the number of houses.
The next line contains an integer ‘Q’,the number of days for which the transfer of raw materials take place.’Q’ lines follow:
Each of the ‘Q’ lines contains three integers : L ,R and K.
L and R : Indices of the factories
K : Amount of raw material in ‘grams’.
The next line contains an integer ‘E’, the number of eligible
factories.
For each of the ‘E’ factories :
The first line contains two integers ‘F’,the index of the eligible factory and ‘H’,the number of houses for which the ‘F’th factory is allowed to transfer chocolates.’H’ lines follow:
Each of the ‘H’ lines contains two integers ‘Hi’ and ‘R’, the index of the house and the amount of raw materials(in grams) the ‘Hi’th house provides ‘F’.
Constraints:
1<=T<=100
1<=N,M<=100
1<=Q<=5000
1<=L,R<=N
0<=E<=N
1<=F<=N
0<=H<=M
1<=Hi<=M
0<=K,R<=10,000
Output:
For each test case,print a single line: The maximum sum of the chocolates of all houses.
Example:
Input #1:
1
3 2
2
1 3 1
2 3 2
2
1 1
1 1
3 1
2 2
Output #1:
7
Author:  abacus_opc 
Tags  abacus_opc 
Date Added:  16022015 
Time Limit:  0.3 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.5, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 