Maximum Unique Segment
All submissions for this problem are available.
Read problems statements in mandarin chinese, russian and vietnamese as well.
You are given 2 arrays W = (W1, W2, .., WN) and C = (C1, C2, .., CN) with N elements each. A range [l, r] is unique if all the elements Cl, Cl+1, .., Cr are unique (ie. no duplicates). The sum of the range is Wl + Wl+1 + ... + Wr.
You want to find an unique range with the maximum sum possible, and output this sum.
- The first line of the input contains an integer T, denoting the number of test cases. The description of each testcase follows.
- The first line of each test case contains a single integer N, denoting the size of the arrays.
- The second line contains N space-separated integers : C1, C2, .., CN.
- The third line contains N space-separated integers : W1, W2, .., WN.
For each testcase, output a single integer in a new line, which should be the maximum possible sum of an unique range.
- 1 ≤ T ≤ 100
- 1 ≤ N ≤ 1000000
- 0 ≤ Ci < N
- 0 ≤ Wi ≤ 1000000000
- 1 ≤ Sum of N over all test cases ≤ 1000000
- Subtask #1 (30 points): Sum of N over all test cases ≤ 10000
- Subtask #2 (70 points): Original constraints
Input: 1 5 0 1 2 0 2 5 6 7 8 2 Output: 21
The range [2, 4] is an unique range because (1, 2, 0) has no duplicates. Its sum is 6 + 7 + 8 = 21. This is the maximum possible, and hence is the answer.
|Tags||easy, grumpy_gordon, ltime49, two-pointers|
|Time Limit:||1 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, CLOJ, FS|
Fetching successful submissions