#include <bits/stdc++.h>
using namespace std;
using namespace chrono;
using bvec = vector<bool>;
const int N = 10000, E = 100;

class timer: high_resolution_clock {
    time_point start_time;
public:
    inline timer () : start_time (now ()) {}
    inline rep elapsed_time() const { 
    	return duration_cast<milliseconds> (now () - start_time).count (); } };
    	
inline int random_value(int min, int max) {
	static mt19937_64 random(system_clock::now().time_since_epoch().count()); 
    return uniform_int_distribution<int>(min,max)(random); }

struct random_positions: bvec {
	inline random_positions(int n) : bvec(N+1,false) {
		for (int x, count = 0; count < n; )
			if (x = random_value(1,N), not at(x))
				at(x) = true, ++count; } };

struct ivec: vector<int> {
	ivec(int n) {
		const random_positions used(n); 
		resize(n);
		for (int i = 1, j = 0; i <= N and j < n; ++i)
			if (used[i])
				at(j++) = i; } };
				
struct distinct_distances: bvec {
	int elapsed_time; const int n, m;
	inline distinct_distances(const ivec &a) : n(a.size()), m(a.back()-a.front()) { resize(m+1,false); } };
	
struct unbounded: distinct_distances {
	inline unbounded(const ivec &a) : distinct_distances(a) {
		timer t;
    	for (int j = n-1; j > 0; --j)
        	for (int e = a[j], i = 0; i < j; ++i)
        		at(e-a[i]) = true; 
        elapsed_time = t.elapsed_time(); } };
                
struct bounded: distinct_distances {
	inline bounded(const ivec &a) : distinct_distances(a) {
		timer t;
		for (int count = 0, j = n-1; j > 0 and count < m; --j)
        	for (int d, e = a[j], i = 0; i < j and count < m; ++i)
        		if (d = e-a[i], not at(d))
        			at(d) = true, ++count; 
        elapsed_time = t.elapsed_time(); } };
	
inline void write(const string &prompt, int time) { cout << prompt << " Time = " << time << " msec." << endl; }
	
int main() {
	int t1 = 0, t2 = 0;
	for (int e = 0; e < E; ++e) {
		const int n = random_value(1,N); 
		const ivec a(n);
		const unbounded s(a); const bounded t(a);
		if (t1 += s.elapsed_time, t2 += t.elapsed_time, s != t)
			throw logic_error("Distinct distances are not equal"); }
	write("Unbounded",t1), 
	write("  Bounded",t2); }
