TopcoderArchive
Archive
Algorithm problem

CostMaximizer

Problem statement, definition, constraints, and public examples.

Problem Statement

You are given 2*N nodes. For each i (1 <= i <= N) there is a red node number i and a blue node number i.

Each pair of nodes is connected by an edge. For nodes i and j of the same colour this edge has weight wt(i,j). For nodes i and j of the opposite colour this edge has weight K - wt(i,j).

The value K is the same for all pairs i, j. For each edge, the value K will satisfy the following: wt(i,j) <= K <= 2*wt(i,j).


You must select exactly N out of these nodes: for each number either the red node or the blue node, but not both. Among those, you must select at least one red node and at least one blue node.

Determine and return the largest possible sum of weights in the complete graph formed by the nodes you selected and all edges between them.


You are given the ints N and K mentioned above and a int[] w with the upper triangle of the weights matrix.

You can reconstruct the whole matrix as follows:


int k = 0;
for (int i=1; i<=N; ++i) for (int j=i+1; j<=N; ++j) {
    wt(i,j) = wt(j,i) = w[k];
    ++k;
}

Definition

Class:
CostMaximizer
Method:
maxPossible
Parameters:
int, int, int[]
Returns:
long
Method signature:
long maxPossible(int N, int K, int[] w)
(be sure your method is public)

Constraints

  • 2 <= N <= 65
  • w will contain exactly N*(N-1) / 2 elements.
  • Each element of w will be between 0 and 999,999, inclusive.
  • For each i != j: K will be between wt(i,j) and 2*wt(i,j), inclusive.

Examples

  1. 5
    20
    {13, 15, 10, 14, 12, 12, 12, 18, 20, 13}
    Returns: 121
    One optimal solution is to take red nodes 1, 3, 4, 5 and blue node 2. The total cost of edges in the complete graph we took will be 7 + 15 + 10 + 14 + 8 + 8 + 8 + 20 + 18 + 13.
  2. 5
    500433
    {401130, 423791, 344538, 499549, 328579, 480488, 456447, 479666, 368601, 250822}
    Returns: 2924315
Back to all problems