This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<stdio.h>
#include<algorithm>
using namespace std;
int n, w[2010], Res, Sum, S, m, Sr;
int D[2010];
int main()
{
int i, a, b, j, x;
scanf("%d", &n);
for (i = 1; i < n; i++)D[i] = -2100000000;
S++;
for (i = 1; i <= n; i++){
scanf("%d%d", &a, &b);
if (!a)w[++m] = b;
else{
if (b >= 0){
Sum += b;
S += a - 1;
continue;
}
x = a - 1;
for (j = n - 1; j >= 0; j--){
if (j + x < n && D[j + x] < D[j] + b)D[j + x] = D[j] + b;
}
}
}
for (i = n-2; i >=0; i--){
if (D[i] < D[i + 1])D[i] = D[i + 1];
}
sort(w + 1, w + m + 1);
Res = Sum;
for (j = 1; j <= m; j++){
Sr += w[m - j + 1];
if (S >= j){
if (Res < Sr + Sum)Res = Sr + Sum;
}
else{
if (Res < Sr + D[j - S] + Sum) Res = Sr + D[j - S] + Sum;
}
}
printf("%d\n", Res);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |