QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#655816#8936. Team ArrangementSo_noSlackTL 0ms3676kbC++142.0kb2024-10-19 09:47:502024-10-19 09:47:51

Judging History

This is the latest submission verdict.

  • [2024-10-19 09:47:51]
  • Judged
  • Verdict: TL
  • Time: 0ms
  • Memory: 3676kb
  • [2024-10-19 09:47:50]
  • Submitted

answer

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
#define MAXN 65

long long read() {
    long long x = 0, f = 1;
    char c = getchar();
    while(c < 48 || c > 57) { if(c == 45) f = -1; c = getchar(); }
    while(c >= 48 && c <= 57) { x = (x << 3) + (x << 1) + (c - 48); c = getchar(); }
    return x * f;
}

long long n, w[MAXN], b[MAXN], g[MAXN], dp[MAXN], ans = -0x3f3f3f3f;
struct node { long long l, r; } a[MAXN];
bool f1 = true, f2 = true;

void update() {
    // for(int i = 1; i <= n; i ++) cout << b[i] << " ";
    // cout << ": ";
    bool flag = false;
    for(int i = 1; i <= n; i ++) g[i] = 0;
    for(int i = 1; i <= n; i ++) g[b[i]] ++;
    for(int i = 1; i <= n; i ++) 
        if(a[i].l > g[b[i]] || a[i].r < g[b[i]]) { flag = true; break; }
    // cout << flag << "\n";
    if(flag) return;
    long long res = 0;
    for(int i = 1; i <= n; i ++) res += w[g[i]];
    ans = max(ans, res);
    return;
}

void dfs(long long x) {
    if(x > n) { update(); return; }
    for(int i = 1; i <= n; i ++) 
        b[x] = i, dfs(x + 1);
    return;
}

int main() {
    n = read();
    for(int i = 1; i <= n; i ++) {
        a[i].l = read(), a[i].r = read();
        if(a[i].l != a[i].r) f1 = false;
        if(a[i].l != 1 || a[i].r != n) f2 = false;
    }
    for(int i = 1; i <= n; i ++) w[i] = read();
    if(f1) {
        bool f = false;
        for(int i = 1; i <= n; i ++) g[a[i].l] ++;
        for(int i = 1; i <= n; i ++)
            if(a[i].l != g[a[i].l]) { f = true; break; }
        if(f) { cout << "impossible\n"; return 0; }
        long long res = 0;
        for(int i = 1; i <= n; i ++) res += w[g[i]];
        cout << res << "\n";
        return 0;
    }
    if(f2) {
        for(int i = 1; i <= n; i ++) for(int j = n; j >= i; j --)
            dp[j] = max(dp[j], dp[j - i] + w[i]);
        cout << dp[n] << "\n";
        return 0;
    }
    dfs(1);
    if(ans == -0x3f3f3f3f) cout << "impossible\n";
    else cout << ans << "\n";
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3676kb

input:

3
2 3
1 2
2 2
4 5 100

output:

9

result:

ok single line: '9'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3604kb

input:

3
1 3
3 3
2 3
1 1 100

output:

100

result:

ok single line: '100'

Test #3:

score: 0
Accepted
time: 0ms
memory: 3608kb

input:

2
1 1
2 2
1 1

output:

impossible

result:

ok single line: 'impossible'

Test #4:

score: 0
Accepted
time: 0ms
memory: 3584kb

input:

3
2 3
1 2
2 2
-100 -200 100000

output:

-300

result:

ok single line: '-300'

Test #5:

score: -100
Time Limit Exceeded

input:

9
1 4
2 5
3 4
1 5
1 1
2 5
3 5
1 3
1 1
1 1 1 1 1 1 1 1 1

output:


result: