This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL n;
LL dp[500000+5][5];
// status 0 : belum
// status 1: awal
//status 2 : sudah
bool visited[500000+5][5];
pair<LL,LL>arr[500000+5];
LL f(LL idx, LL status)
{
// printf("%lld %lld\n",idx,status);
if(visited[idx][status])return dp[idx][status];
visited[idx][status]=1;
if(idx==n+1 && status==3)return dp[idx][status] =0;
if(idx==n+1 )
{
// printf("MASUK %lld %lld\n",idx,status);
return dp[idx][status] = -1e18;
}
if(status ==3)return dp[idx][status]=0;
LL add=0;
if(status==0) dp[idx][status]=max(f(idx+1,status),f(idx+1,status+1));
if(status==1) dp[idx][status]=max(f(idx+1,status+1)+arr[idx].second+arr[idx].first,f(idx+1,status+2)+arr[idx].second);
if(status==2) dp[idx][status]=max(f(idx+1,status+1)+arr[idx].second-arr[idx].first,f(idx+1,status)+arr[idx].second);
// printf("dp[%lld][%lld] = %lld\n",idx,status,dp[idx][status]);
return dp[idx][status];
}
int main()
{
scanf("%lld",&n);
for(LL a=1;a<=n;a++)
{
LL x,y;
scanf("%lld %lld",&x,&y);
arr[a]=make_pair(x,y);
}
sort(arr+1,arr+n+1);
cout<<max(f(1,0),f(1,1));
}
Compilation message (stderr)
art.cpp: In function 'long long int f(long long int, long long int)':
art.cpp:23:5: warning: unused variable 'add' [-Wunused-variable]
23 | LL add=0;
| ^~~
art.cpp: In function 'int main()':
art.cpp:32:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf("%lld",&n);
| ~~~~~^~~~~~~~~~~
art.cpp:37:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%lld %lld",&x,&y);
| ~~~~~^~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |