Untitled

mail@pastecode.io avatar
unknown
c_cpp
7 months ago
466 B
9
Indexable
Never
int getMinimumCost(vector<int> a, vector<int> b, int m) {
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
    vector<int> ctr(a.size(), 1);
    for (int i=0; i<a.size(); i++) {
        pq.push({a[i], i});
    }
    int ans = 0;
    while(m--) {
        auto pi = pq.top();
        pq.pop();
        ans += pi.first;
        int i = pi.second;
        pq.push({a[i] + ctr[i]*b[i], i});
        ctr[i]++;
    }
    return ans;
}
Leave a Comment