Submission #244841

# Submission time Handle Problem Language Result Execution time Memory
244841 2020-07-05T07:13:01 Z NONAME Kas (COCI17_kas) C++14
100 / 100
908 ms 391800 KB
#include <bits/stdc++.h>
#define dbg(x) cerr << #x << " = " << x << "\n"
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie()
using namespace std;
using ll = long long;

const int fx = -(3e5 + 100);

int n, a[500], total, f[500][int(2e5) + 1];

int rec(int p, int cur) {
    if (p == n)
        return (cur == 0) ? 0 : -3e5;

    if (f[p][int(1e5) + cur] != fx)
        return f[p][int(1e5) + cur];

    int res = max(rec(p + 1, cur),
              max(rec(p + 1, cur - a[p]), rec(p + 1, cur + a[p]) + a[p]));

    return f[p][int(1e5) + cur] = res;
}

int main() {
    fast_io;

    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        total += a[i];
    }

    for (int i = 0; i < n; ++i)
    for (int j = 0; j <= int(2e5); ++j)
        f[i][j] = fx;

    int res = rec(0, 0);
    cout << res + (total - 2 * res) << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 8192 KB Output is correct
2 Correct 9 ms 8192 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 8192 KB Output is correct
2 Correct 11 ms 8960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 11 ms 10496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 10496 KB Output is correct
2 Correct 10 ms 10496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 20 ms 27776 KB Output is correct
2 Correct 22 ms 31616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 26 ms 35584 KB Output is correct
2 Correct 28 ms 39416 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 237 ms 156968 KB Output is correct
2 Correct 160 ms 196088 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 259 ms 235128 KB Output is correct
2 Correct 343 ms 313596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 501 ms 352760 KB Output is correct
2 Correct 908 ms 391800 KB Output is correct