Submission #1013156

# Submission time Handle Problem Language Result Execution time Memory
1013156 2024-07-03T08:43:16 Z vjudge1 Split the sequence (APIO14_sequence) C++17
0 / 100
42 ms 131072 KB
#include<bits/stdc++.h>

// #pragma GCC target("avx2")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;

#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file(s) freopen(s".in", "r", stdin);freopen(s".out", "w", stdout);
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ft first
#define sd second
#define ll long long
#define ull unsigned ll
#define pii pair<int,int>
#define pll pair<ll,ll>
const int N = 1e5 + 10;
const int M = 1e6 + 5;
const int B = 316;
const ll msize = 2;
const ll mod1 = 1e9 + 7;
const ll mod2 = 998244353;
const long double Phi = acos(-1);
const long long inf = 2e18;
const vector <pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

ll binmul(ll x, ll ti, ll m);
ll binpow(ll x, ll ti, ll m);

ll n, k, a[N], suf[N];
pll dp[N][205];

const void solve() {
	cin >> n >> k; ++ k;
	for (int i = 1; i <= n; ++ i) cin >> a[i];
	for (int i = n; i >= 1; -- i) suf[i] = suf[i + 1] + a[i];

	for (int i = 0; i <= n; ++ i) {
		for (int j = 0; j <= k; ++ j) {
			dp[i][j] = {-inf, 0};
		}
	}

	dp[0][0] = {0, 0};
	dp[1][1] = {a[1] * suf[2], 1};
	for (int i = 2; i <= n; ++ i) {
		for (int j = 1; j <= min((ll) i, k); ++ j) {
			int pos = dp[i - 1][j].sd;
			// cout << i - 1 << " " << j << " " << pos << "\n";
			// if (pos == 0) continue;

			if (pos > 0) {
				dp[i][j].ft = dp[pos - 1][j - 1].ft + (suf[pos] - suf[i + 1]) * suf[i + 1];
				dp[i][j].sd = pos;
			}

			if (j > 1 && dp[i - 1][j - 1].ft + a[i] * suf[i + 1] > dp[i][j].ft) {
				dp[i][j].ft = dp[i - 1][j - 1].ft + a[i] * suf[i + 1];
				dp[i][j].sd = i;
			}
		}
	}
	cout << dp[n][k].ft << "\n";
	
	vector <int> path;
	for (int i = dp[n][k].sd; i > 0;) {
		path.pb(i - 1);
		-- k;
		i = dp[i - 1][k].sd;
	}

	path.pop_back();
	reverse(all(path));

	for (auto x : path) cout << x << " ";
}  

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    srand(time(NULL));

    // file("maze");

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int tt = 1;
    // cin >> tt;
    for (int i = 1; i <= tt; ++ i) {
        // cout << "Case " << i << ":\n";
        solve();
    }

    #ifndef ONLINE_JUDGE
    cerr << "\n" << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif

    return 0;
} 


// Template functions
ll binmul(ll x, ll ti, ll m) { ll res = 0;while (ti){if(ti & 1)res += x;x += x;ti >>= 1; x %= m; res %= m;} return res;}
ll binpow(ll x, ll ti, ll m) { ll res = 1;while (ti){if(ti & 1)(res *= x)%=m;(x*=x)%=m;ti >>= 1; x %= m; res %= m;} return res;}
 





# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB contestant found the optimal answer: 108 == 108
2 Correct 0 ms 2396 KB contestant found the optimal answer: 999 == 999
3 Correct 1 ms 2396 KB contestant found the optimal answer: 0 == 0
4 Incorrect 1 ms 2396 KB contestant didn't find the optimal answer: 1542522 < 1542524
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 2652 KB contestant didn't find the optimal answer: 1092528 < 1093956
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 4700 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 1 ms 4700 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 1 ms 4700 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Incorrect 1 ms 4700 KB contestant didn't find the optimal answer: 211605090300 < 1499437552673
5 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 6748 KB contestant didn't find the optimal answer: 20166072 < 21503404
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 9 ms 34392 KB contestant didn't find the optimal answer: 1694250000 < 1818678304
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 42 ms 131072 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -