1500.Fireworks

Time Limit: 1s Memory Limit: 256MB

It's family beaches that I desire
Sacred nights, where we watch the fireworks...

--- Animal Collective, Fireworks


At a family gathering, you want to go to the fireworks. Unfortunately, flashy and splendid sparklers would frighten the babies.

There are $n$ locations where fireworks can be placed. Let's define $a_i$ as the viewing level of location $i$ and $b_i$ as the frightening level of location $i$, where $a_i$ and $b_i$ can only take values of $0$ or $1$. In order to enjoy the beauty of the fireworks while ensuring a pleasant atmosphere, you are going to pick a continuous section $[l, r]$ $(1 \le l \le r \le n)$ to place the fireworks, in such a way that the value of the following function is maximized:

$
f(l, r) = \sum_{i=l}^{r} a_i - \Big(\sum_{i=l}^{r} b_i\Big)^2
$

Input Format(From the terminal/stdin)

The first line of input contains one integer $n$ $(1 \le n\le 10^5)$ --- the length of the array.

The second line contains an array $a$ of length $n$ $(a_i \in {0, 1})$.

The third line contains an array $b$ of length $n$ $(b_i \in {0, 1})$.

Output Format(To the terminal/stdout)

Print a single integer, representing the maximum value of $f$.

Sample Input 1

Copy
5
1 0 0 0 1
0 1 0 0 0
 \n
 · · · · \n
 · · · · \n

Sample Output 1

Copy
1
 \n

Sample Input 2

Copy
11
1 0 1 1 1 1 1 1 1 1 1
0 0 0 1 0 0 0 0 1 1 0
  \n
 · · · · · · · · · · \n
 · · · · · · · · · · \n

Sample Output 2

Copy
6
 \n
Source: The 2024 Hunan University Programming Contest

Submit

请先 登录

© 2025 FAQs