Cod sursa(job #3290203)

Utilizator lucamLuca Mazilescu lucam Data 29 martie 2025 14:43:23
Problema Subsecventa de suma maxima Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.68 kb
#include <bits/stdc++.h>
using namespace std;

namespace str {
const char *k_spaces = " \r\n\t";
void rstrip(string &s) {
    auto pos = s.find_last_not_of(k_spaces);
    if (pos != string::npos) {
        s.erase(pos + 1);
    } else {
        s.clear();
    }
}
} // namespace str
namespace io {
bool $log = true;
struct redirect {
    ifstream fin;
    ofstream fout;
    redirect(const char *in, const char *out) : fin(in), fout(out) {
        cin.rdbuf(fin.rdbuf());
        cout.rdbuf(fout.rdbuf());
    }
};
struct nolog {
    nolog() {
        $log = false;
        clog.rdbuf(nullptr);
        cerr.rdbuf(nullptr);
    }
};
struct skipnl {
    skipnl() {}
    friend istream &operator>>(istream &in, skipnl) {
        return in.ignore(numeric_limits<streamsize>::max(), '\n');
    }
};
template <typename T>
struct iter {
    const T &v;
    const char *sep;
    iter(const T &v, const char *sep = " ") : v(v), sep(sep) {}
    friend ostream &operator<<(ostream &out, iter it) {
        const char *s = "";
        for (auto &&x : it.v) {
            out << s << x;
            s = it.sep;
        }
        return out;
    }
};
#define io_iter_l(v) (::io::iter<decltype(v)>(v))
struct Log_ {
    ostringstream stream;
    const char *fname;
    int line;
    const char *funcname;
    Log_(const char *fname, int line, const char *funcname)
        : fname(fname), line(line), funcname(funcname) {}
    ~Log_() {
        (void)fname;
        if (!$log) {
            return;
        }
        string s(stream.str());
        cerr << "[" << funcname << "@" << line << "] ";
        if (s.back() != '\n') {
            s.push_back('\n');
        }
        cerr << s;
    }
};
#define log (::io::Log_(__FILE__, __LINE__, __FUNCTION__).stream)
#define logval(v)                  \
    ({                             \
        log << #v << " = " << (v); \
        0;                         \
    })
string getline(bool strip = true) {
    string s;
    ::std::getline(cin, s);
    if (!strip) {
        return s;
    }
    str::rstrip(s);
    return s;
}
} // namespace io

int main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (auto &x : v) {
        cin >> x;
    }
    vector<int> dp(n);
    dp[0] = v[0];
    int smax = dp[0];
    int ismax = 0;
    for (int i = 1; i < n; ++i) {
        dp[i] = v[i];
        if (dp[i - 1] > 0) {
            dp[i] += dp[i - 1];
        }
        if (dp[i] > smax) {
            smax = dp[i];
            ismax = i;
        }
    }

    int istart;
    for (istart = ismax; istart >= 0; --istart) {
        if (dp[istart] == v[istart]) {
            break;
        }
    }
    cout << smax << " " << istart + 1 << " " << ismax + 1 << endl;
}

auto $_r = io::redirect("ssm.in", "ssm.out");