#P8490. [IOI 2022] 鲶鱼塘
[IOI 2022] 鲶鱼塘
Background
Due to technical limitations, please do not use C++ 14 (GCC 9) to submit this problem.
This is an interactive problem. You only need to implement the function required in the code.
Your code does not need to include any additional header files, and you do not need to implement the main function.
Problem Description
Bu Dengklek has a catfish pond. The pond is a grid of cells. Each cell is a square of the same size. The columns of the grid are numbered from west to east as to , and the rows are numbered from south to north as to . We denote the cell in column and row (, ) as cell .
There are catfish in the pond, numbered from to , and each is in a different cell. For each with , catfish is in cell and has weight grams.
Bu Dengklek wants to build some dikes to catch catfish. A dike of length in column (for all and ) is a rectangle that goes from row up to row , covering cells . For each column, Bu Dengklek may build a dike of some length of her choice, or build no dike.
Catfish (for all ) can be caught if there is a dike directly adjacent to its west side or east side, and there is no dike covering the cell it is in; that is, if:
- At least one of the cells or is covered by some dike, and
- No dike covers cell .
For example, consider a pond with and catfish:
- Catfish is in cell and has weight grams.
- Catfish is in cell and has weight grams.
- Catfish is in cell and has weight gram.
- Catfish is in cell and has weight grams.
Bu Dengklek can build dikes as follows:
| Before building dikes | After building dikes |
|---|---|
![]() |
![]() |
The number in a cell indicates the weight of the catfish in that cell.
Shaded cells are covered by dikes.
In this scenario, catfish (in cell ) and catfish (in cell ) can be caught.
Catfish (in cell ) is not caught because a dike covers its cell. Catfish (in cell ) is not caught because there is no dike directly adjacent to its west or east side.
Bu Dengklek wants to build dikes so that the total weight of caught catfish is as large as possible. Your task is to compute the maximum total weight of catfish that Bu Dengklek can catch by building dikes.
Input Format
You need to implement the following function:
int64 max_weights(int N, int M, int[] X, int[] Y, int[] W)
- : the size of the pond.
- : the number of catfish.
- , : two arrays of length giving the positions of the catfish.
- : an array of length giving the weights of the catfish.
- The function should return an integer representing the maximum total weight of catfish that Bu Dengklek can catch by building dikes.
- This function will be called exactly once.
Output Format
Consider the following call:
max_weights(5, 4, [0, 1, 4, 3], [2, 1, 4, 3], [5, 2, 1, 3])
The explanation of this example can be found earlier in the statement.
After building the described dikes, Bu Dengklek can catch catfish and , with total weight grams.
Since it is impossible to build dikes that catch catfish with total weight greater than grams, the function should return .
Hint
Constraints
- , (for all )
- (for all )
- No two catfish will be in the same cell.
In other words, or (for all ).
Subtasks
- (3 points) is even (for all ).
- (6 points) (for all ).
- (9 points) (for all ).
- (14 points) , (for all ).
- (21 points) .
- (17 points) .
- (14 points) In each column, there are at most catfish.
- (16 points) No additional constraints.
Sample grader
The sample grader reads input in the following format:
- Line :
- Line ():
The sample grader prints your answer in the following format:
- Line : the return value of
max_weights.
Notes
In the statement, when giving the function interface, the following generic type names are used: void, int, int64, int[] (array), and int[][] (array of arrays).
In C++, the grader will use appropriate data types or implementations as shown in the following tables:
void |
int |
int64 |
int[] |
|---|---|---|---|
void |
int |
long long |
std::vector<int> |
int[][] |
Length of array a |
|---|---|
std::vector<std::vector<int>> |
a.size() |
Translated by ChatGPT 5

