Recommendations

VK news recommendation system daily selects interesting publications of one of n disjoint categories for each user. Each publication belongs to exactly one category. For each category i batch algorithm selects ai publications.

The latest A/B test suggests that users are reading recommended publications more actively if each category has a different number of publications within daily recommendations. The targeted algorithm can find a single interesting publication of i-th category within ti seconds.

What is the minimum total time necessary to add publications to the result of batch algorithm execution, so all categories have a different number of publications? You can’t remove publications recommended by the batch algorithm.

Input
The first line of input consists of single integer n — the number of news categories (1≤n≤200000).

The second line of input consists of n integers ai — the number of publications of i-th category selected by the batch algorithm (1≤ai≤109).

The third line of input consists of n integers ti — time it takes for targeted algorithm to find one new publication of category i (1≤ti≤105).

Output
Print one integer — the minimal required time for the targeted algorithm to get rid of categories with the same size.

Examples

input
5
3 7 9 7 8
5 2 5 7 5
output
6
input
5
1 2 3 4 5
1 1 1 1 1
output
0

Note
In the first example, it is possible to find three publications of the second type, which will take 6 seconds.

In the second example, all news categories contain a different number of publications.
贪心策略自小到大扫描新闻,如果相等,则将相等的新闻中除时间最大的之外都加上1

#include <bits/stdc++.h>

#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
using namespace std;
const int N = 2e5 + 10;
pii a[N];
priority_queue<int> q;
int n;
ll ans;

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)scanf("%d", &a[i].fi);
    for (int i = 1; i <= n; i++)scanf("%d", &a[i].se);
    sort(a + 1, a + 1 + n);
    int now = 1, num = 0;
    ll tmp = 0;
    while (!q.empty() || now <= n) {
        num++;
        if (q.empty()) {
            num = a[now].fi;
            while (now <= n && num == a[now].fi)
                tmp += a[now].se, q.push(a[now].se), now++;
        } else
            while (now <= n && num == a[now].fi)
                tmp += a[now].se, q.push(a[now].se), now++;
        tmp -= q.top(), q.pop();
        ans += tmp;
    }
    printf("%lld\n", ans);
}
发布了360 篇原创文章 · 获赞 28 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45323960/article/details/105247961