← 返回 amazon 的题目列表Maximum Affinity Distribution
类型:online_judge
In Amazon's SDE internship interview, developers need to handle an algorithm for distributing data pieces based on an 'Affinity' parameter. There are n (n is even) different data pieces, where the affinity parameter of each piece is represented in the array 'affinity'. The data needs to be distributed across two regions: RegionA and RegionB. In each step of the algorithm, a region is selected, and a data piece is migrated to that region. The same region cannot be chosen in consecutive steps. Whenever a region is selected, it's set up to maximize the total affinity of the data it will store, deciding which data piece to choose accordingly.
Additionally, there are certain predefined rules for data distribution represented by m unique pairs of indices. For each unique pair (x, y), if the data piece at index x or y is chosen for a region, the other must be selected for the next region in the next step. Please find the maximum possible sum of memory stored in RegionA. The input includes an integer n, an array affinity, an integer m, and a rule matrix rules. Return the maximum memory sum possible in RegionA.
Example
Given n = 6, affinity = [3, 2, -4, 8, 3, -7], m = 2, rules = [[2, 4], [3, 6]].
Compute the maximum possible memory sum in RegionA.
Example
Input
6
3 2 -4 8 3 -7
2
2 4
3 6