We are given an array A of N positive integers. Operation: replace two adjacent elements Ai and Ai+1 by their sum. That is, we pick an adjacent pair and replace them with a single element equal to their sum. So the array length decreases by 1 each operation. We can perform any number of operations. Essentially we can partition the original array into consecutive segments, and each segment will be replaced by the sum of numbers in that segment. The final array is the sequence of sums of these segments. The order of segments is preserved. The operation can merge any adjacent elements, potentially repeatedly, so final array corresponds to any partition of the original array into contiguous blocks. For each block, we sum its elements. The final array length is the number of blocks.
We want to maximize the number of elements with the same value in the final array (i.e., the frequency of the most frequent value). We can choose any partition (any merging) to achieve a maximum possible count of equal values. And we need to output that maximum count, and also all possible values that can appear that many times (i.e., the set of values X such that there exists a partition with exactly maxcount elements equal to X; actually the problem says: "what are the values it could be?" likely meaning: which values can appear as the repeated value achieving that maximal count? In the sample output, max count is 2, and values are 8,10,11,16. So they list all values for which there exists a partition achieving two equal elements. Possibly also values that could be the most frequent? But since max count is 2, any value that appears twice in some partition is output, regardless of whether the rest of elements might also have the same value? Wait: we are asked: "the maximal number of elements with the same value Sonya can get and what are the values it could be?" That suggests: we find the maximum possible frequency fmax. Then, consider all possible values v such that there exists a partition where the resulting array has at least fmax elements equal to v? But since fmax is maximum possible, any such v would have exactly fmax or possibly more? Actually if we can achieve frequency fmax, then for that partition the most frequent value appears fmax times. It could be that there is also other values that appear multiple times but not more. So we need to output all values that can be the value that appears the maximum number of times? But the sample output for fmax=2 lists 8,10,11,16. Let's verify if each of these can be achieved as a value that appears exactly twice (or maybe more?) but because max is 2, they can't appear more. Check: can we get two 8s? Yes, as shown. Two 10s: yes. Two 11s: yes. Two 16s: yes. Could there be a value like 5? To get two 5s would require partition with two 5s. Is that possible? Possibly not. So they list only those values that can appear at least 2 times. But could we get two 15s? Not sure. But they didn't list many other values. So likely we need to list all values v such that there exists a partition where at least fmax elements are equal to v (and fmax is the maximum possible overall). However, there might be multiple values v for which we can achieve fmax occurrences, and we list them sorted.
But careful: the phrase "the maximal number of elements with the same value" means we want the highest possible count of equal elements in the final array. Then "what are the values it could be?" might mean: for that maximal count, which values can achieve that count? So yes: output fmax, then the set of values v such that there exists a partition with fmax elements equal to v (or at least fmax? But fmax is maximum, so exactly fmax). So the second line lists all v such that you can get an array with exactly fmax copies of v (and perhaps other elements not equal to v). The sample matches.
Now, we need to solve for N up to 100, Ai up to 1000. Total sum S up to 100,000. But we can do DP. The operation essentially partitions the array into blocks. The final array values are block sums. We want to choose a partition to maximize the frequency of some sum value. Equivalent to: we have an array of N numbers. We can merge contiguous segments into a single number (their sum). After merging, we have a new array of numbers (sums of blocks). We want to maximize the count of equal numbers. We can think of selecting some target value v, and we want to partition the array into blocks such that as many blocks as possible have sum = v, and the rest are arbitrary. But the total number of blocks is not fixed; we can merge as we like. The frequency of v we achieve is the number of blocks with sum exactly v. We can try to maximize over v and over partitions.
Observations: Since all Ai >0, block sums are positive and strictly increasing if we take prefix sums. A partition corresponds to choosing cut positions. Block sums are sums of subarrays.
So essentially, we are looking for a multiset of disjoint contiguous subarrays (they cover the entire array, non-overlapping, covering all indices) such that many of these subarrays have sum equal to v. We want the maximum possible number of v-sum subarrays in a partition of the array. For each v, we want the maximum number of blocks with sum v in any partition (i.e., we want to partition the whole array into blocks, maximizing the count of blocks that sum to v). Then fmax = max over v of that maximum count. And the set of v achieving fmax are those for which the optimal partition yields that count.
Now the problem: N=100, Ai up to 1000, total sum up to 100,000. v can be any possible subarray sum. Number of possible subarray sums is O(N^2) ~ 10,000. But total sum up to 100k, but we can probably compute DP for each v? Might be doable: For each v, we need to compute the maximum number of blocks with sum = v in a partition of the entire array (covering all elements). This is reminiscent of "maximum number of segments with sum v partitioning the array" – essentially, we can treat it as: we need to cover the array from left to right; we can cut segments; each segment can either be a v-sum segment (good) or a non-v-sum segment (bad). We want to maximize the number of good segments while covering the whole array. However, note that we are allowed to merge any segments arbitrarily; but this DP essentially captures all partitions: we can decide to cut at positions; each segment is a block. The order matters: we must cover all indices exactly once.
Thus for a fixed v, we can compute the maximum number of v-sum blocks in a partition using DP: let dp[i] = maximum number of v-sum segments we can get in a partition of prefix up to i (i.e., A[1..i]). Then dp[0]=0. For each i from 1 to N, we consider j from i down to 1, compute sum of A[j..i]. If sum == v, then we can have dp[i] = max(dp[i], dp[j-1] + 1). Also we can have blocks that are not v-sum: we can also consider simply taking the element i alone? But if we don't match v, we still need to cover the suffix. Actually, a partition of the prefix must end with a block covering from some start to i. That block can have sum != v, in which case we do not increase the count. So we also need to allow dp[i] = max(dp[i], dp[j-1]) if we take block from j to i with any sum? But that would overcount because we could arbitrarily choose any j? Actually we want to cover the entire prefix. The DP recurrence: dp[i] = max over j from 1 to i of (dp[j-1] + (1 if sum(j,i)==v else 0) ). However, is that always valid? For any partition of A[1..i], the last block is some subarray A[k..i] for some k. The prefix before k-1 is partitioned optimally. The count of v-sum blocks in the whole is the count in the prefix plus whether the last block sum == v. So indeed dp[i] = max_{k=1..i} [ dp[k-1] + (sum(k,i)==v ? 1 : 0) ]. This gives the maximum number of v-sum blocks possible in any partition of prefix i (all elements must be covered). So we can compute dp[N] for each v. That's O(N^2) per v, which is O(N^4) overall if we do for each v, too heavy (10^8 maybe borderline? N=100, v choices up to 5000? Actually number of distinct subarray sums could be up to 100101/2=5050, times N^2=10,000 gives about 50 million, which might be okay in Python? 50 million operations might be borderline but possibly acceptable if optimized. But we also need to compute sums efficiently. We can precompute prefix sums to get sum(j,i) in O(1). But doing for each v: for i in 1..N, for j in 1..i, check if sum(j,i)==v. That's O(N^2) checks per v. With ~5000 v, that's 50005050 ≈ 25 million? Actually N=100, so number of (j,i) pairs is 5050. Times number of v distinct sums? There can be up to 5050 distinct sums; 50505050 = about 25.5 million. That's not huge. However, the DP for each v also includes an inner loop over j for each i? Wait: The DP recurrence dp[i] = max_{k} (dp[k-1] + indicator). To compute dp[i] efficiently, we need to consider all k from 1..i. That is exactly the same set of pairs (k,i). So for each v, we would iterate all pairs (k,i) once to compute dp. That's 5050 pairs. So total pairs processed across all v: number_of_v * 5050. That could be up to 50505050 ≈ 25.5 million. Plus overhead. That's definitely fine.
But we also need to consider that v might be any integer from 1 to S, not just subarray sums? Could a block sum be a value that is not any subarray sum? Since we are summing contiguous elements, any block sum is a subarray sum. So v that are not a subarray sum cannot appear as a block sum. So we can restrict v to the set of all possible subarray sums. However, it's possible that the optimal frequency might involve a value that is not a subarray sum? No, because to have a block with sum v, that block's sum must equal v. So v must be a subarray sum. So we only need to consider v from the set of subarray sums.
But careful: The final array can have values that are not subarray sums? Actually each block's sum is sum of some contiguous segment, so yes it's a subarray sum. So v must be some subarray sum. So we can limit to all subarray sums.
Now, for each v, compute dp[N] which gives the maximum number of v-sum blocks in a partition of the whole array. The answer fmax = max_v dp[N]. Then collect all v with dp[N] == fmax.
But is this correct? Let's test with sample. Compute subarray sums: we have many. For v=8: find max number of blocks with sum 8. Let's simulate: array: 11,5,6,2,8,10. Can we partition to get two 8s? Partition: [11], [5,6]? Wait 5+6=11 not 8. Actually the sample gives: {11,5,8,8,10} corresponding to partition: 11;5;6+2? Actually they merged 6 and 2 into 8, and then also there is a separate 8 from original? Wait they transformed: {11,5,6,2,8,10} => {11,5,8,8,10}. Steps: merge 6 and 2 to 8, so array becomes: 11,5,8,8,10. So partition is: [11], [5], [6,2] (sum 8), [8], [10]? Actually there are two 8s: one from merging 6+2, and one original 8. So in final array, we have two elements equal to 8. In terms of partition of original, we cover all indices: block1: A1=11; block2: A2=5; block3: A3+A4=6+2=8; block4: A5=8; block5: A6=10. That's 5 blocks. So number of v=8 blocks = 2. Could we get 3 blocks of 8? Possibly not. So dp[N]=2 for v=8. Similarly for v=10, partition: [11], [5,6,2]? 5+6+2=13 not 10. Actually sample: {11,5,6,10,10} from merging 8+10? Wait: they gave {11,5,6,10,10}. That's from merging 2+8? Actually original: 11,5,6,2,8,10. To get two 10s, they likely merged 2 and 8 to 10? That gives 11,5,6,10,10. So partition: [11], [5], [6], [2,8] (sum 10), [10]. That's 5 blocks, two 10s. Also maybe [11,5]? Anyway dp for v=10 should be 2. For v=11: partition: [11], [5,6]? 5+6=11, then [2], [8], [10] gives two 11s? Actually they gave {11,11,2,8,10} after merging 5+6? That yields [11], [5,6] (sum 11), [2], [8], [10] -> two 11s. So dp=2. For v=16: partition: {16,16,10} after merging: first merge 5+6+2+? Actually they did: 11,5,6,2,8,10 -> {11,5,8,8,10} -> {11,5,16,10} -> {16,16,10}. Let's see the final partition: {16,16,10} corresponds to blocks: first block sum 16, second block sum 16, third block sum 10. The original array: to get that, we need to find a partition into three blocks: block1 sum=16, block2 sum=16, block3 sum=10. Indeed: maybe block1 = 11+5? That's 16; block2 = 6+2+8? That's 16; block3 = 10. That works. So dp for v=16 is 2. So fmax =2. So that matches.
Now we need to be cautious: Is it always true that the maximum frequency of a value v in the final array equals dp[N] as defined? Let's think: We want to maximize the count of elements equal to v. In any partition, each element corresponds to a block. So the number of elements equal to v is exactly the number of blocks with sum v. So indeed it's the count of good blocks. And we want to maximize this count over all partitions. So dp[N] is correct.
But is there any nuance? Could we possibly have extra copies of v arising from merging blocks that originally were not v? No, the value of a block is its sum, so to get v we need a block sum = v. So it's exactly that.
Thus the problem reduces to, for each possible subarray sum v, compute the maximum number of disjoint (and covering) subarrays with sum v that can be selected to partition the array, with the condition that the subarrays are disjoint, cover all indices, and we can also have other subarrays (any sum) to fill the gaps. This is exactly the DP above.
Now DP recurrence: dp[i] = max_{k=1..i} (dp[k-1] + indicator(s(k,i)==v)). This is straightforward. Complexity O(N^2) per v. Since N=100, and number of distinct sums <= 5050, total pairs 5050*5050 = 25M. This is fine in Python, but we need to be careful about overhead of loops and dictionary lookups. But we can optimize by precomputing all subarray sums and grouping by sum? Possibly we can compute dp for all v simultaneously? Could use DP to track best frequency for each possible sum as we go? But perhaps the simple approach works.
But there might be a nuance: The DP as described only considers partitions that end exactly at i. It always covers all indices. That is correct.
Edge case: The entire array could be merged into a single block. Then dp[N] could be 1 if its sum == v, else 0.
Now, we need to output all v for which dp[N] == fmax, sorted ascending.
One more nuance: Could there be multiple occurrences of the same v from overlapping subarrays? Not allowed because partition requires non-overlapping and covering.
Now, check if DP yields correct answer for sample.
But we need to consider: Is it possible to achieve a frequency higher than the maximum number of v-sum blocks in any partition? Could we have a scenario where we have multiple equal values but they are not all of the same type? Wait we only count elements with the same value. So we want the maximum cardinality of a value class. That's what we compute.
Now, confirm DP correctness: Suppose we have dp[i] = max number of good blocks for prefix length i. The recurrence as described includes also the possibility of the last block not being good. This is covered because we consider all k; the indicator will be 0 if sum != v. However, what if it's beneficial to sometimes not cover some prefix optimally? The DP's definition uses optimal prefix, so it's fine.
But is it always optimal to take the max of dp[k-1] + indicator? Yes.
Now, we need to ensure that we consider the possibility of not merging some elements into any block that sums to v, but we still need to cover them, so they become other blocks. That's covered by indicator = 0.
Now, careful: For dp[i], we must consider that the block from k to i could be part of the prefix; but dp[k-1] is defined as the maximum number of good blocks in an optimal partition of A[1..k-1]. However, it's possible that in an optimal partition for prefix i, the prefix part (1..k-1) is not optimally covered with respect to good blocks, because maybe a suboptimal prefix yields a better overall if the last block is good? But DP using max ensures we take the best combination: for each possible last block, we take the best prefix partition (dp[k-1]) and add indicator. Since dp[k-1] is the maximum possible for that prefix, using it can't be worse. So recurrence is correct.
Thus DP works.
Now, there might be an alternative interpretation: Could we perform operations in any order, not necessarily merging into a partition of the original? But merging adjacent sums repeatedly is equivalent to partitioning, because each operation merges two adjacent elements; after a series of merges, the final array corresponds to a partition into original contiguous segments. This is a standard equivalence: each merge reduces two adjacent blocks into one; the final blocks are sums of contiguous original elements. So yes.
Now, the DP approach as described computes the maximum number of blocks of sum v. That yields fmax.
But we also need to consider the possibility that fmax could be 0? But at least one element, so maximum number of elements with same value is at least 1 (since we can leave the array as is, and the most frequent element might be 1 if all distinct, but we could also merge to get duplicates? Actually we can always have a partition of all single elements, and the frequency of any value is at most the count of that value in the original array. Could it be that we can't get any duplicate? Possibly yes if all numbers are distinct and no combination equals any other element or another combination? But we could merge some to create duplicates. For example, if array is [1,2,3], can we get two equal numbers? Possibly 1+2=3, then array becomes [3,3] so two 3s. So we can get frequency 2. So maybe fmax >=1 always, but it's possible that only 1? Consider [1,2]. Can we get two equal numbers? Merging 1+2=3, then array is [3], only one element. So maximum frequency is 1 (either original [1,2] has max count 1, or [3] has count 1). So fmax can be 1. So need to handle that.
Then for fmax=1, what values could be? Possibly all values that can appear as an element in some final array? But the problem asks: "the maximal number of elements with the same value Sonya can get and what are the values it could be?" For fmax=1, that means the best we can do is have only one element of each value (i.e., no duplicates). Then what values can be the most frequent? If max frequency is 1, then any value that appears at least once in some final array could be considered the most frequent (since it appears once and all others maybe also appear once or not?). But careful: In a final array with length L, if all elements are distinct, then the frequency of each value is 1, so the maximum frequency is 1, and any value in that array qualifies as "the value" that appears the maximum number of times (which is 1). But the problem likely expects the set of values that can appear with that maximum frequency. If max frequency is 1, then any value that can appear in any partition is possible as the value that appears at least once (and since max=1, the value appears exactly once in that array). However, the problem statement: "what are the values it could be?" It might refer to the value(s) that achieve the maximal number of elements. If the maximum is 1, then any value that appears in the final array in some partition could be considered to be the value that appears 1 time, which is the maximum. But does the problem intend to list all possible values that can be the value that appears the maximum number of times? That would be huge: all possible subarray sums. That seems messy and maybe they intend that for maximum count >=2, but with constraints up to 100 and Ai up to 1000, the set of possible sums can be up to 5000. That's still manageable to list all? But if fmax=1, then we would need to list all possible subarray sums that can be a block in some partition? Actually any subarray sum can be a block if we partition accordingly? For any subarray sum v, can we create a partition that includes a block of sum v? Yes: we can partition the array such that one block is exactly a subarray that sums to v, and the rest are arbitrary partitions (e.g., individual elements). So any subarray sum can appear as a value in some final array. So the set of values would be all subarray sums. But is that always possible? Need to ensure that we can partition the rest into blocks covering the remaining indices, but that's always possible by taking each remaining element individually. So yes, any subarray sum can be realized as an element in the final array. So for fmax=1, the set of values would be all possible sums of any contiguous subarray. That could be up to 5050 numbers. But the problem output expects to print them in ascending order separated by space. That's feasible. So we need to handle that.
But maybe the problem expects to list values that can appear the maximal number of times, meaning we need to output those v for which there exists a partition where at least fmax elements equal v (i.e., achieving the maximum frequency). So if fmax=1, we need to list all v for which we can have a partition with at least one element equal to v? But that would be all subarray sums. However, is it guaranteed that fmax=1 if no duplicates are possible? Possibly, but it's plausible. So we need to output up to ~5000 numbers, which is fine given constraints.
Now, confirm sample: fmax=2, they output 8 10 11 16. So indeed they listed only those v for which dp[N]=2. So algorithm stands.
Now we must ensure we compute dp correctly. Implementation details:
- Input N and array A (1-indexed maybe).
- Precompute prefix sums: pref[0]=0, pref[i] = sum_{1..i} A[i].
- Compute set of all subarray sums: For all 1<=i<=j<=N, sum = pref[j]-pref[i-1]. Collect in a set S.
- For each v in S, compute dp array size N+1, dp[0]=0. For i from 1 to N: initialize dp[i] = -inf? Actually we want max. But we know that dp[i] can be at least something. Recurrence: for k in 1..i: if dp[k-1] is defined (which it will be) then candidate = dp[k-1] + (1 if (pref[i]-pref[k-1]==v) else 0). Take max. This is O(N^2) per v.
- But note: dp values can't be negative; the minimum number of good blocks is 0. So we can initialize dp[i]=0? Wait, if we set dp[i] = max_{k} (dp[k-1] + indicator). If we start with dp[i]=0, that might be valid if there exists a partition that yields 0 good blocks? But is it always possible to have a partition with 0 good blocks? Yes, just partition into all single elements; none of those necessarily equal v? But it could be that some single element equals v. That would yield indicator=1 for that block if we choose that partition. So 0 is achievable only if we can avoid having any block equal to v. Is it always possible to avoid having any block equal to v? Possibly yes if we never form a block summing to v. But we can always take all elements as singletons, but if any Ai equals v, then that singleton would be a good block. So 0 might not be achievable if there exists at least one element equal to v. But dp[i] defined as maximum, so we want maximum. So if we initialize dp[i] with a low number (like -inf), we can compute max correctly. However, we can also set dp[i] = dp[i-1] + 0? Actually we need to consider all k. For each i, we can start with dp[i] = dp[i-1] + 0? That would correspond to taking last block as a single element (k=i) and indicator = (A[i]==v). But we also need to consider that we could combine more elements in the last block to avoid a good block even if A[i]==v: if we merge A[i] with previous element, the sum might not equal v. So it's possible to avoid having a good block even if some element equals v. So the maximum could be >0, but we compute max over all partitions. So we just compute max.
Thus we need to do nested loops: for i from 1 to N: dp[i] = max_{k=1..i} (dp[k-1] + (pref[i]-pref[k-1]==v)). We can compute this by iterating k. Complexity O(N^2) per v.
Potential optimization: For each v, we can precompute for each (k,i) whether sum(k,i)==v. But we can compute on the fly.
Now, we must consider that N=100, S up to 100,000, number of v maybe up to 5000. Doing 5000 * 5050 = 25 million iterations. Each iteration includes simple integer operations and a comparison. That's fine in Python. But we need to be careful about the DP array allocation per v (size 101). That's fine. The main loops: for each v in sums: for i in range(1,N+1): for k in range(1,i+1): compute candidate. That's triple nested: v * N^2/2 = about 5000*5000 = 25M. That's okay.
But we can also precompute for each v the list of pairs (i,k) where sum equals v to speed up? Not necessary.
However, we need to store dp per v; but we also need to track the maximum fmax across v and then collect v with dp[N]==fmax.
But one nuance: The DP recurrence as described gives the maximum number of v-sum blocks in a partition of the entire array. However, is it possible that we could achieve a higher frequency by not covering the entire array? No, because the operations always result in an array covering all original elements. So we must cover all indices. So partition must be a full covering.
Thus dp[N] is correct.
Edge Cases:
-
N=1: Only one element. We can't do any merges (or can we? operation requires adjacent pair, none exists). So final array is [A1]. So max frequency = 1. Which values can achieve that? The only possible value is A1. But also could we consider merging? No. So answer: 1 and A1. Our algorithm: subarray sums set = {A1}. For v=A1, dp[1] = max_{k=1..1} dp[0] + (sum(1,1)==A1) = 0+1=1. So fmax=1, and v=A1 qualifies. Good.
-
N=2: e.g., [1,2]. Subarray sums: 1,2,3. For v=1: dp[2]? compute: i=1: dp[1] = max_{k=1} dp[0]+ (sum(1,1)==1) = 0+1=1. i=2: for k=1: dp[0]+(sum(1,2)==1? 3==1 false) =>0; k=2: dp[1]+(sum(2,2)==1? 2==1 false) =>1; max=1. So dp[2]=1. v=2: similarly dp[2]=? i=1: dp[1]= (sum(1,1)==2? false) =>0? Actually k=1: dp[0]+(1==2?) =>0; so dp[1]=0. i=2: k=1: dp[0]+(sum(1,2)==2? 3==2 false)=0; k=2: dp[1]+(2==2? true)=0+1=1; so dp[2]=1. v=3: dp[1] from k=1: sum(1,1)==3? false ->0; dp[1]=0. i=2: k=1: dp[0]+(sum(1,2)==3? true)=1; k=2: dp[1]+(sum(2,2)==3? false)=0; max=1. So all give dp[2]=1. So fmax=1, and the set of v with dp[N]==1 is {1,2,3}. Is that correct? Can we achieve a final array with two equal numbers? Let's test: we can only merge to get [3] (single element) with frequency 1, or keep [1,2] with frequency 1 for each. So maximum frequency is 1. Which values could be the most frequent? In the array [1,2], the frequencies are both 1, so both 1 and 2 are values that appear once, which is max. In the array [3], the value 3 appears once, which is max. So indeed values 1,2,3 can all be the most frequent value in some final array. So output would be "1\n1 2 3". That makes sense. So our algorithm yields that. Good.
Now, confirm that dp[N] is indeed the maximum number of good blocks. But we must ensure that we don't double count blocks that overlap? The DP ensures a partition.
Now, consider if there are multiple ways, dp[N] gives the maximum count.
Now, potential pitfalls: The DP recurrence can be optimized by noting that for a fixed v, we only need to consider k where the sum of A[k..i] == v for increasing dp. But the naive loop over all k is simple.
One subtlety: dp[i] might be -inf if no partition exists? But there is always a partition (e.g., all single elements), so dp[i] is at least something. But if we start dp[0]=0 and compute dp[i] as max, it will be at least something. However, consider the case where we try to force a block sum v at the end: if we take k such that dp[k-1] is defined, it's fine. So dp[i] will be computed.
Now, we must ensure we consider all possibilities: Could there be a scenario where merging two blocks that are not adjacent in the original partition? No, because the operation merges adjacent blocks. But the DP covers all partitions: any sequence of merges yields a partition; any partition can be obtained by merges. So it's fine.
Now, we should also consider that the operation can be done any number of times, but we might be able to achieve a partition where some blocks are formed by merging more than two elements, that's covered.
Thus the algorithm stands.
Now, we need to implement efficiently. Steps:
- Read N.
- Read list A (size N).
- Compute prefix sums pref[0..N].
- Compute set S of all subarray sums: for i in 1..N: for j in i..N: S.add(pref[j]-pref[i-1]).
- Initialize fmax = 0, and list result_vals = [].
- For each v in S (maybe we should iterate in sorted order to later output sorted? But we can collect after; we need to output ascending order, so we can sort at the end. We'll compute dp[N] for each v, track fmax, and if dp[N] > fmax, update fmax and reset result_vals = [v]; if equal, append v.
- After loop, sort result_vals.
- Print fmax.
- If result_vals not empty, print them space-separated.
Edge Cases: N=100, S size up to 5050. The DP for each v is O(N^2)=~5000. So total ~25 million, fine.
But we need to be careful about using Python's loops. 25 million is okay in 1-2 seconds? Possibly. However, each DP inner loop does a few operations: indexing, subtraction, comparison, addition, max. With 25M iterations, should be okay (< 0.5s?). But Python might be around 0.2-0.5 seconds per 10M simple ops? Actually Python can do about 50-100 million simple operations per second on modern hardware? Not exactly; loops are slower. Typically, a double nested loop of 5000*5000 = 25M with simple operations might take about 1-2 seconds, which is acceptable for typical contest constraints (maybe 1 sec). But we also have overhead of set iteration. Should be okay.
But we can optimize further: Precompute for each pair (k,i) the sum, and store in a dictionary mapping sum->list of (i,k) or something to avoid iterating over all k,i for each v. Instead, we could compute DP for all v simultaneously using DP with state representing possible last cut? Another approach: For each i, we can compute dp[i] as max over previous dp[j] plus maybe 1 if sum(j+1,i) equals some v. But we want per v. Hmm.
But maybe we can compute the maximum frequency directly without per-v DP using a more clever DP that tracks counts for each possible block sum? Actually we can compute for each possible sum the maximum number of blocks with that sum in a partition. This is like we want to compute for each sum v the maximum number of v-sum blocks. This can be seen as: let dp[i] be the maximum number of blocks of a particular sum v? No.
But perhaps we can compute a DP that for each i, we keep track of the maximum count for each possible value of the last block? That might be complex.
Given constraints, the per-v DP is fine.
We must also be careful: The number of distinct subarray sums can be less than 5050 because Ai up to 1000, but still could be up to that. So okay.
Now, I want to test on some random cases to verify correctness.
Consider N=3, A=[1,1,1]. Subarray sums: 1,2,3. Let's compute per v:
v=1: We want max number of blocks summing to 1. Can we partition to have three blocks of 1? Yes, keep as singles: [1,1,1] gives three 1s. So dp[3] should be 3. Is that achievable? The DP: dp[0]=0; i=1: k=1: dp[0]+(1==1)=1 => dp[1]=1. i=2: possibilities: k=1: dp[0]+(sum(1,2)=2==1? false)=0; k=2: dp[1]+(A2=1==1)=1+1=2 => dp[2]=2. i=3: k=1: dp[0]+(1+1+1=3==1? false)=0; k=2: dp[1]+(sum(2,3)=2==1? false)=1; k=3: dp[2]+(A3=1==1)=2+1=3 => dp[3]=3. Good.
v=2: Can we get two blocks of 2? Possibly partition: [1,1] (sum2) and [1] (sum1) => only one block of 2. Could we get two blocks of 2? Need three elements total sum 3, so impossible to have two blocks of 2 because sum would be at least 4. So max count likely 1. DP: compute: i=1: k=1: sum=1!=2 =>0; dp[1]=0. i=2: k=1: sum(1,2)=2 => dp[0]+1=1; k=2: sum(2,2)=1!=2 => dp[1]+0=0; max=1 => dp[2]=1. i=3: k=1: sum(1,3)=3!=2 =>0; k=2: sum(2,3)=2 => dp[1]+1=0+1=1; k=3: sum(3,3)=1!=2 => dp[2]+0=1; max=1 => dp[3]=1. So dp[3]=1.
v=3: can we get 1 block of 3? Yes, merge all: [3] -> one 3. Could we get two blocks of 3? No. So dp[3]=1.
Thus fmax = 3, result_vals = [1]. So output: 3 and 1. That makes sense: maximum frequency is 3 (all ones). And the only value that can appear 3 times is 1. Indeed we could also get three 1s. Could we get three of any other value? No. So answer matches.
Now, consider [2,2,2]. Similarly fmax=3, val=2.
Now, consider [1,2,3]. Sums: 1,2,3,4,5,6. Let's compute dp:
We can get two 3s? Example: 1+2=3, and the 3 original -> [3,3] -> two 3s. So dp for v=3 should be 2. Could we get two of any other? 1+2=3, can't get two 1s because 1 appears once. Two 2s? 2 appears once, but we could merge 1+? Actually 1+? to get 2? 1+1? Not available. Could merge 3-? No. So maybe only 3. Also maybe 6? Only one block of 6. So fmax=2, values:3. Let's test DP for v=3: i=1: sum(1,1)=1!=3 -> dp1=0. i=2: k=1 sum=3 -> dp0+1=1; k=2 sum=2 -> dp1+0=0 => dp2=1. i=3: k=1 sum=6!=3 ->0; k=2 sum=5!=3 ->dp1+0=1; k=3 sum=3 -> dp2+1=1+1=2 => dp3=2. Good.
Now, for v=2: dp3? i=1: sum1=2? 1!=2 ->0; dp1=0. i=2: k=1 sum=3!=2 ->0; k=2 sum=2 -> dp1+1=1 => dp2=1. i=3: k=1 sum=6!=2; k=2 sum=5!=2 => dp1+0=0; k=3 sum=3!=2 => dp2+0=1 => dp3=1. So max 1.
v=1: dp3 =? i=1: sum1=1 ->1; dp1=1. i=2: k=1 sum=3!=1->0; k=2 sum=2!=1 -> dp1+0=1 => dp2=1. i=3: k=1 sum=6!=1; k=2 sum=5!=1; k=3 sum=3!=1 -> dp2+0=1 => dp3=1. So 1.
Thus fmax=2, only v=3. So output 2 and 3.
Now, is it possible that there is a scenario where we could get fmax greater than the maximum frequency of any value in the original array? Obviously yes, as we saw.
Now, more complex scenario: N=4, [1,1,2,2]. Let's think. Could we get three of some value? Perhaps 2? We have two 2s originally, but we can create another 2 by merging 1+1? 1+1=2, so we could have three 2s: partition: [1,1] (2), [2], [2] -> three 2s. That yields array [2,2,2] length 3 with three 2s. So fmax=3, value=2. Could we get three 1s? No because sum of two 2s is 4, not 1. Could we get three 4s? Total sum 6, can't have three 4s. So answer: 3 and 2. Check DP for v=2: should give 3. i=1: sum1=1!=2 ->dp1=0. i=2: k=1 sum=2 -> dp0+1=1; k=2 sum=1!=2 -> dp1+0=0 => dp2=1. i=3: k=1 sum=4!=2; k=2 sum=3!=2; k=3 sum=2 -> dp2+1=2 => dp3=2. i=4: k=1 sum=6!=2; k=2 sum=5!=2; k=3 sum=4!=2; k=4 sum=2 -> dp3+1=3 => dp4=3. Yes.
Now, consider [1,3,2,2]. Can we get three 2s? Possibly? Original two 2s, and 1+? 1+? to get 2 would need 1, but we have 1 and 3 and 2,2. 1+? 1+1? not available. Could merge 1+3=4; not 2. So maybe not. Could get two 2s? Yes. Could get two 4s? 1+3=4 and 2+2=4 -> two 4s. So fmax=2, values: 4? Also maybe 2? Two 2s? we already have two 2s originally? Actually original: [1,3,2,2] has two 2s, but we could also have two 2s without merging, so dp for v=2 should be 2. Also could we get two 3s? 1+2=3? But we have one 3 already. Could merge 1+2=3 and keep the original 3? That would require partition: [1,2] sum 3, [3], [2]? But then we have only one 3? Wait: we need two blocks summing to 3. Options: block1: 1+2 (the third element 2? careful indices: A = [1,3,2,2]. To get two 3s: we could have block1: 1+2 (first and third?) But they need to be contiguous: 1 and 3? 1+3=4; not 3. 3+2=5; 2+2=4. So no contiguous pair sums to 3 except 1+? there is no 2 adjacent to 1. However, we could merge multiple: e.g., 1+3+? to get 3? No. So maybe only 2 and 4. Could also 5? 3+2=5 and maybe 2+? no. So maybe fmax=2 with values 2 and 4. Let's compute DP for v=2: i=1: sum=1!=2 ->0; i=2: k=1 sum=4!=2; k=2 sum=3!=2 ->0; dp2=0. i=3: k=1 sum=6!=2; k=2 sum=5!=2; k=3 sum=2 -> dp2+1=1; dp3=1. i=4: k=1 sum=8!=2; k=2 sum=7!=2; k=3 sum=4!=2; k=4 sum=2 -> dp3+1=2; also check k=... so dp4=2. So v=2 yields 2. For v=4: i=1:1!=4->0; i=2: k=1 sum=4->1; so dp2=1; i=3: k=1 sum=6!=4; k=2 sum=5!=4; k=3 sum=2!=4 -> dp2? Actually for k=2, dp1+? dp1=0? Wait dp1=0? Let's compute systematically. But likely dp4=2 for v=4. So both give 2. So result_vals would include 2 and 4. So answer: 2 and 2 4. That seems plausible.
Now, consider a scenario where multiple values achieve the same max frequency, as sample shows.
Now, we must be careful: The DP for each v only cares about maximizing count of blocks sum v. But could there be a scenario where the optimal partition for v yields dp[N] = k, but there exists another partition with same k copies of v, but maybe also extra copies of some other value making overall frequency higher? That's irrelevant because we're only counting the frequency of a single value. The maximum possible frequency of any value is the maximum over v of the maximum number of blocks with sum v. So dp[N] for v gives the best for that v. So fmax = max_v dp[N]. So it's consistent.
Now, we need to confirm that dp[N] for a given v indeed is achievable. Yes, because we can reconstruct partition.
Now, we must ensure we consider all possible partitions. The DP does.
Potential issue: The DP might overcount if we allow non-good blocks to be arbitrarily large but we only care about count of good blocks. That's fine.
Now, one more nuance: Could we get more than dp[N] by having blocks that are not contiguous in the original sense? No.
Thus algorithm seems correct.
Now, we should think about time complexity. 25 million loops is fine, but we must implement efficiently. Use local variables, list for prefix sums, maybe use Python's built-in max with generator? But for each i, we need to compute max over k. Doing a loop over k inside i is straightforward.
We can also precompute all subarray sums in a 2D list sum[i][j] but we can compute on the fly: s = pref[i] - pref[k-1]. That's fine.
We could also precompute for each i the list of sums for k from 1..i, but not needed.
Now, consider optimizing by noting that for a given v, we only need to consider k such that sum(k,i) == v to possibly add 1; otherwise candidate = dp[k-1] (no increment). But the maximum over all k can be computed as max( max_{k: sum(k,i)==v} (dp[k-1]+1), max_{k} dp[k-1] ). The second term is just the maximum dp value among all prefixes up to i-1. Since dp is increasing? Not necessarily monotonic. But we can keep track of the maximum dp among indices < i. Let's examine: For any partition of prefix i, the last block may or may not be v. If it's not v, then the number of v-sum blocks is just some dp[j] where j < i (since we can partition prefix i into prefix j and a block from j+1 to i that is not v). However, does it allow any j? The partition of prefix i with last block not v: we can have last block from k to i with sum != v. Then total v-sum count = dp[k-1]. So the maximum for such case is max_{k: sum(k,i) != v} dp[k-1]. But that's essentially the maximum dp[t] for any t from 0 to i-1, except we might need to exclude those k where sum(k,i) == v? Actually if sum(k,i)==v, that candidate is considered in the +1 case. So the candidate with no addition is dp[k-1] for all k (including those where sum==v, but we would treat them separately to add 1; but if we treat them as no addition, that would be suboptimal because we could add 1. So the overall maximum is max( max_{k: sum(k,i)==v} (dp[k-1] + 1), max_{k} dp[k-1] ). But the second max over all k from 1..i of dp[k-1] is simply the maximum dp value among indices 0..i-1. Let's denote bestPrev = max(dp[0..i-1]). Then dp[i] = max( bestPrev, max_{k: sum(k,i)==v} (dp[k-1] + 1) ). This could be more efficient because bestPrev can be computed incrementally. For each i, we can compute bestPrev = max(bestPrev, dp[i-1]) as we iterate i. However, careful: bestPrev should be max over dp[0..i-1]. So we can maintain a variable max_dp_prefix = max(dp[0..i-1]). Then for each i, we compute candidates from k where sum(k,i)==v. If we can quickly get the max of dp[k-1] for those k, we can compute candidate1 = that_max + 1. Then dp[i] = max(max_dp_prefix, candidate1 if any such k else max_dp_prefix). This reduces the inner loop to only those k where sum equals v, instead of all k. However, we need to efficiently find, for each i, the set of k such that sum(k,i) == v. That is, for a fixed v, we need to know for each i, which starting indices k yield sum = v. We could precompute a mapping from end index i to list of start indices where sum from start to i equals v. Since we are iterating v, we could for each v build this mapping by scanning all pairs (k,i). Actually we could precompute for each pair? But we can compute on the fly: while iterating i, we can loop over k from i down to 1 and compute sum and test. That's still O(N^2) per v. But the optimization would still be O(N^2) in worst case if we only reduce constant factor, but if many v, it may not reduce complexity because we still have to check each pair to see if sum == v. However, we could precompute for each i, a dictionary mapping sum -> list of k? Actually we could precompute for all pairs the sum, and then group by sum. That would be: compute all subarray sums and store for each sum the list of (i, k) or specifically for each sum v, for each end index i, the list of start indices k. Then for each v we can retrieve those relevant pairs quickly. This could eliminate the need to loop over all k for each v when doing DP; instead, for each v we only iterate over the pairs where sum equals v. The number of such pairs per v might be small on average, but in worst case, for a specific v, there could be many pairs (like if all numbers are 0, but they are positive, so each sum is distinct? Actually with positive integers, there can be many subarrays with same sum? Possibly, but maximum number of subarrays with the same sum can be O(N^2) in worst case? Consider all Ai=1, then sum = length. So each possible length appears many times? For length L, there are N-L+1 subarrays. So total pairs for a given v is up to N (since each length corresponds to a specific sum, and if all Ai=1, sum = length, so for each length L, sum = L. So each sum appears N-L+1 times. For L around N/2, that's about N/2, so O(N). Actually maximum is O(N) per sum because for a fixed sum, the subarrays have that sum; with positive numbers, the subarrays with a given sum can be at most O(N) because each start index determines at most one end index? Not exactly: With positive numbers, for a fixed start index, there is at most one end index such that sum equals a given v (since prefix sums are strictly increasing). So the number of subarrays with sum v is at most N (each start yields at most one end). So total pairs for a given v is at most N. That's good. Because for each start index k, as we increase end i, the sum strictly increases, so for a given v, there is at most one i such that sum(k,i)=v. Thus the number of subarrays with sum v is at most N. Therefore, for each v, the set of (k,i) where sum(k,i)=v is O(N). This is important. So if we precompute for each v the list of pairs (k,i) (or perhaps for each i, the k such that sum(k,i)=v), then the DP for each v can be done in O(N + number of pairs for v). Since total number of pairs across all v is total number of subarrays = O(N^2). So we can process all v in total O(N^2) time, which is even better.
Let's confirm: With positive integers, prefix sums are strictly increasing. For a fixed start k, the function f(i) = sum(k,i) is strictly increasing with i. Therefore, for a given v, there is at most one i where f(i)=v. So yes, per start k, at most one subarray with sum v. So total subarrays with sum v ≤ N. Therefore total across all v is sum_{v} (#subarrays with sum v) = total number of subarrays = N(N+1)/2.
Thus we can design an algorithm that does not iterate over all k for each v, but rather for each v we only consider the O(N) pairs where sum equals v. This can drastically reduce complexity if we have many v (like up to 5000). Instead of O(N^2 * #v) ~ O(N^3) (if we did naive triple loops) but we previously had O(N^2 * #v) but with #v up to N^2, that's O(N^4) in theory? Wait, careful: earlier we said O(N^2) per v and #v up to O(N^2) gives O(N^4). But we argued #v is O(N^2) and each DP per v is O(N^2) leading to O(N^4) = 10^8 for N=100? Actually 100^4 = 100 million; but we earlier estimated 25 million because #v max is about 5050 and N^2=10000, product 50 million; still okay. But using the better method we can get O(N^2) total plus some DP per v but with only O(N) pairs per v, resulting in total O(N^2 + sum over v (N + number_of_pairs_for_v? Actually DP per v still requires computing dp[i] for i=1..N, but we can compute dp in O(N + number_of_pairs) by using the recurrence with bestPrev. So total across all v is O(#v * N + total_pairs). Since #v can be up to O(N^2) and N=100, #vN = 5050100 = 505,000, plus total_pairs = 5050, so ~510k operations, which is trivial. That's a big improvement.
So let's adopt the optimized DP using the observation that for each v, we only need to consider the specific k for each i where sum(k,i)=v. And we can compute dp[i] using:
Let bestPrev[i] = max_{0 <= t <= i-1} dp[t]. Actually we can compute on the fly.
We'll precompute for each possible v, the mapping from end index i to the start index k (or maybe from start to end). Since for each (k,i) we have sum v. For each v, we can create an array end_to_start_list? Actually because for each start k there is at most one i, but for each end i there could be multiple starts? Possibly, because different start indices could produce the same sum ending at i? With positive integers, could two different start indices produce same sum ending at i? For a fixed i, sum(k,i) decreases as k increases. Since sums are distinct for different k? Could two different k give same sum? That would require that prefix sums differ by the same amount, i.e., pref[i] - pref[k1-1] = pref[i] - pref[k2-1] => pref[k1-1] = pref[k2-1], but prefix sums are strictly increasing, so they are distinct. So for a fixed i, there is at most one k such that sum(k,i)=v. Because if there were two k1<k2, then pref[k1-1] != pref[k2-1], so the sums would differ. So indeed for each i, there is at most one start k such that sum(k,i)=v. Therefore, for each v, we can define an array start_at[i] = the unique k such that sum(k,i)=v, or None if none.
Thus we can compute for each v a list of end indices i where such a subarray exists, and also the corresponding k.
Now, how to compute these mappings efficiently for all v? We can iterate over all subarrays (k,i) and for each sum v, record that for end i, start = k. Since each v has at most one start per i, we can store in a dict: for each v, an array of length N+1 (1-indexed) where arr[i] = k if exists else 0. But number of v up to 5050, each with array of size N+1=101 would be about 5050*101 ~ 510k integers, which is fine. Or we could use dictionary mapping v to a dict mapping i->k. But array is simple.
Alternatively, we can compute DP on the fly without storing large structures: For each v, we can compute the list of (i,k) by scanning all subarrays once and appending to a list per v. Then for DP, we need for each i, the k if any. We could build an array of size N+1 with default 0. So approach:
- Precompute prefix sums.
- Create a dictionary sum_to_starts: key = v, value = list of (i, k) maybe. But we need O(1) lookup for each i when computing dp. Since N is small, we can just for each v build an array match_start of size N+1 (index i from 1..N) initialized to 0. Then for each pair (k,i) compute v = pref[i] - pref[k-1], then set match_start[i] = k for that v. But wait: for a given v, there might be multiple pairs? Actually for a fixed v, there is at most one k per i, but there could be multiple i for the same v. So we can store match_start[i] = k. However, we need to handle multiple v. So we need a mapping from v to such an array. Since there are many v, we can store for each v a list of (i,k) pairs, then during DP for that v we can convert to a dictionary or array for quick lookup. Because building an array of size N+1 for each v might be memory heavy but still fine: 5050 * 101 ≈ 510,050 integers, which is fine. But we also need to store them for all v simultaneously? That's also fine (0.5 MB). But we can also compute DP for each v on the fly while iterating over all v, but we need the match information for that v. We could precompute for each v the list of (i,k) and then in DP loop, we can create a set or array to look up. Since N=100, we could just during DP for v, iterate over its list of (i,k) to update dp. However, we need to compute dp[i] for all i sequentially. A possible DP approach using the bestPrev and the special edges: dp[i] = max(bestPrev[i], (dp[k-1]+1) if there exists a subarray (k,i) with sum v). Where bestPrev[i] = max_{0<=t<=i-1} dp[t]. So we can compute dp in one pass i=1..N, while maintaining current max of dp[0..i-1] as bestPrev. For each i, we check if there is a matching start k for this i. If yes, candidate = dp[k-1] + 1. Then dp[i] = max(bestPrev, candidate). Also, note: bestPrev is the maximum dp over all indices < i, which includes the possibility of taking a partition where the last block is not v. But is it always valid to set dp[i] = bestPrev? Let's verify: Suppose we want a partition of prefix i where the last block is not v. Then we need to choose some k such that sum(k,i) != v. The number of v-sum blocks in that partition would be dp[k-1]. The maximum over k of dp[k-1] (for any k, regardless of sum) is indeed bestPrev (since k-1 can be any index from 0 to i-1). But is it always possible to achieve a partition of prefix i with a last block that is not v and with dp[k-1] equal to the maximum dp[t]? We need to ensure that for the k that gives the max dp[t], we can extend with a block from t+1 to i that does not sum to v. However, we can always take the block covering indices (t+1..i). But its sum may equal v. If it does equal v, then that partition would actually give dp[t] + 1, which is better than dp[t] alone. So if sum(t+1,i)==v, then that case is covered by the candidate +1. So for the bestPrev to be achievable as dp[i] via a non-v last block, we need some t where dp[t] = bestPrev and sum(t+1,i) != v. Is it guaranteed? Not necessarily. Suppose bestPrev is attained only by some t0, and for that t0, sum(t0+1,i) == v. Then the partition using that t0 as prefix would actually yield dp[t0]+1, which is greater than bestPrev. So dp[i] would be at least bestPrev+1. But if bestPrev is also attained by some other t1 where sum(t1+1,i) != v, then bestPrev is achievable. However, if the only t that gives bestPrev has sum(t+1,i)==v, then bestPrev is not achievable as a partition with last block not v, but we can still achieve at least bestPrev+1, so dp[i] would be larger. So the recurrence dp[i] = max(bestPrev, candidate) might be incorrect if bestPrev is not achievable as a non-v last block. But we can still compute dp[i] as max( candidate, max_{k: sum(k,i) != v} dp[k-1] ). The max over k: sum(k,i) != v of dp[k-1] might be less than bestPrev if the bestPrev index's sum equals v. However, we can compute this max by considering all k. But we want to avoid scanning all k.
We can handle this by computing dp[i] as: Let max_all = max_{0 <= t <= i-1} dp[t] (call it bestPrev). Let candidate1 = max_{k: sum(k,i)==v} (dp[k-1] + 1) if any. Then dp[i] = max( max_{k: sum(k,i) != v} dp[k-1], candidate1). But max_{k: sum(k,i) != v} dp[k-1] could be equal to bestPrev if there exists some t (t = k-1) with dp[t]=bestPrev and sum(k,i) != v. If not, we need to take the next best.
So we need a way to compute, for each i, the maximum dp[t] among t such that sum(t+1,i) != v. Since the set of t for which sum(t+1,i) == v is at most one (because for fixed i, at most one k yields sum(k,i)=v, so at most one t = k-1). Therefore, the set of t with sum(t+1,i) != v is all t except possibly that one. So max_{k: sum(k,i) != v} dp[k-1] is either bestPrev if the t that gives bestPrev is not the unique forbidden t, else it is the second best dp value among 0..i-1 (i.e., bestPrev2). So we can compute, for each prefix, the top two dp values. That is manageable.
Thus we can compute for each i: let best1 = maximum dp[t] for t=0..i-1, and best2 = second maximum (strictly less than best1, but if multiple indices share best1, then best2 could equal best1? Actually if there are multiple t with dp[t]=best1, then even if one of them is forbidden, another may not be, so best2 could be equal to best1. So we need to know the count of indices achieving best1. So we can track best value and its multiplicity. Since N is small, we could just compute the maximum among all t and then if the forbidden t is the only one achieving that max, we need to use the next max. But we can also compute on the fly by scanning all t? That would be O(N) per i, leading to O(N^2) per v. But we are trying to avoid O(N^2). But we can maintain a running best1 and best2 as we increase i, but dp values are computed sequentially. At step i, we have dp[0], dp[1], ..., dp[i-1] known. We can keep a data structure to query max excluding a specific index? Since we only need to exclude at most one index (the one that corresponds to k-1 where sum(k,i)=v). And we know that index t. So we could maintain a frequency map of dp values, but easier: since N is small, we could just compute max over all t (0..i-1) and then if the forbidden t is the only one achieving that max, take the second max. This can be done by scanning all t (i of them) which is O(N) per i, resulting O(N^2) per v. That would bring us back to O(N^3) total if we do for each v. However, with N=100, O(N^3) = 1e6 per v? Actually N^2 per v is 10k; times #v up to 5k gives 50M; that's still fine. The simpler double loop per v (over k) is easier and not much slower.
Given the small N, the simplest O(N^2) per v is acceptable. The optimization is not needed. So we can just implement the straightforward DP with inner loop over k for each i, which is O(N^2) per v. That's simpler and less error-prone.
Let's stick with straightforward DP.
But we must ensure the DP is correct as we described: dp[i] = max_{k=1..i} (dp[k-1] + (sum(k,i)==v)). This is clearly correct.
Implementation:
for v in all_sums:
dp = [0]*(N+1) # dp[0]=0
for i in range(1, N+1):
best = -1 # since dp values are non-negative, we can start at -1
for k in range(1, i+1):
s = pref[i] - pref[k-1]
cand = dp[k-1] + (1 if s == v else 0)
if cand > best:
best = cand
dp[i] = best
maxcount = dp[N]
then update global.
But note: dp[k-1] might be undefined for k=1? dp[0] is defined. So fine.
Complexities: inner loop over k up to i, total inner iterations per v = N(N+1)/2 ≈ 5050.
Now, we must be careful that dp[i] may be negative if no partition exists? But as argued, there is always a partition, so best will be at least 0 (for k such that we take dp[k-1] maybe 0 and indicator 0). Actually if all cand are 0? For i=1, k=1 gives cand = dp[0] + (A1==v). So if A1 != v, cand = 0. So best = 0. So dp[i] >= 0. So we can initialize best = -1 and update accordingly.
But we need to ensure that dp[i] is the maximum; it will be computed.
Now, we can further optimize by precomputing for each pair (k,i) the sum, and maybe we can store in a 2D array to avoid subtraction each time? But subtraction is cheap.
Given N small, fine.
Now, we need to handle the case where all_sums set can be large. We'll compute it.
Edge Cases: Possibly fmax could be 0? If N=0? But N>=1, so fmax>=1. So output will always have at least one value.
Now, after computing fmax and list of v, we output.
Now, testing on sample:
N=6, A = [11,5,6,2,8,10].
Compute all_sums set. The ones that achieve dp[6]=2: according to sample: 8,10,11,16. Let's test with our DP.
We should check if any other v yields 2? Possibly 13? Let's quickly test: v=13? Could we get two 13s? Sum total = 42, so two 13s sum 26, leaving 16, not 13. Could we partition into three blocks: two 13s and one 16? But need contiguous: can we have block sum 13? Subarrays: 5+6+2=13 (indices 2-4) and maybe 11+2? 11+? 11+?=13 requires 2, but not contiguous? 11 and 2 are not adjacent because there are 5,6 in between. So only one 13 possible. So dp likely 1. So our DP would reflect that.
Thus answer matches.
Now, confirm that we only consider v from subarray sums. In sample, 16 is a subarray sum? 5+6+2+? Actually 5+6+2+8? That's 21. 11+5=16, yes. So 16 is subarray sum.
Now, check if any other v yields 2? Maybe 21? Only one block of 21? Possibly 11+5+6=22, not 21. So no.
Thus fine.
Now, what about the possibility of having multiple equal values that are not the result of blocks that are exactly v, but we could have a partition where some blocks are equal but not necessarily all equal to v? But we are counting the frequency of a particular value. So it's fine.
Now, complexity wise, we can also consider using a DP that computes for each i the best count for each possible last block sum, but that's more complex.
Given N small, the simple method is good.
But we must be cautious: The number of distinct subarray sums can be up to 5050. For each we run O(5050) operations, total about 25.5 million. Python should handle that within a couple seconds. However, we also have the overhead of constructing the set of sums and iterating over them. That's fine.
But we can do a micro-optimization: Instead of iterating over all v in the set, we could iterate over all subarray sums, but we need to compute dp for each distinct sum. But we could also compute dp for each subarray sum as we generate it, but then we would compute multiple times for same v. So better to distinct.
Implementation steps in code:
- Read input.
- Compute prefix sums.
- Initialize a set
sums_set and also maybe a list sums_list for iteration.
Optionally also store for each sum the pairs? Not needed.
- For each v in sums_list (distinct), compute dp[N].
- dp = [0]*(N+1)
- For i in range(1,N+1):
best = 0 # we can start with 0 as min.
for k in range(1,i+1):
if (pref[i] - pref[k-1]) == v:
cand = dp[k-1] + 1
else:
cand = dp[k-1]
if cand > best:
best = cand
dp[i] = best
- count = dp[N]
- If count > fmax: fmax=count, result=[v]
- elif count == fmax: result.append(v)
- Sort result.
- Print fmax.
- Print " ".join(map(str, result))
Edge Cases: fmax may be 0? but not.
Potential bug: In DP, for i=1, k=1, cand = dp[0] + (A1==v). That's correct.
Now, let's test on some small cases.
Case N=1, A=[5]. sums_set={5}. v=5: dp[1] = max_{k=1} dp[0]+1 = 1. So fmax=1, result=[5]. Outputs: 1 and 5. Good.
Case N=2, A=[1,2]. sums_set={1,2,3}. For each v, dp[2] computed as earlier, all yield 1. So fmax=1, result=[1,2,3] sorted. Output: 1 and 1 2 3. That matches expectation.
Now, more complex: N=3, A=[1,2,3]. sums_set={1,2,3,4,5,6}. Compute dp for each:
- v=1: dp[3]=1? Let's compute manually: i=1: dp1=1 if A1=1 else 0? A1=1 =>1. i=2: possibilities: k=1 sum=3!=1 => dp0=0; k=2 sum=2!=1 => dp1=1; max=1 => dp2=1. i=3: k=1 sum=6!=1=>0; k=2 sum=5!=1=>dp1=1; k=3 sum=3!=1=>dp2=1; max=1 => dp3=1.
- v=2: similar, dp3=1.
- v=3: dp3=2 as computed earlier.
- v=4: dp3? i=1: A1=1!=4 => dp1=0; i=2: k=1 sum=3!=4=>0; k=2 sum=2!=4=>0 => dp2=0; i=3: k=1 sum=6!=4=>0; k=2 sum=5!=4=>dp1=0; k=3 sum=3!=4=>dp2=0 => dp3=0? Wait but there is a partition that includes a block sum 4? For instance, merge 1+3=4? But 1 and 3 are not adjacent; we have 1,2,3. To get a block sum 4, we could take 1+2+? Actually 1+2+? No. Could take 1+3? Not contiguous. Could take 4 as a block? The only subarray sums: 1,2,3,1+2=3,2+3=5,1+2+3=6. So 4 is not a subarray sum. So v=4 not in sums_set. So we won't consider it. So sums_set only contains actual subarray sums. So v=4 is not considered. Good.
Thus fmax=2, result=[3].
Now, check if any v=6 yields dp3=1. v=6: i=1: sum=1!=6 =>0; i=2: k=1 sum=3!=6; k=2 sum=2!=6 =>0; i=3: k=1 sum=6==6 => dp0+1=1; k=2 sum=5!=6 => dp1=0; k=3 sum=3!=6 => dp2=0; max=1 => dp3=1. So okay.
Now, we should also consider whether dp[N] can be > N? No, at most N (if all blocks are of size 1 and each equals v, but then each element must equal v, so max N). So fine.
Now, confirm that the DP correctly handles the case where a block can be formed by merging many elements, and that we can skip merging. The recurrence includes all possible k, so all block lengths.
Now, one more nuance: The operation replaces two adjacent elements by their sum, and we can perform any number. The final array corresponds to a partition, but is there any restriction that merging must be done step by step? For instance, can we achieve any partition? Yes, by merging appropriate adjacent pairs in any order we can combine any set of contiguous elements into a single block. So it's possible.
Now, is there any scenario where we could get a final array that is not a partition? No.
Thus solution is correct.
Now, we must consider the output format: first line the maximal number, second line the values in ascending order separated by space. If there are no values? But there will be at least one.
Now, potential pitfalls: The list of values might be long (up to 5050). Printing them space-separated on one line might be long but within output limits (5050 numbers, each up to 100,000, so about 6 digits each, plus spaces => ~40k chars, fine).
Now, we need to ensure we sort the values ascending.
Now, one more test: N=4, A=[1,1,1,1]. All ones. Subarray sums: 1,2,3,4. For v=1: dp[4] should be 4 (since we can have four ones). For v=2: can we get two 2s? Partition: [1,1] (2), [1,1] (2) -> two 2s. Could we get three 2s? Need total sum 4, three 2s sum 6 >4, impossible. So dp[4]=2. For v=3: maybe one 3? Partition: [1,1,1] (3) and [1] -> one 3. Could we get two 3s? impossible. So dp=1? Actually maybe 3+? but sum 4, can't have two 3s. For v=4: one block of 4 -> dp=1. So fmax=4, only v=1. Output: 4 and 1.
Now, test [1,2,1,2]. N=4. Sums: many. Could we get three of some value? Total sum =6. Maybe three 2s? 2+2+2=6, we have two original 2s, and can we make another 2? 1+1? there is no two 1s? Actually there are two 1s (positions 1 and 3). They are not adjacent initially, but we could merge to bring them together? Sequence: [1,2,1,2] -> merge 2 and 1? Not directly. Can we rearrange? No, operations preserve order. Can we merge such that we get a block sum 2 from non-adjacent ones? They must be contiguous. So we need a contiguous subarray sum 2. We have [1,?] Could be [2] itself, or [1,1] but 1 and 1 are not adjacent (there is a 2 between). Could we merge the 2 with something to eliminate it? For instance, merge 2 and 1 to 3, then we have [1,3,2] then maybe merge 1 and 3 to 4? Not 2. Another try: merge 1 and 2 to 3: [3,1,2] then merge 1 and 2 to 3: [3,3] -> two 3s. So max frequency 2 for v=3. Could we get three 2s? Not likely. Could we get two 1s? Original two 1s, but they are separate; if we keep them as singles, we get two 1s and two 2s, so frequency of 1 is 2, of 2 is 2. So fmax=2, values could be 1,2,3? Check 3 we have two 3s, so 3 qualifies. Also 2 qualifies (two 2s in original partition). Also 1 qualifies (two 1s). So result should be 1,2,3. But need to check if 1 can be achieved with frequency 2 while covering all? Yes, just keep array as is: [1,2,1,2] has two 1s. So dp for v=1: should be 2. Let's compute: dp[4] for v=1. i=1: A1=1 ->1; i=2: k=1 sum=3!=1 ->0; k=2 sum=2!=1-> dp1=1 -> dp2=1; i=3: k=1 sum=4!=1; k=2 sum=3!=1; k=3 sum=1 -> dp2+1=2 -> dp3=2; i=4: k=1 sum=6!=1; k=2 sum=5!=1; k=3 sum=3!=1; k=4 sum=2!=1 -> dp3=2? Also check k where sum !=1 gives dp[k-1] values: for k=1 dp0=0; k=2 dp1=1; k=3 dp2=1; k=4 dp3=2; max is 2. So dp4=2. So yes. v=2: should also be 2. v=3: as we computed earlier? Let's compute v=3: i=1:1!=3->0; i=2: k=1 sum=3 -> dp0+1=1; k=2 sum=2->0 -> dp2=1; i=3: k=1 sum=4!=3; k=2 sum=3 -> dp1+1=0+1=1; k=3 sum=1 -> dp2=1; max=1? Wait dp1? dp1=0. So dp3=1? But we expected dp4=2 from partition [3,3]. Let's continue i=4: k=1 sum=6!=3; k=2 sum=5!=3; k=3 sum=4!=3; k=4 sum=2!=3 -> dp3=1? That gives dp4=1? That suggests DP says maximum number of 3-sum blocks is 1. But we found a partition that yields two 3s: merge 1+2 (first two) to 3, and merge the last two (1+2) to 3, resulting in [3,3]. That's a valid partition: block1 = A1+A2 = 1+2=3, block2 = A3+A4 = 1+2=3. So it should be possible. Let's compute DP correctly.
The array indices: A1=1, A2=2, A3=1, A4=2.
We need dp[4] for v=3.
Compute dp[0]=0.
i=1: only k=1, sum=1 !=3 => cand = dp[0] + 0 = 0. So dp[1]=0.
i=2:
k=1: sum(1,2)=3 == v => cand = dp[0] + 1 = 1.
k=2: sum(2,2)=2 != v => cand = dp[1] + 0 = 0.
max=1 => dp[2]=1.
i=3:
k=1: sum(1,3)=4 != v => cand= dp[0]=0.
k=2: sum(2,3)=3 == v => cand = dp[1] + 1 = 0+1=1.
k=3: sum(3,3)=1 != v => cand = dp[2] + 0 = 1.
max=1 => dp[3]=1.
i=4:
k=1: sum(1,4)=6 != v => 0.
k=2: sum(2,4)=5 != v => cand = dp[1] = 0.
k=3: sum(3,4)=3 == v => cand = dp[2] + 1 = 1+1 = 2.
k=4: sum(4,4)=2 != v => cand = dp[3] = 1.
max = 2 => dp[4]=2.
I earlier miscalculated i=4 by forgetting k=3. Indeed k=3 gives sum(3,4)=3. So dp[4]=2. Good. So v=3 yields 2. So result list for this case should be [1,2,3] sorted.
Thus DP works.
Now, we should think about whether fmax could be greater than the maximum number of disjoint subarrays with sum v that can cover the array. That's exactly what DP does.
Now, one more nuance: The operation allows us to replace adjacent elements with their sum any number of times. This is equivalent to partitioning. However, there might be a nuance: When we merge two elements, we replace them with their sum, which becomes a single element. Later we can merge that sum with neighbors. So ultimately, we get a partition. Yes.
Now, check for potential pitfalls: The DP as defined yields the maximum number of blocks with sum v, but is it possible that by having some blocks of sum v we might be forced to have other blocks that also equal v inadvertently? That's fine, it only increases the count.
Now, we must also consider the possibility that the maximum frequency could be achieved by a value that is not a subarray sum? As argued, if it appears as an element, it's sum of some block, so it's a subarray sum. So no.
Now, we need to ensure we don't miss values that could be achieved with fmax but not as subarray sum? Not possible.
Thus algorithm is correct.
Now, implement.
Implementation details:
- Read N.
- Read list A (might be in one line, but there could be multiple lines? The input says second line contains N integers, so we can read all.
- Compute prefix sums: pref = [0] * (N+1); for i in range(1,N+1): pref[i] = pref[i-1] + A[i-1].
- Compute all subarray sums into a set: sums_set = set(); for i in range(1,N+1): for j in range(i,N+1): sums_set.add(pref[j] - pref[i-1]).
- Convert to a list sums_list = list(sums_set). But we may want to iterate in some order (maybe sorted) but not necessary. We'll later sort result.
- Initialize fmax = 0, best_vals = [].
- For each v in sums_list:
dp = [0]*(N+1)
for i in range(1, N+1):
best = -1
for k in range(1, i+1):
s = pref[i] - pref[k-1]
cand = dp[k-1] + (1 if s == v else 0)
if cand > best:
best = cand
dp[i] = best
cnt = dp[N]
if cnt > fmax:
fmax = cnt
best_vals = [v]
elif cnt == fmax:
best_vals.append(v)
- After loop, sort best_vals.
- Print fmax.
- Print " ".join(map(str, best_vals))
We need to be careful that dp values are integers.
Now, test on sample.
Let's manually simulate sample with code in mind: should output 2 and values 8 10 11 16. But note order: sample output: "2\n8 10 11 16". That's ascending? 8,10,11,16 is ascending yes.
Now, what about possible values like 5? Could we get two 5s? Let's test quickly: v=5. Array: 11,5,6,2,8,10. Is it possible to get two blocks sum 5? We have one original 5. Can we create another 5 from contiguous elements? Sum of any subarray equals 5? Options: 5 itself (index2), maybe 2+? 2+3? No. So only one. Could we partition to have two 5s? Maybe by merging others such that we have two blocks each sum 5? That would require two disjoint subarrays each summing to 5, covering the array with other blocks. But we have only one subarray sum 5 (the single element). So impossible. So dp[6] for v=5 likely 1. So not included.
Now, consider v=22? Could we get two 22s? Total sum is 42, so two 22s would sum 44 >42 impossible. So dp[6] for v=22 is 0? But 22 is not a subarray sum? 11+5+6=22, yes. So we could have one block sum 22 (indices 1-3). But can we have two? Need another disjoint subarray sum 22, covering rest? The remaining sum would be 42-22=20, not 22. So only one. So dp=1.
Thus fmax=2.
Now, check for v=21? Not a subarray sum? Actually 5+6+2+8=21, so v=21 is a subarray sum. Could we have two 21s? 21*2=42, total sum 42, so it's possible in principle if we can partition into two blocks each sum 21. Is there a partition into two blocks each sum 21? That would require a cut at some point where prefix sum equals 21. Prefix sums: after A1=11 -> pref1=11; after A2=5 -> pref2=16; after A3=6 -> pref3=22; after A4=2 -> pref4=24; after A5=8 -> pref5=32; after A6=10 -> pref6=42. Prefix sum 21 does not appear. So no partition into two blocks of 21. Could we have more than two blocks? To have two blocks of 21, we need exactly two blocks covering whole array each sum 21, so the cut must be where cumulative sum is 21, which doesn't exist. However, could we have three blocks, with two of them 21 and one other? That would exceed total sum (21+21+x =42 => x=0, impossible). So max count for v=21 is 1. So not in result.
Thus algorithm seems correct.
Now, think about corner case: Could there be a value v for which dp[N] is higher than the maximum number of disjoint subarrays with sum v that cover the array? Actually DP considers partitions covering the array, which is exactly that.
Now, check if there is any scenario where merging blocks in a different order could yield a different final array that is not representable as a partition? I think not.
Now, note: The DP we defined yields the maximum number of v-sum blocks, but is it always optimal to treat the entire array? Yes.
Now, also note: The DP's state only depends on prefix and we maximize count. There's no restriction that the other blocks cannot sum to v as well; they can, but we count them.
Now, consider if there is any scenario where we could get more v-sum blocks by not covering the array with all elements? No, because final array must contain all original elements exactly once.
Now, we should also consider that the operation can be performed any number of times, but we could also choose not to merge at all, which corresponds to partition into singletons.
Thus DP covers all.
Now, let's think about time constraints: N=100, Ai up to 1000, total sum up to 100,000. Number of distinct sums can be up to 5050. For each sum, we do around 5050 iterations. So total ~25.5 million. Each iteration does a subtraction, a comparison, an addition, and a max check. That's okay. In Python, 25M operations might take about 0.5-1 second. But we also have overhead of set and list creation. Should be fine.
But we can also reduce constant by using local variables and maybe precompute all subarray sums in a 2D array to avoid subtraction? Actually subtraction is cheap. But we can also precompute for each i a list of sums from 1 to i? Not needed.
Potential micro-optimizations: Use a list of prefix sums, then inside inner loops, use local variables for pref, dp. Use Python's built-in max with generator? But generator overhead may be higher. Keep simple loops.
We can also compute dp[i] using the fact that best = max(dp[i-1], something). But not necessary.
Alright.
Now, we must also consider memory: dp array size 101 per v, fine.
Now, implement.
After implementing, test on sample.
Let's run mental for sample.
But I'm confident.
Now, think about possible alternative interpretation: Could the question ask for "the maximal number of elements with the same value" and "what are the values it could be?" meaning the value(s) that appear that many times in the final array when the maximum is achieved? That's what we did.
But also ensure we output all possible values, not just those achieving the max count but also maybe values that can appear with that count in some configuration. That's same.
Now, about the second line: If there are many values, we print them separated by space. It's okay.
Now, also note: The sample output has a trailing space? No, probably not.
Now, implement in code.
Potential bug: When reading input, there might be extra spaces or newlines. We'll use sys.stdin.read or split.
Now, in Python, we can do:
import sys
def main():
data = sys.stdin.read().strip().split()
if not data: return
N = int(data[0])
A = list(map(int, data[1:1+N]))
# rest
Now, compute prefix sums:
pref = [0]*(N+1)
for i in range(1,N+1):
pref[i] = pref[i-1] + A[i-1]
Now, compute sums_set:
sums_set = set()
for i in range(1,N+1):
for j in range(i,N+1):
sums_set.add(pref[j]-pref[i-1])
Now, iterate over sums_set.
But we could also compute dp for each v directly within the loops? Maybe compute dp while generating sums? But because we need distinct v, better to compute distinct.
Now, for each v, compute dp:
dp = [0]*(N+1)
for i in range(1,N+1):
best = 0 # we can init as 0 because dp values are >=0
# but careful: maybe candidate could be negative? No.
# However, if we init best=0, and all candidates are 0, it's fine.
# But if dp[k-1] can be negative? No.
# So best=0 works because at worst we can have 0.
# But we must ensure that we consider all candidates; if all candidates are 0, best stays 0.
# However, is it possible that the max candidate is less than 0? No, because dp[k-1]>=0 and indicator 0 or 1, so candidate >=0.
# So best init 0 is fine.
for k in range(1,i+1):
s = pref[i] - pref[k-1]
cand = dp[k-1] + (1 if s == v else 0)
if cand > best:
best = cand
dp[i] = best
But wait: This may be incorrect if dp[k-1] can be >0 but cand may be less than best from other k? We take max, so it's okay.
Now, dp[N] is count.
Now, after loop, compare.
Now, we need to store best_vals list.
Now, sort best_vals.
Now, output.
Edge Cases: If best_vals is empty? That would only happen if sums_set is empty, but N>=1, so at least one sum, so not empty.
Now, check for N=100, Ai=1, all ones. sums_set will have numbers 1..100, so 100 distinct sums. For each v, dp[N] for v=1 gives 100; for v=2 gives floor(100/2)=50? Actually maximum number of blocks summing to 2 is 50 (by pairing ones). But our DP will compute that. So fmax=100, best_vals=[1]. So output 100 and 1. That's fine.
Now, consider N=100 with random numbers; sums_set may be up to 5050.
Now, is there any scenario where dp[N] might be incorrectly computed because of the way we handle indicator? Let's test a tricky case: N=3, A=[2,2,4]. Total sum=8. Could we get two 4s? Partition: [2,2] (4) and [4] -> two 4s. So dp for v=4 should be 2. Compute: v=4, dp[0]=0. i=1: k=1 sum=2 !=4 -> cand=0 -> dp1=0. i=2: k=1 sum=4 -> dp0+1=1; k=2 sum=2 -> dp1+0=0 => dp2=1. i=3: k=1 sum=8!=4; k=2 sum=6!=4; k=3 sum=4 -> dp2+1=2; also k=2? we did. So dp3=2. Good.
Now, tricky case: Overlapping possible v-sum blocks, DP chooses non-overlapping because partition enforces non-overlap.
Now, ensure we didn't miss any operation subtlety: The operation replaces two adjacent elements by their sum. It is allowed any number of times. Suppose we have array [a,b,c]. If we first merge a and b to a+b, then merge (a+b) and c to a+b+c, we end with a single element. That's partition into one block. If we merge b and c first to b+c, then merge a and (b+c) to a+b+c, same. So any partition is reachable.
Now, check if there is any restriction that after merging, the new element is placed, and we can merge it with neighbors. That's exactly building blocks.
Thus fine.
Now, one more nuance: The problem statement: "She can replace some adjacent elements Ai and Ai+1 by their sum." It doesn't say she can replace any pair at any time, but it's typical. Yes.
Now, I think solution is correct.
But we must also consider whether the DP should consider the possibility of not covering the entire array? No, because after operations, we always have some array that covers all original elements. There is no operation that discards elements. So covering is mandatory.
Now, confirm with a more complex example where merging might affect the ability to achieve certain partitions? The DP assumes any partition is achievable; we argued yes. So it's fine.
Now, let's analyze a more complex scenario: Suppose we have array [1,3,2,2]. We saw DP works.
Now, think about potential bug: In DP, we take max over k of dp[k-1] + indicator. This recurrence might double-count blocks if we have a block that is not v but its sum later becomes v after merging with something else? No, because the block is fixed; we don't change it later. The DP builds partition in one go.
Thus ok.
Now, I'll code.
But before coding, let's consider if there is an even more efficient method: maybe we can compute fmax as the maximum over all possible v of the maximum number of disjoint subarrays with sum v that can cover the array, which can be solved by DP that simultaneously tracks counts for each possible sum at each position? Could be like DP[i] is a map from possible last block sum to best count? But we need to maximize count of a particular sum, so we could compute for each i, for each possible sum of a suffix, the maximum number of blocks with that sum in prefix? That's essentially DP with state (i, v) but v up to 100k, too large.
Given N small, per v DP is simpler.
Thus finalize.
But before finalizing, double-check the DP's correctness in a case where the optimal partition uses a block that is not v, but the bestPrev may not be achievable as we discussed earlier. In our naive DP that iterates over all k, we explicitly consider all k, so it's correct. So we are fine.
Now, coding.
Potential optimization: we can precompute a 2D array of sums to avoid recomputing subtraction? Actually we need s = pref[i] - pref[k-1] each time. That's fine.
Now, I'll write code.
Pseudo-code:
import sys
def solve():
data = sys.stdin.read().split()
if not data: return
it = iter(data)
N = int(next(it))
A = [int(next(it)) for _ in range(N)]
pref = [0]*(N+1)
for i in range(1,N+1):
pref[i] = pref[i-1] + A[i-1]
sums_set = set()
for i in range(1,N+1):
for j in range(i,N+1):
sums_set.add(pref[j]-pref[i-1])
fmax = 0
best_vals = []
# For performance, maybe convert sums_set to list
# But iterating over set is fine.
for v in sums_set:
dp = [0]*(N+1)
# dp[0] = 0 already
for i in range(1, N+1):
best = 0 # because all candidates are >=0
# We'll compute max over k
# Using local variables for speed
pref_i = pref[i]
for k in range(1, i+1):
s = pref_i - pref[k-1]
cand = dp[k-1] + (1 if s == v else 0)
if cand > best:
best = cand
dp[i] = best
cnt = dp[N]
if cnt > fmax:
fmax = cnt
best_vals = [v]
elif cnt == fmax:
best_vals.append(v)
best_vals.sort()
# Output
print(fmax)
if best_vals:
print(' '.join(map(str, best_vals)))
if name == "main":
solve()
That should work.
Now, test with sample.
But I'm thinking: In DP, we set best = 0 initially. However, consider i=1, v such that A1 != v. Then best should be 0, which is fine. But consider a case where dp[k-1] could be 0, and indicator 0, cand=0, best remains 0. That's fine.
But what if all candidates are 0, best=0 is correct.
Now, ensure that dp[i] is not forced to be at least dp[i-1]? Not necessarily. For example, v=some large number, you might have to merge in a way that reduces count? Actually dp[i] is maximum count, so it should be at least dp[i-1] because you can always extend the optimal partition of prefix i-1 with a singleton block at i (which may or may not be v). In that extension, count = dp[i-1] + (A[i]==v?1:0). That candidate is considered when k=i. So dp[i] >= dp[i-1] + (A[i]==v?1:0). So dp[i] is non-decreasing? Not necessarily monotonic because dp[i-1] + indicator could be less than dp[i-1]? Actually dp[i] is max, so it's at least that candidate, which is >= dp[i-1] because indicator is 0 or 1, but if indicator is 0, then candidate = dp[i-1], so dp[i] >= dp[i-1]. So dp[i] is non-decreasing. So we can set best = dp[i-1] initially? But we are looping over k anyway. So it's okay.
Now, I'll test with a quick mental case: N=2, A=[1,2], v=3. dp1=0 (since 1!=3). Then for i=2, candidate with k=2 gives dp1+0=0; with k=1 gives dp0+1=1. So dp2=1, which is >= dp1=0. So non-decreasing.
Thus fine.
Now, about performance: The inner loops might be a bit heavy, but with N=100, it's okay.
Now, we can also consider using list comprehension for dp? Not needed.
Now, there is also the possibility of using a caching technique: Since for each v we compute dp from scratch, but we could compute all DP results simultaneously using a DP that for each i, for each possible last block sum, we could track the max count for that sum? But that would require storing for each i a map from sum (up to S) to max count, which could be large. Not needed.
Thus final answer.
But before finalizing, let's double-check the DP recurrence logic in terms of covering: When we pick a k, we are taking block from k to i. The prefix up to k-1 must be partitioned completely. That's fine. The partition of prefix up to k-1 can be any partition, and we take the optimal one (dp[k-1]). That yields a valid partition for prefix i: the partition for prefix k-1 plus the block [k,i]. So the recurrence correctly enumerates all partitions.
Thus correct.
Now, we should test a tricky case where overlapping subarrays of sum v exist but cannot both be chosen because they overlap. DP handles that because it forces a cut at k-1 before the block.
Now, all good.
Now, final code.
Potential improvements: Use array of ints for dp and reuse? Not needed.
Now, we should also consider that the input might have extra spaces, but our read handles.
Now, we will produce output.
Now, think about large output: If best_vals has many numbers (like up to 5050), printing them all in one line is fine.
Now, confirm the sample: We'll test by running mental but trust.
Now, one more test: N=1, A=[5]. sums_set={5}. For v=5: dp1 = max_{k=1} dp0+1 = 1. fmax=1, best_vals=[5]. Output "1\n5". Good.
Now, test N=2, A=[1,2]. sums_set={1,2,3}. v=1: dp2=1; v=2: dp2=1; v=3: dp2=1; fmax=1; best_vals = [1,2,3] sorted -> [1,2,3]. Output "1\n1 2 3". Good.
Now, test N=3, A=[1,1,1]. sums_set={1,2,3}. v=1: dp3=3; v=2: dp3=1? Actually let's compute: v=2: dp1: 1!=2 ->0; dp2: k=1 sum=2 ->1; k=2 sum=1->0 -> dp2=1; dp3: k=1 sum=3!=2 ->0; k=2 sum=2 -> dp1+1=0+1=1; k=3 sum=1 -> dp2+0=1 -> dp3=1. So fmax=3, best_vals=[1]. Output "3\n1". Good.
Now, test N=4, A=[1,1,1,1]. sums_set={1,2,3,4}. v=1: dp4=4; v=2: dp4=2; v=3: dp4=1? Actually can we get one 3? Yes. Could we get two 3s? total 6 >4, no. So dp4=1; v=4: dp4=1. fmax=4, best_vals=[1]. Output.
Now, test N=4, A=[1,2,1,2] we already saw fmax=2, best_vals=[1,2,3] sorted. So output "2\n1 2 3". That seems plausible.
Now, check if there is any value 4? v=4: can we get two 4s? total 6, can't. So not.
Now, all good.
Now, potential bug: The DP as implemented might not consider the possibility of merging all elements into one block that sums to v? That's considered when k=1.
Now, confirm that dp[i] is computed correctly when i is large. Good.
Now, let's consider if there is any scenario where the maximum frequency is achieved by a value that is not a subarray sum but can be formed by merging across non-contiguous? No.
Thus finalize.
Now, I'll produce the final answer.
But hold on: There's a nuance: The operation replaces two adjacent elements with their sum. This operation reduces array length by 1 each time. The final array will have length between 1 and N. The DP partition corresponds exactly to that.
But is it possible that after some merges, we get an array where two equal values are not from disjoint subarrays? Actually each element corresponds to a contiguous block, so yes.
Now, final answer.
However, before concluding, let's test with a random small case by hand using code mental? Not needed.
Now, consider a more complex scenario: N=5, A = [2,2,2,2,2]. All twos. Subarray sums: 2,4,6,8,10. For v=2, dp5 = 5. For v=4, we can have at most floor(10/4)=2 blocks of 4? Actually total sum=10, so two 4s sum 8, leaving 2, so partition: [2,2] (4), [2,2] (4), [2] (2) -> two 4s. Could we have three 4s? No. So dp5=2. For v=6: can we have one 6? Yes [2,2,2] (6) and rest 2,2 -> one 6. Could we have two 6s? 12>10 no. So dp=1. For v=8: [2,2,2,2] (8) and [2] -> one 8. For v=10: one 10. So fmax=5, best_vals=[2]. So output 5 and 2.
Now, think about v=0? Not possible.
Now, consider N=5, A=[1,2,3,4,5]. Total sum=15. Can we get three 5s? 5+5+5=15. Is there a partition into three blocks each sum 5? We need to split array into three contiguous segments each summing to 5. Subarrays summing to 5: [2,3]? 2+3=5 (indices 2-3), [5] index5, [1,4]? 1+4=5 but not contiguous? 1 and 4 are not adjacent because 2,3 in between. But we need contiguous segments. So we need three disjoint contiguous subarrays each sum 5 covering all elements. That would require partition positions where cumulative sums are 5,10. Prefix sums: after index1:1, after index2:3, after index3:6, after index4:10, after index5:15. We have prefix sum 5? No. So cannot have three blocks of 5 covering all. Could we have two 5s? That would require two blocks sum 5 and rest sum 5? Actually total 15, two 5s sum to 10, leaving 5, so three blocks of 5. So if three is impossible, two is possible? Let's see: If we have two blocks of 5, total from those is 10, leaving sum 5 for the remaining block(s). So we could have two 5s and one 5? That would be three 5s. So essentially, to have two 5s, we must have at least one other block that is not 5. But then total from two 5s is 10, remaining sum 5, which would be another 5 if we can make it contiguous. So actually if we can make a block sum 5 from the remaining, then we would have three 5s. So the maximum number of 5s is either 0,1, or 3 if possible. Since 3 not possible, max is maybe 1? But could we have two 5s with the remaining not 5? If remaining sum is 5, any block covering the rest must sum to 5 if we keep it as one block; if we split it into multiple blocks, the sum of those blocks is 5, but could they avoid being 5? They could be split into e.g., 2+3, but then those blocks are 2 and 3, not 5. So we could have two blocks of 5 and then two blocks of 2 and 3. That would give two 5s. Let's see if that's possible: Need two disjoint contiguous subarrays each sum 5, covering some portions, and the remaining elements partitioned into blocks that are not 5. For example, pick subarray indices 2-3 (2+3=5) as one 5, and index5 (5) as another 5. Then remaining indices: 1 and 4. Those are 1 and 4. We can keep them as separate blocks (1 and 4) which are not 5. So the partition: [1], [2,3], [4], [5] -> this yields two blocks summing to 5? Wait [2,3] sum 5, [5] sum 5, so two 5s. But the partition covers all: block1: A1=1; block2: A2+A3=5; block3: A4=4; block4: A5=5. So final array: [1,5,4,5]. That's two 5s. So dp[5] for v=5 should be 2. Let's compute DP manually to confirm.
Array: [1,2,3,4,5] (indices 1..5). v=5.
dp[0]=0.
i=1: sum(1,1)=1!=5 -> dp1=0.
i=2: k=1 sum(1,2)=3!=5 ->0; k=2 sum(2,2)=2!=5 -> dp1=0 -> dp2=0.
i=3: k=1 sum(1,3)=6!=5; k=2 sum(2,3)=5 -> dp1+1=1; k=3 sum(3,3)=3!=5 -> dp2=0; max=1 -> dp3=1.
i=4: k=1 sum(1,4)=10!=5; k=2 sum(2,4)=9!=5; k=3 sum(3,4)=7!=5; k=4 sum(4,4)=4!=5 -> so all indicators 0, best = max(dp[0..3])? Actually we need to compute candidates: for each k, cand = dp[k-1] (since indicator 0). So best = max(dp[0], dp[1], dp[2], dp[3]) = max(0,0,0,1)=1. So dp4=1.
i=5: k=1 sum(1,5)=15!=5; k=2 sum(2,5)=14!=5; k=3 sum(3,5)=12!=5; k=4 sum(4,5)=9!=5; k=5 sum(5,5)=5 -> dp4+1=2; also other k give cand = dp[k-1] (0,0,0,0, maybe dp4=1? dp[4]=1, for k=5 we used; for k=4 cand = dp[3]=1; for k=3 cand = dp[2]=0; k=2 cand=dp[1]=0; k=1 cand=dp[0]=0). So max is 2. So dp5=2. Good. So fmax may be 2 for v=5. Could there be a value with higher count? Perhaps v=6? Let's see v=6: total 15, can we get two 6s? 12, remaining 3. Can we have two subarrays sum 6? For example, [1,2,3] sum6, [4,?] 4+?=6 requires 2 but 2 is used. Or [2,4]? 2+4=6 but not contiguous? 2 and 4 are separated by 3. Could we do [1,2,3] and [6?] no 6. But maybe [1,5]? 1+5=6 (indices 1 and 5) not contiguous. So unlikely. Could we get three 5s? we saw not. Could we get three 3s? total 9? No. So fmax might be 2. Are there other values with count 2? v=4? Possibly: [4] and [1,3]? 1+3=4 but not contiguous? 1,2,3: 1+3 not contiguous. But maybe [4] and [2,2]? no. Let's compute DP for v=4: i=1:1!=4 ->0; i=2: sum(1,2)=3!=4, sum(2,2)=2!=4 ->0; i=3: sum(1,3)=6!=4, sum(2,3)=5!=4, sum(3,3)=3!=4 -> dp3=0; i=4: sum(4,4)=4 -> dp3+1=1; also other k give dp[k-1] maybe 0; so dp4=1; i=5: sum(5,5)=5!=4; other sums: need check if any sum=4 from other intervals: maybe sum(2,3)=5, sum(1,4)=10, sum(4,5)=9, sum(3,4)=7, sum(2,4)=9, sum(1,5)=15; none 4. So dp5 = max over k: for k=5 cand=dp4+0=1; k=4 cand=dp3+? sum(4,5)=9!=4 ->0; k=3 cand=dp2+? sum(3,5)=12!=4->0; k=2 cand=dp1+? sum(2,5)=14!=4->0; k=1 cand=dp0+? sum(1,5)=15!=4->0; best=1. So dp5=1. So v=4 gives 1. v=3? Possibly two 3s? [3] and [1,2]? But 1+2=3 contiguous (indices1-2). So partition: [1,2] (3), [3] (3), [4], [5] -> two 3s. So dp5 for v=3 should be 2. Let's check quickly: i=1:1!=3->0; i=2: sum(1,2)=3 -> dp0+1=1; sum(2,2)=2->0 => dp2=1; i=3: sum(1,3)=6!=3; sum(2,3)=5!=3; sum(3,3)=3 -> dp2+1=2; also others dp[?]; dp3=2; i=4: check if any sum=4? sum(4,4)=4!=3; other sums not 3; best = max(dp[0..3]) = max(0,0,1,2)=2 -> dp4=2; i=5: check if any sum=3: sum(5,5)=5!=3; sum(4,5)=9; sum(3,5)=12; sum(2,5)=14; sum(1,5)=15; none; best = max(dp[0..4]) = max(0,0,1,2,2)=2 -> dp5=2. So v=3 yields 2. v=2? Possibly two 2s? original 2 at index2, and maybe from? 2 alone, also can we get another 2 from 1+1? no. Could get from 5-3? not. So maybe 1. v=1? similarly 1 at index1 only, can't get another 1, so 1. v=10? maybe one? So max fmax might be 2, and values achieving 2: 3,5, maybe others? Also v=7? 3+4=7, but only one? Possibly 2? Not sure. Let's test v=8? 3+5=8? Not contiguous? 3 and 5 are not adjacent because 4 between? Could do 1+2+5=8? 1+2+5=8 (indices1-2 and 5 not contiguous). Not possible. So likely 3 and 5. Also maybe v=6? Could get two 6s? 1+2+3=6 and 6? no 6 alone? Actually we have 6? No element 6. But could we have 2+4=6 (indices2 and4 not adjacent), or 1+5=6 (indices1 and5 not adjacent). So probably only 3 and 5. But maybe also v=15? only one. So fmax=2, best_vals = [3,5] sorted. But is 5 included? We computed dp5 for v=5=2, yes. So answer would be 2 and 3 5. That seems plausible. So our algorithm would output that.
Now, confirm if there is any v with count 3? Could v=1? no. v=2? no. v=4? no. v=6? maybe? Let's attempt to see if three blocks of sum 4? total 12, remaining 3, so maybe two 4s and a 7? But sum 4+4+7=15, 7 is possible? 3+4=7 contiguous? indices3-4 sum to 7. So partition: [1,?] to get 4? 1+3? not contiguous. Could be [1,2,?] 1+2+?=4 requires 1, not possible. So not. So max appears 2.
Thus algorithm appears robust.
Now, final code.
But we need to be careful about Python recursion? Not used.
Now, one more nuance: The DP recurrence we used considers all k from 1 to i. But we could also consider not covering the array? No.
Now, all good.
Now, I'll write the final solution.
But wait: In the problem statement, they say "She can replace some adjacent elements Ai and Ai+1 by their sum." This operation can be performed any number of times. Could she also choose to not replace some adjacent pairs? Yes. So any sequence of merges is allowed.
Now, consider if we can get a final array where some element is the sum of non-consecutive original elements? No, because merging only combines adjacent, so the resulting element always corresponds to a contiguous segment.
Thus solved.
Now, final answer: provide explanation and code.
Now, I'll produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
N = int(next(it))
A = [int(next(it)) for _ in range(N)]
# prefix sums
pref = [0] * (N + 1)
for i in range(1, N + 1):
pref[i] = pref[i - 1] + A[i - 1]
# all possible subarray sums
sums_set = set()
for i in range(1, N + 1):
for j in range(i, N + 1):
sums_set.add(pref[j] - pref[i - 1])
best_cnt = 0
best_vals = []
# DP for each candidate value v
for v in sums_set:
dp = [0] * (N + 1) # dp[0] = 0
for i in range(1, N + 1):
best = 0
# try every possible last block [k .. i]
for k in range(1, i + 1):
s = pref[i] - pref[k - 1]
cand = dp[k - 1] + (1 if s == v else 0)
if cand > best:
best = cand
dp[i] = best
cnt = dp[N]
if cnt > best_cnt:
best_cnt = cnt
best_vals = [v]
elif cnt == best_cnt:
best_vals.append(v)
best_vals.sort()
print(best_cnt)
print(" ".join(map(str, best_vals)))
if name == "main":
solve()