cswork / OAOpen practice workspace

Goldman SachsOA #6

Cutting Metal Surplus

Estimated MediumJudge-ready

Topics (AI-labelled)GreedyIn-place Arrays

Given several metal rods, the cost of each cut, and the sale price per unit length. You may choose one positive integer saleLength; rods can be cut into pieces of length saleLength, and any leftover scrap is discarded with no revenue. You may leave a rod unprocessed. The goal is to maximize the total sales revenue minus the cutting costs.

Input

The first line contains n, costPerCut, salePrice; the second line contains n rod length values. Original ranges: 1≤n≤50, 1≤length[i]≤10000, 1≤costPerCut,salePrice≤100. This site explicitly lets each rod independently choose how many finished saleLength pieces to keep. When a rod divides evenly and is fully used, the last piece needs no cut to separate it; when there is scrap or only some of the pieces are kept, each finished piece requires one cut, and scrap earns nothing.

Output

Output the maximum achievable total profit.

Example 1

Input:

3 1 10
30 59 110

Output:

1913

Example 2

Input:

3 100 10
26 103 59

Output:

1230

Example 3

Input:

1 100 1
1

Output:

1

Editorial

Loading solution…