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:||ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.3.2, CPP 6.3, CPP14, CS2, D, ERL, FORT, FS, GO, HASK, ICK, ICON, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYPY, PYTH, PYTH 3.5, RUBY, SCALA, SCM chicken, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC|
Fetching successful submissions
If you are still having problems, see a sample solution here.