prompt
string | response
string |
---|---|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n,m;
while(cin >> n >> m,n){
int p[1000],sum = 0;
for(int i = 0;i < n;i++) cin >> p[i];
sort(p,p + n,greater<int>());
for(int i = 0;i < n;i++){
if(i % m != m - 1) sum += p[i];
}
cout << sum << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n,m,sum;
int p[1010];
while(cin >> n >> m,n!=0&&m!=0){
for(int i=0;i<n;i++){
cin >> p[i];
}
sort(p,p+n);
reverse(p,p+n);
sum = 0;
for(int i=0;i<n;i++){
if( (i+1)%m != 0) sum += p[i];
}
cout << sum << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
using namespace std;
int main()
{
int n,m,i,j,p[1000];
for(;;)
{
cin >> n >> m;
if(n==0)
break;
int ans=0;
for(i=0;i<n;i++)
{
cin >> p[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(p[i]<p[j])
{
int a;
a=p[i];
p[i]=p[j];
p[j]=a;
}
}
}
for(i=0;i<n;i++)
{
if((i+1)%m!=0)
ans+=p[i];
}
cout << ans << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m;
while(cin >> n >> m, n){
vector<int> a(n);
for(int i=0;i<n;i++)cin >> a[i];
sort(a.begin(),a.end(),greater<int>());
int ans = 0;
for(int i=0;i<n;i++){
if((i+1)%m)ans += a[i];
}
cout << ans << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<cstdio>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int main(void){
int n,m;
while(scanf("%d %d", &n, &m) && n != 0 ){
vector<int > a;
int temp;
int ans = 0;
for(int i = 0; i < n; i++){
scanf("%d", &temp);
a.push_back(temp);
}
sort(a.begin(),a.end(),greater<int>());
for(int i = 0; i < n; i++){
if( ( i+1)%m != 0 ) ans += a[i];
}
printf("%d\n",ans);
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,m,ans,a,nk,count;
while(cin>>n>>m){
if(n==0&&m==0) break;
int p[n];
nk = n;
count = 0;
for(int i=0;i<n;i++) cin >> p[i];
sort(p,p+n,greater<int>());
ans = 0;
a = 0;
while(nk>=m){
for(int i=a;i<(a+m)-1;i++) ans += p[i];
a += m;
nk -= m;
count += m;
}
if(n>count){
for(int i=count;i<n;i++) ans += p[i];
}
cout << ans << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON3):
|
while True:
n, m = [int(i) for i in input().split()]
if n == 0 and m == 0:
break
p = [int(i) for i in input().split()]
p.sort()
p.reverse()
ans = 0
for i in range(len(p)):
if (i+1) % m > 0:
ans = ans + p[i]
print(ans)
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON):
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import itertools
import math
from collections import Counter, defaultdict
class Main(object):
def __init__(self):
pass
def solve(self):
'''
insert your code
'''
while True:
n, m = map(int, raw_input().split())
if n == 0 and m == 0:
break
vegi = map(int, raw_input().split())
vegi.sort(reverse=True)
ans = 0
for i in range(len(vegi)):
if i % m != m - 1:
ans += vegi[i]
print ans
return None
if __name__ == '__main__':
m = Main()
m.solve()
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
int main(){
int n,m;
int yasai[1000];
int zenbu=0;
int c=0;
while(true){
cin>>n>>m;
if(n==0&&m==0)break;
for(int i=0;i<n;i++){
cin>>yasai[i];
}
sort(yasai,yasai+n,greater<int>());
for(int i=0;i<n;i++){
if(c==m-1)c=0;
else {
zenbu+=yasai[i];
c++;
}
}
cout<<zenbu<<endl;
zenbu=0;
c=0;
for(int i=0;i<n;i++)yasai[i]=0;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
using namespace std;
int n,m,p[1000],ans;
int main(){
while(1){
ans=0;
cin>>n>>m;
if(n==0&&m==0) break;
for(int i=0;i<n;i++)cin>>p[i];
sort(p,p+n);
for(int i=n-1;i>=0;i--)
if((n-i)%m!=0)ans+=p[i];
cout<<ans<<endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <algorithm>
#include <deque>
#include <functional>
#include <iostream>
using namespace std;
int main() {
int n, m, j=0, x, min;
deque<int> p;
while(cin >> n >> m) {
if(n==0&&m==0)
break;
int a=m-1;
min=0;
p.clear();
for(int i=0; i<n; i++) {
cin >> x;
p.push_back(x);
}
sort(p.begin(), p.end(), greater<int>());
for(int i=0; i<p.size(); i++) {
(i==a) ? a+=m : min+=p[i];
}
cout << min << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<set>
using namespace std;
int main()
{
multiset< int,greater<int> > s;
multiset<int>::iterator it;
int n,m,p,i;
int cost;
while(cin>>n>>m,n||m){
s.clear();
for(i=0;i<n;i++){
cin>>p;
s.insert(p);
}
cost=0;
i=0;
for(it=s.begin();it!=s.end();it++){
if(++i==m) i=0;
else cost += *it;
}
cout<<cost<<endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int a,b,i,sum=0;
int c[10000];
while(1){
cin >> a >> b;
if(a==0 && b==0) break;
for(i=0;i<a;i++){
cin >> c[i];
sum+=c[i];
}
sort(c,c+a);
reverse(c,c+a);
for(i=b-1;i<a;i+=b){
sum-=c[i];
}
cout << sum << endl;
sum=0;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void solve()
{
int n, m;
while(cin >> n >> m, n || m)
{
vector<int> Vec;
for(int i = 0; i < n; ++i)
{
int price;
cin >> price;
Vec.push_back(price);
}
sort(Vec.begin(), Vec.end());
int count = 1;
int sum = 0;
for(int i = Vec.size() - 1; i >= 0; --i)
{
if(count == m)
{
count = 1;
continue;
}
sum += Vec[i];
++count;
}
cout << sum << endl;
}
}
int main()
{
solve();
return(0);
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
new Main().run();
}
private void run() throws IOException {
Scanner scanner = new Scanner(System.in);
while (true) {
int n = scanner.nextInt();
int m = scanner.nextInt();
if ((n | m) == 0)
break;
int[] num = new int[n];
int sum = 0;
for (int i = 0; i < n; i++) {
num[i] = scanner.nextInt();
sum += num[i];
}
int mod = n % m;
Arrays.sort(num);
for (int i = mod; i < n; i += m) {
sum -= num[i];
}
System.out.println(sum);
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
int main(){
int n,m; //問題文のn,m
while(std::cin >> n >> m){
if(n==0&&m==0) break;
std::vector<int> p(n); //野菜価格一覧
for(int i=0;i<n;++i){
std::cin >> p[i];
}
sort(p.begin(), p.end());
reverse(p.begin(), p.end()); //降順ソート
for(int i=m-1;i<=n-1;i+=m){
p[i]=0;
}
std::cout << accumulate(p.begin(), p.end(), 0) << std::endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON3):
|
while 1:
qua,bag = input().split()
qua = int(qua)
bag = int(bag)
if qua == 0:
break
price = list(map(int,input().split()))
price.sort(reverse=True)
for i in range(qua):
if (i+1)%bag == 0:
price[i]=-1
for i in range(qua//bag):
price.remove(-1)
pay = sum(price)
print(pay)
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,m;
while(cin>>n>>m,n||m){
int d[1000],sum=0;
for(int i=0;i<n;i++)cin>>d[i];
sort(d,d+n,greater<int>());
for(int i=0;i<n;i++){
if((i+1)%m!=0)sum+=d[i];
}
cout<<sum<<endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON3):
|
while True:
try:
num, size = map(int, input().split())
arr = list(map(int, input().split()))
arr.sort(reverse = True)
for i in range(size-1, num, size):
arr[i] = 0
print(sum(arr))
except:
break
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
#define pb push_back
int main()
{
int n,m;
while(1)
{
cin >> n >> m;
if(n == 0 && m == 0) return 0;
vector<int>vec;
for(int i=0;i<n;i++)
{
int x; cin >> x; vec.pb(x);
}
sort(vec.begin(),vec.end(),greater<int>());
int res = 0;
for(int i=0;i<vec.size();i++) if(i%m != m-1) res += vec[i];
cout << res << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,sum=0,d;
int p[1100];
while(1){
sum = 0;
cin >> n >> m;
if(n==0 && m==0) break;
for(int i=0; i<n; i++) {
cin >> p[i];
sum += p[i];
}
sort(p,p+n);
reverse(p,p+n);
/*for(int i=1; i<=n/m; i++) p[i*m-1]=0;
for(int i=0; i<n; i++) sum += p[i];*/
for(int i=1; i<=n/m; i++){
d = m*i-1;
sum -= p[d];
}
cout << sum << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <queue>
int main(){
int m,n;
while(std::cin >> n >> m){
//n,mがゼロで終了
if(n == 0 && m == 0){
break;
}
int sum_cost = 0;
std::priority_queue<int> vegetable;
for(int i = 0; i < n; ++i){
int cost;
std::cin >> cost;
vegetable.push(cost);
}
for(int i = 1; i <= n; ++i){
if(i % m){
sum_cost += vegetable.top();
vegetable.pop();
}
else
vegetable.pop();
}
std::cout << sum_cost << std::endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<stdio.h>
#include<algorithm>
#include<numeric>
int main()
{
int m,n,p[10000],i,d;
while(scanf("%d%d",&n,&m),m)
{
for(d=i=0;i<n;++i)scanf("%d",&p[i]);
std::sort(p,p+n);
for(i=n-m;i>=0;i-=m)d+=p[i];
printf("%d\n",std::accumulate(p,p+n,0)-d);
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
bool solve(){
int n, m;
cin >> n >> m;
if(n == 0) return false;
vector<int> p(n);
for(int i=0;i<n;i++){
cin >> p[i];
}
sort(p.rbegin(), p.rend());
int ans = 0;
for(int i=0;i<n;i++){
if((i+1)%m==0) continue;
ans += p[i];
}
cout << ans << endl;
return true;
}
int main() {
while(solve());
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, m;
while (cin >> n >> m, n || m) {
vector<int> v;
for (int i = 0; i < n; ++i) {
int p; cin >> p;
v.push_back(p);
}
sort( v.begin(), v.end() );
reverse( v.begin(), v.end() );
int ans = 0, count = 0;
for (int i = 0; i < n; ++i)
if (++count >= m)
count = 0;
else
ans += v[i];
cout << ans << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int n,m;
while(cin>>n>>m&&n)
{
vector<int> a;
int ans=0;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
a.push_back(x);
}
sort(a.begin(),a.end());
for(int i=0;i<n%m;i++)
{
ans+=a[0];;
a.erase(a.begin());
}
n-=n%m;
for(int i=0;i<a.size();i++)ans+=a[i];
for(int i=0;i<n/m;i++)
{
ans-=a[i*m];
}
cout<<ans<<endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int ans=0;
int be[10002];
int x=-1,n,m;
while(1){
x=-1;
ans=0;
cin>>n>>m;
if(m==0&&n==0)break;
for(int i=0;i<n;i++){
cin>>be[i];
ans+=be[i];
}
sort(be,be+n,greater<int>());
while(n>=m){
x+=m;
ans-=be[x];
n-=m;
}
cout<<ans<<endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main(){
int i,n,m,a,price[1000],count,out;
while(cin >> n >> m && (n != 0 && m != 0)){
count = out = 0;
for(i = 0;i < n;i++){
cin >> a;
price[i] = a;
}
sort(price,price+n,greater<int>());
for(i = 0;i < n;i++){
out += price[i];
count++;
if(count == m) {
out -= price[i];
count = 0;
}
}
cout << out << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in);
static int n, m, p;
static int[] commodity;
public static void main(String[] args) {
while(read()){
solve();
}
}
static boolean read(){
n = sc.nextInt(); m = sc.nextInt();
if(n == 0 && m == 0)return false;
commodity = new int[n];
for(int i = 0; i < n; i++)
commodity[i] = sc.nextInt();
return true;
}
static void solve(){
int nBag = n/m;
java.util.Arrays.sort(commodity);
for(int i = 1; i <= nBag; i++)
commodity[n - m*i] = 0;
int Sum = 0;
for(int i = 0; i < n; i++)
Sum += commodity[i];
System.out.println(Sum);
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
while(true) {
int n = stdIn.nextInt();
int m = stdIn.nextInt();
if(n == 0 && m == 0) {
break;
}
int[] p = new int[n];
for(int i = 0; i < n; i++) {
p[i] = stdIn.nextInt();
}
Arrays.sort(p);
int sum = 0;
int counter = 0;
for(int i = p.length-1; i >= 0; i--) {
if(counter == m - 1) {
counter = 0;
continue;
}
sum += p[i];
counter++;
}
System.out.println(sum);
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<cstdio>
#include<algorithm>
using namespace std;
int main(){
int n,m;
while(scanf("%d%d",&n,&m)){
if(n==0) break;
int x[1000],i,sum=0;
for(i=0;i<n;i++){
scanf("%d",&x[i]);
sum+=x[i];
}
sort(x,x+n);
for(i=n%m;i<n;i+=m){
sum-=x[i];
}
printf("%d\n",sum);
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <string>
#include <cmath>
#include <cfloat>
#include <map>
using namespace std;
int main(){
int n,m;
while(cin>>n>>m,n!=0||m!=0){
vector<int> a(n);
for(int i=0;i<n;i++)
cin>>a[i];
sort(a.begin(),a.end());
int s=0;
int k=1;
for(int i=n-1;i>=0;i--){
if(k!=m){
s+=a[i];
k++;
}
else
k=1;
}
cout<<s<<endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include"iostream"
using namespace std;
int main() {
while (1) {
int n, m,p[10000],sum=0;
cin >> n >> m;
if (n == 0 && m == 0)break;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int j = 0; j < n - 1; j++) {
for (int i = 1; i < n ; i++) {
if (p[i] < p[i + 1]) {
swap(p[i], p[i + 1]);
}
}
}
for (int i = 1; i <= n; i++) {
if (i%m != 0) {
sum += p[i];
}
}
cout << sum << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
int n,m,*b,ans;
int main(){
while(true){
cin >> n >> m;
if(!n && !m)
return 0;
b = new int[n];
ans = 0;
for(int i=0; i<n; i++)
cin >> b[i];
sort(b,b+n,greater<int>());
for(int i=1; i<=n; i++){
if(i%m == 0)
continue;
ans += b[i-1];
}
cout << ans << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
#define MAX 1000;
using namespace std;
int main(){
int n,m,s;
while(cin >> n >> m){
int p[n];
if(n==0 || m==0) return 0;
for(int i=0; i<n; i++){
cin >> p[i];
}
int sum=0,ans=0, min;
sort(p, p+n, greater<int>());
for(int i=0; i<n; i+=m){
sum=0;
for(int j=i; j<i+m && j<n; j++){
sum += p[j];
}
if(i+m <= n) sum -= p[i+m-1];
ans += sum;
}
cout << ans << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON3):
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
while True:
n,m = map(int,input().split(" "))
if n == 0 and m == 0:
break
prices = list(map(int,input().split(" ")))
count = 0
cost = 0
for p in reversed(sorted(prices)):
count += 1
if count >= m:
count = 0
continue
else:
cost += p
print(cost)
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <vector>
#include <algorithm>
int main(){
int n,m;
while(std::cin >> n >> m){
if(n == 0 && m == 0){
break;
}
std::vector<int> vegetable(n,0);
for(int i = 0; i < n; ++i){
std::cin >> vegetable[i];
}
std::sort(vegetable.begin(), vegetable.end(), std::greater<int>());
int price = 0;
for(int i = 0; i < n; ++i){
price += vegetable.at(i);
}
for(int i = 1; i <= n/m; ++i){
price -= vegetable.at(i * m - 1);
}
std::cout << price << std::endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
for(;;)
{
int n=in.nextInt();
int m=in.nextInt();
if((n|m)==0)
return;
ArrayList<Integer> L=new ArrayList<Integer>();
int sum=0;
int item=1;
for(int i=0;i<n;i++)
L.add(in.nextInt());
Collections.sort(L);
Collections.reverse(L);
for(int i=0;i<n;i++)
{
sum+=L.get(i);
if(item%m==0)
sum-=L.get(i);
item++;
}
System.out.println(sum);
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON3):
|
while 1:
n, m = map(int, input().split())
if n == 0:
break
veg = list(map(int, input().split()))
veg.sort(reverse=True)
cnt = 0
ans = 0
while veg != []:
cnt += 1
tmp = veg.pop(0)
if cnt % m == 0:
pass
else:
ans += tmp
print(ans)
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
#include <vector>
int main(){
int n,m;
while(std::cin >> n >> m){
if(n == 0 && m == 0) return 0;
std::vector<int> a(n);
for(int i = 0; i < n; ++i){
std::cin >> a[i];
}
std::sort(a.begin(), a.end(), std::greater<int>());//降順
int cost = 0;
for(int i = 0; i < n; ++i){
if((i+1) % m != 0) cost += a[i];//mの倍数番目に高い野菜を無料にする
}
std::cout << cost << std::endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,temp;
while(1){
cin >> n >> m;
int a[n+1],sum=0;
if(n==m && n==0) break;
for(int i=1;i<=n;i++){
cin >> a[i];
}
for(int i=1;i<n;i++){
for(int j=i+1;j<n+1;j++){
if(a[i]<a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(int i=1;i<n+1;i++){
if(i%m!=0){
sum+=a[i];
}
}
cout << sum << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <array>
#include <vector>
#include <functional>
using namespace std;
int main() {
while( true ) {
int n, m;
cin >> n >> m;
if ( n == 0 && m == 0 ) break;
vector<int> xs( n + 1 );
for ( int i = 1; i <= n; i++ ) {
cin >> xs[ i ];
}
sort( xs.begin() + 1, xs.end(), greater<int>() );
int sum = 0;
for ( int i = 1; i <= n; i++ ) {
sum += ( i % m == 0 ? 0 : xs[ i ] );
}
cout << sum << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<queue>
using namespace std;
int main(){
int n,m,sum,x;
while(cin>>n>>m,n||m){
sum=0;
priority_queue<int> que;
for(int i=0;i<n;i++){
cin>>x;
que.push(x);
}
for(int i=1;i<=n;i++){
if((m+i)%m!=0){
sum+=que.top();
}
que.pop();
}
cout<<sum<<endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
int p[1010];
int main(){
while(1){
int n,m,t=0;
cin>>n>>m;
if(n==0&&m==0) break;
for(int i=0;i<n;i++) cin>>p[i];
int f=1;
while(f==1){
f=0;
for(int j=0;j<n-1;j++){
if(p[j]<p[j+1]){
swap(p[j],p[j+1]);
f=1;
}
}
}
for(int i=1;i<=n/m;i++){
p[i*m-1]=0;
}
for(int i=0;i<n;i++){
t+=p[i];
}
cout<<t<<endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<algorithm>
#include<vector>
#include<iostream>
using namespace std;
vector<int> l;
int n,m;
bool input(){
cin>>n>>m;
if(n==m&&n==0)return false;
l.clear(),l.resize(n);
for(int i=0;i<n;i++){
cin>>l[i];
}
return true;
}
int solve(){
sort(l.rbegin(),l.rend());
int ans = 0;
for(int i=0;i<n;i++){
if( (i+1) %m == 0){
continue;
}
ans += l[i];
}
return ans;
}
int main(){
while(input()){
cout<<solve()<<endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
for(;;){
int n,m;
cin >> n >> m;
if(n == 0 || m == 0) break;
vector<int> v(n);
for(int i=0; i<n; i++){
cin >> v[i];
}
sort(v.rbegin(),v.rend());
int sum = 0;
for(int i=0; i<n; i++){
if(i % m == m-1) continue;
sum += v[i];
}
cout << sum << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <vector>
#include <algorithm>
int main(){
int num,cap,price;
while(std::cin >> num >> cap){
if(num == 0 && cap == 0){
break;
}
std::vector<int> vegetables;
for(int i=0; i<num; ++i){
std::cin >> price;
vegetables.push_back(price);
}
sort(vegetables.begin(), vegetables.end(), std::greater<int>());
int sum = 0;
int i = 0;
while(i < num){
if((i+1) % cap != 0){
sum += vegetables.at(i);
}
++i;
}
std::cout << sum << std::endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
vector<int>l;
int n, m;
while (cin >> n >> m){
if (n == 0 && m == 0)break;
int k[1001];
for (int i = 0; i < n; i++){
cin >> k[i];
}
sort(k, k + n);
int sum = 0;
for (int i = 0; i < n; i++)
if (n % m != i % m)sum += k[i];
l.push_back(sum);
}
for (int i : l) {
cout << i << endl;
}
char c;
cin >> c;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,m;
while(1){
cin>>n>>m;
if(n==0 && m==0) break;
int array[n],sum=0;
for(int i=0;i<n;i++)
cin>>array[i];
sort(array,array+n);
reverse(array,array+n);
for(int i=0;i<n;i++){
if((i+1)%m==0) array[i]=0;
sum+=array[i];
}
cout<<sum<<endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <numeric>
#include <algorithm>
#include <vector>
using namespace std;
int main(void){
int n,m,t;
while(cin >> n >> m){
if((n|m) == 0) break;
vector<int> val;
int ret;
for(int i=0;i<n;i++){
cin >> t;
val.push_back(t);
}
sort(val.begin(), val.end(), greater<int>());
ret = accumulate(val.begin(),val.end(),0);
for(int i=m-1;i<n;i+=m) ret -= val[i];
cout << ret << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
int main(){
int n, m;
int p[1000];
while(1){
cin >> n >> m;
if(n == 0 && m == 0) break;
for(int i = 0;i < n;i++){
cin >> p[i];
}
int ans = 0;
sort(p, p + n, greater<int>());
for(int i = 0;i < n;i++){
if(i % m == m - 1) continue;
ans += p[i];
}
cout << ans << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.Arrays;
import java.util.Scanner;
//Thanksgiving
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
int n = sc.nextInt();
int m = sc.nextInt();
if((n|m)==0)break;
int[] a = new int[n];
int s = 0;
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
s+=a[i];
}
Arrays.sort(a);
int k = 0;
for(int i=n-1;i>=0;i--){
k++;
if(k%m==0)s-=a[i];
}
System.out.println(s);
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
int main(){
long n,m,i,p[10000],s;
while(cin>>n>>m){
if(n==0)break;
for(i=0;i<n;i++)cin>>p[i];
sort(p,p+n,greater<long>());
for(s=i=0;i<n;i++)if(i%m!=m-1)s+=p[i];
cout<<s<<endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON):
|
while True:
N, M = [ int(_) for _ in raw_input().split() ]
if N == 0: break
p = [ int(_) for _ in raw_input().split() ]
p.sort()
p.reverse()
s = 0
for i in xrange(N):
if i % M != M - 1:
s += p[i]
print s
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
using namespace std;
int n, m;
int p[1005];
int sum=0;
void solve(){
for(int i=0;i<n;i++){
cin >> p[i];
sum+=p[i];
}
sort(p,p+n);
reverse(p,p+n);
for(int i=m-1;i<n;i+=m){
sum-=p[i];
}
cout << sum << endl;
sum=0;
}
int main(){
while(cin >> n >> m){
if(n==0 && m==0) break;
solve();
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
import static java.lang.Integer.parseInt;
/*
* Problem B: Thanksgiving
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
String[] words;
while ((line = br.readLine()) != null && !line.isEmpty()) {
int n, m;
n = parseInt(line.substring(0, line.indexOf(' ')));
m = parseInt(line.substring(line.indexOf(' ') + 1));
if ((n | m) == 0) break;
int[] p = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < n; i++) p[i] = parseInt(st.nextToken());
Arrays.sort(p);
//
int ans = 0;
if (n >= m) {
int r = n % m;
for (int i = 0; i < r; i++) {
ans += p[i];
}
for (int i = r; i < n; i++) {
if (((i - r) % m) != 0) ans += p[i];
}
} else {
for (int i = 0; i < n; i++) {
ans += p[i];
}
}
System.out.println(ans);
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON3):
|
while True:
try:
n,m = list(map(int,input().split()))
if n == 0 and m == 0:
break
amari = n % m
yasai = list(map(int,input().split()))
yasai.sort()
s = 0
if amari > 0:
s = sum(yasai[0:amari])
i = 0
for y in yasai[amari:]:
if i % m > 0:
s += y
i+=1
print(s)
except:
pass
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <stdio.h>
#include <algorithm>
int main(void)
{
int n;
int m;
int data[1000];
int ans;
while (1){
scanf("%d%d", &n, &m);
if (n == 0){
break;
}
for (int i = 0; i < n; i++){
scanf("%d", &data[i]);
}
std::sort(data, data + n);
std::reverse(data, data + n);
ans = 0;
for (int i = 0; i < n; i++){
if (i % m != m - 1){
ans += data[i];
}
}
printf("%d\n", ans);
}
return (0);
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.Scanner;
import java.util.Arrays;
public class Main{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
while(scan.hasNext()){
int n = scan.nextInt();
int m = scan.nextInt();
if(n == 0 && m == 0){
break;
}
int[] p = new int[n];
for(int i = 0;i < n;i++){
p[i] = scan.nextInt();
}
Arrays.sort(p);
int sum = 0;
int count = 0;
for(int i = 0;i < n;i++){
if(count == m-1){
count = 0;
continue;
}
sum += p[n-1-i];
count++;
}
System.out.println(sum);
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON3):
|
while True:
n, m = map(int, input().split())
if n == 0:
break
price = sorted(map(int, input().split()), reverse=True)
print(sum(price) - sum(price[m - 1::m]))
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, m;
int p[];
while (true) {
n = sc.nextInt();
m = sc.nextInt();
if ((n | m) == 0) {
break;
}
p = new int[n];
for (int i = 0; i < n; i++) {
p[i] = sc.nextInt();
}
Arrays.sort(p);
int sum = 0;
for (int i = n - 1; 0 <= i; i--) {
if ((n - i) % m != 0) {
sum += p[i];
}
}
System.out.println(sum);
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
int n,m, cost;
while( cin >> n >> m , n|m ){
int sum = 0;
vector<int> vc;
for(int i=0 ; i<n ; ++i){
cin >> cost;
vc.push_back( cost );
}
sort( vc.begin() , vc.end() );
reverse( vc.begin() , vc.end() );
for(int i=0 ; i<(int)vc.size() ; ++i ){
if( (i+1)%m != 0 ){
sum += vc[i];
}
}
cout << sum << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m;
while (cin >> n >> m && n) {
vector<int> p(n);
for (int i=0; i<n; ++i) {
cin >> p[i];
}
sort(p.begin(), p.end());
int res = accumulate(p.begin(), p.end(), 0);
for (int i=n%m; i<n; i+=m) {
res -= p[i];
}
cout << res << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <algorithm>
#include <numeric>
using namespace std;
int main() {
int n, m;
while (cin >> n >> m && n) {
vector<int> p(n);
for (int i=0; i<n; ++i) {
cin >> p[i];
}
sort(p.begin(), p.end());
int res = accumulate(p.begin(), p.end(), 0);
for (int i=n%m; i<n; i+=m) {
res -= p[i];
}
cout << res << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.*;
public class Main {
Scanner sc = new Scanner(System.in);
private void doit() {
while (true) {
int n = sc.nextInt();
int m = sc.nextInt();
if((n|m) == 0) break;
int [] data = new int[n];
int sum = 0;
for(int i = 0; i < n; i++){
data[i] = sc.nextInt();
sum += data[i];
}
Arrays.sort(data);
for(int i = n-m; i >= 0; i-= m){
sum -= data[i];
}
System.out.println(sum);
}
}
private void debug(Object... o) {
System.out.println("debug = " + Arrays.deepToString(o));
}
public static void main(String[] args) {
new Main().doit();
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <algorithm>
#include <iostream>
#include <numeric>
using namespace std;
int main() {
int n, m, cost;
int p[1000];
while (true) {
cin >> n >> m;
if (n == 0) {
return 0;
}
for (int i = 0; i < n; ++i) {
cin >> p[i];
}
sort(p, p + n);
cost = accumulate(p, p + n, 0);
for (int i = n - m; i >= 0; i -= m) {
cost -= p[i];
}
cout << cost << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (;;) {
int n = sc.nextInt(), m = sc.nextInt();
if ((n | m) == 0) {
break;
}
int[] l = new int[n];
for (int i = 0; i < n; i++) {
l[i] = sc.nextInt();
}
Arrays.sort(l);
int sum = 0;
for (int i = n - 1, j = 1; i >= 0; i--, j++) {
if (j % m != 0) {
sum += l[i];
}
}
System.out.println(sum);
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.*;
public class Main {
Scanner in = new Scanner(System.in);
public static void main(String[] args) {
new Main();
}
public Main() {
new AOJ0227();
}
class AOJ0227{
public AOJ0227() {
while(true){
int n = in.nextInt();//購入する野菜
int m = in.nextInt();//袋に入る野菜
if(n==0&&m==0)break;
int[] p = new int[n];
for(int i=0;i<n;i++)p[i] = in.nextInt();
Arrays.sort(p);
int result = 0;
int sum = 0;
for(int i=0;i<n;i++)sum+=p[i];
int cnt = 0;
for(int i=n-1;i>=0;i--){
cnt++;
if(cnt==m){
cnt=0;
result+=p[i];
}
}
System.out.println(sum-result);
}
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,m;
while(true){
cin >>n>>m;
if(n==0){
break;
}
int p[n];
for(int i=0;i<n;i++){
cin >>p[i];
}
sort(p,p+n);
int mon=0;
for(int i=0;i<n;i++){
mon += p[i];
}
int wari = 0;
int t=n-m;
while(t>=0){
wari += p[t];
t -= m;
}
cout <<mon-wari<<endl;
continue;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<stdio.h>
#include<stdlib.h>
int comp(const void *a,const void *b){
return *(int*)b-*(int*)a;
}
int main(){
int n,m;
int p[1000];
int i,j;
int ans;
while(1){
scanf("%d %d",&n,&m);
if(n==0)return 0;
ans=0;
for(i=0;i<n;i++)scanf("%d",&p[i]);
qsort(p,n,sizeof(int),comp);
for(i=0;i<n;i++){
if(i%m!=m-1)ans+=p[i];
}
printf("%d\n",ans);
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <stdio.h>
#include <algorithm>
#pragma warning(disable : 4996)
using namespace std;
int n, m, p[1000];
int main() {
while (true) {
scanf("%d%d", &n, &m);
if (n == 0 && m == 0) break;
for (int i = 0; i < n; i++) scanf("%d", &p[i]);
sort(p, p + n);
int ret = 0;
for (int i = 0; i < n; i++) ret += p[i];
for (int i = n % m; i + m <= n; i += m) ret -= p[i];
printf("%d\n", ret);
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,m;
while(1){
cin>>n>>m;
if(!n&&!m)break;
priority_queue <int> Q;
for(int i=0,a;i<n;i++)cin>>a,Q.push(a);
int ans=0,cnt=0,sum=0;
while(!Q.empty()){
int t=Q.top();Q.pop();
cnt++;
sum+=t;
if(n&&cnt==m) ans+=sum-t,sum=cnt=0,n--;
}
ans+=sum;
cout <<ans<<endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
// your code goes here
int m,p,tmp;
while(cin >> m >> p && m!=0){
vector<int> v;
int sum=0;
for(int i=0;i<m;i++){
cin >> tmp;
v.push_back(tmp);
}
sort(v.begin(),v.end());
int mods=m%p;
for(int i=0;i<mods;i++)
sum+=v[i];
for(int i=mods;i<v.size();i++){
if((i-mods)%p==0);
else
sum+=v[i];
}
cout << sum << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <cstdio>
#include <algorithm>
using namespace std;
static const int MAX_N = 1001;
int vegs[MAX_N];
int main()
{
int n, m, sum;
for (; ; ) {
scanf("%d %d", &n, &m);
if (n == 0 && m == 0) break;
sum = 0;
for (int i = 0; i < n; i++) scanf("%d", &vegs[i]);
sort(vegs, vegs+n, greater<int>());
for (int i = 0; i < n; i++) {
if (i % m == m - 1) continue;
sum+= vegs[i];
}
printf("%d\n", sum);
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON):
|
while True:
try:
num, size = map(int, raw_input().split())
arr = map(int, raw_input().split())
arr.sort(reverse = True)
for i in range(size-1, num, size):
arr[i] = 0
print sum(arr)
except:
break
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,m;
int p[11111];
int sum;
while(1){
cin>>n>>m;
sum=0;
if(n+m==0) break;
for(int i=0;i<n;i++){
cin>>p[i];
}
sort(p,p+n,greater<int>());
for(int i=0;i<n;i++){
sum=sum+p[i];
if((i+1)%m==0){
sum=sum-p[i];
}
}
cout<<sum<<endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
#include<functional>
#include<numeric>
using namespace std;
int main(){
int n,m;
int p[1000];
while(cin>>n>>m,n|m){
for(int i=0;i<n;i++)cin>>p[i];
sort(p,p+n,greater<int>());
int s=accumulate( p,p+n, 0 );
for(int i=m-1;i<n;i+=m){
s-=p[i];
}
cout<<s<<endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON):
|
while 1:
try:
nm = map(int, raw_input().split())
if nm == [0, 0]:
break
n = nm[0]
m = nm[1]
p = map(int, raw_input().split())
p_sorted = sorted(p)
sum = 0
for i in range(n):
if (i+1) % m != 0:
sum += p_sorted[-1-i]
print sum
except:
pass
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON3):
|
while 1:
_,m=map(int, input().split())
if m==0:break
p=sorted(map(int,input().split()),reverse=1)
print(sum(p)-sum(p[m-1::m]))
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
public class Main{
Scanner sc=new Scanner(System.in);
int INF=1<<28;
double EPS=1e-9;
void run(){
for(;;){
int n=sc.nextInt();
int m=sc.nextInt();
if((n|m)==0){
break;
}
int[] a=new int[n];
for(int i=0; i<n; i++){
a[i]=sc.nextInt();
}
Arrays.sort(a);
int sum=0;
for(int i=1; i<=n; i++){
if(i%m!=0){
sum+=a[n-i];
}
}
println(sum+"");
}
}
void debug(Object... os){
System.err.println(Arrays.deepToString(os));
}
void print(String s){
System.out.print(s);
}
void println(String s){
System.out.println(s);
}
public static void main(String[] args){
// System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
new Main().run();
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sn = new Scanner(System.in);
while(true){
int n = sn.nextInt();
int m = sn.nextInt();
if(n == 0 && m == 0) break;
ArrayList<Integer> vegies = new ArrayList<Integer>();
for(int i = 0; i < n; i++){
vegies.add(sn.nextInt());
}
Collections.sort(vegies);
Collections.reverse(vegies);
int sum = 0;
for(int i = 0; i < n; i++){
if((i+1)%m != 0) sum += vegies.get(i);
}
System.out.println(sum);
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
for(;;){
int n=sc.nextInt(),m=sc.nextInt();
if(m==0)break;
int ans=0;
int []in=new int[n];
int []bezi=new int[n+1];
for(int i=0; i<n;i++)in[i]=sc.nextInt();
Arrays.sort(in);
for(int i=n-1;i>=0;i--)bezi[n-i]=in[i];
for(int i=1;i<=n;i++){
ans+=bezi[i];
if(i%m==0)ans-=bezi[i];
}
System.out.println(ans);
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
using namespace std;
int main(void){
int n,m;
while(cin>>n>>m,n){
int t[n];
for(int i=0;i<n;i++){
cin>>t[i];
}
sort(t,t+n);
reverse(t,t+n);
int ans = 0;
for(int i=0;i<n;i+=m){
int sum = 0;
for(int j=i;j<n && j<i+m;j++){
sum += t[j];
}
if(i+m <= n) sum -= t[i+m-1];
ans += sum;
}
cout<<ans<<endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n, m;
long long p[1000];
while(1) {
cin >> n >> m;
if (n == 0 && m == 0) break;
long long ans = 0;
for (int i = 0; i < n; i++) {
cin >> p[i];
ans += p[i];
}
sort(p, p+n, std::greater<long long>());
for (int i = m-1; i < n; i += m) {
ans -= p[i];
}
cout << ans << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
using namespace std;
int main() {
int n, m;
while (cin >> n >> m, n, m) {
int p[1000];
int sum = 0;
for (int i = 0;i < n;i++) {
cin >> p[i];
sum += p[i];
}
sort(p, p+ n);
reverse(p, p + n);
for (int i = m - 1;i < n;i += m) {
sum -= p[i];
}
cout << sum << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main{
public static void main(String[] args) {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
String line = null;
String[] in = null;
int numBuy, numIn;
ArrayList<Integer> costVegitables = new ArrayList<>();
try {
while((line = input.readLine()) != null){
in = line.split(" ");
if(in[0].equals("0") && in[1].equals("0")){
break;
}
numBuy = Integer.parseInt(in[0]); numIn = Integer.parseInt(in[1]);
line = input.readLine();
in = line.split(" ");
costVegitables.clear();
for(String str : in){
int cost = Integer.parseInt(str);
costVegitables.add(cost);
}
Collections.sort(costVegitables, Collections.reverseOrder());
int index = 1;
int minimumCost = 0;
for(int i: costVegitables){
if(index%numIn != 0){
minimumCost += i;
}
index++;
}
System.out.println(minimumCost);
}
} catch (IOException e) {
System.out.println("IOException");
} catch (NumberFormatException e){
System.out.println("including not int");
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <cstdio>
#include <algorithm>
#include <functional>
using namespace std;
int ps[1000];
int main(){
int n,m;
for(;;){
scanf("%d%d",&n,&m);
if(n==0&&m==0) break;
for(int i=0;i<n;i++){
scanf("%d",&ps[i]);
}
sort(ps,ps+n,greater<int>());
int sum=0;
for(int i=0;i<n;i++){
if(i%m!=m-1) sum+=ps[i];
}
printf("%d\n",sum);
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n,m,total,c;
int p[10000];
while(scanf("%d%d",&n,&m) && n != 0 && m != 0){
for(int i = 0; i < n; i++){
scanf("%d",&p[i]);
}
sort(&p[0],&p[n-1]+1);
c = total = 0;
for(int i = n-1; i >= 0; i--){
c++;
if(c % m != 0){
total += p[i];
}
}
printf("%d\n",total);
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in PYTHON3):
|
for i in range(1, 101):
count = 0
temp = 0
SetCount = 0
try:
str1 = input()
list1 = str1.split(" ")
str2 = input()
list2 = str2.split(" ")
list2.sort(key=int, reverse=True)
for m in range(0, int(list1[0])):
if (m+1) % int(list1[1]) == 0:
SetCount = SetCount + 1
#elif (m+1) % int(list1[1]) != 0 | SetCount == int(list1[0])//int(list1[1]):
# count = count + int(list2[m])
else:
count = count + int(list2[m])
print(count)
except:
break
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
while (true) {
int n = sc.nextInt();
int m = sc.nextInt();
if ((n | m) == 0)
break;
Integer[] ps = new Integer[n];
int sum = 0;
for (int i = 0; i < n; i++) {
ps[i] = sc.nextInt();
sum += ps[i];
}
Arrays.sort(ps, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return b - a;
}
});
for (int i = 0; i < ps.length / m; i++) {
sum -= ps[i*m+m-1];
}
System.out.println(sum);
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n,m;
while(1){
cin >> n >> m;
if(n == 0 && m==0) break;
int item[1010];
for(int i=0;i<n;i++){
cin >> item[i];
}
sort(item,item+n);
int ans=0,f=1;
for(int i=n-1;i>=0;i--){
if(f==m){
f=1;
}
else{
ans += item[i];
f++;
}
}
cout << ans <<"\n";
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,m,i,j;
int p[1001];
int sum;
while(1){
cin>>n>>m;
if(n==0&&m==0) break;
for(i=0;i<n;++i) cin>>p[i];
sort(p,p+n);
sum=0;
if(n%m==0){
for(i=0;i<n;i+=m)
for(j=1;j<m;++j)
sum+=p[i+j];
}else{
for(i=0;i<n%m;++i)
sum+=p[i];
for(;i<n;i+=m)
for(j=1;j<m;++j)
sum+=p[i+j];
}
cout<<sum<<endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in JAVA):
|
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true) {
int n = sc.nextInt();
int m = sc.nextInt();
if (n == 0) {
break;
}
int[] p = new int[n];
for(int i=0;i<n;i++) {
p[i] = sc.nextInt();
}
Arrays.sort(p);
int ans = 0;
for(int i=0;i<n%m;i++) {
ans += p[i];
}
for(int i=0;i<n/m;i++) {
for(int j=1;j<m;j++) {
ans += p[n%m+m*i+j];
}
}
System.out.println(ans);
}
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n,m;
void solve()
{
vector<int> veg(n);
int ans = 0;
for(int i = 0; i < n; i++)
cin >> veg[i];
sort(veg.begin(), veg.end() );
for(int i = 1; i <= n; i++){
int d = veg.back();
veg.pop_back();
if(i % m)
ans += d;
}
cout << ans << endl;
}
int main(void)
{
while(cin >> n >> m, n | m){
solve();
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
while (1) {
int a,b;
cin >> a>>b;
if (a == 0&&b==0)break;
vector<int>s(a);
for (int c = 0; c < a; c++) {
cin >> s[c];
}
sort(s.begin(), s.end());
int goukei = 0;
for (int e = 0; e < a; e++) {
if ((e + 1) % b != 0)goukei += s[s.size() - 1 - e];
}
cout << goukei << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
using namespace std;
int a[1000],n,m,ans;
int main(){
while(true){cin>>n>>m;if(n==0&&m==0)break;ans=0;for(int i=0;i<n;i++){cin>>a[i],ans+=a[i];}sort(a,a+n);for(int i=n%m;i<n;i+=m)ans-=a[i];cout<<ans<<endl;}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <bits/stdc++.h>
using namespace std;
int main() {
int N, M;
while (cin >> N >> M, N || M) {
vector<int> v(N);
for (auto& i : v) cin >> i;
sort(v.begin(), v.end());
int ans = 0;
for (int i = 1; i <= N; i++) {
if (i % M) ans += v[N - i];
}
cout << ans << endl;
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
using namespace std;
#define MAX 1000
int main(){
int n, m, P[MAX], sum;
while(1){
cin >> n >> m;
if ( n == 0 && m == 0 ) break;
sum = 0;
for ( int i = 0; i < n; i++ ) {
cin >> P[i];
sum += P[i];
}
sort(P, P+n, greater<int>());
for ( int i = m-1; i < n; i += m ) sum -= P[i];
cout << sum << endl;
}
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include<iostream>
#include<algorithm>
#include<functional>
#include<vector>
using namespace std;
vector<int> price;
int main(){
int n,m;
int ans;
int i;
while( cin >> n >> m )
{
if( n == 0 && m == 0 )
break;
ans = 0;
price.resize( n );
for( i = 0;i < n;i++ ){
cin >> price[i];
ans += price[i];
}
sort( price.begin(),price.end(),greater<int>() );
for( i = 0;i < n;i++ ){
if( i%m < m-1 )
continue;
ans -= price[i];
}
cout << ans << endl;
price.clear();
}
return 0;
}
|
Problem: As bad weather continues and vegetable prices soar, Seven-Eleven is offering customers bulk purchase sales of vegetables. The store is very busy, as you can get vegetables that are hard to find in stores at reasonable prices.
One day, a group of three good friends living in the Matsunaga housing complex bloomed with an advertisement for Seven-Eleven. Since this sale was called "Customer Thanksgiving Day", the highlight is that the cheapest vegetables in the bag are free. When I read the advertisement, it looks like the following sale.
* Up to m vegetables can be packed in one bag.
* For bags packed with m vegetables, the cheapest vegetables are free.
* Bags with less than m vegetables are not eligible for the discount.
The three went shopping at Seven Mart immediately.
When the three people who met outside the store after shopping were satisfied with the fact that they were able to buy a lot at a low price, they apparently found that they were all buying the same vegetables. One person said, "It's really cheap, isn't it? This is XXX yen!", And the other was surprised, "What? I was ** yen higher than that! Why?", And the rest One of them saw the receipt and noticed that he bought the cheapest one.
Now, how can you pack the bag at the lowest purchase price? Enter the number of vegetables to purchase, the number of vegetables in the bag, and the price of each vegetable, and create a program that outputs the minimum purchase price.
Input
A sequence of multiple datasets is given as input. The end of the input is indicated by two lines of zeros. Each dataset is given in the following format:
n m
p1 p2 ... pn
The first line gives the number of vegetables to buy n (1 ≤ n ≤ 1000) and the number of vegetables to put in the bag m (1 ≤ m ≤ 1000). The second line gives the price pi (10 ≤ pi ≤ 10000) for each vegetable.
The number of datasets does not exceed 100.
Output
Prints the minimum purchase price on one line for each input dataset.
Example
Input
4 2
50 40 100 80
7 3
400 300 100 700 200 600 500
0 0
Output
150
2100
Solution (in CPP):
|
#include <cstdio>
#include <algorithm>
#include <functional>
using namespace std;
int n, m, p[1000];
int main(){
for(; scanf("%d%d", &n, &m), n||m;){
for(int x = 0; x < n; x++){
scanf("%d", p+x);
}
sort(p, p+n, greater<int>());
int ans = 0, z;
for(int x = 0; x < n; x++){
if((x+1)%m) ans += p[x];
}
printf("%d\n", ans);
}
return 0;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.