Submission #1820510


Source Code Expand

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <ctime>
#include<complex>
using namespace std;

#define FOR(i,a,b) for (int i = (a); i < (b); i++)
#define RFOR(i,b,a) for (int i = (b)-1; i >= (a); i--)
#define ITER(it,a) for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a,value) memset(a, value, sizeof(a))

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;


const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;

const int MAX = 2 * 1000 * 100 + 47;
vector<PII> g[MAX];
vector<PLL> A[MAX];
int sz;

bool cmp(PLL a, PLL b)
{
	return a.second < b.second;
}

int dfs(int v, LL T)
{
	if (SZ(g[v]) == 0)
	{
		A[sz].push_back(MP(0, 0));
		sz++;
		return sz - 1;
	}

	int i1 = dfs(g[v][0].first, T);
	int i2 = dfs(g[v][1].first, T);
	int vi = g[v][0].second;
	int vk = g[v][1].second;
	int p = 0;
	sort(ALL(A[i1]), cmp);
	sort(ALL(A[i2]));
	LL mind = LINF;
	FOR(i, 0, SZ(A[i1]))
	{
		LL a = A[i1][i].first;
		LL b = A[i1][i].second;
		while (p != SZ(A[i2]) && b + A[i2][p].first + vi + vk <= T)
		{
			mind = min(A[i2][p].second, mind);
			p++;
		}

		if (mind == LINF) continue;
		A[sz].push_back(MP(a + vi, mind + vk));
	}

	sort(ALL(A[i1]));
	sort(ALL(A[i2]), cmp);
	LL minc = LINF;
	p = 0;
	
	FOR(i, 0, SZ(A[i1]))
	{
		LL a = A[i1][i].first;
		LL b = A[i1][i].second;

		while (p != SZ(A[i2]) && a + A[i2][p].second + vi + vk <= T)
		{
			minc = min(minc, A[i2][p].first);
			p++;
		}

		if (minc == LINF) continue;
		A[sz].push_back(MP(minc + vk, b + vi));
	}

	sz++;
	return sz - 1;
}

bool ok(LL c)
{
	FOR(i, 0, sz) A[i].clear();
	sz = 0;
	int ind = dfs(0, c);
	return SZ(A[ind]);
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);
	ios::sync_with_stdio(false); cin.tie(0);
	int n, p, v;
	cin >> n;
	FOR(i, 0, n - 1)
	{
		cin >> p >> v;
		p--;
		g[p].push_back(MP(i + 1, v));
	}

	LL l = -1, r = 1000 * (LL)1000 * (LL)1000 * (LL)1000;
	while (r - l > 1)
	{
		//cout << l << " " << r << endl;
		LL c = (r + l) / 2;
		if (ok(c))
		{
			r = c;
		}
		else
		{
			l = c;
		}
	}

	cout << r << endl;
}

Submission Info

Submission Time
Task E - Shik and Travel
User philologist
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2736 Byte
Status WA
Exec Time 5258 ms
Memory 62432 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1400
Status
AC × 4
AC × 34
WA × 19
TLE × 1
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt, example3.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, example0.txt, example1.txt, example2.txt, example3.txt
Case Name Status Exec Time Memory
000.txt WA 27 ms 9984 KB
001.txt WA 21 ms 9856 KB
002.txt WA 42 ms 10112 KB
003.txt WA 21 ms 9856 KB
004.txt WA 20 ms 9856 KB
005.txt AC 25 ms 9984 KB
006.txt AC 17 ms 9856 KB
007.txt WA 22 ms 9856 KB
008.txt WA 18 ms 9856 KB
009.txt WA 15 ms 9856 KB
010.txt WA 2072 ms 32520 KB
011.txt TLE 5258 ms 62432 KB
012.txt WA 1743 ms 29816 KB
013.txt AC 2066 ms 31736 KB
014.txt WA 2694 ms 35196 KB
015.txt AC 1751 ms 29688 KB
016.txt AC 1464 ms 27648 KB
017.txt AC 2037 ms 32520 KB
018.txt AC 2167 ms 34300 KB
019.txt WA 1960 ms 31620 KB
020.txt WA 2896 ms 37908 KB
021.txt WA 2347 ms 37004 KB
022.txt WA 2826 ms 35964 KB
023.txt WA 1567 ms 28348 KB
024.txt WA 1800 ms 30824 KB
025.txt WA 1602 ms 28176 KB
026.txt AC 2084 ms 32408 KB
027.txt AC 2705 ms 35680 KB
028.txt WA 1876 ms 31104 KB
029.txt AC 2244 ms 33340 KB
030.txt AC 1731 ms 31864 KB
031.txt AC 1892 ms 31736 KB
032.txt AC 1891 ms 31736 KB
033.txt AC 1713 ms 31736 KB
034.txt AC 1726 ms 31736 KB
035.txt AC 1916 ms 31736 KB
036.txt AC 1739 ms 31736 KB
037.txt AC 1741 ms 31736 KB
038.txt AC 1739 ms 31736 KB
039.txt AC 1586 ms 31736 KB
040.txt AC 1252 ms 31736 KB
041.txt AC 1505 ms 31736 KB
042.txt AC 1518 ms 31736 KB
043.txt AC 1499 ms 31736 KB
044.txt AC 245 ms 25984 KB
045.txt AC 255 ms 21376 KB
046.txt AC 260 ms 22784 KB
047.txt AC 260 ms 21760 KB
048.txt AC 264 ms 24576 KB
049.txt AC 6 ms 9600 KB
example0.txt AC 5 ms 9600 KB
example1.txt AC 5 ms 9600 KB
example2.txt AC 5 ms 9600 KB
example3.txt AC 5 ms 9600 KB