# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1168683 | M0stafa | Žarulje (COI15_zarulje) | C++20 | 3 ms | 2628 KiB |
// O(N) solution
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define ii pair <int, int>
#define ill pair <ll, ll>
#define ild pair <ld, ld>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define file "test"
using namespace std;
const int N = 3e5 + 2;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const ll base = 311;
const int BLOCK_SIZE = 400;
int n, k;
ll a[N], b[N], f[N];
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifndef ONLINE_JUDGE
freopen(file".inp","r",stdin); freopen(file".out","w",stdout);
#endif
cin >> n;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++) cin >> b[i];
memset(f, -0x3f3f, sizeof f);
stack <ill> h;
for (int i = 1; i <= n; i ++) {
ll cur = f[i - 1];
while (!h.empty() && a[h.top().se] >= a[i]) {
cur = max(cur, h.top().fi);
h.pop();
}
f[i] = max((h.empty() ? b[i] : f[h.top().se]), cur + b[i]);
h.push({cur, i});
}
cout << f[n];
}
/*
/\_/\ zzZ
(= -_-)
/ >u >u
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |