Source Code
//#include <bits/stdc++.h>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;

int main() {
	int n;
	cin >> n;
	vector<int> arr1(n);
	vector<int> arr2;
	for (int i = 0; i < n; i++) {
		cin >> arr1[i];
	}
	int i;
	int num = 0;
	bool edited = 0;
	for (i = 0; i < n; i++) {
		if (arr1[i] > 0 && edited) {
			arr2.push_back(num);
			edited = 0;
			num = arr1[i];
		}
		else if (arr1[i] > 0)
			num = arr1[i];
		else if (arr1[i] <= 0) {
			num += arr1[i];
			edited = 1;
		}
	}
	if (edited) {
		arr2.push_back(num);
	}
	else {
		arr2.push_back(arr1[i-1]);
	}

	int size = arr2.size();
	for (int i = 0; i < size; i++) {
		if (i == size - 1)
			cout << arr2[i] << endl;
		else {
			cout << arr2[i] << " ";
		}
	}
	return 0;
}
Copy
b NoName
GNU G++17
0 ms
604 KB
Wrong Answer