Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define X first
- #define Y second
- #define LL long long
- #define PB push_back
- #define SZ(a) (int)(a.size())
- const int MAX = 2 * 100005;
- const int INF = 1000 * 1000 * 1000;
- LL curProfit = 0;
- LL balance;
- vector<vector<int> > g(MAX);
- long finalBalance(vector<int> cost, vector<int> profit, vector<int> p, int days, int dollars) {
- balance = dollars;
- int n = cost.size();
- set<pair<int , int> > s;
- for(int i = 0; i < n; i++)
- {
- assert(-1 <= p[i] && p[i] < n);
- if(p[i] == -1)
- s.insert({cost[i] , i});
- else
- g[p[i]].PB(i);
- }
- int cnt = 0;
- while(days)
- {
- if(SZ(s))
- {
- int idx = s.begin()->Y;
- if(cost[s.begin()->Y] <= balance)
- {
- balance -= cost[s.begin()->Y];
- curProfit += profit[s.begin()->Y];
- balance += curProfit;
- days--;
- }
- else
- {
- if(curProfit <= 0)
- {
- balance += curProfit * days;
- days = 0;
- }
- else
- {
- LL need = (cost[s.begin()->Y] - balance + curProfit - 1) / curProfit;
- if(need > days)
- {
- balance += days * curProfit;
- days = 0;
- }
- else
- {
- days -= need;
- balance += need * curProfit;
- }
- }
- }
- cnt++;
- s.erase({cost[idx] , idx});
- for(auto v : g[idx])
- {
- s.insert({cost[v] , v});
- }
- }
- else
- {
- balance += curProfit * days;
- days = 0;
- }
- }
- cerr << cnt << endl;
- return balance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement