In the year of 200, two generals whose names are Cao Cao and Shao Yuan are fighting in Guandu. The battle of Guandu was a great battle and the two armies were fighting at $M$ different battlefields whose numbers were 1 to $M$. There were also $N$ villages nearby numbered from 1 to $N$. Cao Cao could train some warriors from those villages to strengthen his military. For village $i$, Cao Cao could only call for some number of warriors join the battlefield $x_{i}$. However, Shao Yuan's power was extremely strong at that time. So in order to protect themselves, village $i$ would also send equal number of warriors to battlefield $y_{i}$ and join the Yuan Shao's Army. If Cao Cao had called for one warrior from village $i$, he would have to pay $c_{i}$ units of money for the village. There was no need for Cao Cao to pay for the warriors who would join Shao Yuan's army. At the beginning, there were no warriors of both sides in every battlefield.
As one of greatest strategist at that time, Cao Cao was considering how to beat Shao Yuan. As we can image, the battlefields would have different level of importance $w_{i}$. Some of the battlefields with $w_{i}=2$ were very important, so Cao Cao had to guarantee that in these battlefields, the number of his warriors was greater than Shao Yuan's. And some of the battlefields with $w_{i}=1$ were not as important as before, so Cao Cao had to make sure that the number of his warriors was greater or equal to Shao Yuan's. The other battlefields with $w_{i}=0$ had no importance, so there were no restriction about the number of warriors in those battlefields. Now, given such conditions, could you help Cao Cao find the least number of money he had to pay to win the battlefield?