let max_subarray xs =
  let max_so_far = ref min_int in
  let cur = ref 0 in
  List.iter (fun x ->
    cur := max x (!cur + x);
    max_so_far := max !max_so_far !cur
  ) xs;
  !max_so_far

;;

max_subarray [-2; 1; -3; 4; -1; 2; 1; -5; 4]
