Skip to content

Instantly share code, notes, and snippets.

@jeremyjs
Created September 27, 2014 21:38
Show Gist options
  • Select an option

  • Save jeremyjs/a3858e1159a88a71608d to your computer and use it in GitHub Desktop.

Select an option

Save jeremyjs/a3858e1159a88a71608d to your computer and use it in GitHub Desktop.
/**
* meyer88
* $SRCFILE
*/
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <bitset>
#include <deque>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long int lli;
typedef pair<int, int> pii;
typedef pair<lli, lli> pll;
int gInt () {
int i;
scanf("%d", &i);
return i;
}
pii g2Int () {
pii xy;
scanf("%d %d", &xy.first, &xy.second);
return xy;
}
pll g2Long () {
pll xy;
scanf("%lld %lld", &xy.first, &xy.second);
return xy;
}
lli gLong () {
lli i;
scanf("%lld", &i);
return i;
}
double gDouble () {
double i;
scanf("%lf", &i);
return i;
}
void quit () {
fflush(stdout);
exit(0);
}
int main (int argc, char ** argv) {
int n = gInt();
int k = gInt();
bool ary[n][2];
int rank[2*n];
int ai[2*n];
int aj[2*n];
std::map<int, pii> m;
int h=0;
int ret = 0;
for(int i = 0; i < n; i++) {
ary[i][0] = false;
ary[i][1] = false;
ret += rank[h] = gInt();
ai[h] = i;
aj[h] = 0;
h++;
ret += rank[h] = gInt();
ai[h] = i;
aj[h] = 1;
h++;
}
int i, j, tmp;
for (i = 1; i < h; i++) {
j = i;
while (j > 0 && rank[j - 1] > rank[j]) {
tmp = rank[j];
rank[j] = rank[j - 1];
rank[j - 1] = tmp;
tmp = ai[j];
ai[j] = ai[j - 1];
ai[j - 1] = tmp;
tmp = aj[j];
aj[j] = aj[j - 1];
aj[j - 1] = tmp;
j--;
}
}
int kk=0;
for(int fd = 0; fd < h; fd++) {
i = ai[fd]; j = aj[fd];
int kuju = (j == 1) ? 0 : 1;
if(i == 0 && ary[i][kuju] || ary[i+1][kuju]) {
continue;
}
else if(i == n-1 && ary[i-1][kuju] || ary[i][kuju]) {
continue;
}
else if(ary[i-1][kuju] || ary[i][kuju] || ary[i+1][kuju]) {
continue;
}
ary[i][j] = true;
ret -= rank[fd];
kk++;
if(kk == k) {
break;
}
}
printf("%d\n", ret);
quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment