QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#112835#6525. New Housesbatrr#TL 3ms3352kbC++141.6kb2023-06-14 00:41:232023-06-14 00:41:30

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-14 00:41:30]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:3352kb
  • [2023-06-14 00:41:23]
  • 提交

answer

#include <bits/stdc++.h>

#define f first
#define s second
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;

const int N = 300500, inf = 1e9, mod = 998244353;
const ll INF = 1e18;

int sum(int a, int b) {
    a += b;
    if (a >= mod)
        a -= mod;
    return a;
}

int sub(int a, int b) {
    a -= b;
    if (a < 0)
        a += mod;
    return a;
}

int mult(int a, int b) {
    return 1ll * a * b % mod;
}

int bp(int a, int b) {
    int res = 1;
    while (b) {
        if (b & 1)
            res = mult(res, a);
        a = mult(a, a);
        b >>= 1;
    }
    return res;
}

int inv(int x) {
    return bp(x, mod - 2);
}
int n, m;
const int maxN = 5e5 + 10;
int dif[maxN];
int a[maxN], b[maxN];
void solve() {
    cin >> n >> m;
    ll S = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i] >> b[i];
        dif[i] = a[i] - b[i];
        S += b[i];
    }
    sort(dif + 1, dif + n + 1);
    reverse(dif + 1, dif + n + 1);
    ll best = -1e18;
    ll cur = 0;
    for (int cnt = 0; cnt <= n; cnt++) {
        if (cnt > 0) cur += dif[cnt];
        if (cnt != 1 && (n - cnt) + n <= m) best = max(best, S + cur);
        if (cnt == 0 && 2 * n - 1 <= m) best = max(best, S + cur);
    }
    cout << best << '\n';
}

int main() {
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    ios_base::sync_with_stdio(false);
    int t = 1;
    cin >> t;
    for (int i = 1; i <= t; i++) {
//        cout << "Case #" << i << endl;
        solve();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 3352kb

input:

3
4 5
1 100
100 1
100 1
100 1
2 2
1 10
1 10
2 3
100 50
1 1000

output:

400
2
1050

result:

ok 3 number(s): "400 2 1050"

Test #2:

score: -100
Time Limit Exceeded

input:

100000
6 11
191141536 365120521
799679686 648574232
102602909 467685128
405440859 796808887
384858152 191995380
433392826 195648471
5 13
831367906 510447872
795639287 575551283
811207605 176441088
240156289 946977042
133416463 721098873
5 5
806744021 699586200
630510306 637827160
49223781 641709297
...

output:

3247545200
4106290713
2653993029
5122532137
5570513606
2031887824
2044500908
1857678917
6815058419
2237593918
6646615756
5638337819
3690874076
5497726904
5513905900
5404435094
4705403467
2411992217
3430587752
5098767681
3921151709
1445672728
2692878616
3833748807
2716183054
974485573
6464787173
8839...

result: