View Code of Problem 4041

#include<iostream>
#include<string>
#include<cmath>
#include<iomanip>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct book {
	int time;
	long long v;
	double tv;
}book;
bool cmp(book a, book b)
{
	if (a.tv != b.tv)
		return a.tv > b.tv;
	if (a.time != b.time)
		return a.time > b.time;
}
bool cmp1(book a, book b)
{
	if (a.tv != b.tv)
		return a.tv > b.tv;
	if (a.time != b.time)
		return a.time < b.time;
}
int max(int a, int b)
{
	if (a > b)
		return a;
	else
		return b;
}
int main()
{
	int n, m;
	cin >> n >> m;
	book a[301];
	for (int i = 0;i < n;i++)
	{
		cin >> a[i].time >> a[i].v;
		a[i].tv = a[i].v / (double)a[i].time;
	}
	sort(a, a + n, cmp);
	long long int sum = 0;
	int z = m;
	long long sum1 = 0;
	for (int i = 0;i < n;i++)
	{
		if (m - a[i].time >= 0)
		{
			m -= a[i].time;
			sum += a[i].v;
		}
	}
	sort(a, a + n, cmp1);
	for (int i = 0;i < n;i++)
	{
		if (z - a[i].time >= 0)
		{
			z -= a[i].time;
			sum1 += a[i].v;
		}
	}
	int num = max(sum, sum1);
	cout << num;
}

Double click to view unformatted code.


Back to problem 4041