我也用的回溯,做了一些剪枝,但还是9%通过
int n, k;
int ret = 0;
vector<int> group;
void Loop(const vector<Info> &infos, int i, int m) {
if (group.size() + n - m - 1 < k)
return;
if (i == k) {
int a_sum = 0, b_min = -1;
for (int j = 0; j < k; ++j) {
a_sum += infos[group[j]].a;
if (b_min == -1)
b_min = infos[group[j]].b;
else
b_min = min(b_min, infos[group[j]].b);
}
ret = max(ret, a_sum * b_min);
return;
}
for (int j = m + 1; j < n; ++j) {
group.push_back(j);
Loop(infos, i + 1, j);
group.pop_back();
}
}