//Dedicated to my love, ivaziva
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(a) ((int)a.size())
const int N=1e5+5;
int n, q;
int w[N], a[N], b[N], e[N];
vector<ll> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E) {
n=sz(W), q=sz(E);
for (int i=1; i<=n; i++)
w[i]=W[i-1], a[i]=A[i-1], b[i]=B[i-1];
for (int i=1; i<=q; i++)
e[i]=E[i-1];
if (q<=5&&n<=2000&&W==vector<int>(n, 1)) {
if (n%2==0) {
ll ans=0;
for (int i=1; i<=n; i++)
ans+=b[i];
return (vector<ll>(q, ans));
}
else {
ll sumb=0, ans=(ll)(1e18);
for (int i=1; i<=n; i++)
sumb+=b[i];
for (int i=1; i<=n; i++)
ans=min(ans, a[i]+sumb-b[i]);
return (vector<ll>(q, ans));
}
}
vector<int> sub2;
for (int i=0; i<n; i++) sub2.pb(i+1);
if (q<=5&&W==sub2) {
if (n%2==0) {
ll ans=0;
for (int i=1; i<=n; i++)
ans+=b[i];
return (vector<ll>(q, ans));
}
else {
vector<ll> ret;
for (int i=1; i<=q; i++) {
if (e[i]>=2) {
ll sumb=0, ans=(ll)(1e18);
for (int i=1; i<=n; i++)
sumb+=b[i];
for (int i=1; i<=n; i++)
ans=min(ans, a[i]+sumb-b[i]);
ret.pb(ans);
}
else {
vector<ll> ps(n+1), ss(n+2), minps(n+1, (ll)(1e9)), minss(n+2, (ll)(1e9));
ll ans=(ll)(1e9);
for (int i=1; i<=n; i++) {
ps[i]=ps[i-1]+b[i];
minps[i]=min(minps[i-1], (ll)(a[i]-b[i]));
}
for (int i=n; i>=1; i--) {
ss[i]=ss[i+1]+b[i];
minss[i]=min(minss[i+1], (ll)(a[i]-b[i]));
}
for (int i=1; i<=n; i++) {
if (i&1)
ans=min(ans, a[i]+ps[n]-b[i]);
else
ans=min(ans, ps[i-1]+minps[i-1]+ss[i+1]+minss[i+1]);
}
ret.pb(ans);
}
}
return ret;
}
}
return vector<ll>(q, -1);
}
// int32_t main () {
// ios::sync_with_stdio(false), cin.tie(0);
// int N_;
// cin >> N_;
// vector<int> W(N_), A(N_), B(N_);
// for (int i=0; i<N_; i++)
// cin >> W[i] >> A[i] >> B[i];
// int Q_;
// cin >> Q_;
// vector<int> E(Q_);
// for (int i=0; i<Q_; i++)
// cin >> E[i];
// vector<ll> ans=calculate_costs(W, A, B, E);
// cout << "-------------------\n";
// cout << "Answer:\n";
// for (ll x:ans) {
// cout << x << ' ';
// }
// cout << '\n';
// return 0;
// }
# | 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... |
# | 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... |