Code Submission Evaluation System Login

CSES - HIIT Open 2016

HIIT Open 2016

Contest start:2016-05-28 11:00:00
Contest end:2016-05-28 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2016-05-28 12:53:23
2016-05-28 12:47:23
Task:Cent saving
Sender:LTR
Submission time:2016-05-28 12:53:23
Status:READY
Result:ACCEPTED

Show test data

Code

#include <iostream>
#include <vector>
#include <cstdio>
#include <string>

int main()
{
	int t;
	std::cin >> t;
	for (int ti = 0; ti < t; ++ti) {
		int n;
		std::cin >> n;

		long long cost = 0;
		int threes = 0;
		int fours = 0;
		for (int ni = 0; ni < n; ++ni) {
			long long p;
			std::cin >> p;
			long long m = p % 5;
			switch (m) {
				case 0:
				case 1:
				case 2:
					cost += (p - m);
					break;
				case 3:
					cost += (p - m);
					threes++;
					break;
				case 4:
					cost += (p - m);
					fours++;
					break;
			}
		}

		// Prefer 3 + 4
		int combos = std::min(threes, fours);
		cost += combos * 5;
		threes -= combos;
		fours -= combos;

		// Sum remaining 3
		if (threes > 0) {
			int m = threes % 2;
			int combines = ((threes - m) / 2);
			cost += (combines + m) * 5;
		}

		// sum remaining 4
		if (fours > 0) {
			int m = fours % 3;
			int combines = ((fours - m) / 3);
			cost += ((2 *combines) + m) * 5;
		}

		std::cout << cost << std::endl;
	}
}