Cod sursa(job #756759)

Utilizator freak93Adrian Budau freak93 Data 10 iunie 2012 13:42:46
Problema Euro Scor 30
Compilator cpp Status done
Runda Remember Mihai Pătrașcu Marime 1.23 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

long double timeSwap(pair<int, int> &A, pair<int, int> &B, vector<long long> &dp) {
    return static_cast<long double>(dp[A.second] - dp[B.second]) / static_cast<long double>(A.first - B.first);
}

int main() {
    ifstream cin("euro.in");
    ofstream cout("euro.out");

    int N, T; cin >> N >> T;

    // It's no use to have an element positive unless it's the last element
    int currentSum = 0;
    vector< pair<int, int> > A;
    A.push_back(make_pair(0, 0));

    for (int i = 0; i < N; ++i) {
        int value; cin >> value;

        currentSum += value;

        //if (currentSum < 0) {
            A.push_back(make_pair(currentSum, i + 1));
            currentSum = 0;
        //}
    }

    if (A.size() == 0 || A.back().second != N)
        A.push_back(make_pair(currentSum, N));

    N = A.size() - 1;
    vector<long long> dp(N + 1, -(1LL << 60));
    for (int i = 1; i <= N; ++i)
        A[i].first += A[i - 1].first;

    dp[0] = 0;
    for (int i = 1; i <= N; ++i)
        for (int j = 0; j < i; ++j)
            dp[i] = max(dp[i], dp[j] + static_cast<long long>(A[i].first - A[j].first) * A[i].second - T);

    cout << dp[N] << "\n";
}