Submission #249643

# Submission time Handle Problem Language Result Execution time Memory
249643 2020-07-15T13:11:11 Z dimash241 Potatoes and fertilizers (LMIO19_bulves) C++17
100 / 100
174 ms 15396 KB
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("avx2")
//#pragma GCC optimize("O3")

//# include <x86intrin.h>
# include <bits/stdc++.h>

# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
 
template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define _USE_MATH_DEFINES_
#define ll long long
#define ld long double
#define Accepted 0
#define pb push_back
#define mp make_pair
#define sz(x) (int)(x.size())
#define every(x) x.begin(),x.end()
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define For(i,x,y)  for (ll i = x; i <= y; i ++) 
#define FOr(i,x,y)  for (ll i = x; i >= y; i --)
#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
// ROAD to...                                                                                                                                                                                                                Red

void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void setIO(string s = "") {
	// cin.exceptions(cin.failbit); 
	// throws exception when do smth illegal
	// ex. try to read letter into int
	if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}

const double eps = 0.000001;
const ld pi = acos(-1);
const int maxn = 1e7 + 9;
const int mod = 1e9 + 7;
const ll MOD = 1e18 + 9;
const ll INF = 1e18 + 123;
const int inf = 2e9 + 11;
const int mxn = 1e6 + 9;
const int N = 6e5 + 123;                                          
const int M = 22;
const int pri = 997;
const int Magic = 2101;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
mt19937 gen(chrono::steady_clock::now().time_since_epoch().count());
 
int rnd (int l, int r) {
	return uniform_int_distribution<int> (l, r)(gen);
}
 
long long c[500001], ans = 0;
 
int main() {
	SpeedForce;
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		long long a, b;
		cin >> a >> b;
		c[i] = c[i - 1] + a - b;
	}
	priority_queue<long long> pq;
	for (int i = 1; i <= n; i++) {
		if (c[i] > c[n]) {
			ans += c[i] - c[n];
			c[i] = c[n];
		} else if (c[i] < 0) {
			ans -= c[i];
			c[i] = 0;
		}
		pq.push(c[i]); pq.push(c[i]);
		ans += pq.top() - c[i];
		pq.pop();
	}
	cout << ans;
	return Accepted;
}

// B...a

Compilation message

bulves.cpp: In function 'void setIn(std::__cxx11::string)':
bulves.cpp:34:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
 void setIn(string s) { freopen(s.c_str(),"r",stdin); }
                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
bulves.cpp: In function 'void setOut(std::__cxx11::string)':
bulves.cpp:35:32: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
 void setOut(string s) { freopen(s.c_str(),"w",stdout); }
                         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 10 ms 1792 KB Output is correct
5 Correct 24 ms 2940 KB Output is correct
6 Correct 74 ms 8052 KB Output is correct
7 Correct 144 ms 15212 KB Output is correct
8 Correct 114 ms 13420 KB Output is correct
9 Correct 116 ms 12756 KB Output is correct
10 Correct 92 ms 10476 KB Output is correct
11 Correct 91 ms 10484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 10 ms 1792 KB Output is correct
5 Correct 24 ms 2940 KB Output is correct
6 Correct 74 ms 8052 KB Output is correct
7 Correct 144 ms 15212 KB Output is correct
8 Correct 114 ms 13420 KB Output is correct
9 Correct 116 ms 12756 KB Output is correct
10 Correct 92 ms 10476 KB Output is correct
11 Correct 91 ms 10484 KB Output is correct
12 Correct 36 ms 4212 KB Output is correct
13 Correct 91 ms 10988 KB Output is correct
14 Correct 146 ms 15212 KB Output is correct
15 Correct 127 ms 13412 KB Output is correct
16 Correct 114 ms 12780 KB Output is correct
17 Correct 94 ms 10476 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 0 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 512 KB Output is correct
13 Correct 1 ms 512 KB Output is correct
14 Correct 1 ms 436 KB Output is correct
15 Correct 1 ms 512 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 408 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 512 KB Output is correct
4 Correct 0 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 10 ms 1792 KB Output is correct
12 Correct 24 ms 2940 KB Output is correct
13 Correct 74 ms 8052 KB Output is correct
14 Correct 144 ms 15212 KB Output is correct
15 Correct 114 ms 13420 KB Output is correct
16 Correct 116 ms 12756 KB Output is correct
17 Correct 92 ms 10476 KB Output is correct
18 Correct 91 ms 10484 KB Output is correct
19 Correct 36 ms 4212 KB Output is correct
20 Correct 91 ms 10988 KB Output is correct
21 Correct 146 ms 15212 KB Output is correct
22 Correct 127 ms 13412 KB Output is correct
23 Correct 114 ms 12780 KB Output is correct
24 Correct 94 ms 10476 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 512 KB Output is correct
27 Correct 1 ms 512 KB Output is correct
28 Correct 1 ms 436 KB Output is correct
29 Correct 1 ms 512 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 408 KB Output is correct
32 Correct 50 ms 4212 KB Output is correct
33 Correct 106 ms 10892 KB Output is correct
34 Correct 174 ms 15212 KB Output is correct
35 Correct 162 ms 12904 KB Output is correct
36 Correct 135 ms 13420 KB Output is correct
37 Correct 171 ms 15396 KB Output is correct
38 Correct 101 ms 11500 KB Output is correct
39 Correct 117 ms 10988 KB Output is correct
40 Correct 107 ms 10472 KB Output is correct
41 Correct 120 ms 10476 KB Output is correct
42 Correct 112 ms 10476 KB Output is correct
43 Correct 103 ms 10476 KB Output is correct
44 Correct 159 ms 15340 KB Output is correct
45 Correct 116 ms 10728 KB Output is correct
46 Correct 109 ms 10352 KB Output is correct
47 Correct 1 ms 384 KB Output is correct