AtCoder ح ABC 125 Person Editorial

AtCoder | ABC 125 Person Editorial

					<div> 
														 Koshkaaa															
														 2026-01-18 06:36:02 

				</div>
									<pre><code>const int N = 100000 + 5;<br/>

int main() {

ios_base::sync_with_stdio(false), cin.tie(0);<br/>
// L_i = gcd(A_1, A_2, A_3, ... A_i)<br/>
// R_i = gcd(A_i, A_{i+1}, A_{i+1}, ..., A_n)<br/>
int n;<br/>
ll a[N], L[N], R[N];<br/>
cin &gt;&gt; n;

for (int i = 1; i &lt;= n; ++i) {

    cin &gt;&gt; a[i];<br/>
    L[i] = R[i] = a[i];<br/>
}

// precomputing L[i]

for (int i = 1; i &lt;= n; i++) {<br/>
    L[i] = __gcd(L[i], L[i - 1]);<br/>
}<br/>
// precomputing R[i]<br/>
for (int i = n; i &gt; 0; i--) {<br/>
    R[i] = __gcd(R[i], R[i + 1]);<br/>
}

// computing each m_i and the max value

int res = 0;<br/>
for (int i = 1; i &lt;= n; i++) {<br/>
    int m = __gcd(L[i - 1], R[i + 1]);<br/>
    res = max(m, res);<br/>
}

cout &lt;&lt; res;

return 0;<br/>

}

using ll = long long;
const int N = 1e5 + 10;
int main() {

ios_base::sync_with_stdio(false), cin.tie(0);<br/>
int n, A[N];<br/>
// store the sum of the result in a long long pair<br/>
ll s = 0;<br/>
cin &gt;&gt; n;<br/>
for (int i = 1; i &lt;= n; ++i) cin &gt;&gt; A[i];

for (int i = 1; i &lt;= n - 2; ++i) {

    if (A[i] &lt; 0) A[i] *= -1, A[i + 1] *= -1;<br/>
    s += A[i];<br/>
}

// if n is 2, we only need to consider those

// Also , if the last 2 elements are either both positive<br/>
// or both negative then we simply flip their signs too<br/>
if ((n == 2) or (A[n] &gt; 0 and A[n - 1] &gt; 0) or (A[n] &lt; 0 and A[n - 1] &lt; 0))<br/>
    s += max(A[n - 1] + A[n], -A[n - 1] - A[n]);<br/>
else {<br/>
    // find the min element in the list (excluding last 2 elements)<br/>
    int* mni = min_element(A + 1, A + n - 2);<br/>
    int mnv = *mni;

// if the minimum value in the remaining list is larger than

    // the smaller value of the last 2 elements, then we should<br/>
    // just keep it positive<br/>
    if (mnv &gt; min(abs(A[n - 1]), abs(A[n]))) {<br/>
        s += max(A[n - 1] + A[n], -A[n - 1] - A[n]);<br/>
    }<br/>
    // otherwise, we can make that value negative, and include the<br/>
    // last 2 elements as positive elements<br/>
    else {<br/>
        s -= mnv * 2;<br/>
        s += abs(A[n - 1]);<br/>
        s += abs(A[n]);<br/>
    }<br/>
}<br/>
cout &lt;&lt; s &lt;&lt; &#34;\n&#34;;

return 0;
}

														<div>