fork download
  1. import java.io.DataInputStream;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.util.Arrays;
  6. import java.util.Comparator;
  7.  
  8.  
  9. class ASHIGIFT {
  10. static long dispop[][],X;
  11. public static void main(String[] args) throws IOException {
  12. // TODO Auto-generated method stub
  13. Reader r=new Reader();
  14. int t=r.nextInt();
  15. StringBuilder sb=new StringBuilder();
  16. while(t-->0){
  17. X=r.nextInt();
  18. int B=r.nextInt();
  19. dispop=new long[B+10000][3];
  20. int i=0;
  21. long limit=0;
  22. for(;i<B;i++){
  23. dispop[i][0]=r.nextLong();
  24. dispop[i][1]=r.nextLong();
  25. limit+=dispop[i][1];
  26. dispop[i][2]=-1;
  27. }
  28. int C=r.nextInt();
  29. for(int j=0;j<C;j++,i++){
  30. dispop[i][0]=r.nextLong();
  31. dispop[i][2]=r.nextLong();
  32. dispop[i][1]=r.nextLong();
  33. }
  34. if(C==0){sb.append((limit+1)+"\n");continue;}
  35.  
  36. Arrays.sort(dispop, 0, B+C, new Comparator<long []>() {
  37.  
  38. @Override
  39. public int compare(long[] o1, long[] o2) {
  40. // TODO Auto-generated method stub
  41. if(o1[0]==o2[0])return Integer.valueOf((int) (o2[2]-o1[2]));
  42. return Integer.valueOf((int) (o1[0]-o2[0]));
  43. }
  44. });
  45.  
  46. /*for( i=0;i<B+C;i++){
  47. System.out.println(dispop[i][0]+" "+dispop[i][1]+" "+dispop[i][2]+"\n");
  48. }
  49. */
  50. long ans=binarySearch(limit+1);
  51. sb.append(ans+"\n");
  52. }
  53. pr.write(sb.toString());
  54. pr.flush();
  55. pr.close();}
  56. private static long binarySearch(long X){
  57. long low=1,high=X,mid=(low+((high-low)>>1));
  58. while(low<high){
  59. mid=(low+((high-low)>>1));
  60. //System.out.println(mid);
  61. if(findmin(mid,X-1)){
  62. high=mid;
  63. //System.out.println("True\n");
  64. }
  65. else {
  66. low=mid+1;
  67. //System.out.println("False\n");
  68. }
  69. }
  70. mid=(low+((high-low)>>1));
  71. return mid;
  72. }
  73. private static boolean findmin(long p,long m){
  74. for(int i=0;i<dispop.length;i++){
  75. if(dispop[i][0]==dispop[i][1] && dispop[i][1]==dispop[i][2] && dispop[i][2]==0)break;
  76. if(p>=dispop[i][2] && dispop[i][2]!=-1){
  77. p+=dispop[i][1];
  78. }
  79. else if(dispop[i][2]==-1){
  80. p-=dispop[i][1];
  81. m-=dispop[i][1];
  82. if(p<=0)return false;
  83. }
  84. else if(m>=p)return false;
  85. // System.out.println(p+" "+m);
  86. }
  87.  
  88. return true;
  89. }
  90. static class Reader {
  91. final private int BUFFER_SIZE = 1 << 16;
  92. private DataInputStream din;
  93. private byte[] buffer;
  94. private int bufferPointer, bytesRead;
  95.  
  96. public Reader() {
  97. din = new DataInputStream(System.in);
  98. buffer = new byte[BUFFER_SIZE];
  99. bufferPointer = bytesRead = 0;
  100. }
  101.  
  102. public Reader(String file_name) throws IOException {
  103. din = new DataInputStream(new FileInputStream(file_name));
  104. buffer = new byte[BUFFER_SIZE];
  105. bufferPointer = bytesRead = 0;
  106. }
  107.  
  108. public String readLine() throws IOException {
  109. byte[] buf = new byte[64];
  110. int cnt = 0, c;
  111. while ((c = read()) != -1) {
  112. if (c == '\n')
  113. break;
  114. buf[cnt++] = (byte) c;
  115. }
  116. return new String(buf, 0, cnt);
  117. }
  118.  
  119. public int nextInt() throws IOException {
  120. int ret = 0;
  121. byte c = read();
  122. while (c <= ' ')
  123. c = read();
  124. boolean neg = (c == '-');
  125. if (neg)
  126. c = read();
  127. do {
  128. ret = ret * 10 + c - '0';
  129. } while ((c = read()) >= '0' && c <= '9');
  130. if (neg)
  131. return -ret;
  132. return ret;
  133. }
  134.  
  135. public long nextLong() throws IOException {
  136. long ret = 0;
  137. byte c = read();
  138. while (c <= ' ')
  139. c = read();
  140. boolean neg = (c == '-');
  141. if (neg)
  142. c = read();
  143. do {
  144. ret = ret * 10 + c - '0';
  145. } while ((c = read()) >= '0' && c <= '9');
  146. if (neg)
  147. return -ret;
  148. return ret;
  149. }
  150.  
  151. public double nextDouble() throws IOException {
  152. double ret = 0, div = 1;
  153. byte c = read();
  154. while (c <= ' ')
  155. c = read();
  156. boolean neg = (c == '-');
  157. if (neg)
  158. c = read();
  159. do {
  160. ret = ret * 10 + c - '0';
  161. } while ((c = read()) >= '0' && c <= '9');
  162. if (c == '.')
  163. while ((c = read()) >= '0' && c <= '9')
  164. ret += (c - '0') / (div *= 10);
  165. if (neg)
  166. return -ret;
  167. return ret;
  168. }
  169.  
  170. private void fillBuffer() throws IOException {
  171. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  172. if (bytesRead == -1)
  173. buffer[0] = -1;
  174. }
  175.  
  176. private byte read() throws IOException {
  177. if (bufferPointer == bytesRead)
  178. fillBuffer();
  179. return buffer[bufferPointer++];
  180. }
  181.  
  182. public void close() throws IOException {
  183. if (din == null)
  184. return;
  185. din.close();
  186. }
  187. }
  188. }
  189. /*
  190. 1
  191. 10
  192. 6 1 3 2 4 3 1 5 3 6 3 8 7
  193. 3 1 2 3 4 5 6 7 8 9
  194. */
  195. /*
  196. 1
  197. 10
  198. 4
  199. 1 2 3 2 5 2 7 2
  200. 3
  201. 2 1 1
  202. 4 1 1
  203. 6 1 1
  204. */
Success #stdin #stdout 0.07s 380224KB
stdin
2
10
6 1 3 2 4 3 1 5 3 6 3 8 7
3 1 2 3 4 5 6 7 8 9
10
4
1 2 3 2 5 2 7 2 
3
2 1 1
4 1 1
6 1 1
stdout
13
6