CSES - HIIT Open 2016 - Results
Submission details
Task:Cent saving
Sender:ContinuedLife
Submission time:2016-05-28 16:44:55 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.05 sdetails
#2ACCEPTED0.06 sdetails

Code

#include <bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout.precision(6);

using namespace std;

int main(){ _
	int TC;	cin >> TC;
	while(TC--){
		int n, num;	cin >> n;
		stack<int> stack_1;
		stack<int> stack_3;
		stack<int> stack_4;
		unsigned long long res = 0;
		for(int i = 0; i < n; ++i){
			cin >> num;
			switch(num % 5){
				case 0:
					res+=num; break;
				case 1:
					stack_1.push(num); break;
				case 2: res+=num-2; break;
				case 3: 
					if(!stack_4.empty()){
						res += stack_4.top(); stack_4.pop();
						res += num - 2;
					} else {
						stack_3.push(num);
					}
					break;
				case 4: 
					if(!stack_3.empty()){
						res += stack_3.top() + num - 2; stack_3.pop();
					} else {
						stack_4.push(num);
					}
					break;
			}
			
		}
			if(!stack_4.empty()){
				int j = stack_4.size()/3;
				for(int k=0; k<j; k++){
					res += stack_4.top();	
					stack_4.pop();
					res += stack_4.top();	
					stack_4.pop();
					res += stack_4.top();	
					stack_4.pop();
					res -= 2;
				}
				int m =  min(stack_1.size(), stack_4.size());
				for(int j=0; j< m; j++){
					res += stack_4.top(); stack_4.pop();
					res += stack_1.top(); stack_1.pop();
				}
				while(stack_4.size()>0){
					res += stack_4.top()+1; stack_4.pop();
				}
			}
			if(!stack_3.empty()){
				int j = stack_3.size()/2;
				for(int k=0; k<j; k++){
					res += stack_3.top();
					stack_3.pop();
					res += stack_3.top();
					stack_3.pop();
					res -= 1;
				}
				if(stack_3.size()>0){
					if(stack_1.size() > 1){
						res += stack_3.top();
						stack_3.pop();
						res += stack_1.top();
						stack_1.pop();
						res += stack_1.top();
						stack_1.pop();
					} else if( stack_1.size() == 1) {
						res += stack_3.top() + 1;
						stack_3.pop();
						res += stack_1.top();
						stack_1.pop();
					} else {
						res += stack_3.top() + 2;
						stack_3.pop();
					}
				}
			}

		while( stack_1.size()>0){
			res += stack_1.top() -1;
			stack_1.pop();		
		}
		cout << res << endl;
	}

	return 0;
}

Test details

Test 1

Verdict: ACCEPTED

input
100
1000
528433894 255789530 559301042 ...

correct output
475191144965
460688647850
478543444030
475238936090
456736521510
...

user output
475191144965
460688647850
478543444030
475238936090
456736521510
...
Truncated

Test 2

Verdict: ACCEPTED

input
1
100000
666086355 190481330 514353517 ...

correct output
47176864928795

user output
47176864928795