We need to solve the problem: given a sequence of N integers, for each i, output the maximum value among all elements except A_i.
Constraints: N up to 200,000, A_i up to 200,000.
We can compute overall maximum value and second maximum value. Because if A_i is not the maximum (i.e., it's not equal to max or it is but there are multiple occurrences), then answer is max. If A_i is the unique maximum (only one occurrence), then answer is second max. So we need to determine whether the maximum value appears more than once.
Implementation: Read N, read array A. Find max_val = max(A). Also find count_max = number of times max_val appears. Also find second_max = maximum among values less than max_val. (If all elements are same, then second_max may be the same? Actually if all elements are same, then max appears N times, so for any i, answer is max (since excluding one still leaves max). So we don't need second_max if count_max > 1. But we still can compute second_max for case count_max == 1.)
Edge Cases: N=2, both same, answer both same. N=2, different: e.g., [5,7]: max=7, count_max=1, second_max=5. For i=1 (5) answer max=7; for i=2 (7) answer second_max=5.
So algorithm: O(N).
Let's ensure second_max is computed correctly. Initialize second_max to something like -inf. When iterating, if a != max_val, update second_max = max(second_max, a). If count_max > 1, we don't need second_max but it's fine.
Then for each i: if A_i == max_val and count_max == 1: print second_max; else print max_val.
We should be careful: The values are up to 200000, so all positive. Use int.
Now implement.
Let's produce the final answer.
We'll provide Python solution.
Test with given examples.
Example 1: N=3, A=[1,4,3] => max=4, count_max=1, second_max=3. For i=1: A1=1 != max -> print 4; i=2: A2=4 and count_max==1 -> print second_max=3; i=3: A3=3 -> print 4. Output matches.
Example 2: N=2, A=[5,5] => max=5, count_max=2. For both: print 5.
Works.
Now check some edge: N=200000 all ones. max=1, count_max=200000 -> print 1 for all.
Time and memory OK.
We'll output.
Now craft final response: Explanation and then code.
The problem statement is simple, but let's provide a clear solution.
We'll output the Python code with reading from sys.stdin.
Implementation:
import sys
def main():
input = sys.stdin.readline
N = int(input())
A = [int(input()) for _ in range(N)]
max_val = max(A)
count_max = A.count(max_val) # O(N) but okay.
second_max = -1
for a in A:
if a != max_val:
if a > second_max:
second_max = a
for a in A:
if a == max_val and count_max == 1:
print(second_max)
else:
print(max_val)
if name == "main":
main()
We can also compute second_max and count in one pass:
max_val = -1, second_max = -1, count_max = 0.
For each a:
if a > max_val:
second_max = max_val
max_val = a
count_max = 1
elif a == max_val:
count_max += 1
else:
if a > second_max:
second_max = a
But careful: When we update max_val, we need to set second_max appropriately: previous max_val becomes second_max. However if there are multiple updates? E.g., 1,2,3: after 3, max=3, second=2. Works.
But if multiple max values, second_max should be the max among non-max values. The one-pass method also handles that: When a == max_val, we just increase count; we don't change second_max. However if we encounter a value equal to max_val before we find any other max? Actually if we set max_val initially to -1, and first element is 5: then a > max_val => set second_max = -1, max_val=5, count=1. Next element is 5: a == max_val => count=2. Next element is 4: a < max_val => update second_max = max(second_max, 4) => second_max becomes 4. So second_max ends up being 4, correct. If all values are 5: then we never update second_max beyond -1. That's okay because count_max > 1, we won't use second_max. But we still need to handle that maybe second_max remains -1. But it's fine.
But simpler: just use max and count.
Given N up to 200k, O(N) is fine.
Thus final answer: solution explanation and code.
We'll output the code block.
Also note: The input format is N then each A_i on separate lines. We can read all lines.
Okay.
Now produce final message. We can compute the overall maximum value and check how many times it appears. For each position i:
- If the element at i is not the maximum, or if the maximum appears more than once, the answer is the overall maximum.
- If the element at i is the only occurrence of the maximum, then we need the second largest value in the whole array.
Thus we only need to know the maximum, its frequency, and the second maximum (the largest value different from the maximum). This can be done in one pass.
import sys
def main():
input = sys.stdin.readline
N = int(input())
A = [int(input()) for _ in range(N)]
max_val = max(A)
cnt_max = A.count(max_val) # frequency of the maximum
second_max = -1
for a in A:
if a != max_val and a > second_max:
second_max = a
out_lines = []
for a in A:
if a == max_val and cnt_max == 1:
out_lines.append(str(second_max))
else:
out_lines.append(str(max_val))
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
main()