1. 程式人生 > 其它 >P1216 [USACO1.5][IOI1994]數字三角形 Number Triangles

P1216 [USACO1.5][IOI1994]數字三角形 Number Triangles

// Problem: P1216 [USACO1.5][IOI1994]數字三角形 Number Triangles
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1216
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// User: Pannnn

#include <bits/stdc++.h>

using namespace std;

template<class T>
void debugVector(const T &a) {
    cout << "[ ";
    for (size_t i = 0; i < a.size(); ++i) {
        cout << a[i] << (i == a.size() - 1 ? " " : ", ");
    }
    cout << "]" << endl;
}

template<class T1, class T2>
void debugVectorPair(const vector<pair<T1, T2>> &a) {
    cout << "{";
    for (size_t i = 0; i < a.size(); ++i) {
        cout << "[ " << a[i].first << ": " << a[i].second << (i == a.size() - 1 ? " ]" : " ], ");
    }
    cout << "}" << endl;
}


template<class T>
void debugMatrix2(const T &a) {
    for (size_t i = 0; i < a.size(); ++i) {
        debugVector(a[i]);
    }
}

template<class T>
using matrix2 = vector<vector<T>>;

template<class T>
vector<vector<T>> getMatrix2(size_t n, size_t m, T init = T()) {
    return vector<vector<T>>(n, vector<T>(m, init));
}

template<class T>
using matrix3 = vector<vector<vector<T>>>;

template<class T>
vector<vector<vector<T>>> getMatrix3(size_t x, size_t y, size_t z, T init = T()) {
    return vector<vector<vector<T>>>(x, vector<vector<T>>(y, vector<T>(z, init)));
}

vector<int> genBigInteger(const string &a) {
    vector<int> res;
    if (a.empty()) return res;
    for (int i = a.size() - 1; i >= 0; --i) {
        res.push_back(a[i] - '0');
    }
    return res;
}

ostream &printBigInteger(const vector<int> &a) {
    if (a.empty()) return cout;
    for (int i = a.size() - 1; i >= 0; --i) {
        cout << a[i];
    }
    return cout;
}

vector<int> maxBigInteger(const vector<int> &a, const vector<int> &b) {
    if (a.size() > b.size()) {
        return a;
    } else if (a.size() < b.size()) {
        return b;
    }
    for (int i = a.size() - 1; i >= 0; --i) {
        if (a[i] > b[i]) {
            return a;
        } else if (a[i] < b[i]) {
            return b;
        }
    }
    return a;
}

vector<int> addBigInteger(const vector<int> &a, const vector<int> &b) {
    vector<int> res;
    int pre = 0;
    for (size_t i = 0; i < a.size() || i < b.size() || pre; ++i) {
        if (i < a.size()) pre += a[i];
        if (i < b.size()) pre += b[i];
        res.push_back(pre % 10);
        pre /= 10;
    }
    while (res.size() > 1 && res.back() == 0) {
        res.pop_back();
    }
    return res;
}

vector<int> mulBigInteger(const vector<int> &a, int b) {
    vector<int> res;
    int pre = 0;
    for (size_t i = 0; i < a.size() || pre; ++i) {
        if (i < a.size()) pre += a[i] * b;
        res.push_back(pre % 10);
        pre /= 10;
    }
    while (res.size() > 1 && res.back() == 0) {
        res.pop_back();
    }
    return res;
}

vector<int> divBigInteger(const vector<int> &a, int b, int &r) {
    vector<int> res;
    for (int i = a.size() - 1; i >= 0; --i) {
        r = 10 * r + a[i];
        res.push_back(r / b);
        r %= b;
    }
    reverse(res.begin(), res.end());
    while (res.size() > 1 && res.back() == 0) {
        res.pop_back();
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int r;
    cin >> r;
    matrix2<int> tmp = getMatrix2(r + 1, r + 1, 0);
    for (int i = 1; i <= r; ++i) {
        for (int j = 1; j <= i; ++j) {
            cin >> tmp[i][j];
        }
    }
    for (int i = 1; i <= r; ++i) {
        for (int j = 1; j <= i; ++j) {
            tmp[i][j] += max(tmp[i - 1][j - 1], tmp[i - 1][j]);
        }
    }
    int res = 0;
    for (int i = 1; i <= r; ++i) {
        res = max(res, tmp[r][i]);
    }
    cout << res << endl;
    return 0;
}