Official
A - Two Sequences 2 Editorial by evima
We can represent \(c_n\) as \(c_{n} = \max(c_{n-1}, \max(a_1, a_2, \ldots,a_n)b_n)\), that is, the greater of the following: \(c_{n-1}\), and the maximum value among the first \(n\) values in \(a\) multiplied by \(b_n\).
We can easily find the maximum value among the first \(n\) values in \(a\). It is also easy to find \(c_1\) from \(c_{1} = a_{1}b_{1}\). Thus, we can find \(c_2,c_3,\ldots,c_N\) in this order in a total of \(O(N)\) time, which is fast enough for \(N \leq 2 \times 10^5\).
#include <algorithm>
#include <iostream>
#include <vector>
using ll = long long;
using namespace std;
const char EOLN = '\n';
int n;
int main() {
cin >> n;
vector<ll> a(n), b(n), c(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
ll ma = a[0];
c[0] = a[0] * b[0];
for (int i = 1; i < n; i++) {
ma = max(ma, a[i]);
c[i] = max(c[i - 1], ma * b[i]);
}
for (int i = 0; i < n; i++) cout << c[i] << endl;
return 0;
}
posted:
last update: