#include #include #include using namespace std; struct stock { int n; int money; }; struct profit { int p; int i; }; typedef stock stock; typedef profit profit; bool debug = false; int main() { int d, m; bool done = false; deque p; deque stocks; deque profits; while (true) { p.clear(); stocks.clear(); profits.clear(); cin >> d >> m; if (d == 0) { break;} int pp; for (int i = 0; i < d; i++) { cin >> pp; if ((i > 0) && (pp < p.back())) { //cout << "profit count" << pp << " " << p.back() << endl; profit* prof = new profit; prof->p = p.back(); prof->i = p.size()-1; profits.push_back(prof); } p.push_back(pp); stock* s = new stock; s->n = m/pp; s->money = m%pp; stocks.push_back(s); } profit* prof = new profit; prof->p = p.back(); prof->i = p.size()-1; profits.push_back(prof); if (debug) for(deque::iterator it = p.begin(); it != p.end(); ++it) { cout << *it << endl; } if (debug) for(deque::iterator it = stocks.begin(); it != stocks.end(); ++it) { cout << (*it)->n << " " << (*it)->money << endl; } if (debug) cout << "profit" << endl; if (debug) for(deque::iterator it = profits.begin(); it != profits.end(); ++it) { cout << (*it)->p << " " << (*it)->i << endl; } if (debug) cout << endl; int lastI = 0; int maxzisk = 0; for (deque::iterator it = profits.begin(); it != profits.end(); ++it) { for (int i = lastI; i < (*it)->i; ++i) { if (stocks.at(i)->n * (*it)->p > maxzisk) { maxzisk = stocks.at(i)->n * (*it)->p + (stocks.at(i)->money); if (debug) cout << "maxzisk " << maxzisk << endl; } } } if ((maxzisk - m) > 0) { if (debug) cout << "Total maxzisk " << maxzisk-m << endl; } else { if (debug) cout << "Ziadny zisk " << maxzisk-m << endl; } if ((maxzisk - m) > 0) { cout << (maxzisk - m) << endl; } else { cout << '0' << endl; } } }