//
#include<iostream>
#include<iomanip>
#include<vector>
#include<queue>
#include<stack>
#include<stdio.h>
#include<cstring>
#include<string.h>
#include<cstdio>
#include<string>
#include<algorithm>
#include<utility>
#include<limits.h>
#include<map>
#include<set>
#include<cmath>
using namespace std;
long long x, w, n;
struct homework{
long long x, w, t, cos;//在这个作业能写时上个作业不能写后写w吨作业最少精力
}hw[5050];
bool cmp(homework a, homework b)
{
return a.t > b.t;
}
long long dp[10010];//dp[i]:写i吨作业最少精力
int main()
{
cin >> x >> w >> n;
for(int i = 1; i <= n; i++)
{
scanf("%lld%lld%lld", &hw[i].x, &hw[i].w, &hw[i].t);
}
sort(hw + 1, hw + n + 1, cmp);//时间从大到小排序
memset(dp, 20, sizeof(dp));
dp[0] = 0;
for(int i = 1; i <= n; i++)
{
// cout << i << ":" << hw[i].x << " " << hw[i].w << " " << hw[i].t << endl;
if(hw[i].w > w)
{
dp[w] = min(dp[w], hw[i].x);
}
else
{
for(int j = hw[i].w; j <= w + hw[i].w; j++)
{
dp[j] = min(dp[j], dp[j - hw[i].w] + hw[i].x);
// cout << dp[j] << ' ';
}
}
// cout << endl;
hw[i].cos = 1 << 20;
for(int j = w; j <= w << 1; j++)
{
hw[i].cos = min(hw[i].cos, dp[j]);
}
}
for(int i = n; i > 0; i--)//时间从小到大遍历
{
// cout << i << "_cos:" << hw[i].cos << endl;
long long cos = hw[i].cos * (hw[i].t - hw[i + 1].t);
if(cos <= x)//能写到这份作业不能写
{
x -= cos;
}
else//不能写到这份作业不能写
{
cout << hw[i + 1].t + x / hw[i].cos << " " << x % hw[i].cos << endl;
return 0;
}
}
cout << hw[1].t << " " << x << endl;
return 0;
}
蒟蒻求助,不知道哪里有bug