# Robot

### Problem Statement :

```You have two arrays of integers, V = {V1,V2,...Vn} and P = {P1,P2,...,Pn}, where both have N number of elements. Consider the following function:

score = 0

int Go(step, energy) {
if (step == N) {
score += V[step];
return (score);
}
else {
int way = random(1, 2);
if (way == 1) {
score += V[step];
}
else {
energy = P[step];
}
if (energy > 0) {
Go(step + 1, energy - 1);
}
else {
KillTheWorld();
}
}
}
What is the maximum possible value of score that we can get in the end, if we call Go(1,0)?.
Note that the function should never invoke KillTheWorld function. And random(1,2) generates a random integer from set [1, 2].
It is guaranteed there will be a solution that wont kill the world.

Input Format

The first line contains an integer N. Each of the following N lines contains a pair of integers. The i-th line contains a pair of numbers, Vi,Pi, separated by space.

Constraints

1 <= N <= 5*10^5
0 <= Vi <= 10^9
0 <= Pi <- 10^5

Output Format

Derive the maximum score given by return (score);.```

### Solution :

```                            ```Solution in C :

In C++ :

#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int, long long> pii;

#define INF 1000000000
#define pb push_back
#define itr iterator
#define sz size()
#define mp make_pair

multiset<long long> values;
int v, p;
int n;

int main() {
scanf("%d", &n);

values.insert(0);
long long tot = 0;

for (int i = 0; i < n; i++) {
scanf("%d %d", &v, &p);
tot += v;

for (int k = 0; k < adj[i].size(); k++) {
}

long long cur = -v + *values.rbegin();
//printf("cur = %lld %d %lld\n", tot, -v, *values.rbegin());
values.insert(cur);
int will_rem = min(n, i + p + 1);

if (i == n-1) {
printf("%lld\n", tot + cur + v);
return 0;
}
}
}

In Java :

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

public class Solution {
private static PrintWriter out;

public static void main(String[] args) throws IOException {
out = new PrintWriter(System.out, true);
int N = in.nextInt();
long[] V = new long[N + 1];
int[] P = new int[N + 1];
for (int i = 1; i <= N; i++) {
V[i] = in.nextInt();
P[i] = in.nextInt();
}
long[] dp = new long[N + 1];
long[] psum = new long[N + 1];
for (int i = 1; i <= N; i++)
psum[i] = psum[i - 1] + V[i];
dp[N] = V[N];
SegmentTree root = new SegmentTree(1, N);
root.update(N, dp[N] + psum[N - 1]);
for (int i = N - 1; i >= 1; i--) {
int zero = Math.min(N, i + P[i]);
dp[i] = zero > i ? (root.query(i + 1, zero) - psum[i]) : -(1l << 60);
root.update(i, dp[i] + psum[i - 1]);
}
out.println (dp[1]);
out.close();
System.exit(0);
}

static class SegmentTree {
public SegmentTree left, right;
public int start, end;
public long max;

public SegmentTree(int start, int end) {
this.start = start;
this.end = end;
if (start != end) {
int mid = (start + end) >> 1;
left = new SegmentTree(start, mid);
right = new SegmentTree(mid + 1, end);
}
}

public void update(int pos, long v) {
if (start == pos && end == pos) {
this.max = v;
return;
}
int mid = (start + end) >> 1;
if (mid >= pos) left.update(pos, v);
else right.update(pos, v);
max =  Math.max(max, v);
}

public long query (int s, int e) {
if (start == s && end == e) {
return max;
}
int mid = (start + end) >> 1;
if (mid >= e) return left.query(s, e);
else if (mid < s) return right.query(s, e);
else return Math.max(left.query(s, mid), right.query(mid + 1, e));
}
}

final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;

din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
}

public Reader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
}

public String readLine() throws IOException {
byte[] buf = new byte[1024];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}

public int nextInt() throws IOException {
int ret = 0;
while (c <= ' ')
boolean neg = (c == '-');
if (neg)
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}

public long nextLong() throws IOException {
long ret = 0;
while (c <= ' ')
boolean neg = (c == '-');
if (neg)
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}

