#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
const int N = 501;
db dp[N][N][N]; //id, b, a
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, K;
cin >> n >> K;
vector<int> a(n), b(n), oa(n), ob(n);
int cntb = 0;
bool yay = true;
for (int i = 0; i < n; ++i) {
cin >> a[i] >> b[i];
if (b[i] != -1) {
cntb += 1;
}
yay &= (a[i] == b[i] || b[i] == -1);
}
iota(oa.begin(), oa.end(), 0), iota(ob.begin(), ob.end(), 0);
sort(oa.begin(), oa.end(), [&a](int i, int j) {
return a[i] < a[j];
});
sort(ob.begin(), ob.end(), [&](int i, int j) {
if (b[i] == -1) return false;
if (b[j] == -1) return true;
return b[i] < b[j];
});
const db inf = 1e12;
if (yay) {
db ans = inf;
for (int cnt = 0; cnt <= cntb; ++cnt) {
vector<bool> used(n);
db now = 0;
int done = 0;
for (int i = 0; i < cnt; ++i) {
now += b[ob[i]] / (db(i + 1));
used[ob[i]] = true;
done += 1;
}
if (now >= ans) {
continue;
}
for (int i = 0; i < n && done < K; ++i) {
if (!used[oa[i]]) {
done += 1;
now += a[oa[i]] / (db(cnt + 1));
}
}
assert(done >= K);
ans = min(ans, now);
}
cout << fixed << setprecision(10) << ans;
} else {
auto solve = [&](int mx) -> db {
for (int i = 0; i <= n; ++i) {
for (int j = 0; j <= mx; ++j) {
for (int k = 0; k <= K - j; ++k) {
dp[i][j][k] = inf;
}
}
}
dp[0][0][0] = 0;
for (int i = 1; i <= n; ++i) {
//don't take
for (int j = 0; j <= mx; ++j) {
for (int k = 0; k <= min(i, K) - j; ++k) {
dp[i][j][k] = dp[i - 1][j][k];
}
}
//take a
int idx = ob[i - 1];
for (int j = 0; j <= min(i, mx); ++j) {
for (int k = 1; k <= min(i, K) - j; ++k) {
dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j][k - 1] + a[idx] / db(mx + 1));
}
}
if (b[idx] != -1) {
for (int j = 1; j <= min(i, mx); ++j) {
for (int k = 0; k <= min(i, K) - j; ++k) {
dp[i][j][k] = min(dp[i][j][k], dp[i - 1][j - 1][k] + b[idx] / db(j));
}
}
}
}
return dp[n][mx][K - mx];
};
int r = min(K, cntb);
ans = inf;
for (int x = 0; x <= r; ++x) {
ans = min(ans, solve(x));
}
cout << fixed << setprecision(20) << solve(x);
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:96:9: error: 'ans' was not declared in this scope; did you mean 'abs'?
96 | ans = inf;
| ^~~
| abs
Main.cpp:100:52: error: 'x' was not declared in this scope
100 | cout << fixed << setprecision(20) << solve(x);
| ^