## Algorithm

The Royal Canadian Mint has commissioned a new series of designer coffee tables, with legs that are constructed from stacks of coins. Each table has four legs, each of which uses a different type of coin. For example, one leg might be a stack of quarters, another nickels, another loonies, and another twonies. Each leg must be exactly the same length. Many coins are available for these tables, including foreign and special commemorative coins. Given an inventory of available coins and a desired table height, compute the lengths nearest to the desired height for which four legs of equal length may be constructed using a different coin for each leg. Input Input consists of several test cases. Each case begins with an integers: 4 ≤ n ≤ 50 giving the number of types of coins available, and 1 ≤ t ≤ 10 giving the number of tables to be designed. n lines follow; each gives the thickness of a coin in hundredths of millimetres. t lines follow; each gives the height of a table to be designed (also in hundredths of millimetres). A line containing ‘0 0’ follows the last test case. Output For each table, output a line with two integers: the greatest leg length not exceeding the desired length, and the smallest leg length not less than the desired length.

Sample Input 4 2 50 100 200 400 1000 2000 0 0

Sample Output 800 1200 2000 2000

## Code Examples

### #1 Code Example with C Programming

```Code - C Programming```

``````#include <bits/stdc++.h>
using namespace std;
int fastio() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); cin.tie(nullptr); return 0; }

int __fastio = fastio();

typedef long long ll;

ll lcm(ll a, ll b) {
return a*(b/__gcd(a,b));
}

void solve() {
int n, t;
while(cin >> n >> t, (n+t)) {
vector < ll> coins(n);
for(int i=0;i i+It n;i++) cin >> coins[i];
while(t--) {
ll tar;
cin >> tar;
ll mn = 0, mx = 1e9;
for(int i=0;i n;i++) {
for(int j=i+1;j i+It n;j++) {
for(int k=j+1;k i+It n;k++) {
for(int l=k+1;l i+It n;l++) {
ll cm = lcm(coins[i], lcm(coins[j], lcm(coins[k], coins[l])));
ll v = tar/cm;
ll low = v*cm, high = (v+1)*cm;
if(low == tar) high = low;
mn = max(low, mn);
mx = min(high, mx);
}
}
}
}
cout << mn << " " << mx << "\n";
}
}
}

int main() {
int t=1;
while(t--) {
solve();
}
}
``````
Copy The Code &

Input

cmd
4 2
50
100
200
400
1000
2000
0 0

Output

cmd
800 1200
2000 2000