This page is still under construction.

Parts of this page are still being built. What you see may change.

Zombie's Treasure Chest

Time limit1sMemory limit128 MB

Summary
Given a chest capacity and two unlimited gem types with sizes and values, maximize total value of gems that fit.
Level

Medium7 of 10

Topics
Math, Number theory, Brute force, Greedy
Solved
No attempts yet

Problem

A band of brave warriors reaches a lost village. They are lucky enough to find piles of treasure and a large treasure chest, but also a horde of angry zombies.

The warriors decide to defeat the zombies and haul all the treasure home. After a brutal battle that lasts from morning until night, they discover that the zombies are undead and cannot be killed.

Still, the treasure cannot be left behind. The catch is that the chest has limited capacity, so not every gem can be taken. There are only two kinds of treasure: emeralds and sapphires. Every emerald has the same size and value, and there is an unlimited supply of them; the same holds for sapphires.

Given the capacity NN of the chest and the size and value of each kind of gem, compute the maximum total value the warriors can carry away. Formally, choose non-negative integers xx and yy with x⋅S1+y⋅S2≤Nx \cdot S_1 + y \cdot S_2 \le N that maximize x⋅V1+y⋅V2x \cdot V_1 + y \cdot V_2.

Input

The first line contains the number of test cases TT (T≤200T \le 200).

Each test case is a single line with five integers N,S1,V1,S2,V2N, S_1, V_1, S_2, V_2: the capacity NN of the chest, the size S1S_1 and value V1V_1 of one emerald, and the size S2S_2 and value V2V_2 of one sapphire. All integers are positive and fit in a signed 32-bit integer.

Output

For each test case, print one line with the case number and the maximum total value of treasure that can be carried in the chest. Use the format Case #k: value, where kk is the test case number starting from 1.

Examples4

  1. Example 1

    Input
    2
    100 1 1 2 2
    100 34 34 5 3
    
    Expected output
    Case #1: 100
    Case #2: 86
    
  2. Example 2

    Input
    1
    10 3 5 5 8
    
    Expected output
    Case #1: 16
    
  3. Example 3

    Input
    1
    100 2 2 5 6
    
    Expected output
    Case #1: 120
    
  4. Example 4

    Input
    1
    100 34 34 5 3
    
    Expected output
    Case #1: 86