Submission #93106

# Submission time Handle Problem Language Result Execution time Memory
93106 2019-01-06T13:36:35 Z Ahmad_Elsisy Kas (COCI17_kas) C++14
100 / 100
505 ms 198136 KB
#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl

using namespace std;
typedef long long  ll;
const int N = 505 , M = 1e5 + 4;
int n , memo[N][M];
vector<int> a;

int solve(int indx , int s){
    if(indx == n){
        return (!s ? 0 : -inf);
    }

    int &ret = memo[indx][s];
    if(~ret)
        return ret;

    ret = -inf;
    ret = max(ret , a[indx] + solve(indx + 1 , s + a[indx]));
    ret = max(ret , max(0, a[indx] - s) + solve(indx + 1 , abs(s - a[indx])));
    ret = max(ret , solve(indx + 1 , s));
    return ret;
}
int main() {
    ios::sync_with_stdio() , cin.tie(0) , cin.tie(0);
    int tot = 0;
    cin >> n;
    a.resize(n);
    for(int &x : a)
        cin >> x , tot += x;
    memset(memo , -1 , sizeof memo);
    int v = solve(0 , 0);
    cout << v + (tot - 2 * v) << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 159 ms 198008 KB Output is correct
2 Correct 140 ms 198008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 154 ms 198008 KB Output is correct
2 Correct 150 ms 198096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 152 ms 198008 KB Output is correct
2 Correct 154 ms 197988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 153 ms 198008 KB Output is correct
2 Correct 153 ms 197964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 156 ms 198008 KB Output is correct
2 Correct 156 ms 197908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 154 ms 198008 KB Output is correct
2 Correct 155 ms 197980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 155 ms 198008 KB Output is correct
2 Correct 157 ms 197936 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 224 ms 198136 KB Output is correct
2 Correct 177 ms 197980 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 227 ms 197916 KB Output is correct
2 Correct 244 ms 198108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 333 ms 198036 KB Output is correct
2 Correct 505 ms 198060 KB Output is correct