1 solutions

  • 0
    @ 2025-11-27 11:59:02

    C++ :

    #include <algorithm>
    #include <cstdio>
    #include <vector>
    
    using namespace std;
    
    const int N = 1e5 + 5;
    
    int n;
    char s[N];
    int a[N];
    long long f[N];
    
    int main() {
        scanf("%d", &n);
        scanf("%s", s + 1);
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
        for (int i = 1; i <= n; i++) {
            int mask = 0;
            for (int j = i; j; j--) {
                int cur = 1 << (s[j] - 'a');
                if (mask & cur) break;
                mask |= cur;
                f[i] = max(f[i], f[j - 1] + a[i - j + 1]);
            }
        }
        printf("%lld\n", f[n]);
        return 0;
    }
    
    • 1

    Information

    ID
    122
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By