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 <bits/stdc++.h>
#include "homecoming.h"
using namespace std;
#define int long long
#define form2(i, a, b) for (int i = (a); i < (b); ++i)
#define ford2(i, a, b) for (int i = (a-1); i >= (b); --i)
#define form(i, n) form2(i, 0, n)
#define ford(i, n) ford2(i, n, 0)
#define chmax(x, v) x = max(x, (v))
#define chmin(x, v) x = min(x, (v))
#define fi first
#define se second
const long long BIG = 1000000000000000000LL;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
const int PRIS=0, NON=1;
const int borne = 2*1000*1000 + 5;
int nbLivres, lenCyc;
int rcp[borne];
int prix[borne];
int somPrix[borne];
int dp[borne][2];
int getSom(int a, int b)
{
if (a > b) return getSom(a, nbLivres - 1) + getSom(0, b);
int rep = somPrix[b];
if (a) rep -= somPrix[a-1];
return rep;
}
long long solve(signed N, signed K, signed *A, signed *B)
{
nbLivres = N; lenCyc = K;
form(i, nbLivres) {
rcp[i] = A[i];
prix[i] = B[i];
somPrix[i] = prix[i];
if (i) somPrix[i] += somPrix[i-1];
}
// Cas 1 : on prend le premier livre
dp[0][NON] = -BIG;
dp[0][PRIS] = rcp[0] - getSom(0, lenCyc-1);
form2(iElem, 1, nbLivres) {
int newAdd = (iElem + lenCyc - 1); // TODO PAS MOD
// PRIS, et prev pris
if (true) {
int rep = dp[iElem-1][PRIS];
if (newAdd < nbLivres) rep -= prix[newAdd];
rep += rcp[iElem];
dp[iElem][PRIS] = rep;
}
// PRIS, et prev pas pris
if (true) {
int rep = dp[iElem-1][NON];
rep += rcp[iElem];
rep += getSom(iElem, min(newAdd, nbLivres-1));
chmax(dp[iElem][PRIS], rep);
}
// NON
dp[iElem][NON] = max(dp[iElem-1][PRIS], dp[iElem-1][NON]);
}
int repCas1 = max(dp[nbLivres-1][PRIS], dp[nbLivres-1][NON]);
// Cas 2 : on ne prend pas le premier livre
dp[0][PRIS] = -BIG;
dp[0][NON] = 0;
form2(iElem, 1, nbLivres) {
int newAdd = (iElem + lenCyc - 1) % nbLivres;
// PRIS, et prev pris
if (true) {
int rep = dp[iElem-1][PRIS];
rep += rcp[iElem];
rep -= prix[newAdd];
dp[iElem][PRIS] = rep;
}
// PRIS, et prev pas pris
if (true) {
int rep = dp[iElem-1][NON];
rep += rcp[iElem];
rep -= getSom(iElem, newAdd);
chmax(dp[iElem][PRIS], rep);
}
// NON
dp[iElem][NON] = max(dp[iElem-1][PRIS], dp[iElem-1][NON]);
}
int repCas2 = max(dp[nbLivres-1][PRIS], dp[nbLivres-1][NON]);
int wo = max(repCas1, repCas2);
assert(wo >= 0);
return wo;
}
# | 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... |