Codeforces 1132 B 贪心 水题

http://codeforces.com/problemset/problem/1132/B

ou came to a local shop and want to buy some chocolate bars. There are nn bars in the shop, ii -th of them costs aiai coins (and you want to buy all of them).

You have mm different coupons that allow you to buy chocolate bars. ii -th coupon allows you to buy qiqi chocolate bars while you have to pay only for the qi−1qi−1 most expensive ones (so, the cheapest bar of those qiqi bars is for free).

You can use only one coupon; if you use coupon ii , you have to choose qiqi bars and buy them using the coupon, and buy all the remaining n−qin−qi bars without any discounts.

To decide which coupon to choose, you want to know what will be the minimum total amount of money you have to pay if you use one of the coupons optimally.

Input

The first line contains one integer nn (2≤n≤3⋅1052≤n≤3⋅105 ) — the number of chocolate bars in the shop.

The second line contains nn integers a1a1 , a2a2 , ..., anan (1≤ai≤1091≤ai≤109 ), where aiai is the cost of ii -th chocolate bar.

The third line contains one integer mm (1≤m≤n−11≤m≤n−1 ) — the number of coupons you have.

The fourth line contains mm integers q1q1 , q2q2 , ..., qmqm (2≤qi≤n2≤qi≤n ), where qiqi is the number of chocolate bars you have to buy using ii -th coupon so that the least expensive of them will be for free. All values of qiqi are pairwise distinct.

Output

Print mm integers, ii -th of them should be the minimum amount of money you have to pay if you buy qiqi bars with ii -th coupon, and all the remaining bars one by one for their full price.

Example

Input

7
7 1 3 1 4 10 8
2
3 4

Output

27
30

Note

Consider the first example.

If we use the first coupon, we may choose chocolate bars having indices 11 , 66 and 77 , and we pay 1818 coins for them and 99 coins for all other bars.

If we use the second coupon, we may choose chocolate bars having indices 11 , 55 , 66 and 77 , and we pay 2525 coins for them and 55 coins for all other bars.

题目大意: 你要买n种巧克力, 第i种巧克力花费ai的钱,你有m种优惠券, 第i种优惠券的值为bi, 你可以从n种巧克力中买bi种, 可以优惠这bi种巧克力中最便宜的那种巧克力的价钱。 你要买全部的巧克力, 对应每种优惠券, 请输出所花的最少的钱。

思路: 贪心水题, 可以优惠bi种巧克力中最便宜的那种巧克力的钱, 自然要买最贵的bi种巧克力, 这样才能省的钱多。 用前缀和数组和后缀和数组优化。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
typedef long long ll;
using namespace std;

const int maxn=300005;

int a[maxn];
ll psum[maxn];//前缀和
ll nsum[maxn];//后缀和

int main()
{
    int n,m;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++)
        psum[i]=psum[i-1]+a[i];
    for(int i=n;i>=1;i--)
        nsum[i]=nsum[i+1]+a[i];
    scanf("%d",&m);
    int temp;
    for(int i=0;i<m;i++)
    {
        scanf("%d",&temp);
        printf("%lld\n",nsum[n-temp+2]+psum[n-temp]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiji333/article/details/88344131