Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	io := NewIo()
	defer io.Flush()
	seq := make([]int, 360)
	N := io.NextInt()
	for i := 0; i < N; i++ {
		M, D, V, S := io.NextInt(), io.NextInt(), io.NextInt(), io.NextInt()
		M = (M - 1) * 30
		D--
		for j := M + D; j < M+D+V; j++ {
			seq[j%360] = intMax(S, seq[j%360])
		}
		for j := 0; j < S; j++ {
			seq[(M+D-j+360)%360] = intMax(S-j, seq[(M+D-j+360)%360])
			seq[(M+D+V+j-1)%360] = intMax(S-j, seq[(M+D+V+j-1)%360])
		}
	}
	ans := 360
	for _, val := range seq {
		ans = intMin(val, ans)
	}
	io.Println(ans)
}

func intMin(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func intMax(a, b int) int {
	if a < b {
		return b
	}
	return a
}

// I/O template
type Io struct {
	reader    *bufio.Reader
	writer    *bufio.Writer
	tokens    []string
	nextToken int
}

func NewIo() *Io {
	return &Io{
		reader: bufio.NewReader(os.Stdin),
		writer: bufio.NewWriter(os.Stdout),
	}
}

func (io *Io) Flush() {
	err := io.writer.Flush()
	if err != nil {
		panic(err)
	}
}

func (io *Io) NextLine() string {
	var buff []byte
	for {
		line, isPrefix, err := io.reader.ReadLine()
		if err != nil {
			panic(err)
		}
		buff = append(buff, line...)
		if !isPrefix {
			break
		}
	}
	return string(buff)
}

func (io *Io) Next() string {
	for io.nextToken >= len(io.tokens) {
		line := io.NextLine()
		io.tokens = strings.Fields(line)
		io.nextToken = 0
	}
	s := io.tokens[io.nextToken]
	io.nextToken++
	return s
}

func (io *Io) NextInt() int {
	i, err := strconv.Atoi(io.Next())
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) NextFloat() float64 {
	i, err := strconv.ParseFloat(io.Next(), 64)
	if err != nil {
		panic(err)
	}
	return i
}

func (io *Io) Println(a ...interface{}) {
	fmt.Fprintln(io.writer, a...)
}

func (io *Io) Printf(format string, a ...interface{}) {
	fmt.Fprintf(io.writer, format, a...)
}

func (io *Io) PrintIntSlice(a []int) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func (io *Io) PrintStringSlice(a []string) {
	b := []interface{}{}
	for _, x := range a {
		b = append(b, x)
	}
	io.Println(b...)
}

func Log(name string, value interface{}) {
	fmt.Fprintf(os.Stderr, "%s=%+v\n", name, value)
}

func ExampleIo() {
	io := NewIo()
	defer io.Flush()
	a := make([]int, 0, 3)
	a[0], a[1], a[2] = io.NextInt(), io.NextInt(), io.NextInt()
	io.Println(a)
	io.PrintIntSlice(a)
	Log("io", a)
}

C++: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;

int main() {
	vector<int>nums(360);
	int N;cin>>N;
	for (int i = 0; i < N; ++i) {
		int m,d,v,s;cin>>m>>d>>v>>s;
		m--;d--;
		const int start=m*30+d;
		const int end=(start+v-1)%360;
		if (end > start) {
			for (int j = 0; j < 360; ++j) {
				if (start <= j&&j <= end) {
					nums[j]=max(nums[j],s);
				}
				else {
					if (j < start) {
						int l=min(start-j,j+360-end);
						nums[j]=(max(nums[j],s-l));
					}
					else {
						int l=min(j-end,start+360-j);
						nums[j]=(max(nums[j],s-l));
					}
				}
			}
		}
		else {
			for (int j = 0; j < 360; ++j) {
				if (end < j&&j < start) {
					nums[j]=max(nums[j],s-min(start-j,j-end));
				}
				else {
					nums[j]=max(s,nums[j]);
				}
			}
		}
	}
	auto it=min_element(nums.begin(),nums.end());
	cout<<*it<<endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<n;i++)
#define INF 1e9
int main(){
    int n; cin>>n;
    using pii = pair<int,int>;
    using piii = pair<pii,int>;
    vector<pii>range;
    vector<int>nd(361,0);
    vector<int>s;
    rep(i,n){
        int m,d,v,_s; cin>>m>>d>>v>>_s;
        pii temp;
        temp.first=(m-1)*30+d;
        temp.second=temp.first+v-1;  if(temp.second>360)temp.second-=360;
        range.push_back(temp);
        if(temp.first<=temp.second)
            for(int i=temp.first;i<=temp.second;i++)
                nd[i]=max(nd[i],_s);
        else{
            for(int i=temp.first; i<=360;i++)nd[i]=max(nd[i],_s);
            for(int i=1; i<=temp.second;i++)nd[i]=max(nd[i],_s);
        }
        
        s.push_back(_s);
    }
    
    for(int day=1; day<=360;day++){
        for(int i=0; i<n;i++){
            pii t=range[i];
            if(t.first<=t.second){
                if(t.first<=day&&day<=t.second)continue;
            }else{
                if(t.first<day||day<=t.second)
                    continue;
            }
            pii value;
            if(t.first>=day)value.first=t.first-day;
            else value.first=360-day+t.first;
            if(t.second<=day)value.second=day-t.second;
            else value.second=360-day+t.second;
            nd[day]=max(nd[day],max(0,s[i]-min(value.first,value.second)));
        }
    }
    int ans=INF;
    for(int i=1; i<=360;i++)ans=min(ans,nd[i]);
    cout<<ans<<endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
int n,m,v,s,d,t[361],ans=1e9;
main(){
    cin>>n;
    r(i,n){
        cin>>m>>d>>v>>s;m--;
        int p=m*30+d,ss=s;
        while(v--){
            t[p]=max(t[p],s);
            p++;
            if(p>360)p=1;
        }
        int pp=m*30+d-1;
        if(pp==0)pp=360;
        while(s--){
            t[p]=max(t[p],s);
            p++;
            if(p>360)p=1;
        }
        while(ss--){
            t[pp]=max(t[pp],ss);
            pp--;
            if(pp==0)pp=360;
        }
    }
    r(i,360)ans=min(ans,t[i+1]);
    cout<<ans<<endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int Start[100];
int s[100];
int End[100];
int v[100];

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

	int N;
	cin >> N;
	for(int i = 0; i < N; i++) {
		int M, D;
		cin >> M >> D >> v[i] >> s[i];
		Start[i] = 30 * (M - 1) + D;
		End[i] = Start[i] + v[i] - 1;
	}

	int ans = 1e9;
	for(int i = 0; i < 360; i++) {
		int x = 0;
		for(int j = 0; j < N; j++) {
			if(Start[j] <= i && i <= End[j]) {
				x = max(x, s[j]);
			}
			else {
				if(i < Start[j]) {
					int t = s[j] - min(Start[j] - i, i + 360 - End[j]);
					x = max(x, t);
				}
				else {
					int t = s[j] - min(i - End[j], Start[j] + 360 - i);
					x = max(x, t);
				}
			}
		}

		ans = min(ans, x);
	}
	cout << ans << endl;
}