CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Challenge 2013
    • May Cook-Off 2013
    • May Challenge 2013
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • Campus Chapters
    • Host your Contest
    • Go for Gold
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
    • Top Contributors on Discuss
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » October Challenge 2012 » Fierce Battles

Fierce Battles

Problem code: DRGNBOOL

  • All Submissions

All submissions for this problem are available.

In the world of DragonBool there are fierce warriors called Soints. Also there are even fiercer warriors called Sofloats – the mortal enemies of Soints.

The power of each warrior is determined by the amount of chakra he possesses which is some positive integer. Warriors with zero level of chakra are dead warriors :) When the fight between Soint with power CI and Sofloat with power CF occurs the warrior with lower power will die and the winner will lose the amount of chakra that his enemy have possessed before the fight. So three cases are possible:

  • CI > CF. Then Sofloat will die while the new power of Soint will be CI – CF.
  • CI < CF. Then Soint will die while the new power of Sofloat will be CF – CI.
  • CI = CF. In this special case both warriors die.

Each warrior (Soint or Sofloat) has his level of skills which is denoted by some positive integer. The fight between two warriors can occur only when these warriors are Soint and Sofloat of the same level. In particual, friendly fights are not allowed, i.e., a Soint cannot fight with another Soint and the same holds for Sofloats.

Lets follow the following convention to denote the warriors. A Soint of level L and power C will be denoted as (I, C, L), while Sofloat of level L and power C will be denoted as (F, C, L). Consider some examples. If A = (I, 50, 1) fights with B = (F, 20, 1), B dies and A becomes (I, 30, 1). On the other hand, (I, 50, 1) cannot fight with (F, 20, 2) as they have different levels.

There is a battle between Soints and Sofloats. There are N Soints and M Sofloats in all. The battle will consist of series of fights. As was mentioned above in each fight one Soint and one Sofloat of the same level take part and after the fight the warrior with lower power will die (or both will die if they have the same power). The battle proceeds as long as there exists at least one pair of warriors who can fight. The distribution of warriors by levels satisfies the following condition: for every Soint of level L there exists at least one Sofloat of the same level L and vice-versa. So if for some level L we have at least one warrior of this level then there is at least one Soint of level L and at least one Sofloat of level L.

There is a powerful wizard, whose name is SoChef, on the side of Soints. He can increase the amount of chakra of each Soint by any number. SoChef wants the army of Soints to win this battle. But increasing amount of chakra of any Soint by one costs him a lot of his magic power. Hence he wants to minimize the total amount of additional chakra he should give to Soints in order for them to win. Note, however, that the win here means that all Sofloats should be dead irregardless of whether any Soint is alive. Also note that the battle can proceed by different scenarios and the SoChef need to distribute additional chakra among the Soints in such a way that they will win for any possible battle scenario. Help SoChef and find the minimal amount of additional chakra he should give to Soints in order for them to win.

Input

The first line of the input contains an integer T, the number of test cases. T test cases follow. The first line of each test case contains two space separated integers N and M. Here N is the number of Soints participating in the battle and M is the number of Sofloats for the same. Each of the next N lines contains two space separated integers Ci and Li, the amount of chakra and level of i-th Soint correspondingly. The next M lines describe power and level of Sofloats participating in the battle in the same format.

Output

For each test case output a single integer on a single line, the minimum amount of chakra SoChef should give to Soints in order for them to win the battle.

Constraints

Each integer in the input file is positive and does not exceed 100. That is

1 ≤ T ≤ 100
1 ≤ N ≤ 100
1 ≤ M ≤ 100
1 ≤ Ci ≤ 100
1 ≤ Li ≤ 100

For every Soint of level L there exists at least one Sofloat of the same level L and vice-versa.

It is guaranteed that each official test file will satisfy all these constraints. You DON'T need to verify them in your program.

Example

Input:
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

Output:
8
89

Explanation

Case 1. The warriors are I1 = (I, 10, 1), I2 = (I, 20, 2), F1 = (F, 5, 2), F2 = (F, 5, 2), F3 = (F, 18, 1). Without the SoChef help the battle can proceed as follows.

  • I2 fights with F1, F1 dies, I2 becomes (I, 15, 2).
  • I2 fights with F2, F2 dies, I2 becomes (I, 10, 2).
  • I1 fights with F3, I1 dies, F3 becomes (F, 8, 1).

So if SoChef will give 8 additional units of chakra to I1 the Soints will win the battle and even one Soint (I2) will left alive. Hence the answer is 8.


Author: kaushik_iska
Tester: anton_lunyov
Editorial http://discuss.codechef.com/problems/DRGNBOOL
Date Added: 19-04-2012
Time Limit: 2 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, FORT, FS, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

Is the value of level unique

coder1 @ 1 Oct 2012 09:51 PM
Is the value of level unique for diffferent warriors ??

@coder1 Please look at the

kaush_adm @ 1 Oct 2012 10:39 PM
@coder1 Please look at the first sample case again.

is it possible to have 2 or

seaborgium @ 1 Oct 2012 10:40 PM
is it possible to have 2 or more soints with same level?

@seaborgium Yes

anton_adm @ 1 Oct 2012 11:50 PM
@seaborgium Yes

In first test case , why the

mehemmedv @ 2 Oct 2012 12:29 AM
In first test case , why the output is 8 ? i think it maybe 0 .

hehe nice names Soravi

squeezethekey @ 2 Oct 2012 12:35 AM
hehe nice names Soravi

can ans be 0 ?

kumardevesh @ 2 Oct 2012 01:23 AM
can ans be 0 ?

Does "battle can proceed by

rajrocks1992 @ 2 Oct 2012 01:23 AM
Does "battle can proceed by different scenarios" mean battle in different levels or sumthng else???

SoChef is SoCruel :P

Kokos @ 2 Oct 2012 03:29 AM
SoChef is SoCruel :P

can power of same level be

smaurya73 @ 2 Oct 2012 07:29 AM
can power of same level be different or same, for example is (5 2),(8 2) valid?

suppose sofloat of level i

jkwadhe @ 2 Oct 2012 11:27 AM
suppose sofloat of level i have some power but at same level soint not have any power i.e zero, then whether sochef give power to soint or not?

is there a case where all

bharathkallurs @ 2 Oct 2012 11:35 AM
is there a case where all soints and sofloats have equal power and they cancel out each other..??

is it necessary that at least

ashwinjagadeesh @ 2 Oct 2012 12:31 PM
is it necessary that at least one soint should be alive?

@ashwinjagadeesh:No. All

cool123 @ 2 Oct 2012 12:34 PM
@ashwinjagadeesh:No. All SoFloats should die. Thats it :)

@admin u have left 1 tset

mayank99 @ 2 Oct 2012 04:55 PM
@admin u have left 1 tset case 1 1 1 1 1 1 1 For this ans 0 is getting accepted while actually it should be 1 because chef wants that soint always wins

@admin: could you plz check

k53sc @ 2 Oct 2012 06:48 PM
@admin: could you plz check http://www.codechef.com/viewsolution/1396428 ....would be glad to know if the logic is wrong ......getting correct o/p for test.

@mayank99 The phrase in the

anton_adm @ 2 Oct 2012 08:55 PM
@mayank99 The phrase in the problems statement "Note, however, that the win here means that all Sofloats should be dead irregardless of whether any Soint is alive." explain why the answer is 0 for your test, isn't it?

@mayank99 see the 6th para

cobra @ 3 Oct 2012 10:46 PM
@mayank99 see the 6th para :Note, however, that the win here means that all Sofloats should be dead irregardless of whether any Soint is alive.

yes by solving this problem

ankit_mac @ 5 Oct 2012 11:54 AM
yes by solving this problem in first attempt i feel much confident now... thnx codechef

what should be the class

noorflex @ 6 Oct 2012 01:39 AM
what should be the class name..?

finally successfully

san3523 @ 6 Oct 2012 07:13 AM
finally successfully submitted the code in java...thank u codechef..

why do i get runtime

noorflex @ 6 Oct 2012 09:50 AM
why do i get runtime error(NZEC).. times it shows as 2.77 and memory 177 Mb.? is that the issue

@admin thnxs :) i missed

mayank99 @ 7 Oct 2012 07:50 PM
@admin thnxs :) i missed that line

@admin: I think there is a

akumar3 @ 8 Oct 2012 10:47 AM
@admin: I think there is a bug in the submission procedure. One of my submission-- ID=1434087 (which was definitely wrong) showed "time limit exceeded" and after correcting the bug, it got accepted.

this question tested my

smaurya73 @ 9 Oct 2012 02:42 PM
this question tested my patience......even though it is one of the easiest.......i won..:)

@admin how do we take space

siddharth6150 @ 9 Oct 2012 09:08 PM
@admin how do we take space seperated input?

@admin Agreed that you never

deepsaggas @ 9 Oct 2012 10:22 PM
@admin Agreed that you never said in the problem description that one soint should be alive for a win but then why did u mention that in the explanation of first test case. Why are you trying to confuse people with contradictory statements

@deepsaggas Sorry if this

anton_adm @ 10 Oct 2012 12:55 AM
@deepsaggas Sorry if this confuse you. It should be read as "... and even one Soint (I2) will left alive, though in general it is not required."

do we have to use a file

fire_aura @ 13 Oct 2012 12:01 PM
do we have to use a file input system or cmd input system here?

@fire_aura cmd - standard

anton_adm @ 13 Oct 2012 02:31 PM
@fire_aura cmd - standard input and output

@anton_adm ---- for the

raunak_kumar @ 20 Oct 2012 08:44 PM
@anton_adm ---- for the following input output should be 2 but many correct submissions of this problem gives output 0. 1 2 2 8 3 8 3 10 3 6 3

@raunak_kumar how is the

ronny @ 21 Oct 2012 09:24 PM
@raunak_kumar how is the output 2 according to you there should be a fighter on both sides(SoInts and SoFloats) for each level. "For every Soint of level L there exists at least one Sofloat of the same level L and vice-versa."Hope this clears.

SUCCESSFUL SUBMISSIONS


Fetching successful submissions

CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.