Submission #533519

#TimeUsernameProblemLanguageResultExecution timeMemory
533519idasFeast (NOI19_feast)C++11
30 / 100
49 ms8508 KiB
#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i = (begin); i < (end); i++)
#define FAST_IO ios_base::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr)
#define f first
#define s second
#define pb push_back
#define sz(x) ((int)((x).size()))
#define le(vec) vec[vec.size()-1]
#define all(x) (x).begin(), (x).end()
#define TSTS int ttt; cin >> ttt; while(ttt--) solve()

using namespace std;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<long double, long double> pdd;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef long double ld;
typedef long long ll;

const int INF=1e9, MOD=1e9+7, mod=998244353;
const ll LINF=1e18;

void setIO()
{
    FAST_IO;
}

void setIO(string s)
{
    FAST_IO;
    freopen((s+".in").c_str(), "r", stdin);
    freopen((s+".out").c_str(), "w", stdout);
}

const int N=3e5+10;
ll n, k, a[N];

int main()
{
    setIO();
    cin >> n >> k;
    FOR(i, 0, n) cin >> a[i];
    vector<ll> nw;
    bool tg=a[0]>0;
    ll now=0;
    FOR(i, 0, n)
    {
        if(a[i]==0) continue;
        if(a[i]>0 && !tg){
            tg=true;
            if(now!=0)
            nw.pb(now);
            now=a[i];
        }
        else if(a[i]<0 && tg){
            tg=false;
            if(now!=0)
            nw.pb(now);
            now=a[i];
        }
        else{
            now+=a[i];
        }
    }
    nw.pb(now);
    n=sz(nw);

    FOR(i, 0, n) a[i]=nw[i];

//    FOR(i, 0, n)
//    {
//        if(a[i]<0){
//            if(i!=0) a[i]=max(-a[i-1], a[i]);
//            if(i!=n-1) a[i]=max(-a[i+1], a[i]);
//        }
//    }
//    ll sm=0;
//    vector<ll> negs;
//    FOR(i, 0, n)
//    {
//        if(a[i]<0) negs.pb(a[i]);
//        sm+=a[i];
//    }
//
//    sort(all(negs));
//
//    FOR(i, 0, k-1)
//    {
//        if(i>=sz(negs)) break;
//        sm-=negs[i];
//    }
//
//    cout << sm;

    ll sm=0;
    FOR(i, 0, n)
    {
        sm+=a[i];
        if(a[i]<0){
            if(sm<0){
                a[i]-=sm;
                sm=0;
            }
        }
    }

    sm=0;
    for(int i=n-1; i>=0; i--){
        sm+=a[i];
        if(a[i]<0){
            if(sm<0){
                a[i]-=sm;
                sm=0;
            }
        }
    }

    sm=0;
    FOR(i, 0, n) sm+=a[i];
    vector<ll> negs;
    FOR(i, 0, n)
    {
        if(a[i]<0) negs.pb(a[i]);
    }

    sort(all(negs));

    FOR(i, 0, k-1)
    {
        if(i>=sz(negs)) break;
        sm-=negs[i];
    }

    cout << sm;
}

Compilation message (stderr)

feast.cpp: In function 'void setIO(std::string)':
feast.cpp:32:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |     freopen((s+".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   33 |     freopen((s+".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...