結果

問題 No.3289 Make More Happy Connection
ユーザー tsutaj
提出日時 2025-10-04 08:16:08
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 1,598 bytes
コンパイル時間 1,632 ms
コンパイル使用メモリ 154,304 KB
実行使用メモリ 10,152 KB
最終ジャッジ日時 2025-10-04 08:16:16
合計ジャッジ時間 7,886 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
 
ll dp[300010][2];
int main() {
    int N; cin >> N;
    vector<int> X(N), Y(N);
    for(int i=0; i<N; i++) cin >> X[i] >> Y[i];

    auto cost = [&](int x, int y) {
        return x == y ? x : 0;
    };
    fill(dp[0], dp[N+1], -1);
    dp[1][0] = dp[1][1] = cost(X[0], Y[0]);

    for(int i=1; i<N; i++) {
        for(int j=0; j<2; j++) {
            for(int k=0; k<2; k++) {
                int cl = (j == 0 ? Y[i-1] : X[i-1]);
                int cm = (k == 0 ? X[i] : Y[i]);
                int cr = (k == 0 ? Y[i] : X[i]);
                chmax(dp[i+1][k], dp[i][j] + cost(cl, cm) + cost(cm, cr));
            }
        }
    }
    cout << max(dp[N][0], dp[N][1]) << endl;
    return 0;
}
0