Submission #1168683

#TimeUsernameProblemLanguageResultExecution timeMemory
1168683M0stafaŽarulje (COI15_zarulje)C++20
0 / 100
3 ms2628 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)

zarulje.cpp: In function 'int main()':
zarulje.cpp:26:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |         freopen(file".inp","r",stdin); freopen(file".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
zarulje.cpp:26:47: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   26 |         freopen(file".inp","r",stdin); freopen(file".out","w",stdout);
      |                                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...