2512번: 예산

문제접근🤔


놓쳤던 부분😅


코드😁


2176 KB

0 ms

#include <iostream>
#include <vector>
#include <algorithm>

int n, m;
std::vector<int> input_arr;

void input_setting()
{
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
}

void input()
{
	std::cin >> n;
	input_arr.resize(n);
	for (int i = 0; i < n; i++)
		std::cin >> input_arr[i];
	std::cin >> m;
}

void solution()
{
	int start, mid, end;
	int sum;

	start = 0;
	end = *std::max_element(input_arr.begin(), input_arr.end());
	while (start <= end)
	{
		sum = 0;
		mid = (start + end) / 2;
    int a = input_arr.size();
		for (int i = 0; i < a ; i++)
		{
			if (input_arr[i] - mid > 0)
				sum += mid;
			else
				sum += input_arr[i];
		}
		if (sum <= m)
			start = mid + 1;
		else
			end = mid - 1;
	}
	std::cout << end;
}

int main(void)
{
	input_setting();
	input();
	solution();
	return (0);
}