Language: C++

#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>

static void C_FiveTransportations();
static void D_Cake123();
class CakeScore;

int main()
{
	D_Cake123();

	fflush(stdout);
	return 0;
}


int GetStrFromStrm(char* szBuff, int buffSize, FILE* strm = stdin)
{
	int len = 0;

	while (len == 0) {
		fgets(szBuff, buffSize, strm);

		// 最初のスペースは除く
		int spaceCnt = 0;
		for (int i = 0; i < buffSize; i++) {
			if (isspace(szBuff[i])) {
				spaceCnt++;
			}
			else {
				break;
			}
		}
		for (int i = spaceCnt; i < buffSize; i++) {
			szBuff[i - spaceCnt] = szBuff[i];
			if (szBuff[i] == '\0') {
				break;
			}
		}

		char* n;
		if ((n = strchr(szBuff, '\n'))) {
			*n = '\0';
		}
		len = strlen(szBuff);
	}
	return len;
}

int Tokenize(const char* srcStr, const char* dlmtr, std::vector<std::string>& tokens)
{
	size_t pos = 0, ofs = 0, len = 0;
	size_t dlmtrLen = strlen(dlmtr);
	std::string target = srcStr;

	pos = target.find(dlmtr, ofs);
	tokens.push_back(target.substr(0, pos));
	ofs = pos + dlmtrLen;
	while (pos != std::string::npos) {

		pos = target.find(dlmtr, ofs);

		if (pos != std::string::npos) {
			len = pos - ofs;
		}
		else {
			len = strlen(srcStr) - ofs;
		}
		tokens.push_back(target.substr(ofs, len));
		ofs = pos + dlmtrLen;
	}

	return 0;
}

unsigned long long GetUllFromStream(FILE* strm = stdin)
{
	char szNum[64] = { 0 };

	GetStrFromStrm(szNum, sizeof(szNum), stdin);

	return (strtoull(szNum, NULL, 10));
}

int GetUllsFromStream(std::vector<unsigned long long>& vUlls, FILE* strm = stdin, size_t bufSize = 4092)
{
	char * szNum = new char[bufSize];
	memset(szNum, 0, bufSize);

	GetStrFromStrm(szNum, bufSize, stdin);

	std::vector<std::string> tokens;
	Tokenize(szNum, " ", tokens);

	int tokenCnt = tokens.size();
	for (int i = 0; i < tokenCnt; i++) {
		vUlls.push_back(strtoull(tokens[i].c_str(), NULL, 10));
	}

	delete[] szNum;

	return tokenCnt;
}


static void D_Cake123()
{
	const int xNum = 0, yNum = 1, zNum = 2, kNum = 3;
	int X, Y, Z, K;
	std::vector<unsigned long long> vAx, vBy, vCz;

	// get conditions
	scanf(" %d %d %d %d", &X, &Y, &Z, &K);

	// get score
	GetUllsFromStream(vAx, stdin, X * 16);
	GetUllsFromStream(vBy, stdin, Y * 16);
	GetUllsFromStream(vCz, stdin, Z * 16);

	std::sort(vAx.begin(), vAx.end(), std::greater<unsigned long long>());
	std::sort(vBy.begin(), vBy.end(), std::greater<unsigned long long>());
	std::sort(vCz.begin(), vCz.end(), std::greater<unsigned long long>());
	
	int aIdx = 0;
	int bIdx = 0;
	int cIdx = 0;
	int state = 0;
	int scoreCnt = 0;
	unsigned long long maxScore;
	
	std::vector<unsigned long long>::iterator itrMaxScore;
	std::vector<unsigned long long> priqueue;
	//std::vector<unsigned long long> sortedScore;
	
	
	while (scoreCnt < K) {
		if ((aIdx < X) && (bIdx < Y) && (cIdx < Z)) {
			priqueue.push_back(vAx[aIdx] + vBy[bIdx] + vCz[cIdx]);
		}
		maxScore = 0;
		itrMaxScore = priqueue.end();
		for (auto itr = priqueue.begin(); itr != priqueue.end(); itr++) {
			if (maxScore < *itr) {
				maxScore = *itr;
				itrMaxScore = itr;
			}
		}

		//sortedScore.push_back(maxScore);
		printf("%llu\n", maxScore);
		if (itrMaxScore != priqueue.end()) {
			priqueue.erase(itrMaxScore);
		}
		scoreCnt++;

		if (aIdx + 1 < X) {
			priqueue.push_back(vAx[aIdx + 1] + vBy[bIdx] + vCz[cIdx]);
		}
		if (bIdx + 1 < Y) {
			priqueue.push_back(vAx[aIdx] + vBy[bIdx + 1] + vCz[cIdx]);
		}
		if (cIdx + 1 < Z) {
			priqueue.push_back(vAx[aIdx] + vBy[bIdx] + vCz[cIdx + 1]);
		}
		if ((aIdx + 1 < X) && (bIdx + 1 < Y)) {
			priqueue.push_back(vAx[aIdx + 1] + vBy[bIdx + 1] + vCz[cIdx]);
		}
		if ((bIdx + 1 < Y) && (cIdx + 1 < Z)) {
			priqueue.push_back(vAx[aIdx] + vBy[bIdx + 1] + vCz[cIdx + 1]);
		}
		if ((aIdx + 1 < X) && (cIdx + 1 < Z)) {
			priqueue.push_back(vAx[aIdx + 1] + vBy[bIdx] + vCz[cIdx + 1]);
		}
		
		aIdx++;
		bIdx++;
		cIdx++;
	}
}


Compiler output:
a.cc: In function 'int GetStrFromStrm(char*, int, FILE*)':
a.cc:45:26: error: 'strchr' was not declared in this scope
   45 |                 if ((n = strchr(szBuff, '\n'))) {
      |                          ^~~~~~
a.cc:6:1: note: 'strchr' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    5 | #include <stdlib.h>
  +++ |+#include <cstring>
    6 | 
a.cc:48:23: error: 'strlen' was not declared in this scope
   48 |                 len = strlen(szBuff);
      |                       ^~~~~~
a.cc:48:23: note: 'strlen' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
a.cc: In function 'int Tokenize(const char*, const char*, std::vector<std::__cxx11::basic_string<char> >&)':
a.cc:56:27: error: 'strlen' was not declared in this scope
   56 |         size_t dlmtrLen = strlen(dlmtr);
      |                           ^~~~~~
a.cc:56:27: note: 'strlen' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
a.cc: In function 'int GetUllsFromStream(std::vector<long long unsigned int>&, FILE*, size_t)':
a.cc:91:9: error: 'memset' was not declared in this scope
   91 |         memset(szNum, 0, bufSize);
      |         ^~~~~~
a.cc:91:9: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'