Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (c) 2015, Andrew Chen <llkiwi2006@gmail.com>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * 3. Neither the name of the copyright holder nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <vector>
- using namespace std;
- int total_t; // `T`
- struct item {
- int t, a, b;
- item(int _t, int _a, int _b) : t(_t), a(_a), b(_b) {}
- };
- bool cmp(const item& a, const item& b) {
- // sort according descending to `(b / t)`
- return ((double) a.b / a.t) > ((double) b.b / b.t);
- }
- long long f1[5001];
- long long f2[5001];
- int main() {
- int n;
- vector<item> items_pos;
- vector<item> items_neg;
- scanf("%d%d", &total_t, &n);
- for (int i = 0; i < n; i++) {
- int t, a, b;
- scanf("%d%d%d", &t, &a, &b);
- if (b >= 0) items_pos.push_back(item(t, a, b));
- else items_neg.push_back(item(t, a, b));
- }
- sort(items_pos.begin(), items_pos.end(), cmp);
- sort(items_neg.begin(), items_neg.end(), cmp);
- memset(f1, 0, sizeof(f1));
- memset(f2, 0, sizeof(f2));
- for (unsigned int i = 0; i < items_pos.size(); i++) {
- for (unsigned int j = 0; j <= total_t; j++) {
- if (j + items_pos[i].t <= total_t) {
- int value = items_pos[i].a + (j + items_pos[i].t) * items_pos[i].b;
- f1[j] = max(f1[j], f1[j + items_pos[i].t] + value);
- }
- }
- }
- for (int i = total_t - 1; i >= 0; i--) f1[i] = max(f1[i], f1[i + 1]);
- for (int i = items_neg.size() - 1; i >= 0; i--) {
- for (unsigned int j = 0; j <= total_t; j++) {
- if (j + items_neg[i].t <= total_t) {
- int value = items_neg[i].a + (total_t - j) * items_neg[i].b;
- f2[j] = max(f2[j], f2[j + items_neg[i].t] + value);
- }
- }
- }
- for (int i = total_t - 1; i >= 0; i--) f2[i] = max(f2[i], f2[i + 1]);
- long long the_max = 0;
- for (int j = 0; j <= total_t; j++) the_max = max(the_max, f1[j] + f2[total_t - j]);
- printf("%lld\n", the_max);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement