Problem:
イルミネーション (Illumination)
問題文
JOI 氏は，自宅の敷地に
N
本の木を所有している．これらの木は一列に並んでおり，順に
1
から
N
までの整数で番号が付けられている．
この冬，JOI 氏はいくつかの木を選んで，イルミネーションを飾り付けることにした．イルミネーションには
美しさ
と呼ばれる値が定まっている．木
i
にイルミネーションを飾り付ける場合の美しさは
A_i
である．
JOI 氏は，あまりに近い
2
つの木の両方にイルミネーションを飾り付けてしまうと，眩しすぎる場合があることに気がついた．具体的には，
j = 1, 2, ..., M
に対して，木
L_j
,
L_j + 1
,
...
,
R_j
のうち
2
つ以上にイルミネーションを飾り付けるべきではないということが判明した．
この条件に従ってイルミネーションを飾り付けるときの，美しさの合計の最大値を求めよ．
制約
1 ≦ N ≦ 200000 (= 2×10^5)
1 ≦ M ≦ 200000 (= 2×10^5)
1 ≦ A_i ≦ 1000000000 (= 10^9)
(
1 ≦ i ≦ N
)
1 ≦ L_j ≦ R_j ≦ N
(
1 ≦ j ≦ M
)
入力・出力
入力
入力は以下の形式で標準入力から与えられる．
N
M
A_1
A_2
...
A_N
L_1
R_1
L_2
R_2
⋮
L_M
R_M
出力
イルミネーションの美しさの合計の最大値を
1
行で出力せよ．
入出力例
入力例 1
4 1
1 2 3 8
2 4
出力例 1
9
この入力例では，木
1
,
4
にイルミネーションを飾り付けると，美しさの合計が
9
となり最大となる．
L_1 = 2
,
R_1 = 4
なので，木
2
,
3
,
4
のうち
2
つ以上にイルミネーションを飾り付けることはできない．例えば木
1
,
2
,
4
にイルミネーションを飾り付けることはできないことに注意せよ．
入力例 2
5 2
2 3 9 5 6
1 3
2 4
出力例 2
15
入力例 3
20 10
870851814 594414687 615919461 65033245 460143082 617460823 881870957 126041265 623075703 34130727 27054628 853567651 483228744 491145755 220689940 148007930 229257101 790404982 612186806 281076231
15 19
20 20
12 13
1 4
19 19
9 13
3 6
9 12
16 16
18 19
出力例 3
4912419478
情報オリンピック日本委員会作 『第 18 回日本情報オリンピック JOI 2018/2019 予選競技課題』

Submitted solutions:

code_snippet: #include <iostream>
#include <string.h>
#include <map>
#include <queue>
using namespace std;

const int S=200010;
struct E{
	int l,r;
	bool operator<(const E& e1) const {
		if(l!=e1.l)return l>e1.l;
		return r>e1.r;
	}
};

int main() {
	// your code goes here
	long long int memo1[S];
	long long int memo2[S];
	long long int dp[S];
	long long int data[S];
	long long int n,m;
	map<int,int> memo3;
	priority_queue<E> pq;
	memset(memo1,0,sizeof(memo1));
	memset(memo2,0,sizeof(memo2));
	memset(dp,0,sizeof(dp));
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>data[i];
	}
	dp[n]=0;
	E e1;
	for(int i=0;i<m;i++){
		int l,r;
		cin>>l>>r;
		memo1[r]++;
		e1.l=l-1;
		e1.r=r;
		pq.push(e1);
	}

	for(int i=0;i<=n;i++){
		while(pq.size()>0 && pq.top().l<=i){
			E e1=pq.top();
			pq.pop();
			memo3[e1.r]++;
		}
		memo3[i]-=memo1[i];
		if(memo3[i]==0){
			memo3.erase(i);
		}
		
		if(memo3.size()==0||(i+1==n)){
			memo2[i]=i+1;	
		}else{
			map<int,int>::iterator it=memo3.end();
			it--;
			memo2[i]=(*it).first;
		}
		
	}
	for(int i=0;i<n;i++){
		//cout<<i<<" "<<memo2[i]<<endl;
		long long int t=dp[i]+data[i];
		if(dp[memo2[i]]<t)dp[memo2[i]]=t;
		if(dp[i+1]<dp[i])dp[i+1]=dp[i];
	}
	long long int ans=0;
	for(int i=0;i<=n;i++){
		if(ans<dp[i])ans=dp[i];
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 13044
final_rank: 18

code_snippet: #include <bits/stdc++.h>

using namespace std;

void solve()
{
    int n, m;
    cin >> n >> m;

    vector<int> a(n + 1);

    for (int i = 1; i <= n; i++)
        cin >> a[i];

    vector<int> prev(n + 1);

    for (int i = 1; i <= n; i++)
        prev[i] = i - 1;

    for (int i = 1; i <= m; i++)
    {
        int l, r;
        cin >> l >> r;
        prev[r] = min(prev[r], l - 1);
    }

    for (int i = n - 1; i >= 1; i--)
        prev[i] = min(prev[i], prev[i + 1]);

    vector<long long> dp(n + 1);

    dp[0] = 0;

    for (int i = 1; i <= n; i++)
        dp[i] = max(dp[i - 1], dp[prev[i]] + a[i]);

    cout << dp[n] << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    solve();

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5976
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N,M;
    cin>>N>>M;
    vector<ll> A(N);
    for(int i=0;i<N;i++)cin>>A[i];
    vector<vector<ll>> P(N+2);
    for(int i=0;i<M;i++){
        ll L,R;
        cin>>L>>R;
        P[L].push_back(L);
        P[R+1].push_back(-L);
    }
    multiset<ll> S;
    vector<ll> DP(N+1,-1e18),SDP(N+1,-1e18);
    DP[0]=SDP[0]=0;
    for(int i=0;i<N;i++){
        for(auto s:P[i+1]){
            if(s>=0)S.insert(s);
            else S.erase(S.find(-s));
        }
        if(S.empty()){
            DP[i+1]=SDP[i]+A[i];
        }
        else{
            ll s=*(S.begin());
            DP[i+1]=SDP[s-1]+A[i];
        }
        SDP[i+1]=max(SDP[i],DP[i+1]);
    }
    cout<<SDP[N]<<endl;
}
accuracy: 1
time_ms: 100
memory_kb: 21624
final_rank: 19

code_snippet: #include <iostream>
#include <cstdint>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	uint32_t N, M, i, j, prev;
	cin >> N >> M;
	vector<uint32_t> A(N);
	vector<uint64_t> dp(N + 1);
	vector<pair<uint32_t, uint32_t>> LR(M);
	for (i = 0; i != N; ++i) cin >> A[i];
	for (i = 0; i != M; ++i) cin >> LR[i].first >> LR[i].second, --LR[i].first;

	sort(LR.begin(), LR.end());
	dp[0] = 0;
	for (i = 0, j = 0; i != N; ++i)
	{
		while (j != M && LR[j].second <= i) ++j;

		if (j != M) dp[i + 1] = max(dp[min(i, LR[j].first)] + A[i], dp[i]);
		else dp[i + 1] = dp[i] + A[i];
	}

	cout << dp[N] << '\n';
	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 7044
final_rank: 4

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<int> r(n, -1);
    for (int i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;
        x -= 1, y -= 1;
        r[x] = max(r[x], y);
    }
    for (int i = 1; i < n; i++) {
        r[i] = max(r[i], r[i - 1]);
    }
    vector<ll> dp(n + 1);
    for (int i = 0; i < n; i++) {
        if (r[i] <= i) {
            dp[i + 1] = max(dp[i + 1], dp[i] + a[i]);
        } else {
            dp[r[i] + 1] = max(dp[r[i] + 1], dp[i] + a[i]);
        }
        dp[i + 1] = max(dp[i + 1], dp[i]);
    }
    cout << dp[n] << endl;
}
accuracy: 1
time_ms: 30
memory_kb: 6988
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
    int n, m, l, r, mae[200002];
    long long a[200002], ma[200002];
    cin >> n >> m;
    for(int i=1;i<=n;i++){
        cin >> a[i];
        mae[i] = i-1;
    }
    for(int i=0;i<m;i++){
        cin >> l >> r;
        mae[r] = min(mae[r], l-1);
    }
    int mi = mae[n];
    for(int i=n-1;i>0;i--){
        mi = min(mi, mae[i]);
        mae[i] = mi;
    }
    ma[0] = 0;
    for(int i=1;i<=n;i++) ma[i] = max(ma[i-1], ma[mae[i]]+a[i]);
    cout << ma[n] << endl;
    return(0);
}
accuracy: 1
time_ms: 130
memory_kb: 6868
final_rank: 12

code_snippet: #include <bits/stdc++.h>
#include <iostream>
#include <limits>
#include <numeric>
#include <type_traits>
#include <bitset>
#include <map>
#include <unordered_map>
#include <set>
#include <random>

using namespace std;

#define rep(i,n,m) for(ll (i)=(n);(i)<(m);(i)++)
#define rrep(i,n,m) for(ll (i)=(n);(i)>(m);(i)--)
using ll = long long;
const ll mod = 1000000007;
const ll inf = 1e9;
const ll INF = 1e17;

void pline(vector<ll> lis){
    rep(i,0,lis.size()){
        printf ("%lld",lis[i]);
        if (i != lis.size()-1) printf(" ");
        else printf("\n");
    }
}

void pline(vector<pair<ll,ll>> lis){
    rep(i,0,lis.size()){
        printf ("/%lld,%lld/",lis[i].first,lis[i].second);
        if (i != lis.size()-1) printf(" ");
        else printf("\n");
    }
}

/*
void pmap(map<ll,mint>& lis){
    for (auto p : lis){
        printf ("%lld:%lld ,",p.first,p.second.val());
    }
    printf ("\n");
}*/


int main(){

    //ios::sync_with_stdio(false);
    //std::cin.tie(nullptr);

    ll N,M;
    cin >> N >> M;

    vector<ll> A(N);
    rep(i,0,N) cin >> A[i];

    vector<vector<ll>> LtR(N);

    rep(i,0,M){
        ll L,R;
        cin >> L >> R;
        L--;R--;
        LtR[L].push_back(R);
    }

    vector<ll> ans(N+1,0);

    ll nmx = 0;
    ll lastmax = 0;

    rep(i,0,N){
        nmx = max(i,nmx);
        for (ll r : LtR[i]) nmx = max(nmx,r);
        lastmax = max(lastmax , ans[i]);

        ans[nmx+1] = max(ans[nmx+1] , lastmax + A[i]);
    }

    ll mans = 0;
    rep(i,0,N+1) mans = max(ans[i],mans);
    cout << mans << endl;

}

/*

区間だよなぁ
dp[i] = フリー区間の最左がiの時の最大値

*/
accuracy: 1
time_ms: 110
memory_kb: 14692
final_rank: 16

code_snippet: #include<bits/stdc++.h>

#include <algorithm>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n)) x++;
    return x;
}

// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <vector>

namespace atcoder {

template <class S, S (*op)(S, S), S (*e)()> struct segtree {
  public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = internal::ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    void set(int p, S x) {
        assert(0 <= p && p < _n);
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) {
        assert(0 <= p && p < _n);
        return d[p + size];
    }

    S prod(int l, int r) {
        assert(0 <= l && l <= r && r <= _n);
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        assert(0 <= l && l <= _n);
        assert(f(e()));
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        assert(0 <= r && r <= _n);
        assert(f(e()));
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

  private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};

}  // namespace atcoder

using namespace std;
using namespace atcoder;

using S = long long;
S op(S l, S r){
	if(l >= r) return l;
	else return r;
}

S e(){
	return 0;
}

void solve(){
	int n, m;
	cin >> n >> m;
	vector<long long> A(n);
	for(auto &a:A) cin >> a;
	vector<int> mi(n);
	for(int i = 0; i < n; i++) mi[i] = i;
	int l, r;
	for(int i = 0; i < m; i++){
		cin >> l >> r;
		l--; r--;
		mi[r] = min(mi[r], l);
	}
	for(int i = n - 2; i >= 0; i--) mi[i] = min(mi[i], mi[i + 1]);
	segtree<S, op, e> seg(n + 1);
	for(int r = 0; r < n; r++){
		long long ma = seg.prod(0, mi[r] + 1);
		seg.set(r + 1, ma + A[r]);
	}
	
	cout << seg.all_prod() << "\n";

}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    int t;
    t = 1;
    // cin >> t;
    while(t--) solve();
}
accuracy: 1
time_ms: 40
memory_kb: 10936
final_rank: 10

code_snippet: //#define _GLIBCXX_DEBUG

#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define lfs cout<<fixed<<setprecision(10)
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()
#define UNIQUE(a) (a).erase(unique((a).begin(),(a).end()),(a).end())
#define spa << " " <<
#define fi first
#define se second
#define MP make_pair
#define MT make_tuple
#define PB push_back
#define EB emplace_back
#define rep(i,n,m) for(ll i = (n); i < (ll)(m); i++)
#define rrep(i,n,m) for(ll i = (ll)(m) - 1; i >= (ll)(n); i--)
using ll = long long;
using ld = long double;
const ll MOD1 = 1e9+7;
const ll MOD9 = 998244353;
const ll INF = 1e18;
using P = pair<ll, ll>;
template<typename T> using PQ = priority_queue<T>;
template<typename T> using QP = priority_queue<T,vector<T>,greater<T>>;
template<typename T1, typename T2>bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
template<typename T1, typename T2>bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
ll median(ll a,ll b, ll c){return a+b+c-max({a,b,c})-min({a,b,c});}
void ans1(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}
void ans2(bool x){if(x) cout<<"YES"<<endl;else cout<<"NO"<<endl;}
void ans3(bool x){if(x) cout<<"Yay!"<<endl;else cout<<":("<<endl;}
template<typename T1,typename T2>void ans(bool x,T1 y,T2 z){if(x)cout<<y<<endl;else cout<<z<<endl;}  
template<typename T1,typename T2,typename T3>void anss(T1 x,T2 y,T3 z){ans(x!=y,x,z);};  
template<typename T>void debug(const T &v,ll h,ll w,string sv=" "){for(ll i=0;i<h;i++){cout<<v[i][0];for(ll j=1;j<w;j++)cout<<sv<<v[i][j];cout<<endl;}};
template<typename T>void debug(const T &v,ll n,string sv=" "){if(n!=0)cout<<v[0];for(ll i=1;i<n;i++)cout<<sv<<v[i];cout<<endl;};
template<typename T>void debug(const vector<T>&v){debug(v,v.size());}
template<typename T>void debug(const vector<vector<T>>&v){for(auto &vv:v)debug(vv,vv.size());}
template<typename T>void debug(stack<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(queue<T> st){while(!st.empty()){cout<<st.front()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(deque<T> st){while(!st.empty()){cout<<st.front()<<" ";st.pop_front();}cout<<endl;}
template<typename T>void debug(PQ<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(QP<T> st){while(!st.empty()){cout<<st.top()<<" ";st.pop();}cout<<endl;}
template<typename T>void debug(const set<T>&v){for(auto z:v)cout<<z<<" ";cout<<endl;}
template<typename T>void debug(const multiset<T>&v){for(auto z:v)cout<<z<<" ";cout<<endl;}
template<typename T,size_t size>void debug(const array<T, size> &a){for(auto z:a)cout<<z<<" ";cout<<endl;}
template<typename T,typename V>void debug(const map<T,V>&v){for(auto z:v)cout<<"["<<z.first<<"]="<<z.second<<",";cout<<endl;}
template<typename T>vector<vector<T>>vec(ll x, ll y, T w){vector<vector<T>>v(x,vector<T>(y,w));return v;}
ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;}
vector<ll>dx={1,-1,0,0,1,1,-1,-1};vector<ll>dy={0,0,1,-1,1,-1,1,-1};
template<typename T>vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
template<typename... Ts>auto make_v(size_t a,Ts... ts){return vector<decltype(make_v(ts...))>(a,make_v(ts...));}
template<typename T1, typename T2>ostream &operator<<(ostream &os, const pair<T1, T2>&p){return os << p.first << " " << p.second;}
template<typename T>ostream &operator<<(ostream &os, const vector<T> &v){for(auto &z:v)os << z << " ";cout<<"|"; return os;}
template<typename T>void rearrange(vector<int>&ord, vector<T>&v){
  auto tmp = v;
  for(int i=0;i<tmp.size();i++)v[i] = tmp[ord[i]];
}
template<typename Head, typename... Tail>void rearrange(vector<int>&ord,Head&& head, Tail&&... tail){
  rearrange(ord, head);
  rearrange(ord, tail...);
}
template<typename T> vector<int> ascend(const vector<T>&v){
  vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);
  sort(ord.begin(),ord.end(),[&](int i,int j){return make_pair(v[i],i)<make_pair(v[j],j);});
  return ord;
}
template<typename T> vector<int> descend(const vector<T>&v){
  vector<int>ord(v.size());iota(ord.begin(),ord.end(),0);
  sort(ord.begin(),ord.end(),[&](int i,int j){return make_pair(v[i],-i)>make_pair(v[j],-j);});
  return ord;
}
template<typename T> vector<T> inv_perm(const vector<T>&ord){
  vector<T>inv(ord.size());
  for(int i=0;i<ord.size();i++)inv[ord[i]] = i;
  return inv;
}
ll FLOOR(ll n,ll div){assert(div>0);return n>=0?n/div:(n-div+1)/div;}
ll CEIL(ll n,ll div){assert(div>0);return n>=0?(n+div-1)/div:n/div;}
ll digitsum(ll n){ll ret=0;while(n){ret+=n%10;n/=10;}return ret;}
ll modulo(ll n,ll d){return (n%d+d)%d;};
template<typename T>T min(const vector<T>&v){return *min_element(v.begin(),v.end());}
template<typename T>T max(const vector<T>&v){return *max_element(v.begin(),v.end());}
template<typename T>T acc(const vector<T>&v){return accumulate(v.begin(),v.end(),T(0));};
template<typename T>T reverse(const T &v){return T(v.rbegin(),v.rend());};
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
int popcount(ll x){return __builtin_popcountll(x);};
int poplow(ll x){return __builtin_ctzll(x);};
int pophigh(ll x){return 63 - __builtin_clzll(x);};
template<typename T>T poll(queue<T> &q){auto ret=q.front();q.pop();return ret;};
template<typename T>T poll(priority_queue<T> &q){auto ret=q.top();q.pop();return ret;};
template<typename T>T poll(QP<T> &q){auto ret=q.top();q.pop();return ret;};
template<typename T>T poll(stack<T> &s){auto ret=s.top();s.pop();return ret;};
ll MULT(ll x,ll y){if(LLONG_MAX/x<=y)return LLONG_MAX;return x*y;}
ll POW2(ll x, ll k){ll ret=1,mul=x;while(k){if(mul==LLONG_MAX)return LLONG_MAX;if(k&1)ret=MULT(ret,mul);mul=MULT(mul,mul);k>>=1;}return ret;}
ll POW(ll x, ll k){ll ret=1;for(int i=0;i<k;i++){if(LLONG_MAX/x<=ret)return LLONG_MAX;ret*=x;}return ret;}
template< typename T = int >
struct edge {
  int to;
  T cost;
  int id;
  edge():id(-1){};
  edge(int to, T cost = 1, int id = -1):to(to), cost(cost), id(id){}
  operator int() const { return to; }
};

template<typename T>
using Graph = vector<vector<edge<T>>>;
template<typename T>
Graph<T>revgraph(const Graph<T> &g){
  Graph<T>ret(g.size());
  for(int i=0;i<g.size();i++){
    for(auto e:g[i]){
      int to = e.to;
      e.to = i;
      ret[to].push_back(e);
    }
  }
  return ret;
}
template<typename T>
Graph<T> readGraph(int n,int m,int indexed=1,bool directed=false,bool weighted=false){
  Graph<T> ret(n);
  for(int es = 0; es < m; es++){
    int u,v;
    T w=1;
    cin>>u>>v;u-=indexed,v-=indexed;
    if(weighted)cin>>w;
    ret[u].emplace_back(v,w,es);
    if(!directed)ret[v].emplace_back(u,w,es);
  }
  return ret;
}
template<typename T>
Graph<T> readParent(int n,int indexed=1,bool directed=true){
  Graph<T>ret(n);
  for(int i=1;i<n;i++){
    int p;cin>>p;
    p-=indexed;
    ret[p].emplace_back(i);
    if(!directed)ret[i].emplace_back(p);
  }
  return ret;
}

int main(){
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  ll res=0,buf=0;
  bool judge = true;
	ll n,m;cin>>n>>m;
	vector<ll>a(n);
	rep(i,0,n)cin>>a[i];
	vector<ll>l(m),r(m);
	rep(i,0,m)cin>>l[i]>>r[i],l[i]--;
	vector<ll>mx(n);
	rep(i,0,n)mx[i]=i+1;
	rep(i,0,m){
		chmax(mx[l[i]],r[i]);
	}
	rep(i,0,n){
		chmax(mx[i+1],mx[i]);
	}
	vector<ll>dp(n+1,-INF);
	dp[0]=0;
	rep(i,0,n){
		chmax(dp[i+1],dp[i]);
		chmax(dp[mx[i]],dp[i]+a[i]);
	}
	cout<<dp[n]<<endl;
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 10584
final_rank: 9

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 0656.cc: イルミネーション (Illumination)
 */

#include<cstdio>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000;

/* typedef */

typedef long long ll;

/* global variables */

int as[MAX_N], nbrs[MAX_N];
ll dp[MAX_N + 1];

/* subroutines */

inline void setmax(ll &a, ll b) { if (a < b) a = b; }

/* main */

int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  for (int i = 0; i < n; i++) nbrs[i] = i + 1;

  for (int i = 0; i < m; i++) {
    int l, r;
    scanf("%d%d", &l, &r), l--;
    nbrs[l] = max(nbrs[l], r);
  }

  for (int i = 0; i < n; i++) {
    setmax(dp[i + 1], dp[i]);

    if (i > 0) nbrs[i] = max(nbrs[i], nbrs[i - 1]);
    setmax(dp[nbrs[i]], dp[i] + as[i]);
  }

  printf("%lld\n", dp[n]);
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5912
final_rank: 1

code_snippet: #line 1 "solve/main.cpp"
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>

/* template start */

#line 2 "lib/utility/type_alias.hpp"

#line 4 "lib/utility/type_alias.hpp"

using u64 = std::uint64_t;
using u32 = std::uint32_t;
using u16 = std::uint16_t;
using u8 = std::uint8_t;

using i64 = std::int64_t;
using i32 = std::int32_t;
using i16 = std::int16_t;
using i8 = std::int8_t;

using usize = std::size_t;
using isize = std::ptrdiff_t;

constexpr i64 operator"" _i64(unsigned long long num) { return i64(num); }

constexpr u64 operator"" _u64(unsigned long long num) { return u64(num); }
#line 33 "solve/main.cpp"

#define rep(i, a, b) for (i64 i = (a); (i) < (b); (i)++)
#define all(i) i.begin(), i.end()

#ifdef LOCAL
#define debug(...)                                                    \
  std::cerr << "LINE: " << __LINE__ << "  [" << #__VA_ARGS__ << "]:", \
      debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << std::endl; }

template <typename Head, typename... Tail>
void debug_out(Head h, Tail... t) {
  std::cerr << " " << h;
  if (sizeof...(t) > 0) std::cerr << " :";
  debug_out(t...);
}

template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}

template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (std::size_t i = 0; i < vec.size(); i++)
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}

template <typename T1, typename T2>
inline bool chmax(T1& a, T2 b) {
  return a < b && (a = b, true);
}

template <typename T1, typename T2>
inline bool chmin(T1& a, T2 b) {
  return a > b && (a = b, true);
}

template <typename Num, typename Func = std::multiplies<Num>>
constexpr Num mypow(Num a, u64 b, Num id = 1, Func mul = Func()) {
  if (b == 0) return id;
  Num x = id;
  while (b > 0) {
    if (b & 1) x = mul(x, a);
    a = mul(a, a);
    b >>= 1;
  }
  return x;
}

/* template end */

using P = std::pair<i64,i64>;

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);

  i64 n,m;
  std::cin>>n>>m;

  std::vector<i64> a(n);
  
  for(auto&& e:a)std::cin>>e;

  std::vector<P> seg(m);

  for(auto&& [l,r]:seg)std::cin>>l>>r;
  std::sort(all(seg));

  debug(seg);

  std::vector<i64> dp(n+1);
  std::priority_queue<P,std::vector<P>,std::greater<P>> pq;

  dp[0] = 0;

  i64 seg_count = 0;

  rep(i,1,n+1){
    pq.emplace(i,i);
    while(seg_count < m && seg[seg_count].first == i){
      debug(i,seg[seg_count]);
      pq.emplace(seg[seg_count]);
      seg_count++;
    }

    P left_seg = pq.top();
    while(left_seg.second < i){
      pq.pop();
      left_seg = pq.top();
    }

    dp[i] = a[i-1] + dp[left_seg.first-1];
    chmax(dp[i],dp[i-1]);
  }

  debug(dp);

  std::cout<<dp[n]<<"\n";

  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 17764
final_rank: 15

code_snippet: #include <iostream>
#include <vector>
#include <map>

using namespace std;
using Int = int_fast64_t;

int main()
{
	int N, M; cin >> N >> M;
	vector<int> A(N); for (auto& a : A) cin >> a;
	map<int, int> L = {{N + 1, N + 1}}, C = {{N, 1}};
	for (int j = 0, Lj, Rj; j < M; ++j) {
		cin >> Lj >> Rj;
		if (L.count(Rj) == 0 || L[Rj] > Lj - 2) L[Rj] = Lj - 2;
	}
	for (auto [Rj, Lj] : L) ++C[Lj];
	vector<Int> S(N, 0);
	S[0] = A[0];
	for (int i = 1; i < N; ++i) {
		for (; !L.empty() && L.begin()->first <= i; L.erase(L.begin())) {
			if (--C[L.begin()->second] <= 0) C.erase(L.begin()->second);
		}
		auto [Li, n] = *C.begin();
		S[i] = (Int) A[i];
		if (Li >= 0) S[i] += S[min(i - 1, Li)];
		if (S[i] < S[i - 1]) S[i] = S[i - 1];
	}
	cout << S.back() << endl;
	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 15304
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define drep(i,j,n) for(int i=0;i<(int)(n-1);i++)for(int j=i+1;j<(int)(n);j++)
#define trep(i,j,k,n) for(int i=0;i<(int)(n-2);i++)for(int j=i+1;j<(int)(n-1);j++)for(int k=j+1;k<(int)(n);k++)
#define codefor int test;scanf("%d",&test);while(test--)
#define INT(...) int __VA_ARGS__;in(__VA_ARGS__)
#define LL(...) ll __VA_ARGS__;in(__VA_ARGS__)
#define yes(ans) if(ans)printf("yes\n");else printf("no\n")
#define Yes(ans) if(ans)printf("Yes\n");else printf("No\n")
#define YES(ans) if(ans)printf("YES\n");else printf("NO\n")
#define popcount(v) __builtin_popcount(v)
#define vector2d(type,name,h,...) vector<vector<type>>name(h,vector<type>(__VA_ARGS__))
#define vector3d(type,name,h,w,...) vector<vector<vector<type>>>name(h,vector<vector<type>>(w,vector<type>(__VA_ARGS__)))
#define umap unordered_map
#define uset unordered_set
using namespace std;
using ll = long long;
template<class T> using rpriority_queue = priority_queue<T, vector<T>, greater<T>>;
const int MOD=1000000007;
const int MOD2=998244353;
const int INF=1<<30;
const ll INF2=(ll)1<<60;
void scan(int& a){scanf("%d",&a);}
void scan(long long& a){scanf("%lld",&a);}
template<class T,class L>void scan(pair<T, L>& p){scan(p.first);scan(p.second);}
template<class T,class U,class V>void scan(tuple<T,U,V>& p){scan(get<0>(p));scan(get<1>(p));scan(get<2>(p));}
template<class T> void scan(T& a){cin>>a;}
template<class T> void scan(vector<T>& vec){for(auto&& it:vec)scan(it);}
void in(){}
template <class Head, class... Tail> void in(Head& head, Tail&... tail){scan(head);in(tail...);}
void print(const int& a){printf("%d",a);}
void print(const long long& a){printf("%lld",a);}
void print(const double& a){printf("%.15lf",a);}
template<class T,class L>void print(const pair<T, L>& p){print(p.first);putchar(' ');print(p.second);}
template<class T> void print(const T& a){cout<<a;}
template<class T> void print(const vector<T>& vec){if(vec.empty())return;print(vec[0]);for(auto it=vec.begin();++it!= vec.end();){putchar(' ');print(*it);}}
void out(){putchar('\n');}
template<class T> void out(const T& t){print(t);putchar('\n');}
template <class Head, class... Tail> void out(const Head& head,const Tail&... tail){print(head);putchar(' ');out(tail...);}
template<class T> void dprint(const T& a){cerr<<a;}
template<class T> void dprint(const vector<T>& vec){if(vec.empty())return;cerr<<vec[0];for(auto it=vec.begin();++it!= vec.end();){cerr<<" "<<*it;}}
void debug(){cerr<<endl;}
template<class T> void debug(const T& t){dprint(t);cerr<<endl;}
template <class Head, class... Tail> void debug(const Head& head, const Tail&... tail){dprint(head);cerr<<" ";debug(tail...);}
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
ll modinv(ll a, ll m) {ll b = m, u = 1, v = 0;while (b) {ll t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;}
ll updivide(ll a,ll b){if(a%b==0) return a/b;else return (a/b)+1;}
template<class T> void chmax(T &a,const T b){if(b>a)a=b;}
template<class T> void chmin(T &a,const T b){if(b<a)a=b;}

int main(){
    INT(n,m);
    vector<ll> a(n);
    in(a);
    vector<vector<int>> t(n);
    int l,r;
    rep(i,m){
        in(l,r);
        t[--l].push_back(r);
    }
    vector<ll> dp(n+1,0);
    priority_queue<int> pq;
    pq.push(-1);
    for(int i=0;i<n;i++){
        for(auto r:t[i])pq.push(r);
        chmax(dp[max(i+1,pq.top())],dp[i]+a[i]);
        chmax(dp[i+1],dp[i]);
    }
    out(dp[n]);
}
accuracy: 1
time_ms: 50
memory_kb: 15576
final_rank: 14

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;




#define SIZE 200005


ll A[SIZE],L[SIZE],dp[SIZE];


int main(){

	ll N,M;
	scanf("%lld %lld",&N,&M);

	for(ll i = 1; i <= N; i++){

		scanf("%lld",&A[i]);
	}


	for(ll i = 1; i <= N; i++){

		L[i] = i-1;
	}

	ll right,left;

	for(ll i = 0; i < M; i++){

		scanf("%lld %lld",&left,&right);

		L[right] = min(L[right],left-1);
	}

	for(int i = N-1; i >= 1; i--){

		L[i] = min(L[i],L[i+1]);
	}

	dp[0] = 0;

	for(ll i = 1; i <= N; i++){

		//足さない
		dp[i] = dp[i-1];

		dp[i] = max(dp[i],dp[L[i]]+A[i]);
	}

	printf("%lld\n",dp[N]);

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 7764
final_rank: 5

code_snippet: #include<bits/stdc++.h> 
using namespace std;
int segsize=1<<18;

long long getmax(vector<long long> &segtree,int l,int r){
  l+=segsize,r+=segsize;
  long long ret=0;
  while(l<r){
    if(l%2){
      ret=max(ret,segtree[l]);
      l++;
    }
    l/=2;
    if(r%2){
      r--;
      ret=max(ret,segtree[r]);
    }
    r/=2;
  }
  return ret;
}
void segupd(vector<long long> &segtree,int p,long long v){
  p+=segsize;
  segtree[p]=v;
  p/=2;
  while(p>0){
    segtree[p]=max(segtree[p*2],segtree[p*2+1]);
    p/=2;
  }
}

int main(){
  int N,M;
  cin>>N>>M;
  vector<long long>A(N);
  for(long long &i:A)cin>>i;
  vector<int>ngto(N+1);
  for(int i=0,a,b;i<M;i++){
    cin>>a>>b;
    ngto[a]=max(ngto[a],b);
  }
  vector<long long>dp1(segsize*2),dp0(segsize*2);
  vector<int>ngindex(N+1);
  iota(ngindex.begin(),ngindex.end(),0);
  for(int i=0,cnt=0;i<=N;i++){
    if(cnt<i)cnt=i;
    while(ngto[i]>=cnt){
      ngindex[cnt++]=i;
    }
  }
  for(int i=1;i<=N;i++){
    segupd(dp0,i,max(getmax(dp0,0,i),getmax(dp1,0,i)));
    segupd(dp1,i,max(getmax(dp1,0,ngindex[i]),getmax(dp0,0,ngindex[i]))+A[i-1]);
  }
  cout<<max(getmax(dp0,0,N+1),getmax(dp1,0,N+1))<<endl;
}
accuracy: 1
time_ms: 130
memory_kb: 14244
final_rank: 17

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 200005


int L[SIZE];
ll A[SIZE],dp[SIZE];
vector<int> G[SIZE];

int main(){

	ll N,M;
	scanf("%lld %lld",&N,&M);

	for(ll i = 1; i <= N; i++){

		L[i] = i-1; //飾りつけることのできる木の最大index
	}

	for(ll i = 1; i <= N; i++){

		scanf("%lld",&A[i]);
	}

	int left,right;

	for(int loop = 0; loop < M; loop++){

		scanf("%d %d",&left,&right);
		L[right] = min(L[right],left-1);
	}
	for(ll i = N-1; i >= 1; i--){

		L[i] = min(L[i],L[i+1]);
	}

	dp[0] = 0;

	for(ll i = 1; i <= N; i++){

		//飾り付けない
		dp[i] = dp[i-1];

		//飾り着ける
		dp[i] = max(dp[i],dp[L[i]]+A[i]);
	}

	printf("%lld\n",dp[N]);

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 11736
final_rank: 11

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 200005

ll N,M;
ll A[SIZE];
ll L[SIZE],dp[SIZE];

int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 1; i <= N; i++){

		scanf("%lld",&A[i]);
		L[i] = i;
	}

	ll left,right;

	for(ll i = 0; i < M; i++){

		scanf("%lld %lld",&left,&right);

		L[right] = min(L[right],left);
	}

	for(ll i = N-1; i >= 1; i--){

		L[i] = min(L[i],L[i+1]);
	}

	dp[0] = 0;

	for(ll i = 1; i <= N; i++){
		//飾りつけない
		dp[i] = dp[i-1];

		//飾り着ける
		dp[i] = max(dp[i],dp[L[i]-1]+A[i]);
	}

	printf("%lld\n",dp[N]);

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 7796
final_rank: 6

code_snippet: #include<bits/stdc++.h>
#define int long long
#define for1(i, n) for(int i = 1; i <= (n);i++)
#define puts(x) cout << (x) << "\n"
using namespace std;
int n, m, a[234567], l[234567], dp[234567];
signed main() {
	cin >> n >> m;
	for1(i, n) {
		cin >> a[i];
		l[i] = i + 1;
	}
	while (m--) {
		int b, c; cin >> b >> c;
		l[b] = max(l[b], c + 1);
	}
	for1(i, n) {
		l[i] = max(l[i], l[i - 1]);
		dp[i] = max(dp[i], dp[i - 1]);
		dp[l[i]] = max(dp[l[i]], dp[i] + a[i]);
	}
	puts(dp[n + 1]);
}
accuracy: 1
time_ms: 130
memory_kb: 7688
final_rank: 13

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;




#define SIZE 200005


ll N,M;
ll A[SIZE],L[SIZE];
ll dp[SIZE];


int main(){

	scanf("%lld %lld",&N,&M);

	for(ll i = 1; i <= N; i++){

		scanf("%lld",&A[i]);
		L[i] = i-1;
	}

	ll left,right;
	for(ll i = 0; i < M; i++){

		scanf("%lld %lld",&left,&right);
		L[right] = min(L[right],left-1);
	}
	for(ll i = N-1; i >= 1; i--){

		L[i] = min(L[i],L[i+1]);
	}

	dp[0] = 0;
	for(ll i = 1; i <= N; i++){

		dp[i] = max(dp[i-1],dp[L[i]]+A[i]);
	}

	printf("%lld\n",dp[N]);

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 7820
final_rank: 7

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;



#define SIZE 200005
#define NUM -1

int N,M;
ll A[SIZE],dp[SIZE],dp2[SIZE];
int L[SIZE];


int main(){

	scanf("%d %d",&N,&M);

	for(int i = 1; i <= N; i++){

		scanf("%lld",&A[i]);
	}

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

		L[i] = i;
	}

	int left,right;

	for(int i = 0; i < M; i++){

		scanf("%d %d",&left,&right);
		L[right] = min(L[right],left);
	}

	for(int i = N-1; i >= 1; i--){

		L[i] = min(L[i],L[i+1]);
	}

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

		dp[i] = 0;
		dp2[i] = 0;
	}

	ll ans = 0;

	for(int i = 1; i <= N; i++){

		dp[i] = dp2[L[i]-1]+A[i];
		dp2[i] = max(dp2[i-1],dp[i]);
		ans = max(ans,dp[i]);
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 8536
final_rank: 8