A - Police Recruits
Problem Link: [Codeforces 427A##link##]
- [tab]
- C++
#include<iostream> using namespace std; int main() { int n,i,s,sum = 0, counter = 0; cin >> n; for(i = 0 ; i < n; i++) { cin >> s; if(s >= 0) sum += s; else { if(sum == 0) counter++; else sum--; } } cout << counter << endl; return 0; }
- Python3
n = int(input()) list1 = list(map(int, input().split(' '))) cou = 0 ans = 0 for i in list1: if i == -1: if cou > 0: cou -= 1 else: ans += 1 else: cou += i print(ans)
- JAVA
import java.util.Scanner; public class Main{ public static void main (String[] args) { Scanner sc=new Scanner (System.in); int n=sc.nextInt(),sum=0; int mc=0; int in=0; int c=0; for(int i=0;i<n;i++){ in=sc.nextInt(); if(in>0){ mc+=in; }if(in<0){ if(mc<=0){ c++; }else{ mc--; } } } System.out.println(c); } }