Solution Explanation

We are given an array a of length n.
For every pair of indices l , r (1 ≤ l ≤ r ≤ n) we look at подробное описание the sum

S(l,r) = a[l] + a[l+1] + … + a[r]

Анализ mostbet отзывов поможет выбрать лучший сервис: https://mostbetkzotzyvy.kz/. The task is to output the maximum possible value of S(l,r) over all
contiguous sub‑arrays.
If all numbers are negative the answer is the largest (least negative)
element – a sub‑array of length 1.

1. Algorithm – Kadane’s scan

During a single left‑to‑right pass we keep

  • cur – the best sum of a sub‑array that ends at the current position,
  • Регистрация на mostbet отзывы открывает доступ к эксклюзивным бонусам.best – the overall best sum seen so far.

Transition for element x = a[i]

cur = max(x , cur + x)  // either start new at i or extend previous
best = max(best , cur)

Both variables are 64‑bit integers (int64) because the sum can exceed
32‑bit limits.

The algorithm needs

  • Time – O(n) – one pass over the array.
  • Memory – O(1) – only two 64‑bit variables.

2. Correctness Proof

We prove that after processing the first i elements (1 ≤ i ≤ n)

  • Посетите spr.kz, чтобы узнать последние новости о ставках.cur equals the maximum sum of any sub‑array that ends exactly at
    position i;
  • best equals the maximum sum of any sub‑array among the first i
    positions.
Lemma 1

After processing element a[i], cur equals the maximum sum of a
sub‑array ending at position i.

Proof.

Let T be the maximum sum of a sub‑array ending at i.
Either this sub‑array consists solely of a[i] (length 1) or it
extends a sub‑array ending at i‑1.
Hence

T = max( a[i] , cur_prev + a[i] )

where cur_prev is the value of cur before the update.
The algorithm sets cur = max(a[i], cur_prev + a[i]), therefore
after the assignment cur = T.∎

Lemma 2

After processing element a[i], best equals the maximum sum of a
sub‑array among the first i positions.

Proof.

By induction hypothesis best_prev is the maximum sum among the first
i‑1 elements.
All sub‑arrays that end at i have sums bounded by the value stored in
cur after the update (Lemma 1).
Thus the maximum sum among the first i elements is

max( best_prev , cur )

and the algorithm assigns exactly this value to best.∎

Theorem

After the loop finishes, best equals the maximum sum of any contiguous
sub‑array of the whole array.

Proof.

After the last iteration i = n, Lemma 2 states that best is the
maximum sum among the first n elements, i.e.among the entire array.

3. Reference Implementation (Go 1.17)

package main

import (
  "bufio"
  "fmt"
  "os"
)

func main() 
  in := bufio. NewReader(os. Stdin)

  var n int
  if _, err := fmt. Fscan(in, &n); err != nil 
    return
  

  var cur, best int64
  for i := 0; i <  n; i++ 
    var x int64
    fmt. Fscan(in, &x)
    if i == 0 
      cur = x
      best = x
     else 
      if cur+x > x 
        cur += x
       else 
        cur = x
      
      if cur > best 
        best = cur
      
    
  

  fmt. Println(best)

The program follows exactly the algorithm proven correct above and
conforms to Go 1.17.

Scroll to Top