您的位置:首页 > 其它

Codeforces 767E 优先队列

2017-04-05 21:33 471 查看
Change-free

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Student Arseny likes to plan his life for n days ahead. He visits a canteen every day and he has already decided what he will order
in each of the following n days. Prices in the canteen do not change and that means Arseny will spend ci rubles
during the i-th day.

There are 1-ruble coins and 100-ruble
notes in circulation. At this moment, Arseny has m coins and a sufficiently large amount of notes (you can assume that he has an infinite
amount of them). Arseny loves modern technologies, so he uses his credit card everywhere except the canteen, but he has to pay in cash in the canteen because it does not accept cards.

Cashier always asks the student to pay change-free. However, it's not always possible, but Arseny tries to minimize the dissatisfaction of the cashier. Cashier's dissatisfaction for each of the
days is determined by the total amount of notes and coins in the change. To be precise, if the cashier gives Arseny x notes and coins
on the i-th day, his dissatisfaction for this day equals x·wi.
Cashier always gives change using as little coins and notes as possible, he always has enough of them to be able to do this.


"Caution!
Angry cashier"

Arseny wants to pay in such a way that the total dissatisfaction of the cashier for n days would be as small as possible. Help him
to find out how he needs to pay in each of the n days!

Note that Arseny always has enough money to pay, because he has an infinite amount of notes. Arseny can use notes and coins he received in change during any of the following days.

Input

The first line contains two integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 109) —
the amount of days Arseny planned his actions for and the amount of coins he currently has.

The second line contains a sequence of integers c1, c2, ..., cn (1 ≤ ci ≤ 105) —
the amounts of money in rubles which Arseny is going to spend for each of the following days.

The third line contains a sequence of integers w1, w2, ..., wn (1 ≤ wi ≤ 105) —
the cashier's dissatisfaction coefficients for each of the following days.

Output

In the first line print one integer — minimum possible total dissatisfaction of the cashier.

Then print n lines, the i-th
of then should contain two numbers — the amount of notes and the amount of coins which Arseny should use to pay in the canteen on the i-th
day.

Of course, the total amount of money Arseny gives to the casher in any of the days should be no less than the amount of money he has planned to spend. It also shouldn't exceed 106 rubles:
Arseny never carries large sums of money with him.

If there are multiple answers, print any of them.

Examples

input
5 42
117 71 150 243 200
1 1 1 1 1


output
79
1 17
1 0
2 0
2 43
2 0


input
3 0
100 50 50
1 3 2


output
150
1 0
1 0
0 50


input
5 42
117 71 150 243 200
5 4 3 2 1


output
230
1 17
1 0
1 50
3 0
2 0


题意:给出n天  现在你有无限张纸币和m个一元硬币  每天你要花a[i]的钱  每天老板找零会增加  找零钱数*w[i]的不满意度

求最小不满意度以及方案

题解:用贪心的方法  如果1元硬币够  那我们就用100元纸币和1元硬币买

否则我们找到前面一天没有找零的  而且老板找零获得不满意度最小的  把那天改为找零

用优先队列维护即可

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
struct node{
ll num,lab;
bool operator<(const node& a)const{
return num>a.num;
}
}e[100005];
ll a[100005],w[100005],ans1[100005],ans2[100005];
priority_queue<node,vector<node>,less<node> >sp;
int main(){
ll n,m,i,j,ans=0;
scanf("%lld%lld",&n,&m);
for(i=1;i<=n;i++)scanf("%lld",&a[i]);
for(i=1;i<=n;i++)scanf("%lld",&w[i]);
for(i=1;i<=n;i++){
if(m<a[i]%100){
if(sp.empty()){
ans+=(100-a[i]%100)*w[i];
m+=100-a[i]%100;
ans1[i]=a[i]/100+1;
ans2[i]=0;
}
else{
sp.push((node){(100-a[i]%100)*w[i],i});
ans1[i]=a[i]/100;
ans2[i]=a[i]%100;
ans+=sp.top().num;
ans1[sp.top().lab]++;
ans2[sp.top().lab]=0;
sp.pop();
m+=100-a[i]%100;
}
}
else{
if(a[i]%100)sp.push((node){(100-a[i]%100)*w[i],i});
ans1[i]=a[i]/100;
ans2[i]=a[i]%100;
m-=a[i]%100;
}
}
printf("%lld\n",ans);
for(i=1;i<=n;i++)printf("%lld %lld\n",ans1[i],ans2[i]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: