Submission #1000980


Source Code Expand

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <numeric>
#include <random>
#include <vector>
#include <array>
#include <bitset>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); }
int bsr(int x) { return 31 - __builtin_clz(x); }

const int MN = 5050;
using P = pair<ll, ll>;
int n;
vector<P> v;

ll dp[MN][MN];
bool used[MN][MN];
ll calc(int m, int a) {
    if (m == 0) return 0;
    int b = n/2 - ((n-m)-(n/2-a));
    if (used[m][a]) return dp[m][a];
    used[m][a] = true;
    ll &ans = dp[m][a];
    ans = TEN(18);
    P p = v[m-1];
    // left
    if (a) {
        ll x = p.first*(a-1);
        x += p.second*(a);
        ans = min(ans, x + calc(m-1, a-1));
    }
    // right
    if (b) {
        ll x = p.first*(b);
        x += p.second*(b-1);
        ans = min(ans, x + calc(m-1, a));
    }
    return ans;
}

ll main2() {
    memset(used, false, sizeof(used));
    ll ans = 0;
    if (n % 2 == 1) {
        // 演算子の優先順位には…気をつけようね! 
        ans += (v[n-1].first+v[n-1].second)*(n/2);
        n--;
    }
    return ans + calc(n, n/2);
}

int main() {
    ios::sync_with_stdio(0);
    cout << setprecision(20);
    int nn;
    cin >> nn;
    n = nn;
    for (int i = 0; i < n; i++) {
        ll l, r;
        cin >> l >> r;
        v.push_back(P(l, r));
    }
    ll ans = TEN(18);
    sort(v.begin(), v.end(), [&](const P l, const P r){
        ll ls = l.first+l.second;
        ll rs = r.first+r.second;
        if (ls != rs) return ls > rs;
        return l.first > r.first;
    });
    ans = min(ans, main2());
    n = nn;
    sort(v.begin(), v.end(), [&](const P l, const P r){
        ll ls = l.first+l.second;
        ll rs = r.first+r.second;
        if (ls != rs) return ls > rs;
        return l.second > r.second;
    });

    ans = min(ans, main2());

    cout << ans << endl;
    return 0;
}

Submission Info

Submission Time
Task F - Intervals
User yosupo
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2204 Byte
Status WA
Exec Time 846 ms
Memory 94848 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1000
Status
AC × 2
AC × 44
WA × 1
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 834 ms 94720 KB
001.txt AC 159 ms 42496 KB
002.txt AC 828 ms 94720 KB
003.txt WA 623 ms 79744 KB
004.txt AC 825 ms 94720 KB
005.txt AC 313 ms 55680 KB
006.txt AC 826 ms 94720 KB
007.txt AC 407 ms 62848 KB
008.txt AC 829 ms 94720 KB
009.txt AC 87 ms 35584 KB
010.txt AC 833 ms 94720 KB
011.txt AC 350 ms 58496 KB
012.txt AC 830 ms 94720 KB
013.txt AC 75 ms 34176 KB
014.txt AC 827 ms 94720 KB
015.txt AC 161 ms 42624 KB
016.txt AC 828 ms 94720 KB
017.txt AC 614 ms 78848 KB
018.txt AC 829 ms 94720 KB
019.txt AC 385 ms 61312 KB
020.txt AC 29 ms 25216 KB
021.txt AC 29 ms 25216 KB
022.txt AC 827 ms 94720 KB
023.txt AC 830 ms 94720 KB
024.txt AC 829 ms 94720 KB
025.txt AC 831 ms 94720 KB
026.txt AC 826 ms 94720 KB
027.txt AC 827 ms 94720 KB
028.txt AC 830 ms 94720 KB
029.txt AC 826 ms 94720 KB
030.txt AC 828 ms 94720 KB
031.txt AC 826 ms 94720 KB
032.txt AC 830 ms 94720 KB
033.txt AC 827 ms 94720 KB
034.txt AC 832 ms 94720 KB
035.txt AC 830 ms 94720 KB
036.txt AC 839 ms 94848 KB
037.txt AC 846 ms 94720 KB
038.txt AC 830 ms 94720 KB
039.txt AC 831 ms 94720 KB
040.txt AC 827 ms 94720 KB
041.txt AC 827 ms 94720 KB
042.txt AC 830 ms 94720 KB
example0.txt AC 29 ms 25216 KB
example1.txt AC 29 ms 25216 KB