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 vi and a beauty score wi. The knapsack can hold exactly K items arranged in a circle, meaning that if the selected items are numbered from 0 to K1, the item 0 is adjacent to the item K1 and the item 1.

The overall beauty value of the knapsack is defined as:

j=0K1(vj|wjw(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 (3KN2×105), indicating the number of given items and the number of items the knapsack can hold.

Each of the following N lines contains two integers vi and wi (1vi,wi109), indicating the value and the beauty score of the ith item.

Output Specification

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

Constraints

Subtask Points Additional constraints
1 15 N100
2 25 N2000.
3 60 No additional constraints.

Sample Input

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

Sample Output

Copy
6

Explanation

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


Comments

There are no comments at the moment.