Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int optimal (vector<int> a,int n) {
- int max_sum = INT_MIN,sum = 0;
- for (int i = 0; i < n; ++i) {
- sum += a[i];
- max_sum = max(max_sum,sum);
- sum = max(sum,0);
- }
- return max_sum;
- }
- int brute (vector<int> a,int n) {
- int max_sum = 0;
- for (int i = 0; i < n; ++i) {
- for (int j = i; j < n; ++j) {
- int sum = 0;
- for (int k = i; k <= j; ++k) {
- sum += a[k];
- }
- max_sum = max(max_sum,sum);
- }
- }
- return max_sum;
- }
- void test () {
- for (int test_case = 1; test_case <= 100; ++test_case) {
- int n = rand() % 100 + 1;
- vector<int> a(n);
- for (int i = 0; i < n; ++i) {
- a[i] = rand() % 10000;
- int p = rand() % 2;
- if (p != 1) {
- a[i] = -a[i];
- }
- }
- if (brute(a,n) != optimal(a,n)) {
- for (auto i : a) cout << i << " ";
- return;
- }
- }
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- test();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement