Cod sursa(job #3318268)

Utilizator FistfullOfDollar059Andrei Marin Popa FistfullOfDollar059 Data 27 octombrie 2025 18:56:29
Problema Transport Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>
using namespace std;


int main()
{
	ifstream fin("transport.in");
	ofstream fout("transport.out");
	int n, k;
	int toAdd;
	int currentSum = 0;
	fin>>n>>k;
	stack<int> saltele;
	stack<int> decoy;
	int a, b, q;
	a = 0;
	b = 0;
	long long bestVal;

	for(int i = 0; i < n; i++) {
		fin>>toAdd;
		b+= toAdd;
		decoy.push(toAdd);
	}
	for(int i = 0; i < n; i++) {
		saltele.push(decoy.top());
		decoy.pop();
	}

//cout<<b;




	while(a <= b) {
		q = (a+b)/2;
		decoy = saltele;
		for(int i = 0; i < k; i++) {
			currentSum = 0;
			
			while(!decoy.empty() && currentSum + decoy.top() <= q) {
				currentSum += decoy.top();
				decoy.pop();
			}
		}
	//	cout<<decoy.top()<<" "<<q<<"\n";
		
		if(decoy.empty()) {
			//PROCESEAZA ==> MERGE
			bestVal = q;
			b = q - 1;
		} else {
			a = q + 1;
			//PROCESEAZA ==> NU MERGE
		}
	//	cout<<q<<"\n";
	}
	
	fout<<bestVal;



	return 0;
}