fork(2) download
  1. import java.io.*;
  2. class Battles1
  3. {
  4. public static void main(String args[])throws IOException
  5. {
  6. int t=Integer.parseInt(br.readLine());
  7. String s,s1,s2;
  8. int n,m,temp;
  9. int out[]=new int[t];
  10. for(int i=0;i<t;i++)
  11. {out[i]=0;
  12. s=br.readLine();
  13. n=Integer.parseInt(s.substring(0,s.indexOf(' ')));
  14. m=Integer.parseInt(s.substring(s.indexOf(' ')+1,s.length()));
  15. int sointch[][]=new int[n][2];
  16. int sofloatch[][]=new int[m][2];
  17. for(int x=0; x<n;x++)
  18. {
  19. s1=br.readLine();
  20. sointch[x][0]=Integer.parseInt(s1.substring(0,s1.indexOf(' ')));
  21. sointch[x][1]=Integer.parseInt(s1.substring(s1.indexOf(' ')+1,s1.length()));
  22. }
  23. for(int x=0; x<m;x++)
  24. {
  25. s2=br.readLine();
  26. sofloatch[x][0]=Integer.parseInt(s2.substring(0,s2.indexOf(' ')));
  27. sofloatch[x][1]=Integer.parseInt(s2.substring(s2.indexOf(' ')+1,s2.length()));
  28. }
  29. for(int j=0;j<n;j++)
  30. for(int k=0;k<m;k++)
  31. {
  32. if(sointch[j][1]==sofloatch[k][1])
  33. {
  34. if(sointch[j][0]>sofloatch[k][0] && sointch[j][0]>=0 && sofloatch[k][0]>=0)
  35. {
  36. sointch[j][0]-=sofloatch[k][0];
  37. sofloatch[k][0]=0;
  38. }
  39. else if(sointch[j][0]<sofloatch[k][0] && sointch[j][0]>=0 && sofloatch[k][0]>=0)
  40. {
  41. temp=sointch[j][0];
  42. sointch[j][0]-=sofloatch[k][0];
  43. sofloatch[k][0]-=temp;
  44. }
  45. else if(sointch[j][0]>=0 && sofloatch[k][0]>=0)
  46. {
  47. sointch[j][0]=0;
  48. sofloatch[k][0]=0;
  49. }
  50. }
  51. }
  52. for(int q=0;q<n;q++)
  53. if(sointch[q][0]<0)
  54. out[i]-=sointch[q][0];
  55. }
  56. for(int i=0;i<t;i++)
  57. {
  58. System.out.println(out[i]);}
  59. }
  60. }
Success #stdin #stdout 0.02s 245632KB
stdin
2
2 3
10 1
20 2
5 2
5 2
18 1
5 5
73 87
69 13
36 36
77 46
43 93
49 46
74 93
78 87
99 13
59 36
stdout
8
89