eolymp
bolt
Try our new interface for solving problems
Problems

Garden Fence

Garden Fence

Gary is a careful gardener that has a rectangular eld full of trees. There are two kinds of trees in his land: pines and larches. To improve their vitality, he decided to start using a speci c fertilizer for each kind of tree, instead of the generic fertilizer he was using so far. Since Gary has many trees, fertilizers cannot be placed individually on each tree. For this reason he decided to build a fence to separate the eld in two, and use the pine fertilizer on one side and the larch fertilizer on the other side. The new fence will be built over a straight line connecting two distinct points located on the boundary of the land. Sadly, each fertilizer is great for the kind of tree it is intended, but deadly for the other. After building the fence and deciding which fertilizer will be used on each side, larches in pines' side and pines in larches' side will be cut down, to prevent a slow death that will ruin the landscape. Furthermore, before building the fence it is necessary to cut down trees of any kind lying directly over the line where the fence will be located. Of course, Gary loves his trees. Depending on their kind, age and other factors, each tree has a certain value. The gardener wants to build the fence and select where to use each fertilizer in such a way that his loss is minimized, where the loss is the sum of the values of the trees that will be cut down. You were hired to build the fence. Before starting your work, tell Gary how much he will lose when choosing optimally the location of the fence and the fertilizer for each side. \InputFile Each test case is described using several lines. The first line contains two integers \textbf{P} and \textbf{L}, representing respectively the number of pines and the number of larches (\textbf{1} ≤ \textbf{P}, \textbf{L} ≤ \textbf{1000}). Each of the next \textbf{P} lines describes a pine. After that, each of the next \textbf{L} lines describes a larch. Trees are modeled as points in the \textbf{XY} plane. Each tree is described using three integers \textbf{X}, \textbf{Y} and \textbf{V}, where \textbf{X} and \textbf{Y} are the coordinates of the tree (\textbf{-10^5} ≤ \textbf{X}, \textbf{Y} ≤ \textbf{10^5}), and \textbf{V} is its value (\textbf{1} ≤ \textbf{V }≤ \textbf{1000}). You may assume that within each test case no two trees have the same location. The last test case is followed by a line containing two zeros. \OutputFile For each test case output a line with an integer representing the minimum possible loss for the gardener.
Time limit 1 second
Memory limit 64 MiB
Input example #1
2 3
2 2 10
4 4 10
2 4 10
4 2 10
3 3 10
2 3
2 2 20
4 4 20
2 4 10
4 2 10
3 3 10
1 1
-10000 -10000 1000
10000 10000 1000
2 2
0 0 4
0 2 2
0 1 3
0 4 1
4 1
0 1 1000
0 -1 1000
1 0 1000
-1 0 1000
0 0 1
0 0
Output example #1
10
20
0
2
1
Source ACM ICPC Latin America 2011, November 4th-5th, 2011