Igor found out discounts in a shop and decided to buy n items. Discounts at the store will last for a week and Igor knows about each item that its price now is ai, and after a week of discounts its price will be bi.
Not all of sellers are honest, so now some products could be more expensive than after a week of discounts.
Igor decided that buy at least k of items now, but wait with the rest of the week in order to save money as much as possible. Your task is to determine the minimum money that Igor can spend to buy all n items.
In the first line there are two positive integer numbers n and k (1 ≤ n ≤ 2·105, 0 ≤ k ≤ n) — total number of items to buy and minimal number of items Igor wants to by right now.
The second line contains sequence of integers a1, a2, ..., an (1 ≤ ai ≤ 104) — prices of items during discounts (i.e. right now).
The third line contains sequence of integers b1, b2, ..., bn (1 ≤ bi ≤ 104) — prices of items after discounts (i.e. after a week).
Print the minimal amount of money Igor will spend to buy all n items. Remember, he should buy at least k items right now.
3 1 5 4 6 3 1 5
10
5 3 3 4 7 10 3 4 5 5 12 5
25
In the first example Igor should buy item 3 paying 6. But items 1 and 2 he should buy after a week. He will pay 3 and 1 for them. So in total he will pay 6 + 3 + 1 = 10.
In the second example Igor should buy right now items 1, 2, 4 and 5, paying for them 3, 4, 10 and 3, respectively. Item 3 he should buy after a week of discounts, he will pay 5 for it. In total he will spend 3 + 4 + 10 + 3 + 5 = 25.
解题思路:
根据先算出今天的价格低的,如果比K大,则都买,如果比k小,则选k个相差较小的。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e6+5;
typedef struct node{
int x;
int index;
bool operator<(const node &B)
{
return this->x< B.x;
}
}NODE;
string s ;
vi a, b;
vector<NODE> aa;
bool ff[N];
ll n,k,t,ct;
ll re;
int main()
{
ios::sync_with_stdio(false);
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin >> t;
a.push_back(t);
}
for(int i=0;i<n;i++)
{
cin >> t;
b.push_back(t);
}
NODE tt;
ct = 0;
for(int i=0;i<n;i++)
{
tt.x = a[i]-b[i];
if(tt.x<=0)
ct++;
tt.index = i;
aa.push_back(tt);
}
sort(aa.begin(),aa.end());
if(ct<k)
{
for(int i=0;i<k;i++)
{
re += a[aa[i].index];
ff[aa[i].index]=1;
}
for(int i=0;i<n;i++)
{
if(!ff[i])
{
re += b[i];
}
}
}
else
{
for(int i=0;i<ct;i++)
{
re += a[aa[i].index];
ff[aa[i].index]=1;
}
for(int i=0;i<n;i++)
{
if(!ff[i])
{
re += b[i];
}
}
}
cout <<re<<endl;
return 0;
}