The Invasion
Time limit3sMemory limit64 MB
Given a convex polygon with n vertices and m weighted points, find three polygon vertices forming a triangle with the maximum total weight of points inside or on it.
- Level
Hard8 of 10
- Topics
- Geometry, Two pointers, Prefix sum, Brute force
- Solved
- No attempts yet
Problem
The Triangles have invaded Byteotia. Byteotia lies on an island and covers its entire surface, and the island has the shape of a convex polygon (every interior angle is smaller than ). Several software factories stand on the island, and each one produces a fixed gain or a fixed loss.
The Triangles want to seize a part of Byteotia that is:
- a triangular region whose three corners are three different vertices of the island polygon, and
- as profitable as possible, meaning the sum of the gains and losses of all factories inside the region is as large as it can be.
A factory that lies on the border of the seized region, or exactly at one of its corners, counts as belonging to that region. A region that holds no factory yields an income of .
Byteasar, the King of Byteotia, wants to know how much the invasion could cost him. Write a program that reads the shape of the island and the positions of the factories, then reports the largest possible total of gains and losses of the factories captured by a triangle whose three corners are three different vertices of the island.
Input
The first line contains a single integer (), the number of vertices of the island polygon.
Each of the next lines contains two integers and () separated by a single space, the coordinates of the polygon vertices given in clockwise order.
The next line contains a single integer (), the number of factories.
Each of the next lines contains three integers , and (, ) separated by single spaces: the coordinates of the -th factory and the value it produces, which is a gain when and a loss when . Every factory lies inside the island or on its border. Different factories may share the same coordinates.
Output
Print a single integer: the largest possible sum of the gains and losses of the factories that lie inside a triangle whose three corners are three different vertices of the island polygon. The value may be negative.
Hint
