The Art of Writing Short Stories Read Here

# Question Solved | The Card Game

### Question:

#### Input:

``````5
-2 3 -1 -4 -2``````

#### Output:

``13``

### Solution:

#### Python :

``````#https://codewindow.in

import sys

def minsubarraysum(n, a):
min_sum = sys.maxsize
curr_sum = 0;
for i in range(0, n):
curr_sum = curr_sum + a[i]
if curr_sum < min_sum:
min_sum = curr_sum
if curr_sum > 0:
curr_sum  = 0
return min_sum;

n = int(input())
arr = list(map(int, input().split(" ")))
s = sum(arr)
min_sum = minsubarraysum(n, arr)
ans = s + min_sum * (-2)
print(ans)``````

#### C++ :

``````//https://codewindow.in

#include<bits/stdc++.h>
using namespace std;

int minsubarraysum(int a[], int n) {
int min_sum = INT_MAX;
int curr_sum = 0;
for (int i = 0 ; i < n; i++) {
curr_sum = curr_sum + a[i];
if (curr_sum < min_sum)
min_sum = curr_sum;
if (curr_sum > 0)
curr_sum  = 0;
}
return min_sum;
}

int main() {
int n;
cin >> n;
int a[n];
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
sum += a[i];
}
int min_sum = minsubarraysum(a, n);
int ans = sum + (min_sum * (-2));
cout << ans;
}``````
You may also like :