Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using pi32 = pair<int, int>;
- using pi64 = pair<int64_t, int64_t>;
- const int maxN = 1e5 + 5;
- const ll INF = 1e18 + 7;
- const int MOD = 1e9 + 7;
- int n;
- ll a[maxN];
- void findSubArrayMax() {
- ll best = -INF, sum = 0;
- for (int i = 1; i <= n; i++) {
- sum = max(a[i], sum + a[i]);
- best = max(best, sum);
- }
- cout << best << '\n';
- }
- void findSubArrayMaxWithIndices() {
- ll best = -INF, sum = 0;
- int best_start = -1, best_end = -1, tmp = 1;
- for (int i = 1; i <= n; i++) {
- if (sum + a[i] < a[i]) {
- tmp = i;
- sum = a[i];
- } else {
- sum += a[i];
- }
- if (best < sum) {
- best = sum;
- best_start = tmp;
- best_end = i;
- }
- }
- cout << best << '\n';
- cout << "Start from " << best_start << " to " << best_end << '\n';
- }
- int main() {
- #ifdef LOCAL
- freopen("in1.txt", "r", stdin);
- #else
- freopen("DAYSO.inp", "r", stdin);
- freopen("DAYSO.out", "w", stdout);
- #endif
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cin >> n;
- for (int i = 1; i <= n; ++i)
- cin >> a[i];
- findSubArrayMax();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment