RT
60 pts,WA on #3, #17
#include <iostream>
#include <cstring>
#define int long long
using namespace std;
const int N = 510;
int n, m, l[N], r[N], has[N], w[N], f[N][N];
signed main() {
cin >> n >> m;
memset(f, -0x3f, sizeof(f));
for (int i = 1, x; i <= n; ++i) {
cin >> x;
if (!l[x]) l[x] = i, has[x] = true;
r[x] = i;
}
for (int i = 1; i <= n; ++i) {
cin >> w[i];
// attention
if (has[i])
f[r[i]][1] = w[i];
}
for (int i = 1; i <= n; ++i) if (has[i]) {
for (int j = 1; j < l[i]; ++j) {
for (int k = 2; k <= m; ++k) {
f[r[i]][k] = max(f[j][k - 1] + w[i], f[r[i]][k]);
}
}
}
int ans = -1;
for (int i = 1; i <= n; ++i) {ans = max(ans, f[i][m]);}
cout << ans << '\n';
}
100pts
#include <iostream>
#include <cstring>
#define int long long
using namespace std;
const int N = 510;
int n, m, l[N], r[N], has[N], w[N], f[N][N];
signed main() {
cin >> n >> m;
memset(f, -0x3f, sizeof(f));
for (int i = 1, x; i <= n; ++i) {
cin >> x;
if (!l[x]) l[x] = i, has[x] = true;
r[x] = i;
}
for (int i = 1; i <= n; ++i) {
cin >> w[i];
}
for (int i = 1; i <= n; ++i) if (has[i]) {
// here
f[r[i]][1] = w[i];
for (int j = 1; j < l[i]; ++j) {
for (int k = 2; k <= m; ++k) {
f[r[i]][k] = max(f[j][k - 1] + w[i], f[r[i]][k]);
}
}
}
int ans = -1;
for (int i = 1; i <= n; ++i) {ans = max(ans, f[i][m]);}
cout << ans << '\n';
}