public double nextDouble() throws IOException {
double ret = 0, div = 1;
while (c <= ' ')
boolean neg = (c == '-');
if (neg)
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.')
while ((c = read()) >= '0' && c <= '9')
ret += (c - '0') / (div *= 10);
if (neg)
return -ret;
return ret;
}

private void fillBuffer() throws IOException {
buffer[0] = -1;
}

private byte read() throws IOException {
fillBuffer();
return buffer[bufferPointer++];
}

public void close() throws IOException {
if (din == null)
return;
din.close();
}
}

}

In C :

/*
4
4 2
0 2
4 0
3 4

7
*/

#include <stdio.h>
#include <stdlib.h>
typedef struct _node{
int w;
} node;
void heap_insert(node *heap,node *elem,int *size,int idx);
long long get_val(int w,int idx,int *flag);
int *V,*P;
long long *sum,*dp;

int main(){
int N,i,heap_size=0,flag;
long long ans,max,t;
node *heap,elem;
scanf("%d",&N);
V=(int*)malloc(N*sizeof(int));
P=(int*)malloc(N*sizeof(int));
sum=(long long*)malloc(N*sizeof(long long));
dp=(long long*)malloc(N*sizeof(long long));
heap=(node*)malloc(2*N*sizeof(heap));
for(i=0;i<N;i++)
scanf("%d%d",V+i,P+i);
for(i=0;i<N;i++)
if(i==0)
sum[i]=V[i];
else
sum[i]=sum[i-1]+V[i];
for(i=0;i<N-1;i++){
max=0;
if(heap_size)
do{
t=get_val(heap[1].w,i,&flag);
if(flag>0 && t>max)
max=t;
else if(flag==0 && P[i] && t-V[i]>max){
max=t-V[i];
}
else if(flag<=0)
}while(flag<=0 && heap_size);
dp[i]=max;
elem.w=i;
heap_insert(heap,&elem,&heap_size,i);
}
max=0;
if(heap_size)
do{
t=get_val(heap[1].w,i,&flag);
if(flag>=0 && t>max)
max=t;
else if(flag<0)
}while(flag<0 && heap_size);
printf("%lld",max);
return 0;
}
void heap_insert(node *heap,node *elem,int *size,int idx){
(*size)++;
int index=(*size);
while(index>1 && get_val(elem->w,idx,0)>get_val(heap[index/2].w,idx,0)){
heap[index]=heap[index/2];
index/=2;
}
heap[index]=(*elem);
return;
}
int index=1;
while(index*2<*size && get_val(heap[*size].w,idx,0)<get_val(heap[index*2].w,idx,0) || index*2+1<*size && get_val(heap[*size].w,idx,0)<get_val(heap[index*2+1].w,idx,0)){
index=(get_val(heap[index*2].w,idx,0)<get_val(heap[index*2+1].w,idx,0))?index*2+1:index*2;
heap[index/2]=heap[index];
}
heap[index]=heap[*size];
(*size)--;
return;
}
long long get_val(int w,int idx,int *flag){
if(flag){
if(idx-w>P[w])
*flag=-1;
else if(idx-w==P[w])
*flag=0;
else
*flag=1;
}
long long ans;
if(!w)
ans=0;
else
ans=dp[w-1];
return ans+sum[idx]-sum[w];
}

In Python3 :

import sys
from operator import itemgetter

N = int(input())

V, P = [None] * N, [None] * N
for i in range(N):
v_item, p_item = input().split()
V[i] = int(v_item)
P[i] = int(p_item)

games = []
for i in range(N):
maxVal = -1
games.sort(key=itemgetter(1))
for j in range(len(games) - 1, -1, -1):
game = games[j]
if game[1] == 0:
del games[0:j+1]
break

if maxVal < game[0]:
maxVal = game[0]
else:
del games[j]

game[0] += V[i]
game[1] += -1
if maxVal == -1:
maxVal = 0
games.append([maxVal, P[i]])
print(max(games, key=itemgetter(0))[0])```
```

## Find the Running Median

The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.

## Minimum Average Waiting Time

Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h

## Merging Communities

People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w

## Components in a graph

There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu

## Kundu and Tree

Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that

## Super Maximum Cost Queries

Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and