#pragma GCC optimize ("O3") // #pragma GCC target ("avx") #include "bits/stdc++.h" using namespace std; using ll = long long int; #define debugos cout #define debug(v) {printf("L%d %s > ",__LINE__,#v);debugos<<(v)< ",__LINE__,#v);for(auto e:(v)){debugos< ",__LINE__,#m);for(int x=0;x<(w);x++){debugos<<(m)[x]<<" ";}debugos<\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){debugos<<(m)[y][x]<<" ";}debugos< f) { if (!assertion) { cerr << "assertion fault:" << endl; f(); abort(); } } template inline ostream& operator <<(ostream &o, const pair p) { o << '(' << p.first << ':' << p.second << ')'; return o; } template inline ostream& _ostream_vecprint(ostream& os, const Vec& a) { os << '['; for (const auto& e : a) os << ' ' << e << ' '; os << ']'; return os; } template inline ostream& operator<<(ostream& o, const vector& v) { return _ostream_vecprint(o, v); } template inline ostream& operator<<(ostream& o, const array& v) { return _ostream_vecprint(o, v); } template inline T& chmax(T& to, const T& val) { return to = max(to, val); } template inline T& chmin(T& to, const T& val) { return to = min(to, val); } void bye(string s, int code = 0) { cout << s << endl; exit(code); } mt19937_64 randdev(8901016); template::value>::type* = nullptr> inline T rand(T l, T h, Random& rand = randdev) { return uniform_int_distribution(l, h)(rand); } template::value>::type* = nullptr> inline T rand(T l, T h, Random& rand = randdev) { return uniform_real_distribution(l, h)(rand); } // template struct Tag { T1 first; T2 second; Tag(T1 t1, T2 t2) :first(t1), second(t2) {} Tag(const pair& p) :first(p.first), second(p.second) { } Tag(const pair&& p) :first(p.first), second(p.second) { } inline bool operator == (const Tag& t) const { return first == t.first; } inline bool operator <(const Tag& t) const { return first < t.first; } }; // enum EMachine { kHand = 0, kLily, kFactory, kCasino, kGrimoire, kClicker }; enum ECommand { kClick = 0, kBuy, kReinforce, kSell }; enum EEffect { kNothing = 0, kBonus, kFever, kSale }; inline char* machineToString(EMachine m) { static char* s[] = { "hand", "lily", "factory", "casino", "grimoire" }; return s[(int)m]; } inline EEffect charToEffect(char c) { return c == 'N' ? EEffect::kNothing : c == 'B' ? EEffect::kBonus : c == 'F' ? EEffect::kFever : c == 'S' ? EEffect::kSale : EEffect::kNothing; } // struct Action { ECommand command; EMachine target; Action(ECommand aCommand = ECommand::kClick, EMachine aTarget = EMachine::kHand) :command(aCommand), target(aTarget) { } void print() const { switch (command) { case kClick: cout << "click" << endl; break; case kBuy: cout << "buy " << machineToString(target) << endl; break; case kReinforce: if (target == EMachine::kClicker) cout << "enhclick" << endl; else cout << "reinforce " << machineToString(target) << endl; break; case kSell: cout << "sell " << machineToString(target) << endl; break; default: clog << "invalid command\n"; break; } } }; class State { ll wallet_; array numMachine_; array lvlMachine_; array costBuyMachine_ = { 150ll, 2000ll, 30000ll, 600000ll, 10000000ll }; array costRfMachine_ = { 1500ll, 20000ll, 300000ll, 6000000ll, 100000000ll, 15ll }; public: State() :wallet_(0) { numMachine_.fill(0); numMachine_[5] = 1; lvlMachine_.fill(0); } static ll discount(ll cost) noexcept { return (cost * 9 + 9) / 10; } // inline ll costBuyMachine(EMachine e) const { return costBuyMachine_[(int)e]; } inline ll costReinforceMachine(EMachine e) const { return costRfMachine_[(int)e]; } inline ll wallet() const { return wallet_; } inline int countOfMachine(EMachine e) const { return numMachine_[(int)e]; } // inline bool buyableMachine(EMachine e, bool sale = false) const { return sale ? discount(costBuyMachine_[(int)e]) <= wallet_ : costBuyMachine_[(int)e] <= wallet_; } inline bool reinforceableMachine(EMachine e, bool sale = false) const { return sale ? discount(costRfMachine_[(int)e]) <= wallet_ : costRfMachine_[(int)e] <= wallet_; } // inline ll efficiency(EMachine e) const { const ll s[] = { 1, 10, 120, 2000, 25000, 1 }; return s[(int)e] * (1ll << lvlMachine_[(int)e])*numMachine_[(int)e]; } inline ll nextLevelEfficiency(EMachine e) const { const ll s[] = { 1, 10, 120, 2000, 25000, 1 }; return s[(int)e] * (1ll << (lvlMachine_[(int)e] + 1))*numMachine_[(int)e]; } inline ll oneMoreEfficiency(EMachine e) const { const ll s[] = { 1, 10, 120, 2000, 25000, 1 }; return s[(int)e] * (1ll << lvlMachine_[(int)e])*(numMachine_[(int)e] + 1); } void applyAction(Action act, EEffect eff) { if (act.command == ECommand::kClick) { } else if (act.command == ECommand::kBuy) { numMachine_[act.target] += 1; wallet_ -= eff == EEffect::kSale ? discount(costBuyMachine_[act.target]) : costBuyMachine_[act.target]; costBuyMachine_[act.target] = (costBuyMachine_[act.target] * 6 + 4) / 5; } else if (act.command == ECommand::kReinforce) { lvlMachine_[act.target] += 1; wallet_ -= eff == EEffect::kSale ? discount(costRfMachine_[act.target]) : costRfMachine_[act.target]; costRfMachine_[act.target] = (costRfMachine_[act.target] * 10); } else if (act.command == ECommand::kSell) { numMachine_[act.target] -= 1; costBuyMachine_[act.target] = (costBuyMachine_[act.target] * 5) / 6; wallet_ += (costBuyMachine_[act.target]*3+3)/4; } assert(wallet_ >= 0); ll fever = eff == EEffect::kFever ? 7 : 1; for (int i = 4 + (act.command == ECommand::kClick); i >= 0; --i) { wallet_ += efficiency((EMachine)i); } if (eff == EEffect::kBonus) { wallet_ += (wallet_ + 99) / 100; } } void dumplog() { clog << "money: " << wallet_ << '\n'; clog << "nMachine" << numMachine_ << '\n'; clog << "lMachine" << lvlMachine_ << '\n'; clog << "buycost" << costBuyMachine_ << '\n'; clog << "rfcost" << costRfMachine_ << '\n'; clog << "efficiency:"; repeat(i, 6) clog << efficiency(EMachine(i)) << " "; clog << '\n'; } }; // namespace IN { const int maxTurnCount = 10000; EEffect effects[maxTurnCount]; bool isGenerated = false; } // void convertinput(const string& l) { using namespace IN; int fever = 0; int sale = 0; repeat(i, maxTurnCount) { auto e = charToEffect(l[i]); effects[i] = e == EEffect::kBonus ? EEffect::kBonus : EEffect::kNothing; if (fever > 0) { --fever; effects[i] = EEffect::kFever; } if (sale > 0) { --sale; effects[i] = EEffect::kSale; } if (e == EEffect::kFever) { fever = 20; } else if (e == EEffect::kSale) { sale = 1; } } } void generateInput() { using namespace IN; string l; l.reserve(maxTurnCount); int next = rand(0, 200); repeat(i, maxTurnCount) { if (next > 0) { --next; l.push_back('N'); } else { l.push_back("DFS"[rand(0, 2)]); next = rand(100, 200); } } convertinput(l); isGenerated = true; } void scan() { using namespace IN; int n; cin >> n; if (maxTurnCount != n) { generateInput(); return; } string l; cin >> l; convertinput(l); } // namespace Solver { void renda(State& state, int currentTurnCount, bool echo = false) { int num = 0; repeat(m, 5) num += state.countOfMachine(EMachine(m)); for (; currentTurnCount < IN::maxTurnCount-num; ++currentTurnCount) { state.applyAction(Action(ECommand::kClick), IN::effects[currentTurnCount]); if (echo) Action(ECommand::kClick).print(); } repeat(i, num) { repeat(m, 5) { if (state.countOfMachine(EMachine(m)) > 0) { state.applyAction(Action(ECommand::kSell, EMachine(m)), IN::effects[currentTurnCount]); if (echo) Action(ECommand::kSell, EMachine(m)).print(); break; } } } } void solve() { State state; array stackedActions; pair bestWalletTurn(0, 0); repeat(turncount, IN::maxTurnCount) { const EEffect effect = IN::effects[turncount]; // Action action(ECommand::kClick); Tag best(0, ECommand::kClick); rrepeat(mm, 5) { EMachine m = (EMachine)mm; { if (state.buyableMachine(m, effect == EEffect::kSale)) { chmax(best, Tag( state.oneMoreEfficiency(m) - state.efficiency(m), Action(ECommand::kBuy, m) )); } } if (state.countOfMachine(m) > 0) { if (state.reinforceableMachine(m, effect == EEffect::kSale)) { chmax(best, Tag( state.nextLevelEfficiency(m) - state.efficiency(m), Action(ECommand::kReinforce, m) )); } } } rrepeat(mm, 4) { EMachine mp = (EMachine)(mm+1); EMachine m = (EMachine)mm; if (state.countOfMachine(mp) > 0 && state.countOfMachine(m) > 0) { best.second = Action(ECommand::kSell, m); } } if (state.reinforceableMachine(EMachine::kClicker, effect == EEffect::kSale)) { chmax(best, Tag( state.nextLevelEfficiency(EMachine::kClicker) - state.efficiency(EMachine::kClicker), Action(ECommand::kReinforce, EMachine::kClicker) )); } if (turncount > IN::maxTurnCount * 2 / 3 || best.second.command != ECommand::kClick) { State s = state; renda(s, turncount); chmax(bestWalletTurn, make_pair(s.wallet(), turncount)); } // action.print(); stackedActions[turncount] = best.second; state.applyAction(best.second, effect); } if (!IN::isGenerated) { repeat(i, bestWalletTurn.second) { stackedActions[i].print(); } renda(state, bestWalletTurn.second, true); } else { clog << "bestWalletTurn: " << bestWalletTurn.second << endl; state = State(); repeat(i, bestWalletTurn.second) { state.applyAction(stackedActions[i], IN::effects[i]); } clog << "-- before selling --" << endl; state.dumplog(); renda(state, bestWalletTurn.second); clog << "-- final -- " << endl; clog << "BEST:" << (double)bestWalletTurn.first << endl; state.dumplog(); } } } int main() { using namespace IN; scan(); Solver::solve(); if (!isGenerated) { repeat(i, maxTurnCount) { string str; cin >> str; assert(str == "ok"); } } return 0; }