-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathF.cpp
89 lines (84 loc) · 2.4 KB
/
F.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <bitset>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define fi first
#define se second
#define db double
#define U unsigned
#define P std::pair<int,int>
#define LL long long
#define pb push_back
#define MP std::make_pair
#define all(x) x.begin(),x.end()
#define CLR(i,a) memset(i,a,sizeof(i))
#define FOR(i,a,b) for(int i = a;i <= b;++i)
#define ROF(i,a,b) for(int i = a;i >= b;--i)
#define DEBUG(x) std::cerr << #x << '=' << x << std::endl
const int MAXN = 75+5;
int n,k,a[MAXN],b[MAXN],id[MAXN];
LL f[MAXN][MAXN][MAXN][2];
int opt[MAXN];
std::tuple<int,int,int> p[MAXN];
inline void Solve(){
scanf("%d%d",&n,&k);
FOR(i,1,n){
int x,y;scanf("%d%d",&x,&y);
p[i] = std::tie(y,x,i);
}
std::sort(p+1,p+n+1);
FOR(i,1,n) std::tie(b[i],a[i],id[i]) = p[i];
CLR(f,-0x3f);f[0][0][0][0] = 0;
FOR(i,1,n){
FOR(j,0,n){
FOR(k,0,n-j){
FOR(l,0,1){
if(f[i-1][j][k][l] == -0x3f3f3f3f) continue;
f[i][j+1][k][l] = std::max(f[i][j+1][k][l],f[i-1][j][k][l]+a[i]+b[i]*j);
f[i][j][k+1][l] = std::max(f[i][j][k+1][l],f[i-1][j][k][l]+(::k-1)*b[i]);
if(!l) f[i][j][k][l+1] = std::max(f[i][j][k][l+1],f[i-1][j][k][l]+a[i]+(::k-1)*b[i]);
}
}
}
}
CLR(opt,0);
int nowa=k-1,nowb=n-k,nowc=1;
ROF(i,n,1){
LL w = f[i][nowa][nowb][nowc];
if(nowa > 0 && f[i-1][nowa-1][nowb][nowc]+a[i]+b[i]*(nowa-1) == w){
opt[i] = 1;
nowa--;
}
else if(nowb > 0 && f[i-1][nowa][nowb-1][nowc]+(::k-1)*b[i] == w){
opt[i] = 2;
nowb--;
}
else if(nowc > 0 && f[i-1][nowa][nowb][nowc-1]+a[i]+(::k-1)*b[i] == w){
opt[i] = 3;
nowc--;
}
else{
puts("ERR");
return;
}
}
printf("%d\n",k+(n-k)*2);
FOR(i,1,n) if(opt[i] == 1) printf("%d ",id[i]);
FOR(i,1,n) if(opt[i] == 2) printf("%d %d ",id[i],-id[i]);
FOR(i,1,n) if(opt[i] == 3) printf("%d\n",id[i]);
}
int main(){
int T;scanf("%d",&T);
while(T--) Solve();
return 0;
}