#include <bits/stdc++.h>
#define ll long long
#define INF 1e16
#define MAX 20
using namespace std;
ll N, M;
ll up[MAX], down[MAX], sum[MAX], in[MAX], out[MAX];
vector<ll> X, Y, W;
vector<pair<ll, ll>> pos[MAX];
void cmax(ll &a, ll &b)
{
a = max(a, b);
}
ll max_weights(int n, int m, vector<int> x, vector<int> y, vector<int> w)
{
N = n, M = m;
X.resize(M);
Y.resize(M);
W.resize(M);
for (ll i = 0; i < M; i++)
{
X[i] = x[i]+1;
Y[i] = y[i]+1;
W[i] = w[i];
}
for (ll i = 0; i < M; i++) pos[X[i]].push_back({Y[i], W[i]});
for (ll i = 1; i <= N; i++) pos[i].push_back({N + 1, 0});
for (ll i = 1; i <= N; i++) sort(pos[i].begin(), pos[i].end());
ll cnt = 0;
for (ll i = 1; i <= N; i++)
{
sum[++cnt] = pos[i][0].second;
in[i] = cnt;
Y[cnt] = pos[i][0].first;
for(ll j = 1; j < pos[i].size(); ++j)
{
++cnt;
sum[cnt] = sum[cnt - 1] + pos[i][j].second;
Y[cnt] = pos[i][j].first;
}
out[i] = cnt;
}
for(ll i = 0; i <= N; ++i) up[i] = down[i] = -INF;
up[0] = down[0] = 0;
Y[0] = -1;
for(ll x = 1; x <= N; ++x)
{
ll k = in[x - 1], mx = -INF, p = -1;
for (ll i = in[x]; i <= out[x]; ++i)
{
while(k <= out[x - 1] && Y[k] < Y[i])
{
mx = max(mx, up[k] - (k == in[x - 1] ? 0 : sum[k - 1]));
p = k; ++k;
}
up[i] = max(up[i], mx + (p == -1 ? 0 : sum[p]));
}
k = out[x - 1], mx = -INF, p = out[x];
for(ll i = out[x]; i >= in[x]; --i)
{
while(k >= in[x - 1] && Y[k] > Y[i])
{
while(p >= in[x] && Y[k] <= Y[p]) --p;
mx = max(mx, max(down[k], up[k]) + sum[p]);
--k;
}
down[i] = max(down[i], mx - (i == in[x] ? 0 : sum[i - 1]));
if(k >= in[x - 1] && Y[k] == Y[i]) down[i] = max(down[i], max(down[k], up[k]));
}
if(x < 2) continue;
k = in[x - 2], mx = -INF, p = in[x - 1]; ll p2 = -1;
for(ll i = in[x]; i <= out[x]; ++i)
{
while(k <= out[x - 2] && Y[k] <= Y[i])
{
mx = max(mx, max(up[k], down[k]));
++k;
}
while(p <= out[x - 1] && Y[p] < Y[i]) p2 = p++;
up[i] = max(up[i], mx + (p2 == -1 ? 0 : sum[p2]));
}
k = out[x - 2], mx = -INF, p = out[x - 1];
for(ll i = out[x]; i >= in[x]; --i)
{
while(k >= in[x - 2] && Y[k] > Y[i])
{
while(p >= in[x - 1] && Y[k] <= Y[p]) p--;
ll val = max(up[k], down[k]);
if (p != in[x-1] - 1) val += sum[p];
cmax(mx, val);
--k;
}
cmax(up[i], mx);
}
}
ll res = 0;
for(ll i = 0; i <= N + M; ++i) res = max(res, max(up[i], down[i]));
return res;
}
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:40:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(ll j = 1; j < pos[i].size(); ++j)
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
26 ms |
15040 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
344 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
348 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
26 ms |
15040 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |