Go: package main

import (
	"bufio"
	"os"
	"strings"
	"strconv"
	"sort"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		buf := strings.Split(scanner.Text(), " ")
		n, _ := strconv.Atoi(buf[0])
		m, _ := strconv.Atoi(buf[1])
		if n == 0 && m == 0 {
			break
		}
		scanner.Scan()
		buf_ := strings.Split(scanner.Text(), " ")
		items := make([]int, n)
		for i := 0; i < n; i++ {
			items[i], _ = strconv.Atoi(buf_[i])
		}
		sort.Ints(items)
		sum := 0
		for _, v := range items {
			sum += v
		}
		for i := n-m; i >= 0; i -= m {
			sum -= items[i]
		}
		fmt.Println(sum)
	}
}

C++: //Name: Thanksgiving
//Level: 2
//Category: 貪欲,Greedy
//Note:

/*
 * 野菜を価格の順に並べたとき、取り除ける個数は floor(n/m) 個で固定であることから、
 * 安いほうから n%m 個までの野菜は取り除いても効率が悪い。
 * したがって、残った n - n%m 個の野菜から floor(n/m) 個を取り除くことになるが、
 * このときもっとも安い野菜は必ず取り除かれることになる。
 * この野菜と一緒に袋に詰められる野菜は可能な限り安いものを使ったほうがよい（なるべく高い野菜を次で消せるようにする）から、
 * 結局貪欲に詰めていけばよい。
 *
 * 高い野菜からm個ずつ選んでいくようにすると楽に書ける。
 *
 * オーダーは O(N log N)。
 */
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

bool solve() {
    int N, M;
    if(!(cin >> N >> M)) return false;
    if(!N && !M) return false;

    vector<int> ps(N);
    for(int i = 0; i < N; ++i) {
        cin >> ps[i];
    }
    sort(begin(ps), end(ps), greater<int>());

    int ans = 0;
    for(int i = 0; i < N; ++i) {
        if(i % M == M-1) continue;
        ans += ps[i];
    }
    cout << ans << endl;
    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);

    while(solve()) ;

    return 0;
}

C++: #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;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <functional>

/*The function calculates vegetables which you purchase.*/
int purchase_price(const int n,const int m) {

	//The price of purchased vegetables
	std::vector<int> purchase;

	//input
	for (int i = 0; i < n; ++i){
		int tmp;
		std::cin >> tmp;
		purchase.push_back(tmp);
	}

	//descending sort (higher price)
	std::sort(purchase.begin(), purchase.end(), std::greater<int>());

	int sum = 0;

	//surplus eqals to (m-1) is free
	for (int i = 0; i < n; ++i){
		if (i%m != m-1) {
			sum += purchase.at(i);
		}
	}

	return sum;

}


int main() {

	//n:the number of purchase vegetables
	//m:the number of you can stuff vegetables
	int n, m;
	//sum of price
	int sum;

	while (true) {

		std::cin >> n >> m;

		//case of input is (n,m)=(0,0)
		if (n == 0 && m == 0) {
			break;
		}

		sum = purchase_price(n, m);
		std::cout << sum << std::endl;
	}

	return 0;
}

C++: 
#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+1; i++) {
			cin >> p[i];
	}
		for (int j = 0; j < n + 1; j++) {
			for (int i = 1; i < n + 1; i++) {
				if (p[i] < p[i + 1]) {
					swap(p[i], p[i + 1]);

				}
			}
		}
		for (int i = 1; i <= n; i++) {
			cout << p[i] << endl;
		}
				for (int i = 1; i < n + 1; i++) {
					if (i%m != 0) {
						sum += p[i];
					}

			}
		
		cout << sum << endl;
	}
		
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)
#define ALL(c)   (c).begin(),(c).end()
#define pb push_back
main(){
  int n,m;
  while(cin>>n>>m && n){
    vector<int> a(n);
    rep(i,n)cin>>a[i];
    sort(ALL(a));reverse(ALL(a));
    int ans=0;
    int cnt=1;
    rep(i,n){
      if (cnt == m){
	cnt=0;
      }
      else ans+=a[i];
      cnt++;
    }
    cout << ans << endl;
  }
  return false;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

int main(void){
    std::vector<int> price;
    int num_vegetable, bag_capacity, price_sum = 0;
    while(std::cin >> num_vegetable >> bag_capacity){
        if(num_vegetable == 0 && bag_capacity == 0)
            break;
        for(int p, i = 0; i < num_vegetable; ++i){
            std::cin >> p;
            price.push_back(p);
        }
        
        std::sort(price.begin(), price.end(), std::greater<int>());
        
        for(int i = 0; i < num_vegetable; ++i){
            if((i + 1) % bag_capacity != 0)
                price_sum += price.at(i);
        }
        std::cout << price_sum << std::endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

int main(){

	int num, limit;

	while(std::cin >> num >> limit){
		if(num == 0 && limit == 0)
			break;

		std::vector<int> price;
		int sum_price = 0;
		//input
		for(int i = 0; i < num; ++i){
			int buf;
			std::cin >> buf;
			sum_price += buf;
			price.push_back(buf);
		}
		//sort
		std::sort(price.begin(),price.end(),std::greater<int>());
		//discount
		int discount = 0;
		int in_bag = 0;
		for(auto itr = price.begin(); itr != price.end(); ++itr){
			++in_bag;
			if(in_bag == limit){
				discount += *itr;
				in_bag = 0;
			}
		}

		std::cout << sum_price - discount << std::endl;
	}
	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <functional>
#include<cstdio>  

using namespace std;

int main(void){
	int n,m;
	int a;
	while(1){
		cin >> n >> m;
		if(n==0 && m==0)break;
		int ans=0;
		priority_queue<int,vector<int>,greater<int>> num;
		for(a=0;a<n;a++){
			int nn;
			scanf("%d",&nn);
			num.push(nn);
		}
		for(a=1;a<=n;a++){
			if(a%m==0)ans+=num.top();
			num.pop();
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #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;
	}
	
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vbb;
typedef pair<int, int> pii;
typedef long long ll;
typedef unsigned long long ull;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair

#define loop(i,a,b) for(int i=(a);i<ull(b);++i)
#define rep(i,n) loop(i,0,n)

const double eps = 1e-10;
const double pi  = acos(-1.0);
const double inf = (int)1e8;

int main(){
    int n, m;
    while(cin >> n >> m, n+m){
        vi v(n, 0);
        rep(i, n) cin >> v[i];
        sort(all(v), greater<int>());
        int ans =0;
        rep(i, n){
            if((i+1)%m==0) continue;
            ans +=v[i];
        }
        cout << ans << endl;
    }
}

C++: #include <stdio.h>
#include <algorithm>
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 + m <= n; i += m) {
			ret += p[i];
		}
		printf("%d\n", ret);
	}
	return 0;
}