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

inputCopy

5

3 7 9 7 8

5 2 5 7 5

outputCopy

6

inputCopy

5

1 2 3 4 5

1 1 1 1 1

outputCopy

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,2,3然后处理。

#include <bits/stdc++.h>
using namespace std;
int n, t, a[3000000]; struct node
{
int first, second;
bool operator<(const node &b) const
{
if (first == b.first)
return second < b.second;
else
return first > b.first;
}
};
priority_queue<node> ms;
int main()
{
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> a[i];
}
for (int i = 0; i < n; ++i)
{
int b;
cin >> b;
ms.push(node{a[i], b});
}
long long cnt = 0;
while (ms.size())
{
auto po = ms.top();
ms.pop();
//cout << po.first << " " << po.second << endl;
//cout << 1 << endl;
if (ms.empty())
break;
int f=1;
while (po.first == ms.top().first && ms.size())
{
auto pi = ms.top();
//cout<<pi.first + 1<<" "<<pi.second<<endl;
ms.pop();
ms.push(node{pi.first + f, pi.second});
cnt += pi.second;
f++;
// cout << cnt << endl;
// cout<<ms.top().first<<endl;
// cout << ms.size() << endl;
}
}
cout << cnt << endl;
}

上分代码的思路确实有问题,时间复杂度太高。思路差不多,都是先处理权值大的,让权值小移动。

#include <bits/stdc++.h>
using namespace std;
int n, t; struct node
{
int data;
long long cost;
bool operator<(const node &b) const
{
if (cost == b.cost)
return data < b.data;
else
return cost > b.cost;
}
} a[3000000];
map<int, int> fa;
int find(int a)
{
if (fa[a] == 0)
return a;
else
return fa[a] = find(fa[a]);
}
void unite(int x, int y)
{
x = find(x);
y = find(y);
if (x != y)
fa[x] = y;
}
int main()
{
cin >> n;
fa.clear();
for (int i = 0; i < n; ++i)
scanf("%d", &a[i].data); for (int i = 0; i < n; ++i)
scanf("%lld", &a[i].cost);
sort(a, a + n);
long long ans = 0;
for (int i = 0; i < n; i++)
{
int x=find(a[i].data) ;
ans += a[i].cost * (x- a[i].data);
unite(x,x+1);
}
cout << ans << endl;
}

Codeforces Round 623(Div. 2,based on VK Cup 2019-2020 - Elimination Round,Engine)D. Recommendations的更多相关文章

  1. Codeforces Round #623 (Div. 1, based on VK Cup 2019-2020 - Elimination Round, Engine)A(模拟,并查集)

    #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; pair<]; bool cmp( ...

  2. Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine)

    A. Dead Pixel(思路) 思路 题意:给我们一个m*n的表格,又给了我们表格中的一个点a,其坐标为(x, y),问在这个表格中选择一个不包括改点a的最大面积的矩形,输出这个最大面积 分析:很 ...

  3. Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine) C. Restoring

    C. Restoring Permutation time limit per test1 second memory limit per test256 megabytes inputstandar ...

  4. Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine) B. Homecoming

    After a long party Petya decided to return home, but he turned out to be at the opposite end of the ...

  5. Codeforces Round #623 (Div. 2, based on VK Cup 2019-2020 - Elimination Round, Engine) A Dead Pixel

    讨论坏点的左右上下的矩形大小. #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> ...

  6. Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final)【ABCDF】

    比赛链接:https://codeforces.com/contest/1443 A. Kids Seating 题意 构造一个大小为 \(n\) 的数组使得任意两个数既不互质也不相互整除,要求所有数 ...

  7. Codeforces Round #681 (Div. 1, based on VK Cup 2019-2020 - Final) B. Identify the Operations (模拟,双向链表)

    题意:给你一组不重复的序列\(a\),每次可以选择一个数删除它左边或右边的一个数,并将选择的数append到数组\(b\)中,现在给你数组\(b\),问有多少种方案数得到\(b\). 题解:我们可以记 ...

  8. Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final) D. Extreme Subtraction (贪心)

    题意:有一个长度为\(n\)的序列,可以任意取\(k(1\le k\le n)\),对序列前\(k\)项或者后\(k\)减\(1\),可以进行任意次操作,问是否可以使所有元素都变成\(0\). 题解: ...

  9. Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final) C. The Delivery Dilemma (贪心,结构体排序)

    题意:你要买\(n\)份午饭,你可以选择自己去买,或者叫外卖,每份午饭\(i\)自己去买需要消耗时间\(b_i\),叫外卖需要\(a_i\),外卖可以同时送,自己只能买完一份后回家再去买下一份,问最少 ...

随机推荐

  1. spring04

    今天设计到的内容是是spring的sqel和ioc的生命周期和bean的后置处理器 具体的测试文件如下: <?xml version="1.0" encoding=" ...

  2. 2018蓝桥杯省赛(C/C++ C组)

    因进考场不让带优盘,顾想不起有些啥题了,静待更新吧! 再次强调C++最新标准,main函数必须指定返回类型为int,且返回值最好为0(人走的多了就是路了,有人偏返回999那也没办法) 1.大概是小明给 ...

  3. Python+Tornado开发微信公众号

    本文已同步到专业技术网站 www.sufaith.com, 该网站专注于前后端开发技术与经验分享, 包含Web开发.Nodejs.Python.Linux.IT资讯等板块. 本教程针对的是已掌握Pyt ...

  4. intelij idea 和 eclipse 使用上的区别

    一.项目创建区别 使用基于IntelliJ的IDE,都会对project和module的关系比较糊涂.用简单的一句话来概括是: IntelliJ系中的Project相当于Eclipse系中的works ...

  5. NonRegisteringDriver造成的内存频繁FullGc

    某天上服务器看了下gc情况,发现状况不对,启动了才2天的服务器发生了360次fullgc,这个频率肯定高了 说明 S0C.S1C.S0U.S1U:Survivor 0/1区容量(Capacity)和使 ...

  6. Powershell抓取网页信息

    一般经常使用invoke-restmethod和invoke-webrequest这两个命令来获取网页信息,如果对象格式是json或者xml会更容易 1.invoke-restmethod 我们可以用 ...

  7. jpa是什么,和hibernate 有什么关系

    JPA通过JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中.JPA 的目标之一是制定一个可以由很多供应商实现的API,并且开发人员可以编码来实现该API,而不 ...

  8. 浅谈ArrayList

    浅谈ArrayList 废话不多说(事实是不会说),让我们直接进入正题 首先讲一讲最基本的ArrayList的初始化,也就是我们常说的构造函数,ArrayList给我们提供了三种构造方式,我们逐个来查 ...

  9. 选择IT行业的自我心得,希望能帮助到各位!(二)

    在前面说道的一,希望大家能喜欢,这也只是自己的一种经历,每个人都有年轻的时候,谁的年级都有自以为是,谁的年轻都有风华正茂,谁的年轻都让自己的内涵给我们自己摔了一个狠狠的道理,人外有人天外有天.我记得当 ...

  10. day01,了解gcc

    今天主要是学一下gcc 功能选项: 一. 1.  gcc -E:表示预处理,把指令处理掉 2.gcc -o:改变目标文件名称 3. gcc -c: 表示只编译不链接(也就是不生成a.out) 4. g ...