Circular Knapsack Problem

View as PDF

Submit solution

Points: 25 (partial)
Time limit: 2.0s
Memory limit: 512M

Problem types

You are given a circular knapsack and N items, each with a value and a beauty score. The items are numbered from 1 to N. The i-th item has a value v_i and a beauty score w_i. The knapsack can hold exactly K items arranged in a circle, meaning that if the selected items are numbered from 0 to K-1, the item 0 is adjacent to the item K-1 and the item 1.

The overall beauty value of the knapsack is defined as:

\displaystyle \sum_{j=0}^{K-1}{(v_j - |w_j - w_{(j+1) \% K}|)}

where the overall beauty value of the knapsack is the sum of the item's value minus the absolute difference of the beauty score between the current item with the next adjacent item.

Your task is to help Bob maximize the overall beauty value of the knapsack.

Input Specification

The first line of input contains two integers, N and K (3 \le K \le N \le 2 \times 10^5), indicating the number of given items and the number of items the knapsack can hold.

Each of the following N lines contains two integers v_i and w_i (1\le v_i, w_i \le 10^9), indicating the value and the beauty score of the i-th item.

Output Specification

Output one integer, the max overall beauty value of the knapsack.

Constraints

Subtask Points Additional constraints
1 15 N \le 100
2 25 N \le 2\,000.
3 60 No additional constraints.

Sample Input

5 3
2 1
4 2
6 4
8 8
10 16

Sample Output

6

Explanation

Bob can put items 1, 3 and 2 in a circle, and the overall beauty value is 2 - |1-4| + 6 - |4-2| + 4 - |2-1| = 6, which is the max possible value.


Comments

There are no comments at the moment.