ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1005. Stone Pile

AC with dinamic O(n * sum(1..n) * 2)
Posted by 4llower 27 Oct 2018 04:44
#include <bits/stdc++.h>

#define rust(a, b, c, d) sqrt(sqr(a - c) + sqr(b - d))
#define sqr(a) (a)*(a)
#define m_p make_pair
#define fi first
#define se second
#define fast_io ios_base::sync_with_stdio(0);cin.tie(0);
#define endl "\n"
#define pll pair <ll,ll>
#define next yamogu
typedef long long ll;
const int MAXINT=2147483640;
const ll MAXLL=9223372036854775800LL;
const ll MAXN=1e6;
const double eps = 1e-9;
const ll mod = 1e9 + 7;
using namespace std;
ll n, a[MAXN], old[4 * MAXN + 100], next[4 * MAXN + 100];
int main() {
    srand(time(0));
//    freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
    fast_io;
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> a[i];
    old[2 * MAXN] = 1;
    for (int i = 0; i < n; ++i){
        for (int j = 0; j <= 4 * MAXN; ++j){
            if (j + a[i] <= 4 * MAXN && old[j]){
                next[j + a[i]] |= old[j];
            }
            if (j - a[i] >= 0 && old[j]){
                next[j - a[i]] |= old[j];
            }
        }
        for (int j = 0; j <= 4 * MAXN; ++j) {
            old[j] = next[j];
            next[j] = 0;
        }
    }
    ll mx = MAXLL;
    for (int i = 0; i <= 4 * MAXN; ++i) if (old[i]){
        mx = min(abs(i - 2 * MAXN), mx);
    }
    cout << mx << endl;
    return 0;

}